背景:
搭建实时日志分析平台,使用开源框架ELK(elasticsearch + logstash + kibana),官网地址:https://www.elastic.co/cn/
版本:
elasticsearch:6.2.3
logstash:6.2.3
kibana:6.2.3
一、Elasticsearch
0、介绍:Elasticsearch 是一个分布式的 RESTful 风格的搜索和数据分析引擎;集中存储数据,以供分析数据。另外:5.x以上版本,jdk版本需要1.8及以上,所以需要考虑兼容项目的jdk7版本,具体处理方法在问题汇总处
1、配置文件
位置:../elk/elasticsearch-6.2.3/config/elasticsearch.yml
修改点:cluster 集群 network.name 主机地址 node 节点。。。
2、启动时,root运行Elasticsearch异常。
方法一:在启动脚本中添加环境变量 ES_JAVA_OPTS='-Des.insecure.allow.root=true' (5.x版本后已不支持)
方法二: 新建用户
groupadd elsearch #新建elsearch组
useradd elsearch -g elsearch -p elasticsearch #新建一个elsearch用户
chown -R elsearch:elsearch elasticsearch-6.2.3 #指定elasticsearch所属elsearch组
3、安装head插件(在新版本就不支持下面这样操作了,得新启服务运行head)
下载地址:https://github.com/mobz/elasticsearch-head,在../plugin/解压后重命名为head文件夹即可。
访问地址:localhost:9200/_plugin/head/
4、后台启动elasticsearch
./bin/elasticsearch -d 后台启动,启动日志在 ../logs/clusterName.log
二、Logstash
0、介绍:logstash 是开源的服务器端数据处理管道,可以从多个来源采集数据、转换数据,然后把数据发送到"存储库"(elasticsearch)
1、配置文件
位置:../elk/logstash-6.2.3/config/{自定义文件名}.log (以logstash.log为例)
官网给出配置内容:
input { stdin { } }
output {
elasticsearch { hosts => ["localhost:9200"] }
stdout { codec => rubydebug }
}
具体参数意义及扩展参数,可以看官方文档:https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html
我这里的配置,不过多赘述:
input {
file {
type => "log"
path => "/logs/*.log"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => "127.0.0.1"
index => "log-%{+YYYY.MM.dd}"
}
stdout { codec => rubydebug }
}
2、启动
与elasticsearch不同,可用root启动,并且启动时读取指定配置文件。
../bin/logstash -f ../config/logstash.log
后台启动方式:nohup ./logstash -f ../config/logstash.log > ../logs/out.file 2>&1 &
三、Kibana
0、介绍:可视化 Elasticsearch 中的数据并操作 Elastic Stack
1、配置文件:
位置:../elk/kibana/config/kibana.yml
内容:server.host:kibana运行的ip和端口
elasticsearch.url:elasticsearch部署地址
2、启动
nohup ./kibana > ../logs/out.file 2>&1 &
也可以把上述启动语句用shell执行。
四、问题汇总
1、Elasticsearch和Logstash都需要jdk支持,且高于5.x版本需要jdk8。
工作中项目大多数运行环境为jdk7,所有想要同时运行ELK组件,需要支持双版本jdk。
简单讲,就是修改启动shell中的JAVA_HOME相关参数,
以Elasticsearch为例: elasticsearch中加载elasticsearch-env,其中将以下代码中${JAVA_HOME}替换为实际jdk8(/usr/java/jdk1.8.0_161/)即可。
# now set the path to java
if [ -x "${JAVA_HOME}/bin/java" ]; then
JAVA="${JAVA_HOME}/bin/java"
else
set +e
JAVA=`which java`
set -e
fi
另,Logstash中修改启动文件logstash的 ${JAVACMD}和${JAVA_OPTS}对应值,很简单,看下shell就知道咋改!
2、启动报WARN:“max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]”
在/etc/sysctl.conf 末尾添加 vm.max_map_count=655300,保存并执行 sysctl -p 。重启elasticsearch即可。
3、continue