使用docker方式构建prometheus监控的学习

一、背景:近期学习部署prometheus监控系统,经研究发现prometheus提供docker运行模式。根据我的经验,能够使用docker模式构建系统一定多快好省。

二、环境:

1、centos7.5虚拟机一台,分配4G内存,拟作prometheus服务器,ip:192.168.0.208

2、centos8.0虚拟机一台,分配2G内存,拟作node-exporter客户机,ip:192.168.0.202

3、两台机器都在不同的实体机上,已安装docker-ce软件

三、部署过程

1、服务器(考虑安装prometheus服务器和node-exporter客户端):

(1)pull服务器镜像:

docker pull prom/prometheus

(2)pull客户端镜像:

docker pull prom/node-exporter

(3)配置prometheus.yml

mkdir -p /root/prometheus/prometheus-data          #prometheus的工作目录和数据目录

mkdir -p /root/prometheus/node-exporter-data       #node-exporter的数据目录

cd /root/prometheus

vi promethe.yml

内容如下:

...

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: ['localhost:9090']

- job_name: 'prometheus-node-exporter'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['192.168.0.208:9100']

- job_name: 'node-dell5460'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['192.168.0.202:9100']

(4)运行prometheus服务

[root@ELK prometheus]# docker run -d -p 9090:9090 -v $PWD/prometheus.yml:/etc/prometheus/prometheus.yml -v $PWD/prometheus-data:/prometheus  --hostname ELK.localdomain --name prometheus prom/prometheus

说明:

-p 9090:9090     #服务端口对应到宿主机的相同端口

-v $PWD/prometheus.yml:/etc/prometheus/prometheus.yml    #容器内/etc/prometheus/prometheus.yml配置文件挂载到宿主机/root/prometheus目录下。

-v $PWD/prometheus-data:/prometheus    #容器内prometheus工作目录挂载到宿主机的/root/prometheus/prometheus-data目录下。

--hostname ELK.localdomain   #容器的主机名称(ELK.localdomain是208机的主机名),若不加这个选项,docker会自动把容器的短id号作为容器主机名,在web页面就会发生无法访问的问题。

测试:curl http://localhost:9090/metrics

(5)运行node-exporter

[root@ELK prometheus]# docker run -d   --net="host"   --pid="host"   -v "/root/prometheus/node-exporter-data:/host:ro,rslave"   prom/node-exporter   --path.rootfs=/host

测试:curl http://192.168.0.208:9100/metrics

2、node-exporter客户机

(1)pull客户端镜像

 docker pull prom/node-exporter

(2)运行node-exporter

[root@ELK prometheus]# docker run -d   --net="host"   --pid="host"   -v "/home/node-exporter-data:/host:ro,rslave"   prom/node-exporter   --path.rootfs=/host

3、访问测试:

在浏览器中输入网址:http://192.168.0.208:9090

 四、使用

1、metric的表现形式是键值对{k,v}

2、metrics的页面说明:

#HELP 关于指标的说明

#TYPE 指标的类型,常见gauge,conter等类型

键  值     以空格隔开

3、把metrics页面存在的键拷贝后粘贴到graph页面的搜索栏中即可看到值或图形。

4、在搜索栏中可灵活应用各类函数,如rate、increase、sum、topk、等等

例:

rate(node_cpu_seconds_total{mode="user"}[1m])   #1分钟内用户态cpu时间每秒增加量

rate(node_cpu_seconds_total{mode="system",instance="192.168.0.208:9100",job="prometheus-node-exporter"}[1m])

(1-(sum(increase(node_cpu_seconds_total{mode="idle"}[1m])) by(instance))  /  (sum(increase(node_cpu_seconds_total[1m])) by(instance)))*100   #每台机器1分钟内cpu负荷

topk(3,rate(node_network_receive_bytes_total[5m]))  #网络5分钟内平均每秒接收字节数前3位的数据展示

5、关于web页面的时间显示问题,默认页面显示的是UTS时区时间,与本地时间相差8小时。这是因为Prometheus 为避免时区混乱,在所有组件中专门使用 Unix Time 和 Utc 进行显示。不支持在配置文件中设置时区,也不能读取本机 /etc/timezone 时区。prometheus在新版web页面已提供本地时区时间显示功能,可点击web页面右上角“Try experimental React UI”切换到新版页面,在新版页面上部勾选“Use local time”即可。

 

附:更多的函数参考 https://prometheus.io/docs/prometheus/latest/querying/functions/

 

posted @ 2020-05-20 17:27  sfccl  阅读(973)  评论(0编辑  收藏  举报