ELK 集中化日志收集分析平台
官⽅⽹站: https://www.elastic.co/products
ELK由ElasticSearch、Logstash和Kiabana三个开源⼯具组成:
- Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,⾃动发现,索引⾃动分⽚,索引副本机制,restful⻛格接⼝,多数据源,⾃动搜索负载等。
- Logstash是⼀个完全开源的⼯具,他可以对你的⽇志进⾏收集、过滤,并将其 存储供以后使⽤(如:搜索)。
- Kibana也是⼀个开源和免费的⼯具,它Kibana可以为Logstash和ElasticSearch 提供的⽇志分析友好的 Web界⾯,可以帮助您汇总、分析和搜索重要数据⽇志。
- Filebeat:安装在客户端服务器上,将日志发送到Logstash,Filebeat充当日志传送代理,利用伐木工人网络协议与Logstash通信。
部署环境需求
host1.com # ELK服务器
4GB memory Elasticsearch、Kibana、Logstash、Java
host2.com # 被监控服务器 rsyslog Filebeat
host3.com # 代理服务器 nginx Filebeat
1.Java环境部署
下载jdk地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html
推荐该版本elk安装jdk版本
[root@host1 ~]# tar -zxvf jdk-8u102-linux-x64.tar.gz -C /usr/local/
[root@host1 ~]# cd /usr/local/jdk1.8.0_102/
配置jdk环境变量
[root@host1 ~]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_102;
export PATH=$PATH:$JAVA_HOME/bin;
export CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
[root@host1 ~]# source /etc/profile
或者执行命令,选择安装的Java版本
[root@host1 ~]# alternatives --config java
检查Java版本
[root@host1 ~]# java -version
2.部署Elasticsearch环境
点击查看:ElasticSearch 安装部署常见错误问题
elasticsearch的安全机制不允许用root用户启动,故新建用户elk,以elk用户启动elasticsearch:
[root@host1 ~]# useradd elk
[root@host1 ~]# passwd elk
[root@host1 ~]# su - elk
[root@host1 ~]# wget -c https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.3.2.rpm
[root@host1 ~]# rpm -ivh elasticsearch-5.3.2.rpm
[root@host1 ~]# chown -R elk:elk /etc/elasticsearch/
[root@host1 ~]# vim /etc/elasticsearch/elasticsearch.yml
network.host: 0.0.0.0 #修改表示为开启网络监听
ttp.port: 9200
[elk@host1 ~]# systemctl start elasticsearch ; systemctl status elasticsearch
[elk@host1 ~]# curl ELK服务器IP地址:9200
注意:内存不足4G,Java环境将无法运行成功,并导致elasticsearch无法启动
3.部署Kibana环境
[root@host1 ~]# wget -c https://artifacts.elastic.co/downloads/kibana/kibana-5.3.2-x86_64.rpm
[root@host1 ~]# rpm -ivh kibana-5.3.2-x86_64.rpm
[root@host1 ~]# vim /etc/kibana/kibana.yml
server.host:"x.x.x.x" #本机服务器IP地址
elasticsearch.url: "http://x.x.x.x:9200" #ELK服务器IP地址
[root@host1 ~]# systemctl start kibana ; systemctl status kibana
测试访问
kibana服务器IP地址:5601
4.部署Nginx代理环境
[root@host3 ~]# yum install nginx httpd-tools
创建访问时登陆用户kibanaadmin和密码
[root@host3 ~]# htpasswd -c /etc/nginx/htpasswd.users kibanaadmin
[root@host3 ~]# vim /etc/nginx/conf.d/kibana.conf
server {
listen 80;
server_name xxx; #host3的主机名
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/htpasswd.users;
location / {
proxy_pass http://kibana服务器IP地址:5601;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
[root@host3 ~]# systemctl start nginx ; systemctl status nginx
测试访问
代理nginx服务器IP地址
5.部署Logstash环境
[root@host1 ~]# wget -c https://artifacts.elastic.co/downloads/logstash/logstash-5.3.2.rpm
[root@host1 ~]# rpm -ivh logstash-5.3.2.rpm
a.使用Filebeat将日志从客户端服务器传送到elk Server,因此需要创建SSL证书和密钥对。该证书由Filebeat用于验证标识ELK服务器.。
[root@host1 ~]# vim /etc/pki/tls/openssl.cnf
[ v3 ca ]
subjectAltName = IP: x.x.x.x #ELK服务器IP地址,有空格
b.利用以下命令来生成SSL认证以及私钥
[root@host1 ~]# cd /etc/pki/tls/
[root@host1 tls]# openssl req -config /etc/pki/tls/openssl.cnf -x509 -days 3650 -batch -nodes \
-newkey rsa:2048 -keyout private/logstash-forwarder.key -out certs/logstash-forwarder.crt
c.Logstash配置:文件采用JSON-格式。配置由三个部分组成:输入、过滤器和输出。
[root@host1 ~]# vim /etc/logstash/conf.d/02-beats-input.conf
input {
beats {
port => 5044
ssl => true
ssl_certificate => "/etc/pki/tls/certs/logstash-forwarder.crt"
ssl_key => "/etc/pki/tls/private/logstash-forwarder.key"
}
}
d.描述一个beats输入,监听tcp端口5044,并且会利用前面创建的ssl认证即秘钥
[root@host1 ~]# vim /etc/logstash/conf.d/10-syslog-filter.conf
filter {
if [type] == "syslog" {
grok {
match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
add_field => [ "received_at", "%{@timestamp}" ]
add_field => [ "received_from", "%{host}" ]
}
syslog_pri { }
date {
match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
}
}
}
e.对syslog类型(Filebeat进行标记)的日志进行过滤,并利用grok将输入的syslog日志解析以使之结构化并且利于查询。
[root@host1 ~]# vim /etc/logstash/conf.d/30-elasticsearch-output.conf
output {
elasticsearch {
hosts => ["localhost:9200"]
sniffing => true
manage_template => false
index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
document_type => "%{[@metadata][type]}"
}
}
[root@host1 ~]# systemctl start logstash ; systemctl status logstash
f.拷贝SSL证书到客户端服务器。
[root@host1 ~]# scp /etc/pki/tls/certs/logstash-forwarder.crt Filebeat客户端IP地址:/etc/pki/tls/certs/
6.部署客户端Filebeat Package环境
[root@host2 ~]# wget -c https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.3.2-x86_64.rpm
[root@host2 ~]# rpm -ivh filebeat-5.3.2-x86_64.rpm
[root@host2 ~]# vim /etc/filebeat/filebeat.yml
filebeat.prospectors:
paths:
- /var/log/nginx/access.log #添加监控的日志路径
output.elasticsearch:
hosts: ["x.x.x.x:9200"] #ELK服务器IP地址
index: "host2" #本机主机名
output.logstash:
hosts: ["x.x.x.x:5044"] #ELK服务器IP地址
ssl.certificate_authorities: ["/etc/pki/tls/certs/logstash-forwarder.crt"] #证书路径
[root@host2 ~]# systemctl start filebeat ; systemctl status filebeat
7.首次运行,会提示创建index(填入filebeat*),直接点击Create按钮即可。
选择Discover进行监控反馈时间设置,显示监控数据
问题一: [2016-11-06T16:27:21,712][WARN ][o.e.b.JNANatives ] unable to install syscall filter:
Java.lang.UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMPandCONFIG_SECCOMP_FILTERcompiledinatorg.elasticsearch.bootstrap.Seccomp.linuxImpl(Seccomp.java:349) ~[elasticsearch-5.0.0.jar:5.0.0]
at org.elasticsearch.bootstrap.Seccomp.init(Seccomp.java:630) ~[elasticsearch-5.0.0.jar:5.0.0]
原因:报了一大串错误,大家不必惊慌,其实只是一个警告,主要是因为你Linux版本过低造成的。
解决方案:
1、重新安装新版本的Linux系统
2、警告不影响使用,可以忽略
问题二: ERROR: bootstrap checks failed max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
原因:无法创建本地文件问题,用户最大可创建文件数太小
解决方案:切换到root用户,编辑limits.conf配置文件, 添加类似如下内容:
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
*
备注:* 代表Linux所有用户名称(比如 hadoop)
保存、退出、重启reboot才可生效
问题三: max number of threads [1024] for user [es] likely too low, increase to at least [2048]
原因:无法创建本地线程问题,用户最大可创建线程数太小
解决方案:切换到root用户,进入limits.d目录下,修改90-nproc.conf 配置文件。
vim /etc/security/limits.d/90-nproc.conf
* soft nproc 1024 #修改为 * soft nproc 2048
问题四: max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
原因:最大虚拟内存太小
解决方案:切换到root用户下,修改配置文件sysctl.conf
vim /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
并执行命令:
sysctl -p
然后重新启动elasticsearch服务,即可启动成功。
问题五: ElasticSearch启动找不到主机或路由
原因:ElasticSearch 单播配置有问题
解决方案:检查ElasticSearch中的配置文件
vim config/elasticsearch.yml
找到如下配置:
discovery.zen.ping.unicast.hosts:["192.168.**.**:9300","192.168.**.**:9300"]
一般情况下,是这里配置有问题,注意书写格式
问题六: org.elasticsearch.transport.RemoteTransportException: Failed to deserialize exception response from stream
原因:ElasticSearch节点之间的jdk版本不一致
解决方案:ElasticSearch集群统一jdk环境
问题七: Unsupported major.minor version 52.0
原因:jdk版本问题太低
解决方案:更换jdk版本,ElasticSearch5.0.0支持jdk1.8.0
问题八: bin/elasticsearch-plugin install license
ERROR: Unknown plugin license
原因:ElasticSearch5.0.0以后插件命令已经改变
解决方案:使用最新命令安装所有插件 bin/elasticsearch-plugin install x-pack
问题九:
Exception in thread “main” java.nio.file.AccessDeniedException:
/home/elk/elasticsearch-6.3.2/config/jvm.options
原因:使用非 root用户启动ES,该用户的文件权限不足
解决方案:添加用户对该文件的权限 chown -R elkuser:elkgroup /home/elk