轉載來源 :ELK根據服務類型收集多個容器數據 : https://www.jianshu.com/p/29ad69776ae5
1.安裝docker-compose
yum install -y python2-pip ##安裝pip工具
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U ##下載加速器
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple ##使用加速器
pip install docker-compose ##使用pip工具安裝docker-compose
2.編輯compose文件
[root@db02 ~]# cat docker-compose.yaml
version: '3'
services:
nginx:
image: nginx:latest
#設置labels
labels:
service: game
# logging設置增加lables.service
logging:
options:
labels: "service"
ports:
- "8080:80"
db:
image: nginx:latest
# 設置lables
labels:
service: school
# logging設置增加lables.service
logging:
options:
labels: "service"
ports:
- "80:80"
3.使用docker-compose啓動容器
[root@db02 ~]# docker-compose up & ##啓動容器並放入後臺
[root@db02 ~]# docker ps -a ##查看容器啓動情況
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20cb4626d034 nginx:latest "/docker-entrypoint.…" 36 seconds ago Up 22 seconds 0.0.0.0:80->80/tcp root_db_1
ab5c8bb167df nginx:latest "/docker-entrypoint.…" 36 seconds ago Up 23 seconds 0.0.0.0:8080->80/tcp root_nginx_1
4.修改kibana配置文件並重啓filebeat
[root@db02 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
paths:
- /var/lib/docker/containers/*/*-json.log
json.keys_under_root: true
json.overwrite_keys: true
setup.kibana:
host: "172.16.210.53:5601"
output.elasticsearch:
hosts: ["172.16.210.53:9200"]
indices:
- index: "docker-game-access-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
attrs.service: "game"
stream: "stdout"
- index: "docker-game-error-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
attrs.service: "game"
stream: "stderr"
- index: "docker-school-access-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
attrs.service: "school"
stream: "stdout"
- index: "docker-school-error-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
attrs.service: "school"
stream: "stderr"
setup.template.name: "docker"
setup.template.pattern: "docker-*"
setup.template.enabled: false
setup.template.overwrite: true
[root@db02 ~]# systemctl restart filebeat
5.回到kibana界面依次添加索引
點擊discover,查看數據