随着分布式、微服务的广泛应用,每次出问题了需要查询日志时很不方便,为了达到快速查看、快速分析解决问题的目的,尝试搭建EFK日志管理系统,总结的不足还望指正。
一、安装Java JDK
Elasticsearch需要运行在Java 8 及以上,所以需要先安装Java8,具体安装过程不在赘述。
二、安装Elasticsearch
本文以Elasticsearch6.2.4为例,注意Elasticsearch、Kibana、FileBeat一定要使用相同的版本。
- 下载Elasticsearch:wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz
- 解压:tar -zxvf elasticsearch-6.2.4.tar.gz
- 修改配置:vim elasticsearch-6.2.4/config/elasticsearch.yml
- 添加如下配置或将对应的配置注释取消修改:
network.host: 0.0.0.0 http.port: 9200
- 由于Elasticsearch不能使用root用户打开,需创建Elasticsearch启动用户,不做描述。
- 启动Elasticsearch:切换到非root用户,执行elasticsearch-6.2.4/bin/elasticsearch
安装时可能会遇到两个错误:
- [1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]。解决办法:vim /etc/security/limits.conf 如果有 “* soft nofile 65535 * hard nofile 65535”,根据具体错误提示,把第一个65535改成4096(descriptors [4096]),第二个65535改成65536( least [65536])。如没有增加“* soft nofile 65535 * hard nofile 65535”这两行。
- [2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]。解决办法:vim /etc/sysctl.conf 添加配置:vm.max_map_count=262144 并执行命令:sysctl -p
以上2个修改需要在root用户权限修改,如果是使用xshell开两个窗口的话修改完成之后一定要断开重新登录一下,启动成功用执行命令:curl 127.0.0.1:9200 会得到类似以下json:
{
"name" : "dQIO4Ad",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "P8KtmO3vQdactRW1jX9JnQ",
"version" : {
"number" : "6.2.4",
"build_hash" : "ccec39f",
"build_date" : "2018-04-12T20:37:28.497551Z",
"build_snapshot" : false,
"lucene_version" : "7.2.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
三、安装FileBeat
- 下载FileBeat6.2.4:wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.4-linux-x86_64.tar.gz
- 解压:tar -zxvf filebeat-6.2.4-linux-x86_64.tar.gz
- 修改配置:vim filebeat-6.2.4/filebeat.yml
- 找到类似以下的配置并修改:注意每行两个空格为子级,enabled参数指定是否收集日志,paths参数指定收集日志的路径,注意-后面有空格,
filebeat.prospectors: - type: log enabled: true paths: - /var/xxx/*.log - /var/xxx/*.out multiline.pattern: ^\[ multiline.negate: true multiline.match: after setup.kibana: host: "localhost:5601" output.elasticsearch: hosts: ["localhost:9200"]
- 启动FileBeat:执行filebeat-6.2.4/filebeat -c /usr/local/filebeat/filebeat.yml 注:/usr/local/filebeat-6.2.4/filebeat.yml为filebeat.yml文件的全路径,根据实际路径填写
四、安装Kibana
- 下载Kibana6.2.4:wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.4-linux-x86_64.tar.gz
- 解压:tar -zxvf kibana-6.2.4-linux-x86_64.tar.gz
- 修改配置:vim kibana-6.2.4/config/kibana.yml
- 添加以下配置或者取消注释并修改:其中elasticsearch.url为Elasticsearch的地址,server.host默认是localhost,如果只是本地访问可以默认localhost,如果需要外网访问,可以设置为0.0.0.0
elasticsearch.url: "http://localhost:9200" server.host: "0.0.0.0" kibana.index: ".kibana"
- 启动Kibana:执行 kibana-6.2.4/bin/kibana
五、配置Kibana
- 打开浏览器进入http://127.0.0.1:5601,会出现如下页面:
- 点击Management进入配置:
- 点击进入Index Patterns:如果Elasticsearch FileBeat Kibana是第一次搭建可能会出现下图页面:
点击Learn how,按照操作步骤在Elasticsearch中插入数据后会出现后面的页面 - 在上图红框中输入filebeat-6.2.4-*,能匹配到Elasticsearch的索引,点击Next step进入下一步
- 选择@timestamp,点击Create index pattern完成配置
- 点击 Discover查看日志,还能搜索,如下图:
至此EFK日志管理系统搭建完毕,如有不足,评论、留言多多指教。