使用 docker-compose 搭建 Prometheus+Grafana 监控系统
使用 docker-compose 搭建 Prometheus+Grafana 监控系统
效果展示
架构
环境准备
角色 |
IP |
软件 |
服务端 |
192.168.1.5 |
node-exporter、cadvisor、Prometheus、Grafana |
客户端 |
192.156.1.4 |
node-exporter、cadvisor、Prometheus |
角色分配
部署服务端
设置Prometheus配置
首先,创建 /docker-file/prometheus/
目录,然后创建 prometheus.yml
,填入如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| global: scrape_interval: 15s evaluation_interval: 15s alerting: alertmanagers: - static_configs: rule_files: scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.1.5:10221'] - job_name: 'node' scrape_interval: 8s static_configs: - targets: ['192.168.1.5:9100'] - job_name: 'cadvisor' scrape_interval: 8s static_configs: - targets: ['192.168.1.5:10223']
|
创建服务端的 docker-compose
继续在服务端中 /docker-file/prometheus/
目录中创建 docker-compose-prometheus.yml
, 添加如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| services: prometheus: image: prom/prometheus container_name: prometheus restart: always volumes: - /docker-file/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml ports: - "10221:9090" grafana: image: grafana/grafana container_name: grafana restart: always ports: - "10220:3000" node_exporter: image: quay.io/prometheus/node-exporter container_name: node_exporter restart: always command: - '--path.rootfs=/host' network_mode: host pid: host volumes: - '/:/host:ro,rslave' cadvisor: image: gcr.io/cadvisor/cadvisor container_name: cadvisor restart: always volumes: - /:/rootfs:ro - /var/run:/var/run:rw - /sys:/sys:ro - /var/lib/docker/:/var/lib/docker:ro ports: - "10223:8080"
|
部署客户端
设置Prometheus配置
首先,创建 /docker-file/prometheus/
目录,然后创建 prometheus.yml
,填入如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| global: scrape_interval: 15s evaluation_interval: 15s alerting: alertmanagers: - static_configs: rule_files: scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.1.4:10442'] - job_name: 'node' scrape_interval: 8s static_configs: - targets: ['192.168.1.4:9100'] - job_name: 'cadvisor' scrape_interval: 8s static_configs: - targets: ['192.168.1.4:10441']
|
创建客户端的 docker-compose
继续在客户端中 /docker-file/prometheus/
目录中创建 docker-compose-prometheus.yml
, 添加如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| services: prometheus: image: prom/prometheus container_name: prometheus restart: always volumes: - /var/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml ports: - "10442:9090" node_exporter: image: quay.io/prometheus/node-exporter container_name: node_exporter restart: always command: - '--path.rootfs=/host' network_mode: host pid: host volumes: - '/:/host:ro,rslave' cadvisor: image: gcr.io/cadvisor/cadvisor container_name: cadvisor restart: always volumes: - /:/rootfs:ro - /var/run:/var/run:rw - /sys:/sys:ro - /var/lib/docker/:/var/lib/docker:ro ports: - "10441:8080"
|
启动 docker-compose
使用下面的命令启动 docker-compose 定义的容器
查看 Prometheus 工作状态
打开 http://192.168.1.5:10221/targets 和 http://192.168.1.4:10442/targets
如果 State 都是 UP 即代表 Prometheus 工作正常,如下图所示:
配置 Grafana
打开 http://192.168.1.5:10220 使用默认账号密码 admin/admin 登录并修改密码后,默认进来是创建数据库的页面
创建数据源
创建图表
创建系统资源图表
创建docker资源图表
和上面一样,Import dashboard时id改成193就行