Prometheus监控docker satck服务
准备所需镜像
拉取所需镜像
docker pull prom/prometheus
docker pull google/cadvisor:latest
docker pull grafana/grafana
启动prometheus
vim /home/project/data/tmp/prometheus.yml
# 写入以下内容
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
rule_files:
# - "first_rules.yml"
# # - "second_rules.yml"
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: "Linux_cadvisor"
static_configs:
- targets: ['124.223.160.39:8080'] # 使用本机器地址,我用的是我自己的公网ip
- job_name: "Docker_grafana"
static_configs:
- targets: ['124.223.160.39:3000'] # 使用本机器地址,我用的是我自己的公网ip
# :wq退出保存
docker run -d --name prometheus -p 9090:9090 -v /home/project/data/tmp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
验证prometheus,打开浏览器输入localhost:9090,如果出现以下界面,则prometheus启动成功
启动cadvisor
# 无需修改参数
docker run -d \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
--publish=8080:8080 \
--detach=true \
--name cadvisor google/cadvisor:latest
验证cadvisor是否启动成功
启动grafana
docker run -d \
--name grafana \
-p 3000:3000 \
grafana/grafana
验证是否启动成功,如果出现该界面,则安装成功
默认账号:admin,默认密码:admin,可以在配置文件中查看(本文是启动的grafana,需要进入容器)
docker exec -it grafana bash
cat /etc/grafana/grafana.ini | grep "= admin$"
# 退出容器ctrl+d
使用grafana来做展示数据
添加数据源
- 点击data sources
- 选择prometheus
- 添加prometheus地址:http://124.223.160.39:9090 (记得更换ip)
- 点击‘save&test’,出现‘Data source is working’则添加成功
添加仪表盘
链仪表盘官网: https://grafana.com
- 点击‘Import’
- 搜索仪表盘,本文用的仪表盘id为‘17023’,如下图所示
仪表盘的地址为:https://grafana.com/grafana/dashboards/17023-docker/
- 回到3000界面,输入grafana ID
- 选择prometheus,点击import
- 点击完成之后,至此可以看到所有的stack服务,并且开始监控