Prometheus搭建
介绍
- Prometheus 采集数据
- Grafana 用于图表展示
- redis_exporter 用于收集redis的metrics
- node-exporter 用于收集操作系统和硬件信息的metrics
- cadvisor 用于收集docker的相关metrics
创建目录
madir /Users/myself/vagrant/php/machine/Pro1/docker_lnmp/data madir /Users/myself/vagrant/php/machine/Pro1/docker_lnmp/data/prometheus
创建文件
/Users/myself/vagrant/php/machine/Pro1/docker_lnmp/data/prometheus/docker-compose-prometheus.yml
version: '3'
networks:
monitor:
driver: bridge
services:
prometheus:
image: prom/prometheus
container_name: prometheus
hostname: prometheus
restart: always
volumes:
- /Users/myself/vagrant/php/machine/Pro1/docker_lnmp/data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- /Users/myself/vagrant/php/machine/Pro1/docker_lnmp/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
redis-exporter:
image: oliver006/redis_exporter
container_name: redis_exporter
hostname: redis_exporter
restart: always
ports:
- "9121:9121"
networks:
- monitor
command:
- '--redis.addr=redis://10.10.170.161:7000'
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
- /Users/myself/vagrant/php/machine/Pro1/docker_lnmp/data/prometheus/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."
- /Users/myself/vagrant/php/machine/Pro1/docker_lnmp/data/prometheus/prometheus.yml
下面的10.10.170.161 要替换为你自己的ip地址,ifconfig里面查看,不要使用127.0.0.1
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ['10.10.170.161:9093']
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "node_down.yml"
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['10.10.170.161:9090']
- job_name: 'redis'
static_configs:
- targets: ['10.10.170.161:9121']
labels:
instance: redis
- job_name: 'node'
scrape_interval: 8s
static_configs:
- targets: ['10.10.170.161:9100']
labels:
instance: node
- job_name: 'cadvisor'
static_configs:
- targets: ['10.10.170.161:8088']
labels:
instance: cadvisor
执行
docker-compose -f /data/prometheus/docker-compose-prometheus.yml up -d
结果如下
luwei@luweideMacBook-Pro-2 data % docker-compose -f /data/prometheus/docker-compose-prometheus.yml up -d
ERROR: .FileNotFoundError: [Errno 2] No such file or directory: '/data/prometheus/docker-compose-prometheus.yml'
luwei@luweideMacBook-Pro-2 data % docker-compose -f ./prometheus/docker-compose-prometheus.yml up -d
Creating network "prometheus_monitor" with driver "bridge"
Pulling prometheus (prom/prometheus:)...
latest: Pulling from prom/prometheus
554879bb3004: Pull complete
9ec611452210: Pull complete
40db3eda64c7: Pull complete
120bf1bca03e: Pull complete
11b7e0fd2f39: Pull complete
64ae9934f737: Pull complete
8423b9ae0b0f: Pull complete
feb3b27a668d: Pull complete
34219829b1aa: Pull complete
5ae59c937f71: Pull complete
b7bd2ca89696: Pull complete
fd22bf019eb5: Pull complete
Digest: sha256:b37103e03399e90c9b7b1b2940894d3634915cf9df4aa2e5402bd85b4377808c
Status: Downloaded newer image for prom/prometheus:latest
Pulling grafana (grafana/grafana:)...
latest: Pulling from grafana/grafana
59bf1c3509f3: Already exists
1c4ed2a446cf: Pull complete
44e84ab477f2: Pull complete
4ee12547c788: Pull complete
50340cf5313f: Pull complete
bfddf8821805: Pull complete
baf8ea076994: Pull complete
24317602c619: Pull complete
13378d02925f: Pull complete
Digest: sha256:32fb8dc203c770b23befb53147c9984bf36a215d7cff66eec0aaa9911fd3df40
Status: Downloaded newer image for grafana/grafana:latest
Pulling redis-exporter (oliver006/redis_exporter:)...
latest: Pulling from oliver006/redis_exporter
b526fe7e10a1: Pull complete
59c05c4ebd6b: Pull complete
d08d48ae3fe9: Pull complete
Digest: sha256:9408c7f95248afc566646b9ade72cb6270a9ef9618ff90d108086ce023ac336f
Status: Downloaded newer image for oliver006/redis_exporter:latest
Pulling node-exporter (quay.io/prometheus/node-exporter:)...
latest: Pulling from prometheus/node-exporter
aa2a8d90b84c: Pull complete
b45d31ee2d7f: Pull complete
b5db1e299295: Pull complete
Digest: sha256:f2269e73124dd0f60a7d19a2ce1264d33d08a985aed0ee6b0b89d0be470592cd
Status: Downloaded newer image for quay.io/prometheus/node-exporter:latest
Pulling cadvisor (google/cadvisor:latest)...
latest: Pulling from google/cadvisor
ff3a5c916c92: Already exists
44a45bb65cdf: Pull complete
0bbe1a2fe2a6: Pull complete
Digest: sha256:815386ebbe9a3490f38785ab11bda34ec8dacf4634af77b8912832d4f85dca04
Status: Downloaded newer image for google/cadvisor:latest
Creating node-exporter ... done
Creating grafana ... done
Creating cadvisor ... done
Creating redis_exporter ... done
Creating prometheus ... done
luwei@luweideMacBook-Pro-2 data %
查看
luwei@luweideMacBook-Pro-2 docker_lnmp % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be3b741fade6 google/cadvisor:latest "/usr/bin/cadvisor -…" About an hour ago Up About an hour 0.0.0.0:8088->8080/tcp cadvisor
4cd966bf14e5 quay.io/prometheus/node-exporter "/bin/node_exporter" About an hour ago Up About an hour 0.0.0.0:9100->9100/tcp node-exporter
c95af9515833 prom/prometheus "/bin/prometheus --c…" About an hour ago Restarting (2) 51 seconds ago prometheus
7d4a2a13587e oliver006/redis_exporter "/redis_exporter --r…" About an hour ago Up About an hour 0.0.0.0:9121->9121/tcp redis_exporter
c3778d133128 grafana/grafana "/run.sh" About an hour ago Up About an hour 0.0.0.0:3000->3000/tcp grafana
f6316312c7f3 registry.cn-hangzhou.aliyuncs.com/google_containers/kicbase:v0.0.18 "/usr/local/bin/entr…" 3 weeks ago Up 2 weeks 127.0.0.1:49682->22/tcp, 127.0.0.1:49683->2376/tcp, 127.0.0.1:49680->5000/tcp, 127.0.0.1:49681->8443/tcp, 127.0.0.1:49679->32443/tcp minikube
92b5e1563062 mysql:5.7.16 "docker-entrypoint.s…" 4 months ago Up 2 weeks 0.0.0.0:3307->3306/tcp my_mysql57
f212f81e0546 redis:4-alpine "docker-entrypoint.s…" 4 months ago Up 2 weeks (healthy) 0.0.0.0:6379->6379/tcp redis
luwei@luweideMacBook-Pro-2 docker_lnmp % pwd
本作品采用《CC 协议》,转载必须注明作者和本文链接