ElK + Filebeat安装教程

EKL Stack简介

ELK 不是一款软件,而是Elasticsearch、Logstash和Kibana首字母的缩写。这三者是开源软件,通常配合一起使用,而且先后归于Elasic.co公司的名下,所以简称ELK Stack。根据Google Trend的信息显示,ELK已经成为目前最流行的的集中式日志解决方案。

  • Elasticsearch:分布式搜索和分析引擎,具有高可伸缩、高可靠和易管理等特点。基于Apache Lucene构建,能对大容量的数据进行接近实时的存储、搜索和分析操作。
  • Logstash:数据收集引擎。它支持从各种数据源收集数据,并对数据进行过滤、分析、丰富、统一格式等操作,然后存储到用户指定的位置。
  • Kibana:数据分析和可视化平台。通常与Elasticsearch配合使用,对其中的数据进行搜索、分析和以统计图表的方式显示。
  • Filebeat:ELK协议栈的新成员,一个轻量级开源日志文件数据搜集器,基于Logstash-Forwarder源码开发,是对它的替换。在需要采集日志数据的服务器上安装Filebeat,并指定日志目录或日志文件后,Filebeat就能读取数据,迅速发送到Logstash进行解析,亦或直接发送到Elasticsearch进行集中式存储和分析。

ELK+Filebeat体系结构图

  • 以FileBeat作为日志收集器
以FileBeat作为日志收集器

这种结构适用日志规模比较小的场景,用Filebeat替换Logstash作日志收集器,解决Logstash在各应用服务器占用资源高的问题,相对Logstash,Filebeat所占用的CPU和内存几乎可以忽略不计。

  • 引入消息队列作为消息缓存
引入消息队列作为消息缓存

这种结构适用于日志规模比较大的场景。但由于Logstash日志解析节点和Elasticsearch节点的负荷比较重,可将他们配置为集群模式,以分担负荷。引入消息队列,减低网络阻塞,缓存数据,避免数据丢失,但Logstash占用资源过多的问题依然存在。

本教程以第1种体系结构为例,提供安装教程。

安装环境信息

操作系统

Centos7- Minimal

JDK版本

jdk-8u40-linux-x64

软件版本

elasticsearch-5.1.1
logstash-5.1.1
kibana-5.1.1
filebeat-5.1.2
elasticsearch-head-master

Elasticsearch安装

创建elk用户组和用户

root用户无法启动Elasticsearch,需要创建非root用户。
运行groupadd elasticsearch #创建用户组
运行useradd -g elasticsearch el01 #添加用户到指定用户组
运行passwd !@#123 #创建密码
切换到el01用户

下载地址

https://www.elastic.co/downloads/elasticsearch
下载完成后ftp上传到服务器

解压到指定目录

运行tar -zxvf elasticsearch-5.1.1.tar.gz -C /opt
授权用户 chown -R el01:elasticsearch elasticsearch-5.1.1

配置./conf/elasticsearch.yml

配置文件说明参考:http://blog.csdn.net/zxf_668899/article/details/54582849
单机部署可不配
注意配置文件的格式要求:参数冒号后加空格,或者是数组中间加空格
还有注释掉的参数不能在#后边加空格不然报错

启动

运行./bin/elasticsearch -d #后台进程运行模式

启动碰到的问题

1)max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
2)max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]

解决1),vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

解决2),vi /etc/security/limits.d/90-nproc.conf
注释如下内容:
* soft nproc 1024
修改为
#* soft nproc 1024

vi /etc/sysctl.conf 添加下面配置: vm.max_map_count=655360
运行 source sysctl.conf
如果source不行,就重启系统

查看启动状态

运行curl ‘http://localhost:9200′
如果看到以下信息,说明安装成功。

安装状态

运行tail -f logs/elasticsearch.log可 查询Elasticsearch启动及运行日志。

Elasticsearch-Head插件安装

切换用户su – root

软件下载地址

下载elasticsearch-head插件
下载地址:https://github.com/mobz/elasticsearch-head.git
下载node.js
下载地址: https://nodejs.org/en/download/
下载完成后ftp上传到服务器

解压到指定目录

运行tar -zxvf elasticsearch-head-master.zip -C /opt
运行tar -zxvf node-v6.9.4-linux-x64.tar.xz -C /opt

配置node.js的环境参数

运行vi /etc/profile

配置完成后运行 source /etc/profile 参数设置生效。
运行node -v 和npm -v 命令,检查node和npm是否安装成功。

npm安装依赖

进入elasticsearch-head-master目录下,
npm install
运行 npm install -g cnpm –registry=https://registry.npm.taobao.org 安装npm的依赖。

配置参数
  • 进入elasticsearch-head-master目录,修改Gruntfile.js文件
    运行vi Gruntfile.js,添加hostname参数
  • 修改elasticsearch的elasticsearch.yml文件,添加参数,允许跨域访问。
    cd elasticsearch-5.1.1/config
    vi elasticsearch.yml
    在文件的最后添加:
    http.cors.enabled: true
    http.cors.allow-origin: “*”

配置完成后kill掉原来的进程,运行elasticsearch./bin/elasticsearch -d,重启Elasticsearch。(需要切换成el01用户来操作)

启动

切换成root 用户
进入elasticsearche-head-master目录
运行./node_modules/grunt/bin/grunt server
运行nohup ./node_modules/grunt/bin/grunt server & #后台进程启动方式
运行tail -f nohup.out 查看启动及运行日志。

查看启动状态

浏览器访问:http://localhost:9100/

Logstash安装

软件下载地址

https://www.elastic.co/downloads/logstash
下载完成后ftp上传到服务器

解压到指定目录

tar -zxvf logstash-5.1.1.tar.gz -C /opt

配置配置文件

进入./logstash-5.1.1/config目录,创建配置文件filebeat.conf

input {
  beats {
       port => 5044
       codec => json{
            charset => "UTF-8"
        } 
       #sincedb_path => "/app/logstash-5.1.1/.sincedb"
  }
}
output {
    elasticsearch {
           hosts => "10.0.2.15:9200"
           index => "test-%{+YYYY.MM.dd}"
           document_type => "%{[type]}"
     }
    stdout {
        codec => rubydebug
     }
}

配置说明参考:
http://udn.yyuap.com/doc/logstash-best-practice-cn/get_start/install.html

启动

运行nohup ./bin/logstash -f config/filebeat.conf & #后台进程运行模式

查看启动状态

运行tail -f nohup.out 查看启动及运行日志

Filebeat安装

软件下载地址

https://www.elastic.co/downloads/beats/filebeat
下载完成后ftp上传到服务器

解压到指定目录

tar -zxvf filebeat-5.1.2-linux-x86_64.tar -C /opt

配置参数

进入./ filebeat-5.1.2-linux-x86_64目录,修改配置文件vi filebeat.yml
input_type:log #配置输入类型,log或stdin
paths:/home/el01/logs/test_json.log #配置文件目录
output.logstash #Filebeat将日志传送给Logstash
hosts:[“localhost:5044”] #Logstash的监听端口

配置说明参考:http://michaelkang.blog.51cto.com/1553154/1864225

启动

运行nohup ./filebeat & #后台进程运行模式

查看启动状态

运行tail -f nohup.out 查看运行日志

Kibana安装

软件下载地址

https://www.elastic.co/downloads/kibana
下载完成后ftp上传到服务器

解压到指定目录

tar -zxvf kibana-5.1.1-linux-x86_64.tar.gz -C /opt

配置参数

进入. /kibana-5.1.1-linux-x86_64/config目录,修改配置文件vi kibana.yml
server.port: 5601 #Kibana默认端口
server.host: “0.0.0.0” #Kibana地址,配置0.0.0.0可外部访问
elasticsearch.url: “http://xxxx:9200” #elasticsearch的地址

配置说明参考:http://blog.csdn.net/molaifeng/article/details/53889547#

启动

运行nohup ./bin/kibana & #后台进程运行模式

查看启动状态

运行tail -f nohup.out 查看启动及运行日志

浏览器访问http://192.168.1.220:5601/

AlastAlert安装

软件下载地址

https://codeload.github.com/Yelp/elastalert/zip/master
下载完成后ftp上传到服务器

解压到指定目录

unzip elastalert-master.zip -d /opt

配置参数

进入/opt/elastalert-master目录
查看是否安装gcc,如果没有yum -y install gcc
测试python是否安装 python,运行python 命令
运行python setup.py install #如果python没安装,请安装python2.6或2.7版本
安装过程如果提示需要安装setuptools,

则运行,下载安装包安装
wget –no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz
安装setuptools:
tar -xvf setuptools-1.4.2.tar.gz
cd setuptools-1.4.2
python setup.py install
更新版本:
pip install -U pip
pip install -U setuptools

重新进入/opt/elastalert-master目录,运行python setup.py install,
如果出现以下错误:

则运行:yum install python-devel -y
重新运行python setup.py install
最后运行:pip install -r requirements.txt #安装依赖库
如果报错:

更新版本:
pip install -U pip
pip install -U setuptools
再执行:pip install -r requirements.txt

配置

进入/opt/elastalert-master目录
创建配置目录:mkdir rules #保存配置文件
cp example_rules/example_frequency.yaml rules/frequency.yaml #复制配置文件
修改 vi rules/frequency.yaml 参考如下:

参考资料:http://blog.csdn.net/gamer_gyt/article/details/52917116

然后cd /app/elastalert-master
创建文件 vi smtp_auth_file.yaml #文件名是固定的

填写发件人邮箱账号和密码(邮箱客户端的密码)。

创建config文件:
cp config.yaml.example config.yaml
vi config.yaml

启动

运行:
python -m elastalert.elastalert –config ./config.yaml –rule ./examele_rules/one_rule.yaml

后台进程运行模式

查看启动状态

运行tail -f nohup.out 查看启动及运行日志

Nginx安装自带身份验证(实现Kibana账号密码登录)

生成账号和登录密码

通过Nginx的ngx_http_auth_basic_module生成账号和密码
查看nginx的模块是否有包含它; ./nginx -V
开始生成账号密码;
cd /app/nginx/conf
运行生成命令:
# printf “username:$(openssl passwd -crypt 20@17xxx)\n” > htpasswd
帐号:username
密码:20@17xxx
日后添加用户则使用追加命令就可以
添加用户:
printf “xxxx2:$(openssl passwd -crypt 20@17xxx)\n” >> htpasswd

配置参数

vi kibana.conf

server {
      listen 80;
      server_name kibana.gzkkonline.com;
      
     #配置身份验证的注释和用户验证文件
      auth_basic "Kibana Auth";
      auth_basic_user_file  htpasswd;
      
      access_log  logs/kibana_access.log main;
      error_log  logs/kibana_error.log;
      
     location / {
          proxy_pass http://10.0.2.15:5601;
      }
}
启动

运行:
systemctl start nginx.service
如何错误日志出现以下问题:
*1 connect() to 127.0.0.1:5601 failed (13: Permission denied)
运行:selinux 的状态
setsebool -P httpd_can_network_connect 1

查看启动状态

查看启动及运行日志
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log

作者:那些年未曾努力过
链接:https://www.jianshu.com/p/84f63fac3710
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

LEAVE A COMMENT