prometheus是什么?
是一个监控软件-》监控容器非常好,也可以监控其他的非容器的业务,例如:MySQL,nginx,redis,系统等
也是一个时序数据库
时序数据库:安装事情发生的时间记录数据的数据库 --》time series database
为什么需要配置文件Prometheus.yml?
我们需要修改配置文件,添加需要监控的主机。
grafana是什么?
grafana 是非常专业的出图软件:专门从别的数据库里抽取数据
监控容器通过安装部署Prometheus+cAdvisor实现
首先是得到配置文件Prometheus.yml
第1步:拉取镜像
[root@docker ~]# docker pull prom/prometheus
Using default tag: latest
latest: Pulling from prom/prometheus
Digest: sha256:f5c29683a301d4e5427fb0d985f794b1f743498310a4eadee75e85f5dba73a6d
Status: Image is up to date for prom/prometheus:latest
docker.io/prom/prometheus:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
prom/prometheus latest a07b618ecd1d 37 hours ago 234MB
第2步:启动Prometheus的容器
[root@docker ~]# docker run -d -p9090:9090 --name sc-prom-1 prom/prometheus
49f6d66868da1cf48b19c79f67d00c58e234615953da4c74715aca06bd6fe091
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49f6d66868da prom/prometheus "/bin/prometheus --c…" 4 seconds ago Up 2 seconds 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp sc-prom-1
第3步:登录Prometheus的web界面
访问宿主机的ip对应的9090端口
![](https://i-blog.csdnimg.cn/blog_migrate/c04f5f11275a5fe51067873cb44315aa.png)
第4步:得到Prometheus的配置文件,从容器里直接拷贝出来
[root@docker ~]# mkdir /lianxi/prom
[root@docker ~]# cd /lianxi/prom
[root@docker prom]# docker cp sc-prom-1:/etc/prometheus/prometheus.yml .
Preparing to copy...
Successfully copied 2.56kB to /lianxi/prom/.
[root@docker prom]# ls
prometheus.yml
Prometheus监控容器需要使用一个软件cAdvisor;得到prometheus.yml之后,开始部署(需要机器里安装docker-compose)
编辑Prometheus.yml主配置文件,添加监控的容器cAdvisor为目标容器
[root@docker prom]# cat prometheus.yml |egrep -v "#|^$"
global:
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: cadvisor
scrape_interval: 5s
static_configs:
- targets:
- cadvisor:8080
2.新建一个docker-compose.yml文件
[root@docker prom]# vim docker-compose.yml
[root@docker prom]# cat docker-compose.yml
version: '3.2'
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- 9090:9090
command:
- --config.file=/etc/prometheus/prometheus.yml
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
depends_on:
- cadvisor
cadvisor:
image: gcr.io/cadvisor/cadvisor:latest
container_name: cadvisor
ports:
- 8080:8080
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
depends_on:
- redis
redis:
image: redis:latest
container_name: redis
ports:
- 6379:6379
3.上传cadvisor.tar镜像到宿主机,导入镜像
[root@docker prom]# ls
cadvisor.tar docker-compose.yml prometheus.yml
[root@docker prom]# docker load -i cadvisor.tar
ace0eda3e3be: Loading layer 5.843MB/5.843MB
33bb68b99ee4: Loading layer 102.4MB/102.4MB
d3174d703c76: Loading layer 13.25MB/13.25MB
8b7599e512b6: Loading layer 44.19MB/44.19MB
Loaded image: gcr.io/cadvisor/cadvisor:latest
[root@docker prom]# docker images
gcr.io/cadvisor/cadvisor latest 68c29634fe49 2 years ago 163MB
4.使用docker compose去启动Prometheus,cAdvisor,redis容器
[root@docker prom]# docker compose up -d
[+] Running 2/3
⠿ Container redis Started 1.4s
⠿ Container cadvisor Started 2.2s
⠿ Container prometheus Starting 2.2s
Error response from daemon: driver failed programming external connectivity on endpoint prometheus (b038de792eca4716282f4ebbb9cc4678f7afbdff0a6557c37de5964a23eabbec): Bind for 0.0.0.0:9090 failed: port is already allocated
这里遇到一个报错:说Prometheus容器的端口已被分配。
解决:停掉之前获取Prometheus的配置文件的容器或者修改Prometheus配置文件或者换个端口号。
[root@docker prom]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49f6d66868da prom/prometheus "/bin/prometheus --c…" 13 minutes ago Up 5 seconds 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp sc-prom-1
[root@docker prom]# docker stop sc-prom-1
sc-prom-1
[root@docker prom]# docker rm sc-prom-1
sc-prom-1
[root@docker prom]# docker compose down
[+] Running 4/4
⠿ Container prometheus Removed 0.0s
⠿ Container cadvisor Removed 0.3s
⠿ Container redis Removed 0.2s
⠿ Network prom_default Removed
0.1s
[root@docker prom]# docker compose up -d
[+] Running 4/4
⠿ Network prom_default Created 0.1s
⠿ Container redis Started 0.5s
⠿ Container cadvisor Started 0.8s
⠿ Container prometheus Started 1.3s
[root@docker prom]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da5b1d6be192 prom/prometheus:latest "/bin/prometheus --c…" 8 seconds ago Up 6 seconds 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus
b60662a84522 gcr.io/cadvisor/cadvisor:latest "/usr/bin/cadvisor -…" 8 seconds ago Up 7 seconds (health: starting) 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp cadvisor
7c2f76e3e516 redis:latest "docker-entrypoint.s…" 8 seconds ago Up 7 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis
[root@docker prom]#
5.访问cadvisor的效果
![](https://i-blog.csdnimg.cn/blog_migrate/6cc4936fa90dd18a43f7b97a2927fa0e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0c558ba35ae446ee7bc9154cda782ba7.png)
![](https://i-blog.csdnimg.cn/blog_migrate/94f440c3b5e3c7cfd4c583946a61c015.png)
![](https://i-blog.csdnimg.cn/blog_migrate/bc640eb885ba2d455193b4ea643ab511.png)