Prometheus + Grafana
功能简介
- Prometheus:采集数据
- Grafana :图表展示数据
- node-exporter:用于收集操作系统和硬件信息的 metrics
- cadvisor:用于收集 docker 的相关 metrics
环境准备
主机 | IP | 角色 | 软件 |
---|---|---|---|
监控主机:docker01 | 10.0.0.101 | 普罗米修斯服务端 | Prometheus、node-exporter、cadvisor、Grafana |
被监控节点:docker02 | 10.0.0.102 | 普罗米修斯客户端 | node-exporter、cadvisor |
部署 Prometheus 和 Grafana
创建文件夹
mkdir /data/prometheus/
cd /data/prometheus/
创建prometheus.yml
注:根据实际情况修改IP地址
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: ['10.0.0.101:9093']
rule_files:
- "node_down.yml"
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['10.0.0.101:9090']
- job_name: 'node'
scrape_interval: 8s
static_configs:
- targets: ['10.0.0.101:9100', '10.0.0.102:9100']
- job_name: 'cadvisor'
scrape_interval: 8s
static_configs:
- targets: ['10.0.0.101:8088', '10.0.0.102:8088']
创建node_down.yml
groups:
- name: node_down
rules:
- alert: InstanceDown
expr: up == 0
for: 1m
labels:
user: test
annotations:
summary: "Instance {{ $labels.instance }} down"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."
在docker01服务器上创建docker-compose.yml
version: '2'
networks:
monitor:
driver: bridge
services:
prometheus:
image: prom/prometheus
container_name: prometheus
hostname: prometheus
restart: always
volumes:
- /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- /data/prometheus/node_down.yml:/etc/prometheus/node_down.yml
ports:
- "9090:9090"
networks:
- monitor
grafana:
image: grafana/grafana
container_name: grafana
hostname: grafana
restart: always
ports:
- "3000:3000"
networks:
- monitor
node-exporter:
image: quay.io/prometheus/node-exporter
container_name: node-exporter
hostname: node-exporter
restart: always
ports:
- "9100:9100"
networks:
- monitor
cadvisor:
image: google/cadvisor:latest
container_name: cadvisor
hostname: cadvisor
restart: always
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
ports:
- "8088:8080"
networks:
- monitor
在docker02服务器上创建docker-compose.yml
version: '2'
networks:
monitor:
driver: bridge
services:
node-exporter:
image: quay.io/prometheus/node-exporter
container_name: node-exporter
hostname: node-exporter
restart: always
ports:
- "9100:9100"
networks:
- monitor
cadvisor:
image: google/cadvisor:latest
container_name: cadvisor
hostname: cadvisor
restart: always
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
ports:
- "8088:8080"
networks:
- monitor
在docker01和docker02服务器上分别启动docker-compose
// 拉取镜像并启动相关容器
docker-compose up -d
// 验证容器是否启动成功
docker ps
验证是否安装成功
在浏览器中输入:http:安装Prometheus的服务器IP:9090/targets,如果State 都是up 即代码Prometheus 安装成功
配置Grafana
在浏览器中输入http:安装Prometheus的服务器IP:3000
默认登录密码:admin/admin
添加报表模板
在浏览器中输入 https://grafana.com/grafana/dashboards/
挑选合适的模版,并将其json文件下载
导入报表模板