090、ELK完成部署和使用 (2019-05-13 周二)
上节我们已经部署了容器化的ELK,本节我们学习如何将日志导入ELK并进行图形化展示。
几乎所有的软件和应用都有自己的日志文件,容器也不例外。前面我们已经知道Docker会将容器日志记录到 /va/lib/docker/containers/<containers ID>/<container ID>-json.log ,那么只要我们能够将此文件发送给ELK就可以实现日志管理。
要实现这一步其实不难,因为ELK提供了一个配套小工具 Filebeat ,他能将指定路径下的日志文件转发给ELK。同时Filebeat很聪明,他会监控日志文件,当日志更新时,Filebeat会将新的内容发送给ELK。
安装Filebeat
在Docker Host 中安装和配置 Filebeat(https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation.html)
root@host1:~# curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.0.1-amd64.deb
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13.0M 100 13.0M 0 0 245k 0 0:00:54 0:00:54 --:--:-- 384k
root@host1:~# dpkg -i filebeat-7.0.1-amd64.deb
Selecting previously unselected package filebeat.
(Reading database ... 66499 files and directories currently installed.)
Preparing to unpack filebeat-7.0.1-amd64.deb ...
Unpacking filebeat (7.0.1) ...
Setting up filebeat (7.0.1) ...
Processing triggers for systemd (229-4ubuntu21.21) ...
Processing triggers for ureadahead (0.100.0-19) ...
配置Filebeat
Filebeat 的配置文件为 /etc/filebeat/filebeat.yml 我们需要告诉Filebeat两件事:
1、监控哪些日志文件 -- 见下面paths部分
2、将日志发送到哪里 -- 见下面output部分,可以发送给Elasticsearch,也可以发送给Logstash
root@host1:~# cat /etc/filebeat/filebeat.yml | grep -v '#'
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
- /var/lib/docker/containers/*/*.log
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 1
setup.kibana:
output.elasticsearch:
hosts: ["localhost:9200"]
#output.logstash:
# The Logstash hosts
#hosts: ["localhost:5044"]
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
上面的配置大概如下图所示
启动 Filebeat
安装的时候已经将Filebeat注册为 systemd 的服务,用的时候直接启动即可
root@host1:~# systemctl start filebeat.service
管理日志
Filebeat 启动后,正常情况下会将监控的日志发送给Elasticsearch。刷新Elasticsearch的JSON接口进行确认 http://10.12.31.211:9200/_search?pretty
[root@makeISO shells]# curl -s http://10.12.31.211:9200/_search?pretty | jq .hits.hits[5]
{
"_index": "filebeat-7.0.1-2019.05.13-000001",
"_type": "_doc",
"_id": "325LsWoBFnozufrcnyIX",
"_score": 1,
"_source": {
"@timestamp": "2019-05-13T13:04:41.668Z",
"input": {
"type": "log"
},
"ecs": {
"version": "1.0.0"
},
"host": {
"id": "f30f106ea21f7a1abddeb5fa5c91ad7e",
"containerized": false,
"name": "host1",
"hostname": "host1",
"architecture": "x86_64",
"os": {
"family": "debian",
"name": "Ubuntu",
"kernel": "4.4.0-31-generic",
"codename": "xenial",
"platform": "ubuntu",
"version": "16.04.1 LTS (Xenial Xerus)"
}
},
"agent": {
"version": "7.0.1",
"type": "filebeat",
"ephemeral_id": "f1376a70-18cd-40ec-b48f-b984da5f3de0",
"hostname": "host1",
"id": "fd05d865-a43a-4baf-bbfb-1a82aaceb903"
},
"log": {
"offset": 234,
"file": {
"path": "/var/log/a.log" # 为了方便控制日志的内容,这里将监控日志设置成了/var/log 下的 a.log、b.log、c.log
}
},
"message": "1"
}
}
Kibana 配置 index pattern( http://10.12.31.211:5601)
配置 index pattern 告诉Kibana查询和分析 Elasticsearch中的哪些日志
指定 index pattern 为 filebeat-* ,这与Elasticsearch中的index一致,然后点击下一步按钮
Time Filter field name 选择 @timestamp,然后点击创建按钮
创建完成后,点击左侧的 Discover 按钮,便可以看到收集到的日志。
检索日志内容
root@host1:/var/log# echo test_log_message >> a.log
上面只是简单的将日志导入 ELK 并朴素的显示出来,实际上 ELK 还可以对日志进行归类汇总、分析聚合、创建炫酷的Dashboard等。可以挖掘的内容很多,用法很丰富。