Prometheus的docker部署搭建

Prometheus的docker部署

一、安装docker环境

安装docker

镜像加速

1
2
3
4
5
6
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["http://hub-mirr.c.163.com"]
}
EOF

安装docker

1
2
3
4
5
export DOWNLOAD_URL="http://mirrors.163.com/docker-ce" 
curl -fsSL https://get.docker.com | sh

systemctl enable docker
systemctl start docker

检查

1
2
3
4
5
6
7
8
docker -v
systemctl status docker

安装自动补全插件
yum install -y bash-completion

source /usr/share/bash-completion/completions/docker
source /usr/share/bash-completion/bash_completion

安装docker-compose

1
2
3
4
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uame -s`-`uname -m` > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

或者直接yum install -y docker-compose

检查

1
docker-compose -v

二、docker-compose安装prometheus

1、手动创建docker-compose和配置文件

创建prometheus监控文件夹

1
2
3
mkdir /data/docker-prometheus -p
mkdir /data/docker-prometheus/{grafana,prometheus,alertmanager} -p
cd /data/docker-prometheus/

创建alertmanager配置文件

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
vim alertmanager/config.ymls
#通过 set paste 方式粘贴

global:
#163服务器
smtp_smarthost: 'smtp.163.com:465'
#发邮件的邮箱
smtp_from: 'test1@163.com'
#发邮件的邮箱用户名,也就是你的邮箱     
smtp_auth_username: 'test1@163.com'
#发邮件的邮箱密码
smtp_auth_password: 'your-password'
#进行tls验证
smtp_require_tls: false

route:
group_by: ['alertname']
# 当收到告警的时候,等待group_wait配置的时间,看是否还有告警,如果有就一起发出去
group_wait: 10s
# 如果上次告警信息发送成功,此时又来了一个新的告警数据,则需要等待group_interval配置的时间才可以发送出去
group_interval: 10s
# 如果上次告警信息发送成功,且问题没有解决,则等待 repeat_interval配置的时间再次发送告警数据
repeat_interval: 10m
# 全局报警组,这个参数是必选的
receiver: email

receivers:
- name: 'email'
#收邮件的邮箱
email_configs:
- to: 'test1@163.com'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']

新建grafana的配置文件

1
2
3
4
vim grafana/config.monitoring

GF_SECURITY_ADMIN_PASSWORD=password
GF_USERS_ALLOW_SIGN_UP=false

新建prometheus的配置文件

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
40
41
42
vim prometheus/prometheus.yml
#通过 set paste 方式粘贴

# 全局配置
global:
scrape_interval: 15s # 将搜刮间隔设置为每15秒一次。默认是每1分钟一次。
evaluation_interval: 15s # 每15秒评估一次规则。默认是每1分钟一次。

# Alertmanager 配置
alerting:
alertmanagers:
- static_configs:
- targets: ['alertmanager:9093']

# 报警(触发器)配置
rule_files:
- "alert.yml"

# 搜刮配置
scrape_configs:
- job_name: 'prometheus'
# 覆盖全局默认值,每15秒从该作业中刮取一次目标
scrape_interval: 15s
static_configs:
- targets: ['localhost:9090']
- job_name: 'alertmanager'
scrape_interval: 15s
static_configs:
- targets: ['alertmanager:9093']
- job_name: 'cadvisor'
scrape_interval: 15s
static_configs:
- targets: ['cadvisor:8080']
labels:
instance: Prometheus服务器

- job_name: 'node-exporter'
scrape_interval: 15s
static_configs:
- targets: ['node_exporter:9100']
labels:
instance: Prometheus服务器

创建alert报警文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vim prometheus/alert.yml

groups:
- name: Prometheus alert
rules:
# 对任何实例超过30秒无法联系的情况发出警报
- alert: 服务告警
expr: up == 0
for: 30s
labels:
severity: critical
annotations:
summary: "服务异常,实例:{{ $labels.instance }}"
description: "{{ $labels.job }} 服务已关闭"

新建docker-compose.yml文件

prometheus docker hub 最新版本查看地址

1
https://hub.docker.com/r/prom/prometheus/tags

alertmanager docker hub 最新版本查看地址

1
https://hub.docker.com/r/prom/alertmanager/tags

node-exporter docker hub 最新版本查看地址

1
https://hub.docker.com/r/prom/node-exporter/tags
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
vim docker-compose.yml

version: '3.3'

volumes:
prometheus_data: {}
grafana_data: {}

networks:
monitoring:
driver: bridge

services:
prometheus:
image: prom/prometheus:v2.37.6
container_name: prometheus
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- ./prometheus/:/etc/prometheus/
- prometheus_data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
- '--web.console.templates=/usr/share/prometheus/consoles'
#热加载配置
- '--web.enable-lifecycle'
#api配置
#- '--web.enable-admin-api'
#历史数据最大保留时间,默认15天
- '--storage.tsdb.retention.time=30d'
networks:
- monitoring
links:
- alertmanager
- cadvisor
- node_exporter
expose:
- '9090'
ports:
- 9090:9090
depends_on:
- cadvisor

alertmanager:
image: prom/alertmanager:v0.25.0
container_name: alertmanager
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- ./alertmanager/:/etc/alertmanager/
command:
- '--config.file=/etc/alertmanager/config.yml'
- '--storage.path=/alertmanager'
networks:
- monitoring
expose:
- '9093'
ports:
- 9093:9093

cadvisor:
image: google/cadvisor:latest
container_name: cadvisor
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
networks:
- monitoring
expose:
- '8080'

node_exporter:
image: prom/node-exporter:v1.5.0
container_name: node-exporter
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
- '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc|rootfs/var/lib/docker)($$|/)'
networks:
- monitoring
ports:
- '9100:9100'

grafana:
image: grafana/grafana:9.4.3
container_name: grafana
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- grafana_data:/var/lib/grafana
- ./grafana/provisioning/:/etc/grafana/provisioning/
env_file:
- ./grafana/config.monitoring
networks:
- monitoring
links:
- prometheus
ports:
- 3000:3000
depends_on:
- prometheus

访问

1
2
3
4
http://192.168.224.12:9090/
http://192.168.224.12:3000
http://192.168.224.12:9093
http://192.168.224.12:9100/metrics

各容器及应用

Prometheus 采集数据

Grafana 用于图表展示

alertmanager 用于接收 Prometheus发送的告警信息

node-exporter 用于收集操作系统的硬件信息的metrics

三、使用grafana暂时prometheus的图形

1、登录Grafana

登录Grafana

1
2
3
http://192.168.224.12:3000
用户名: admin
密码: password

2、创建Promethus数据源

在Grafana中创建Prometheus数据源

1.单击边栏的“齿轮”,打开配置菜单

2.单击数据源

3.单击添加数据源

4.选择”Prometheus”作为类型

5.设置适当的Prometheus服务器URL(例如: http://prometheus:9090)

6.单击保持并测试。

如下图

p9KxSWd.png

3、创建仪表盘

从Grafana.com导入仪表盘

打开官网。在搜索栏输入node exporter回车,点击下载量最大的

1
https://grafana.com/grafana/dashboards/s

p9KvqL6.png

复制ID

1
1860

然后导入仪表盘

p9KvOeK.png

填写id

p9KxCQI.png

修改名字,确认无误就点击Import导入。

p9KxAw8.png

最后效果

p9KxuSs.png

评论


:D 一言句子获取中...

加载中,最新评论有1分钟缓存...