Prometheus

prometheus pushgateway收集数据:
服务端配置

1
2
docker pull prom/pushgateway
docker run -d -p 9091:9091 prom/pushgateway

客户端逻辑:

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
#!/usr/bin/env python 
# -*- coding: utf-8 -*-
# @Time : 2021/12/14 11:08 AM
# @Author : Hailin.Fu
import os
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway

registry = CollectorRegistry()

labels = [
'instance_id',
'state'
]
gauge_test_metrics = Gauge("test_metrics", "Random count", labels, registry=registry)

test_list = [
{
"instance_id": "i-001",
"state": "running",
},
{
"instance_id": "i-001",
"state": "running",
},
{
"instance_id": "i-001",
"state": "stopped",
},
{
"instance_id": "i-001",
"state": "terminated",
},
{
"instance_id": "i-001",
"state": "waiting",
}
]

for instance in test_list:
gauge_test_metrics.labels(
instance['instance_id'],
instance['state'],
).set(12)
push_to_gateway("10.11.11.107:9091", job='ec2_stats', grouping_key={"instance": "i-001"}, registry=registry)

服务 端口 是否有web界面 作用
prometheus 9090 有 作为监控主服务器,拉取并存储时序数据
node-exporter 9100 有 收集内核公开的硬件和操作系统指标
grafana 3000 有 数据展示可视化
cadvisor 9595 有 收集宿主机上运行的所有容器的性能数据

一、安装prometheus server

docker pull prom/prometheus

# 拉取prometheus镜像

docker run -d –name prometheus2020 prom/prometheus

# 启动容器

docker cp -a prometheus2020:/etc/prometheus/ $PWD/prometheus

# 复制容器内部的配置文件到宿主机,不用事先创建$PWD/prometheus目录

docker rm -f prometheus2020

# 删除容器

docker run -d –name prometheus2020 -p 9090:9090 -v $PWD/prometheus:/etc/prometheus prom/prometheus
# 启动容器

firewall-cmd –zone=public –add-port=9090/tcp –permanent

firewall-cmd –reload

# 开放防火墙9090端口

访问

http://192.168.1.113:9090

prometheus使用的是UTC时间,比当前的北京时间晚8个小时,可勾选Use local time选项使用本地时间

![](https://blogimgs-1256340525.cos.ap-chengdu.myqcloud.com/images/Screen Shot 2021-12-13 at 2.45.59 PM.png)

二、安装node-exporter

docker pull prom/node-exporter

# 拉取镜像

docker run -d –name node-exporter2020 -p 9100:9100 -v “/proc:/host/proc:ro” -v “/sys:/host/sys:ro” -v “/:/rootfs:ro” –net=”host” prom/node-exporter

# 启动容器

firewall-cmd –zone=public –add-port=9100/tcp –permanent

firewall-cmd –reload

# 开放防火墙9100端口

访问

http://192.168.1.113:9100/metrics

img

vim prometheus/prometheus.yml

# 修改宿主机prometheus配置文件,重启后会同步到容器内部

img

1
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: [``'192.168.1.113:9090'``]``   ``labels:``     ``instance: prometheus` ` ``-` `job_name: ``'centos_1'``  ``static_configs:``  ``-` `targets: [``'192.168.1.113:9100'``]``   ``labels:``     ``instance: centos_1

docker restart prometheus2020

重启容器

此时再访问

http://192.168.1.113:9090/targets

img

三、安装grafana

docker pull grafana/grafana

# 拉取镜像

mkdir grafana

# 在宿主机的当前目录下创建grafana目录

chmod 777 -R ./grafana/

# 赋予grafana目录读写权限

# grafana是以grafana用户启动,不是以root用户启动

docker run -d –name=grafana2020 -p 3000:3000 -v $PWD/grafana:/var/lib/grafana grafana/grafana
# 启动容器

firewall-cmd –zone=public –add-port=3000/tcp –permanent

firewall-cmd –reload

# 开放防火墙3000端口

访问

http://192.168.1.113:3000

用户名:admin

密码:admin

img

修改密码为:123456

并二次确认

img

首页:

img

添加数据源,选择Prometheus:

img

配置Prometheus数据源:

img

测试一下并保存:

img

配置模板

可在官网搜索一个模板

https://grafana.com/grafana/dashboards?dataSource=prometheus

选择8919这个模板

https://grafana.com/grafana/dashboards/8919

img

img

img

img

四、安装cadvisor

docker pull google/cadvisor

# 拉取镜像

docker run -d
–name=cadvisor2020
–volume=/:/rootfs:ro
–volume=/var/run:/var/run:rw
–volume=/sys:/sys:ro
–volume=/var/lib/docker/:/var/lib/docker:ro
–volume=/cgroup:/cgroup:ro
–publish=9595:8080
–detach=true
–privileged=true
google/cadvisor

# 启动容器

firewall-cmd –zone=public –add-port=9595/tcp –permanent

firewall-cmd –reload

# 开放防火墙9595端口

访问

http://192.168.1.113:9595/

img

http://192.168.1.113:9595/metrics

img