Prometheus+Grafana+Docker
# Prometheus+Grafana+Docker
# 搭建Docker环境
# 1.安装yum工具包
$ yum install -y yum-utils device-mapper-persistent-data lvm2
# 2.拉取Docker的repo源
$ yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 3.安装 DOCKER CE
$ yum -y install docker-ce
启动 docker
$ systemctl start docker && systemctl enable docker
# 4.配置镜像仓库
阿里云镜像加速地址: https://ably8t50.mirror.aliyuncs.com
$ vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://ably8t50.mirror.aliyuncs.com"]
}
加载配置重启服务
$ systemctl daemon-reload
$ systemctl restart docker
# Prometheus
# 1.基础准备
创建prometheus文件夹里面存放prometheus.yml的配置文件,然后需要添加基础的配置
。
prometheus的默认是支持http和metrics结尾的
,所以不需要加前缀和后缀。
job就是创建一个作业
,需要创建一个job_name,然后指定你的configs,目标就是指定你的targets需要监控的客户端地址
。
[root@localhost ~]# mkdir /root/prometheus/
[root@localhost ~]# vim /root/prometheus/prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
# 2.启动Prometheus的容器
[root@localhost prometheus]# docker run -d \
-p 9090:9090 \
--restart always \
--name prometheus \
-v /root/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-e TZ=Asia/Shanghai \
-v /etc/localtime:/etc/localtime \
prom/prometheus
d98bf61589ed68dcabe175bd54e26f0545aad4c9407e27a43f2cd5e92078c294
[root@localhost prometheus]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d98bf61589ed prom/prometheus "/bin/prometheus --c…" 2 seconds ago Up 1 second 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus
# 3.访问Prometheus
使用http://ip:9090访问默认页面即可
# Cadvisor
cadvisor监控指标和prometheus集成融合,Prometheus就可以支持监控docker的性能指标。
# 1.启动cadvisor容器
[root@localhost ~]# docker run -d \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:rw \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
-p 8080:8080 \
--detach=true \
--name=cadvisor
google/cadvisor:latest
ed1afcab2ecd83217d5c6b46dc8d595d349bb24cf431f8ba7d64c7d877a3ac5d
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ed1afcab2ecd google/cadvisor:latest "/usr/bin/cadvisor -…" 4 seconds ago Up 3 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp cadvisor
d98bf61589ed prom/prometheus "/bin/prometheus --c…" 18 minutes ago Up 18 minutes 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus
# 2.访问cadvisor
使用默认端口访问http://ip:8080和http://ip:8080/metrics
如何查看该节点的监控指标,可以访问/metrics的
# 3.配置Prometheus
能够成功查看到docker的监控指标
,然后需要更改Prometheus的配置文件,我们需要添加如下内容
- job_name: "container" #创建一个container的job
static_configs:
- targets: ["192.168.1.104:8080"] #我添加了一个docker的metrics性能指标的地址
添加在配置文件后如下:
[root@localhost ~]# cat /root/prometheus/prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "container"
static_configs:
- targets: ["192.168.1.104:8080"]
这时候需要重新启动我们的Prometheus
[root@localhost ~]# docker restart prometheus
prometheus
该节点全部起来之后访问如下:
# Grafana
# 1.创建数据卷
[root@localhost ~]# mkdir /root/grafana && chmod 777 grafana/
# 2.启动Grafana容器
[root@localhost ~]# docker run -d \
-p 3000:3000 \
--restart=always \
--name=grafana \
-v /root/grafana:/var/lib/grafana \
-e TZ=Asia/Shanghai \
-v /etc/localtim:/etc/localtime \
grafana/grafana
bdbe72abdac4ce96f4aeb90361e93607573652a73cc906b091369df49e184fc2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bdbe72abdac4 grafana/grafana "/run.sh" 3 minutes ago Up 3 minutes 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp grafana
ed1afcab2ecd google/cadvisor:latest "/usr/bin/cadvisor -…" 4 seconds ago Up 3 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp cadvisor
d98bf61589ed prom/prometheus "/bin/prometheus --c…" 18 minutes ago Up 18 minutes 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus
# 3.配置Grafana
这里需要添加prometheus的源
访问grafana的地址,http://ip:3000
添加一个data
,点击DATA SOURCE
选择Prometheus,然后添加url和name
# 4.导入模板
从上面的cadvisor可以看到,我们的数据源已经过来了,所以需要导入模板。
模板编号:395 13631 14841
# Node_exporter
用于监控Linux的服务器。
# 1.启动容器
[root@localhost ~]# docker run -d \
--name node_exporter \
--restart=always \
--net="host" \
--pid="host" \
-v "/proc:/host/proc:ro" \
-v "/sys:/host/sys:ro" \
-v "/:/rootfs:ro" \
-e TZ=Asia/Shanghai \
-v /etc/localtime:/etc/localtime \
prom/node-exporter \
--path.procfs=/host/proc \
--path.rootfs=/rootfs \
--path.sysfs=/host/sys \
--collector.filesystem.ignored-mount-points='^/(sys|proc|dev|host|etc)($$|/)'
# 2.与Prometheus集成
在Prometheus的配置文件后面再添加一个监控端
[root@localhost ~]# cat /root/prometheus/prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "container"
static_configs:
- targets: ["192.168.1.104:8080"]
- job_name: "node"
static_configs:
- targets: ["192.168.1.104:9100"]
重启容器
[root@localhost ~]# docker restart prometheus
prometheus
# 3.查看访问
可以查看到当前已经有两个地址了。
# 4.配置Grafana
然后一样的导入需要的模板
模板编号:9276 8919 1860
上次更新: 2023/11/28, 22:03:59