ELK实时日志分析平台(一)

闲来无事,最近感觉现在企业招聘信息中要求会elk,作为小白的我当然也要紧跟技术界的潮流了【虽然跟的有点晚了哈@[email protected]

本文章是借鉴网上一篇文章并结合我自己实践过程写下的,文章最后会贴出借鉴文章的链接~

系统:

ELK实时日志分析平台(一)

注:我这里为测试和实验方便,ELK整套都装在同一台虚拟机中了,生产环境的话,可以分开搭建在不同的服务器上,只要能互相联通。

虚拟机中防火墙以及selinux都已关闭!

相关部署环境:

ELK实时日志分析平台(一)​一个MVC模型,logstash是controller层,Elasticsearch是一个model层,kibana是view层。

将数据传给logstash,它将数据进行过滤和格式化(转成JSON格式);

然后传给Elasticsearch进行存储、建搜索的索引;

kibana提供前端的页面再进行搜索和图表可视化,它是调用Elasticsearch的接口返回的数据进行可视化。

logstash和Elasticsearch是用Java写的,kibana使用node.js框架。

中文官网 https://www.elastic.co/cn/

一、安装JAVA和设置JAVA变量环境 {注意:如果系统自带的jdk环境不满足需求,则需先卸载【rpm -e --nodeps 软件包名称】在安装}

1.解压:tar zxvf  jdk-8u152-linux-x64.tar.gz

2.配置环境变量:

#vim /etc/profile

在底部添加以下内容 :

export JAVA_HOME=/opt/jdk1.8.0_152
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH

#source /etc/profile        使环境变量生效

ELK实时日志分析平台(一)

二、下载elasticsearch回来后解压

ELK下载:https://www.elastic.co/downloads/

linux系统一般就选择tar包下载可以了。

下载后在服务器/opt目录上解压:tar -zxvf elasticsearch-6.1.3.tar.gz

出于系统安全考虑设置,ElasticSearch不允许以root用户模式运行

由于Elasticsearch可以接收用户输入的脚本并且执行,

为了系统安全考虑,必须创建一个单独的用户用来运行Elasticsearch,

否则会导致运行失败

故为elsearch创建一个组和用户如下:

sudo groupadd elk #创建ELK组

sudo useradd elk -g elk

sudo passwd 111111

#创建新用户elk加入elk组并设置输入密码,注意如果直接-p指定明文密码的话是不对的,-p后面接的是加密形式后的密文。

sudo chown -R elk.elk /opt/elasticsearch-6.1.3 #变更elasticsearch目录的权限所有者

su elk #切换elsearch用户

/opt/elasticsearch-6.1.3/bin/elasticsearch -d #后台启动elasticsearch

或su - elsearch -c "/opt/elasticsearch-6.1.3/bin/elasticsearch -d" #临时切换到elsearch用户执行一条命令

jps    #查看后台运行程序

ELK实时日志分析平台(一)

netstat -lnp | grep 9200 #确认9200端口已监听即elasticsearch启动成功

tcp 0 0 127.0.0.1:9200 0.0.0.0:* LISTEN -

验证

在服务器浏览器(安装links或lynx)中输入:http://localhost:9200/

或 127.0.0.1:9200(elasticsearch默认端口号为9200)

暂时不能在别的客户机测试,因为侦听的只是127.0.0.1本机,之后开放侦听公网的时候记得systemctl stop iptabels关闭一下防火墙

或防火墙及安全组上配置开放端口。

curl 127.0.0.1:9200 出现下载和显示的选择,选择显示后会显示json内容:

ELK实时日志分析平台(一)

修改监听端口,允许外部系统远程访问(不然不方便呐):

编辑文件

sudo vim /opt/elasticsearch-6.1.3/config/elasticsearch.yml

在Network区域中修改默认参数

network.host的数值,另外http.port也可将默认的9200端口修改。

network.host: 0.0.0.0

(注意这里表示设置侦听你所有网卡上的ip地址,也可以只针对单个网卡地址进行设置)

保存好设置,需重启 elasticsearch服务。

elsearch是用java启动的,我们可以先查找java进程再筛选,当然java也有可能运行了多个进程,所以还是直接查找elsearch为好。

ELK实时日志分析平台(一)

重启elseach:

su - elsearch -c "/opt/elasticsearch-6.1.3/bin/elasticsearch -d"

[2018-02-01T18:02:39,534][INFO ][o.e.t.TransportService ] [l2qPmsb] publish_address {0.0.0.0:9300}, bound_addresses {0.0.0.0:9300}

[2018-02-01T18:02:39,548][INFO ][o.e.b.BootstrapChecks ] [l2qPmsb] bound or publishing to a non-loopback address, enforcing bootstrap checks

ERROR: [2] bootstrap checks failed

[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]

[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

[2018-02-01T18:02:39,561][INFO ][o.e.n.Node ] [l2qPmsb] stopping ...

[2018-02-01T18:02:39,591][INFO ][o.e.n.Node ] [l2qPmsb] stopped

[2018-02-01T18:02:39,591][INFO ][o.e.n.Node ] [l2qPmsb] closing ...

[2018-02-01T18:02:39,608][INFO ][o.e.n.Node ] [l2qPmsb] closed

 

出现问题:

ERROR下面的两行:

[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]

[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

[1]无法创建本地文件问题,用户最大可创建文件数太小,刚好差1……

解决方案:

vi /etc/sysctl.conf

设置fs.file-max=655350

保存之后sysctl -p使设置生效

vi /etc/security/limits.conf 修改或新增

  • soft nofile 65536
  • hard nofile 131072
  • soft nproc 2048
  • hard nproc 4096

备注:* 代表Linux所有用户名称

后面设置的nproc实际上设置多线程,以防止再报用户最大可创建线程数太小的故障。

保存、退出、重新登录才可生效

重新使用SSH登录,再次启动elasticsearch即可。

[2]需要设置vm.max_map_count的参数从65530改到262144。是说系统最大虚拟内存的参数过低,这是与系统有关的了。

解决方案:

临时设置:sudo sysctl -w vm.max_map_count=262144

永久修改:

修改/etc/sysctl.conf 文件,添加 " vm.max_map_count = 262144 "

并执行:sysctl -p

这下就正常改变侦听网卡后也可以正常启动elasticsearch了。

用客户端浏览器访问也正常了。

ELK实时日志分析平台(一)

调整jvm内存参数 设置JVM内存参数时一定要注意不要设置超 不然启动报错

另外根据需要,可修改文件ElasticSearch目录下

config/jvm.options文件,调整jvm内存参数。

# Xms represents the initial size of total heap space

# Xmx represents the maximum size of total heap space

-Xms1g

-Xmx1g

默认1g,可根据情况修改成-Xms2g之类,或者调整成-Xms512m、-Xmx512m 【看清楚后面是m哦~】

后台运行elasticsearch还可以切换到elsearch用户之后,在elasticsearch/bin目录下执行:

nohup ./elasticsearch >> /opt/elasticsearch-6.1.3/output.log 2>&1 &

输出日志倒是可有可无,因为elasticsearch本就有log记录。也可以直接:

nohup ./elasticsearch &

创建软链接:

ln -s elasticsearch-6.1.3 elasticsearch

一个良好习惯,

生成一个elasticsearch来作为elasticsearch-6.1.3的映射,平常在别的地方要用到指向elaticsearch时直接指的是不带版本号的软链接。

这样在以后升级elasticsearch到别的版本的时候不致于是影响太多,只需要重新作软链接即可。

三、安装Head插件:

elasticsearch自5.x版本后便不支持直接安装head插件。

ELK实时日志分析平台(一)

不但如此,同时5.1之后也不支持直接放在elasticsearch的 plugins、modules 目录下,不能使用 elasticsearch-plugin install。

直接启动elasticsearch即可,之后再启动head插件。

我第一次安装时是放在elasticsearch的 plugins下了,结果关闭elasticsearch服务后第二天重启发现报错

org.elasticsearch.bootstrap.StartupException: java.lang.IllegalArgumentException: property [elasticsearch.version] is missing for plugin [head]

报head插件丢失

或另一种报错是你没有那些支持的目录与文件。

elasticsearch配置允许跨域访问 【因为我是在虚拟机中操作没有执行,有需要的可以操作】

修改elasticsearch配置文件elasticsearch.yml

vim config/elasticsearch.yml,注意有空格

http.cors.enabled: true

http.cors.allow-origin: "*"

可以参考官网上说的安装方法。如下:

1、下载head插件

https://github.com/mobz/elasticsearch-head

cd /opt/elasticsearch-6.1.3

mkdir head #创建一个head目录用来存放,因为5.1版本后的不支持直接放plugins下了。

cd /opt/elasticsearch-6.1.3/head

开始用git下载head:

(参考https://github.com/mobz/elasticsearch-head上的官网做法,

当然前提是你要先安装git,如果没有也可以直接在它官网下打包下载回来再传到服务器上。)

sudo yum -y install

sudo git clone git://github.com/mobz/elasticsearch-head.git

下载回来的目录大致如下:

[[email protected]*n plugins]$ cd elasticsearch-head/

[[email protected]*n elasticsearch-head]$ ll

total 60

-rw-r--r-- 1 root root 248 Feb 2 15:24 Dockerfile

-rw-r--r-- 1 root root 221 Feb 2 15:24 Dockerfile-alpine

-rw-r--r-- 1 root root 104 Feb 2 15:24 elasticsearch-head.sublime-project

-rw-r--r-- 1 root root 2171 Feb 2 15:24 Gruntfile.js

-rw-r--r-- 1 root root 3482 Feb 2 15:24 grunt_fileSets.js

-rw-r--r-- 1 root root 1088 Feb 2 15:24 index.html

-rw-r--r-- 1 root root 559 Feb 2 15:24 LICENCE

-rw-r--r-- 1 root root 886 Feb 2 15:24 package.json

-rw-r--r-- 1 root root 100 Feb 2 15:24 plugin-descriptor.properties

drwxr-xr-x 4 root root 4096 Feb 2 15:24 proxy

-rw-r--r-- 1 root root 6944 Feb 2 15:24 README.textile

drwxr-xr-x 5 root root 4096 Feb 2 15:24 _site

drwxr-xr-x 4 root root 4096 Feb 2 15:24 src

drwxr-xr-x 4 root root 4096 Feb 2 15:24 test

权限给elsearch用户。

sudo chown -R elk.elk /opt/elasticsearch-6.1.3/head/elasticsearch-head

2、npm安装

cd elasticsearch-head

按照官网的说明到这步是进入到elasticsearch-head目录后直接就开始npm install。

不过我的服务器上显然还没有安装nodejs和grunt-cli,直接执行会报n多错,

所以在进行这一步之前,还需要进行nodejs和grunt-cli/grunt及phantomjs的安装:

ELK实时日志分析平台(一)

下载nodejs最新的bin包

可以在下载页面https://nodejs.org/en/download/中找到下载地址。然后执行指令

wget https://nodejs.org/dist/v9.3.0/node-v9.3.0-linux-x64.tar.xz

然后就是等着下载完毕。

另外你也可以在你喜欢的任意系统上下载最新的bin包,然后通过FTP上传到CentOS上。

3.解压包

依次执行

xz -d node-v9.3.0-linux-x64.tar.xz

tar -xf node-v9.3.0-linux-x64.tar

4. 部署bin文件

先确认你nodejs的路径,我这里的路径为~/node-v9.3.0-linux-x64/bin。确认后依次执行

ln -s ~/node-v9.3.0-linux-x64/bin/node /usr/bin/node

ln -s ~/node-v9.3.0-linux-x64/bin/npm /usr/bin/npm

注意ln指令用于创建关联(类似与Windows的快捷方式)必须给全路径,否则可能关联错误。

5.测试

node -v

npm -v

或者sudo yum install nodejs 【我直接安装的时候源里没有这个软件包,所以就直接下载压缩包安装】

cd /opt/elasticsearch-6.1.3/head/elasticsearch-head

sudo npm install -g grunt-cli 

#注意这个命令要在elasticsearch-head目录下运行,而且一旦目录被迁移或中间的绝对路径有变化,就得重新运行安装。

另外,elsearch的用户权限不足以运行命令安装,需要切换到root用户下执行

[[email protected]*n elasticsearch-head]# npm install -g grunt-cli

/usr/bin/grunt -> /usr/lib/node_modules/grunt-cli/bin/grunt

/usr/lib

└── [email protected]

上述命令执行完后,grunt 命令就被加入到你的系统路径中了,以后就可以在任何目录下执行此命令了。

注意,安装grunt-cli并不等于安装了 Grunt!

Grunt CLI的任务很简单:调用与Gruntfile在同一目录中 Grunt。

这样带来的好处是,允许你在同一个系统上同时安装多个版本的 Grunt。

这样就能让多个版本的 Grunt 同时安装在同一台机器上。

 

安装grunt

Grunt是基于Node.js的项目构建工具。它可以自动运行你所设定的任务。

cd elasticsearch-head

因为我们是在git克隆下来的elasticsearch-head目录下进行安装grunt的,

而这个目录下已经有package.json和Gruntfile.js,所以接下来可以用

sudo npm install grunt --save-dev

sudo npm install -g grunt --registry=https://registry.npm.taobao.org

来进行安装Grunt最新版本到项目目录中,并将其添加到devDependencies内

验证:

[[email protected]*n elasticsearch-head]# grunt 报找不到命令安装node

>> Local Npm module "grunt-contrib-clean" not found. Is it installed?

>> Local Npm module "grunt-contrib-concat" not found. Is it installed?

>> Local Npm module "grunt-contrib-watch" not found. Is it installed?

>> Local Npm module "grunt-contrib-connect" not found. Is it installed?

>> Local Npm module "grunt-contrib-copy" not found. Is it installed?

>> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?

Warning: Task "clean" not found. Use --force to continue.

Aborted due to warnings.

 

出现问题:

ELK实时日志分析平台(一)

使用find命令找到grunt命令在哪-------》 find / -name grunt

去设置全局变量:

vim /etc/profile
export GRUNT_HOME=/opt/elasticsearch-6.3.1/head/elasticsearch-head/node_modules/grunt
export PATH=$GRUNT_HOME/bin:$PATH

这些是Gruntfile.js中引用的,但缺少这些模板,分别去下载安装,还要注意版本号问题。

当然这只是警报,忽略去运行也是能正常启动的。只是不爽。

npm install grunt-contrib-clean

npm install grunt-contrib-concat

npm install grunt-contrib-watch

npm install grunt-contrib-connect

npm install grunt-contrib-copy

npm install grunt-contrib-jasmine

附Grunt常用插件说明:

1)grunt-contrib-uglify:压缩js代码

2)grunt-contrib-concat:合并js文件

3)grunt-contrib-qunit:单元测试

4)grunt-contrib-jshint:js代码检查

5)grunt-contrib-watch:文件监控

6)grunt-contrib-sass:Scss编译

7)grunt-contrib-connect:建立本地服务器

运行以上安装,还有一个npm install grunt-contrib-clean很可能因为版本问题装不上,因为哪怕直接指定版本号:npm install [email protected]也出错,没有这个版本。

[[email protected]*n elasticsearch-head]# npm install grunt-contrib-clean --save-dev

[email protected] /usr/ELK/elasticsearch-6.1.3/head/elasticsearch-head

└── [email protected]

npm WARN [email protected] license should be a valid SPDX license expression

上面的报警信息说的spdx,开源软件或其他合作类软件的一个使用声明影响了。

The SPDX License List is a list of commonly found licenses and exceptions used in free and open source and other collaborative software or documentation.

The purpose of the SPDX License List is to enable easy and efficient identification of such licenses a standardized short identifier, full name, vetted license text including matching guidelines markup as appropriate, and a canonical permanent URL for each license and exception.

SPDX许可证列表是一个常见的许可和例外的列表,在*和开放源代码和其他协作软件或文档中使用。SPDX许可证列表的目的是为了方便和有效地标识此类许可证。

使其具有标准化的短标识符、全名、经过审查的许可文本,包括适当的匹配准则标记,以及每个许可证和异常的标准永久URL。

ELK实时日志分析平台(一)

据网上所说是要打开elasticsearch-head目录下的package.json文件,找到license位置,修改为上面这个网站上存在Identifier,就可以了。

至于为什么elasticsearch-head使用的是Apache License2.0,我也不太清楚。

而且默认已经是license : Apache2,但仍然要改成和spdx网站上显示的Apache-2.0才可以……好坑。

ELK实时日志分析平台(一)

如此修改保存后,再进行npm install grunt-contrib-clean --save-dev就正常安装上了。【我安装时没有报错 如果有报错的小伙伴记得修改一下配置文件】

再运行grunt报的No "clean" targets found.就真的只是一个无关重要的警报了,毕竟没有需要clean的目标。

[[email protected]*n elasticsearch-head]# grunt

>> No "clean" targets found.

Warning: Task "clean" failed. Use --force to continue.

Aborted due to warnings.

安装phantomjs

可以从http://phantomjs.org/download.html官网找到安装包下载。不过我前面执行过npm install grunt-contrib-jasmine已经是安装上了。

使用npm安装太慢,所以从网上下载了包 但是是tar.bz2

ELK实时日志分析平台(一)

复制phantomjs-prebuilt目录到node_modules目录,注意修改权限不然执行sudo npm install 会报错

ELK实时日志分析平台(一)

现在可以进行执行

sudo npm install 

ELK实时日志分析平台(一)

[[email protected]*n elasticsearch-head]$ sudo npm run start #或在elasticsearch-head目录下敲grunt server  两种方式来启动9100端口

ELK实时日志分析平台(一)

放入后台执行:

ELK实时日志分析平台(一)

问题:

ELK实时日志分析平台(一)

安装phantomjs-prebuilt模块

ELK实时日志分析平台(一)

> [email protected] start /usr/ELK/elasticsearch-6.1.3/head/elasticsearch-head

> grunt server

Running "connect:server" (connect) task

Waiting forever...

Started connect web server on http://localhost:9100

上面可以看得启动了head插件服务,且侦听的是localhost9100端口。

ctrl+c停止进程,我们需要修改一下grunt的侦听网卡地址,这样在外网也能访问。

修改head目录下的Gruntfile.js配置,head默认监听127.0.0.1

#sudo vim Gruntfile.js

找到

ELK实时日志分析平台(一)

在这区域的options括号中增添一行

hostname: '0.0.0.0',

注意":"后是接一个空格的,另外末尾的","不能漏。

最终如图:

ELK实时日志分析平台(一)

阿里云ECS安全组和服务器防火墙开放(默认)9100/9200/9300端口

使用客户端浏览ES服务器的9100端口,如图:

ELK实时日志分析平台(一)

证明安装head插件成功。

注意的是,停止elasticsearch主进程并不会影响到head 9100的访问。

如果要将head进程放入后台运行,可以使用nohup 加&后台运行,并且建议输出一个日志记录:

nohup npm run start >> /opt/elasticsearch-6.1.3/head/elasticsearch-head/output.log 2>&1 &

借鉴文章链接:https://www.cnblogs.com/tielemao/p/8479065.html

有兴趣也可以关注这个公众号,经常会发一些运维文章:

ELK实时日志分析平台(一)