ELK搭建实时日志分析平台之一ElasticSearch搭建

文:铁乐与猫

系统:CentOS Linux release 7.3.1611 (Core)
注:我这里为测试和实验方便,ELK整套都装在同一台服务器环境中了,生产环境的话,可以分开搭建在不同的服务器上,只要能互相联通。
相关部署环境
E:ElasticSearch-6.1.3
L:Logstash-6.1.3
K:Kibana-6.1.3
x-pack: X-Pack-6.1.3
JDK: openjdk1.8.0_141
node.js: v6.12.3
npm: 3.10.10
grunt-cli: 1.2.0
phantomjs: 2.1.1
整一套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包下载回来jdk-8u161-linux-x64.tar,放到服务端解压到/usr/java(可以提前新建此文件夹)就完成安装了,接下来是配置环境变量才为重要步骤。

vim /etc/profile
在底部添加以下内容 :

JAVA_HOME=/usr/java/jdk1.8.0_141
JRE_HOME=/usr/java/jdk1.8.0_141/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$JAVA_HOME/jre/lib/ext:
export JAVA_HOME JRE_HOME PATH CLASSPATH


我这次是尝试直接使用yum 安装的JDK,也就是直接 yum install java ,所以并没有做前面那一步。
$ java -version
openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-b16)
OpenJDK 64-Bit Server VM (build 25.141-b16, mixed mode)

二、下载elasticsearch回来后解压

ELK下载:https://www.elastic.co/downloads/
linux系统一般就选择tar包下载可以了。
下载后在服务器/usr/ELK目录上解压:tar -zxvf elasticsearch-6.1.3.tar.gz

出于系统安全考虑设置,ElasticSearch不允许以root用户模式运行。
由于Elasticsearch可以接收用户输入的脚本并且执行,
为了系统安全考虑,必须创建一个单独的用户用来运行Elasticsearch,
否则会导致运行失败。

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

sudo groupadd ELK #创建ELK组
sudo useradd elsearch -g ELK
sudo passwd elsearch

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

sudo chown -R elsearch:ELK /usr/ELK/elasticsearch-6.1.3 #变更elasticsearch目录的权限所有者

su elsearch #切换elsearch用户
/usr/ELK/elasticsearch-6.1.3/bin/elasticsearch -d #后台启动elasticsearch
su - elsearch -c "/usr/ELK/elasticsearch-6.1.3/bin/elasticsearch -d" #临时切换到elsearch用户执行一条命令

**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关闭一下防火墙
或防火墙及安全组上配置开放端口。

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

修改监听端口,允许外部系统远程访问(不然不方便呐):
编辑文件
sudo vim /usr/ELK/elasticsearch-6.1.3/config/elasticsearch.yml
在Network区域中修改默认参数
network.host的数值,另外http.port也可将默认的9200端口修改。

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

保存好设置,这个时候我想停止之前的elsearch换用已修改配置的这个该怎么办?
elsearch是用java启动的,我们可以先查找java进程再筛选,当然java也有可能运行了多个进程,所以还是直接查找elsearch为好。
ps -ef | grep elsearch

[operation@qiaocat-vpn config]$ ps -ef | grep elsearch
root 3045 2489 0 15:41 pts/11 00:00:00 su elsearch
elsearch 3046 3045 0 15:42 pts/11 00:00:00 bash
elsearch 3092 1 0 15:42 pts/11 00:00:23 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -XX:+HeapDumpOnOutOfMemoryError -Des.path.home=/usr/ELK/elasticsearch-6.1.3 -Des.path.conf=/usr/ELK/elasticsearch-6.1.3/config -cp /usr/ELK/elasticsearch-6.1.3/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
operati+ 5666 4141 0 17:24 pts/13 00:00:00 grep --color=auto elsearch

会看到主进程号,kill 主进程号就可以关掉了。
(当然,想平滑不关elsearch就实现配置重启的话还可以用:
sudo kill -USR2 elsearch主PID号这样,实际操作发现没用,可能与我之前使用了临时切换用户启动的命令有关。也有可能我误输入错密码了?)

重启elseach:
su - elsearch -c "/usr/ELK/elasticsearch-6.1.3/bin/elasticsearch -d"
还是不行,那应该是要将前面3045和3046的进程也一并杀掉再试了。

[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

发现错误在于“绑定或发布到一个非回环地址”,执行bootstrap检查后就直接停止进程了。
也就是一定要绑127.0.0.1这样子的回环地址?
上网查证及思考后发现真正错误的提示是紧接着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所有用户名称(比如 hadoop)
    后面设置的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了。
用客户端浏览器访问也正常了。

调整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之类。

后台运行elasticsearch还可以切换到elsearch用户之后,在elasticsearch/bin目录下执行:
nohup ./elasticsearch >> /usr/ELK/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插件。

不但如此,同时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 /usr/ELK/elasticsearch-6.1.3
mkdir head #创建一个head目录用来存放,因为5.1版本后的不支持直接放plugins下了。
cd /usr/ELK/elasticsearch-6.1.3/head
开始用git下载head:
(参考https://github.com/mobz/elasticsearch-head上的官网做法,
当然前提是你要先安装git,如果没有也可以直接在它官网下打包下载回来再传到服务器上。)

sudo git clone git://github.com/mobz/elasticsearch-head.git
下载回来的目录大致如下:
[operation@qiaocat-vpn plugins]$ cd elasticsearch-head/
[operation@qiaocat-vpn 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 elsearch:ELK /usr/ELK/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的安装:

sudo yum install nodejs
当然你也可以跑nodejs官网上去下载解压来安装,配置对应的环境变量就是了。
nodejs官网下载地址https://nodejs.org/dist/

验证
node -v
v6.12.3
npm -v
3.10.10

cd /usr/ELK/elasticsearch-6.1.3/head/elasticsearch-head
**sudo npm install -g grunt-cli **
#注意这个命令要在elasticsearch-head目录下运行,而且一旦目录被迁移或中间的绝对路径有变化,就得重新运行安装。
另外,elsearch的用户权限不足以运行命令安装,需要切换到root用户下执行。

[root@qiaocat-vpn elasticsearch-head]# npm install -g grunt-cli
/usr/bin/grunt -> /usr/lib/node_modules/grunt-cli/bin/grunt
/usr/lib
└── grunt-cli@1.2.0

上述命令执行完后,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内

验证:

[root@qiaocat-vpn elasticsearch-head]# grunt
>> 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.

这些是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 grunt-contrib-clean@1.0.1也出错,没有这个版本。

[root@qiaocat-vpn elasticsearch-head]# npm install grunt-contrib-clean --save-dev
elasticsearch-head@0.0.0 /usr/ELK/elasticsearch-6.1.3/head/elasticsearch-head
└── grunt-contrib-clean@1.0.0

npm WARN elasticsearch-head@0.0.0 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。

据网上所说是要打开elasticsearch-head目录下的package.json文件,找到license位置,修改为上面这个网站上存在Identifier,就可以了。
至于为什么elasticsearch-head使用的是Apache License2.0,我也不太清楚。
而且默认已经是license : Apache2,但仍然要改成和spdx网站上显示的Apache-2.0才可以……好坑。

如此修改保存后,再进行npm install grunt-contrib-clean --save-dev就正常安装上了。

再运行grunt报的No "clean" targets found.就真的只是一个无关重要的警报了,毕竟没有需要clean的目标。
[root@qiaocat-vpn 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已经是安装上了。

比如我找到的最新版本地址就是https://github.com/ariya/phantomjs/archive/2.1.3.tar.gz
sudo wget https://github.com/ariya/phantomjs/archive/2.1.3.tar.gz
sudo tar -zxvf 2.1.3.tar.gz
解压后得phantomjs-2.1.3目录。

现在可以进行执行
**sudo npm install **

虽然执行后还是重新自行去安装PhantomJS了,看提示应该是我前面解压后还要做一步复制phantomjs-prebuilt目录到node_modules目录下才对。

[operation@qiaocat-vpn elasticsearch-head]$ sudo npm run start #或在elasticsearch-head目录下敲grunt server

> elasticsearch-head@0.0.0 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
找到

connect: {
                        server: {
                                options: {
                                        port: 9100,
                                        base: '.',
                                        keepalive: true
                                }
                        }
                }

在这区域的options括号中增添一行
hostname: '0.0.0.0',
注意":"后是接一个空格的,另外末尾的","不能漏。
最终如图:

阿里云ECS安全组和服务器防火墙开放(默认)9100/9200/9300端口
使用客户端浏览ES服务器的9100端口,如图:

证明安装head插件成功。

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

如果要将head进程放入后台运行,可以使用nohup 加&后台运行,并且建议输出一个日志记录:
nohup npm run start >> /usr/ELK/elasticsearch-6.1.3/head/elasticsearch-head/output.log 2>&1 &

(未完,下接ELK搭建实时日志分析平台之二Logstash和Kibana搭建)

posted @ 2018-02-27 15:18  铁乐猫  阅读(7517)  评论(3编辑  收藏  举报