前言
最近在搞公司项目系统架构优化,要弄一套日志监控管理系统,百度一下市面上比较火的就是Elastic公司的ELK框架,即Elasticsearch、Logstash和Kibana,日志监控用的是Elastalert,于是借鉴了一下网上的文章,开启自己搭建日志监控系统之路,顺便记录一下,方便以后忘记了再查阅巩固。 这里还要说明一下,Elasticsearch是用于日志的存储和检索,Logstash用于日志的收集并输出到Elasticsearch存储,Kibana则是前端界面,负责对Elasticsearch中的数据进行展示和可视化分析。前期经过调研发现Logstash日志收集系统比较占用CPU资源,最后选用Filebeat来替代。
集成之路
前提:所有东西都是采用docker直接部署,所以先要保证服务器有docker环境,方便省事。
1.新建网络
保证所有东西运行都在统一网络下
docker network create elk
2.Elasticsearch
2.1 docker拉取Elasticsearch镜像
首先需要部署的就是Elasticsearch,docker先把Elasticsearch镜像拉下来:
docker pull elasticsearch:7.14.1
2.2 创建elasticsearch目录
Elasticsearch既然是用于存储的,首先得让Elasticsearch安装到磁盘最大的路径下,我这里最大的是home路径下:
mkdir -p /home/elasticsearch/{config, data}
2.3 修改Elasticsearch目录权限
因为Elasticsearch默认是不让root用户启动的,所以docker启动的时候默认为将启动用户修改成用户id为1000的系统用户,如果是root用户新建的目录,Elasticsearch启动后是没有操作权限,会启动失败的。
chmod 777 /home/elasticsearch/{config, data}
2.4 创建Elasticsearch配置文件elasticsearch.yml
vim /home/elasticsearch/config/elasticsearch.yml
2.5 配置文件内容:
cluster.name: my-es
node:
name: es-node-0
network.host: 0.0.0.0
http.host: 0.0.0.0
http.port: 9200
xpack.security.enabled: false
http.cors.enabled: true
http.cors.allow-origin: "*"
2.6 docker 启动容器
docker run -d --name elasticsearch --net elk --restart always \
-p 9200:9200 -p 9300:9300 \
-e "ES_JAVA_OPTS=-Xms256m -Xmx256m" \
-e "discovery.type=single-node" \
-v /home/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /home/elasticsearch/data:/usr/share/elasticsearch/data \
elasticsearch:7.14.1
验证是否部署成功,直接访问 http://服务器ip:9200/,返回如下则说明部署成功:
{
"name" : "es-node-0",
"cluster_name" : "my-es",
"cluster_uuid" : "POffkg-FSRma_M50ukFfvg",
"version" : {
"number" : "7.14.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "66b55ebfa59c92c15db3f69a335d500018b3331e",
"build_date" : "2021-08-26T09:01:05.390870785Z",
"build_snapshot" : false,
"lucene_version" : "8.9.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
2.7 docker 部署elasticsearch简便方式
投机取巧的另一种方法,Elasticsearch集群部署,直接一个脚本搞定所有操作,新建脚本docker_create_es.sh**
vim /home/elasticsearch/docker_create_es.sh
脚本内容:
for port in $(seq 0 2); \
do \
mkdir -p /home/elasticsearch/master-${port}/config
mkdir -p /home/elasticsearch/master-${port}/data
chmod -R 777 /home/elasticsearch/master-${port}
cat << EOF > /home/elasticsearch/master-${port}/config/elasticsearch.yml
cluster.name: my-es
node:
name: es-node-${port}
network.host: 0.0.0.0
http.host: 0.0.0.0
http.port: 920${port}
discovery.seed_hosts: ["xx.xx.xx.xx:9300","xx.xx.xx.xx:9301","xx.xx.xx.xx:9302"]
cluster.initial_master_nodes: ["es-node-0"]
xpack.security.enabled: false
http.cors.enabled: true
http.cors.allow-origin: "*"
EOF
docker run --name elasticsearch-node-${port} \
-p 920${port}:920${port} -p 930${port}:930${port} \
--network=elk \
-e ES_JAVA_OPTS="-Xms1g -Xmx1g" \
-v /home/elasticsearch/master-${port}/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /home/elasticsearch/master-${port}/data:/usr/share/elasticsearch/data \
-v /home/elasticsearch/master-${port}/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.14.1
done
3.Kibana
3.1 docker 拉取Kibana镜像:
docker pull kibana:7.14.1
3.2 启动Kibana容器:
docker run -d --name kibana --net elk --restart always -p 5601:5601 kibana:7.14.1
浏览器打开http://服务器ip:5601/,如果页面显示
Kibana server is not ready yet
说明Kibana没有找到Elasticsearch路径,默认还是http://elasticsearch:9200 进入Kibana命令行,修改kibana.yml中的Elasticsearch路径:
docker exec -it kibana /bin/bash
执行完之后显示如下:
[root@localhost home]
bash-4.4$
编辑kibana.yml
vi config/kibana.yml
修改elasticsearch.hosts 重启Kibana,浏览器打开 http://服务器ip:5601/
docker restart kibana
你就可以看到如下界面了
4.Filebeat
4.1 docker 拉取Filebeat镜像:
docker pull elastic/filebeat:7.14.1
4.2 创建Filebeat目录:
mkdir -p /home/filebeat/config
chmod 777 /home/filebeat/config
4.3 新建Filebeat配置文件
vim /home/filebeat/config/filebeat.yml
4.4 配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths:
- /home/docker/containers/1382a029488b1e577986d2d92d801e58112fc2d82c948624fb4cf39851a97f47/1382a029488b1e577986d2d92d801e58112fc2d82c948624fb4cf39851a97f47-json.log
include_lines: ["ERROR", "Exception"]
fields:
type: bootstrap-8008-error
encoding: UTF-8
tail_files: true
json.key_under_root: true
json.overwrite_keys: true
json.message_key: log
json.add_error_key: true
multiline.pattern: '^\d{4}-\d{2}-\d{2}'
multiline.negate: true
multiline.match: after
setup.ilm.enabled: false
setup.kibana:
host: "ip:5601"
output.elasticsearch:
hosts: ['ip:9200', 'ip:9201', 'ip.133:9202']
enable: true
username: elastic
password: SCEceIotSpvTYeaub6X*
indices:
- index: "gateway-bootstrap-error-%{+yyyy.MM.dd}"
when.equals:
fields.type: "bootstrap-8008-error"
4.5 启动Filebeat
docker run -d --name filebeat --restart=always \
--log-driver json-file \
--log-opt max-size=100m \
--log-opt max-file=2 \
-v /home/logs:/home/logs \
-v /home/docker/containers:/home/docker/containers \
-v /home/filebeat/config/filebeat.yml:/usr/share/filebeat/filebeat.yml \
elastic/filebeat:7.14.1
注意,如果Filebeat读取日志失败,多半是Filebeat的启动用户没有日志路径的操作权限,记得chmod 777改一下权限。 至此,EFK部署已完成,打开kibana,就可以看到Filebeat采集的日志索引了 配置一下索引展示,就可以实时查看收集的日志信息了,这里注意下kibana 8.0以上的版本没有Index Pattern这个选项了,变成Date View了,但是创建索引展示信息配置基本都是相同的。
可以看到日志信息了:
5.Elastalert
ElastAlert是一个简单的框架,用于从Elasticsearch中的数据中发出异常,它通过将Elasticsearch与两种类型的组件(规则类型和警报)结合使用。定期查询Elasticsearch,并将数据传递到规则类型,该规则类型确定找到任何匹配项。发生匹配时,它会发出一个或多个警报,这些警报根据不同的类型采取相应的措施。 Elastalert的安装部署需要Python环境,请先提前安装,建议安装Python-3.6.9版本。 从github下载Elastalert源码:
git clone https://github.com/Yelp/elastalert.git
5.1 安装:
cd elastalert/
pip3 install "setuptools>=11.3"
python3 ./setup.py install
pip3 install setuptools-rust
5.2 创建elastalert索引:
elastalert-create-index
5.3 新建elastalert配置config.yaml:
vim /home/elastalert/config.yaml
5.4 config.yaml内容:
rules_folder: /home/elastalert/rules
run_every:
seconds: 60
buffer_time:
minutes: 3
es_host: xx.xx.xx.xx
es_port: 9200
writeback_index: elastalert_status
writeback_alias: elastalert_alerts
alert_time_limit:
days: 2
5.5 cd到规则目录,新建配置告警规则:
cd elastalert/rules
vim frequency.yam
5.6 frequency.yaml内容:
name: app-admin rule
type: frequency
index: app-admin-info-*
num_events: 3
timeframe:
minutes: 2
compare_key: json.log
blacklist:
- "Exception"
- "ERROR"
smtp_host: smtp.163.com
smtp_port: 25
smtp_auth_file: /home/elastalert/config/smtp_auth_file.yaml
email_reply_to: xxxxxxxxxx@163.com
from_addr: xxxxxxxx@xxx.com
alert:
- "elastalert_modules.dingtalk_alert.DingTalkAlerter"
- "email"
dingtalk_webhook: "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
dingtalk_msgtype: "text"
email:
- "xxx@xxx.com"
alert_text_type: alert_text_only
alert_subject: "监控告警"
alert_text: "您有一条告警信息: \n时间:{0}\n级别:高\n关键字:ERROR、Exception \nIP:{3} \n轮询时间:2分钟 \n限制次数:{4}次\n出现次数: {2}次 \n请及时登录Kibana查看\n地址: http://ip:5601/ \n主要信息如下:\n\n{1} \n "
alert_text_args:
- "@timestamp"
- json.log
- num_hits
- fields.ip
- num_events
5.7 配置邮箱认证文件:
vim /home/elastalert/config/smtp_auth_file.yaml
5.8 smtp_auth_file.yaml内容:
user: xxxxxx@163.com
password: XXXXXXXXXXXX
5.9 测试告警规则:
elastalert-test-rule rules/frequency.yaml
5.10 配置钉钉告警还需要安装钉钉插件:
wget https://github.com/xuyaoqiang/elastalert-dingtalk-plugin/archive/master.zip
unzip elastalert-dingtalk-plugin-master.zip
cd elastalert-dingtalk-plugin-master
pip3 install pyOpenSSL==16.2.0
pip3 install setuptools==46.1.3
cp -r elastalert_modules /home/elastalert/
5.11 后台启动elastalert:
nohup python -m elastalert.elastalert --config /home/elastalert/config.yaml --rule /home/elastalert/rules/frequency.yaml 2>&1 &
5.12 监控告警效果:
|