1.场景还原
近日,笔者项目中需要做系统化的日志采集,笔者灵机一动,elk最好不过;今天笔者就es集群搭建作此分享
2.实现方案
①笔者集成的是elasticsearch-2.4.6,下载对应的tar包
②集群配置
节点1配置:
cluster.name: elasticsearch
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["120.26.128.151:9300", "120.26.52.240:9300"]
节点2配置:
cluster.name: elasticsearch
node.name: node-2
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["120.26.128.151:9300", "120.26.52.240:9300"]
效果图
3.日志删除脚本编写
如上图,定期的清理日志文件十分重要,这里笔者就清理三天前的es日志脚本为例
#!/bin/bash
source /etc/profile
#定义删除3天以前的函数
delete_indices(){
check_day=`date -d '-3 days' '+%F'`
index_day=$1
#将日期转换为时间戳
check_day_timestamp=`date -d "$check_day" +%s`
index_day_timestamp=`date -d "$index_day" +%s`
#当索引的时间戳值小于当前日期3天前的时间戳时,删除此索引
if [ ${index_day_timestamp} -lt ${check_day_timestamp} ];then
#转换日期格式
format_date=`echo $1 | sed 's/-/\./g'`
curl -XDELETE http://120.26.52.240:9200/*$format_date
fi
}
curl -XGET http://120.26.52.240:9200/_cat/indices | awk -F" " '{print $3}' | awk -F"-" '{print $NF}' | egrep "[0-9]*\.[0-9]*\.[0-9]*" | sort | uniq | sed 's/\./-/g' | while read LINE
do
#调用索引删除函数
delete_indices $LINE
done
es集群优化,关注博主下期更博;
我是张星,欢迎加入博主技术交流群,群号:526601468