目录
1.prometheus是什么?
2.监控容器和真实服务器分别如何实现
2.1安装
2.1.1docker安装promethues
2.1.2二进制安装promethues
2.2.prometheus怎么监控容器?
prometheus官网网站
Prometheus - Monitoring system & time series database
docker里面有一个命令也可以进行简单的监控
docker stats
Prometheus是一个开源系统监控和警报工具包, 按照时序数据库
prometheus分数据收集和数据存储和数据展示及告警
有prometheus server和exporter和pushgateway和Aletmanger和相关出图和展示工具五大块组成,核心是prometheus server,由retrieval和TSDB和HTTP server组成,retrieval接收pull的消息,有两种pull类型,针对长作业是在上面部署exporters收集数据,针对短作业是用pushgetway拉取指标,而HTTP server支持可视化和告警
图片名词解释:
short-lived jobs :短时间作业
jobs:长时间作业
exporter:一直工作,是prometheus的代理,负责收集工作的指标数据给prometheus server:prometheus服务器
prometheus targets:被监控的服务器
pushgateway:拉取指标
TSDB:时序数据库
promql:prometheus语句
API clients:提供接口
Grafna:提供可视化界面
push alerts:推送
Alertmanager:告警功能
容器里面 ——》要安装exporter
真实服务器 ——》docker自带容器可以收集数据
准备工作:需要一台docker 宿主机,docker 容器去安装prometheus
我之前有出过安装docker compose的详细文章,点击以下链接https://blog.csdn.net/Stephen_Daa/article/details/129502170?spm=1001.2014.3001.5501
[root@nfs /]# cp /usr/libexec/docker/cli-plugins/docker-compose /usr/sbin
[root@nfs /]# which docker-compose
/usr/sbin/docker-compose
[root@nfs /]# docker-compose version
Docker Compose version v2.16.0
[root@nfs ~]# mkdir /prom_docker
[root@nfs ~]# cd /prom_docker/
[root@nfs prom_docker]# vim prometheus.yml
scrape_configs:
- job_name: cadvisor
scrape_interval: 5s
static_configs:
- targets:
- cadvisor:8080
[root@nfs prom_docker]# vim docker-compose.yaml
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
prometheus怎么监控容器?
依赖于一个容器 cAdvisor
cAdvisor:分析并公开来自运行容器的资源使用和性能数据.利用Xtfp传输到linux服务器来。
[root@nfs prom_docker]# ls
cadvisor.tar docker-compose.yaml prometheus.yml
[root@nfs prom_docker]# docker load < 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@nfs prom_docker]# docker-compose up -d
[+] Running 7/7
⠿ redis Pulled 61.0s
⠿ 3f9582a2cbe7 Pull complete 40.0s
⠿ 241c2d338588 Pull complete 40.0s
⠿ 89515d93a23e Pull complete 40.1s
⠿ 65e8ba9473fe Pull complete 40.5s
⠿ 585124038cab Pull complete 40.5s
⠿ b483de716a47 Pull complete 40.6s
[+] Running 4/4
⠿ Network prom_docker_default Created 0.0s
⠿ Container redis Started 0.9s
⠿ Container cadvisor Started 1.1s
⠿ Container prometheus Started 1.5s
http://192.168.1.130:8080/ 访问cadvisor
http://192.168.1.130:9090/ 访问prometheus
docker-compose stop 停止容器
Stopping prometheus … done
Stopping cadvisor … done
Stopping redis … done
docker-compose down --》 down Stop and remove resources 停止并且删除容器
使用容器安装grafana
[root@nfs prom_docker]# docker run -d -p 3001:3000 --name jd-grafana-1 grafana/grafana
Unable to find image 'grafana/grafana:latest' locally
latest: Pulling from grafana/grafana
895e193edb51: Pull complete
a3e3778621b5: Pull complete
e7cf2c69b927: Pull complete
df40c119df08: Pull complete
3b29ea6a27af: Pull complete
3997cd619520: Pull complete
7e759f975aac: Pull complete
ff133072f235: Pull complete
f9a56094a361: Pull complete
Digest: sha256:76dcf36e7d2a4110c2387c1ad6e4641068dc78d7780da516d5d666d1e4623ac5
Status: Downloaded newer image for grafana/grafana:latest
352b843ff6d21d91ad14ed1bb2f4c1cc3fb2b05db42599a7f3fc1c4e0fbac747
访问http://192.168.1.130:3001/
默认的用户名和密码是
用户名admin
密码admin
1.先配置prometheus的数据源
2.导入下面的模板
https://grafana.com/grafana/dashboards/11600-docker-container/
11600
https://grafana.com/grafana/dashboards/10566-docker-and-os-metrics/
效果一览:
cpu使用率:
100 - (avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance) * 100)
内存使用率:
100-(node_memory_MemFree_bytes+node_memory_Buffers_bytes+node_memory_Cached_bytes)/node_memory_MemTotal_bytes*100
磁盘使用率:
100 - (node_filesystem_free_bytes{mountpoint="/data01",fstype=~"ext4|xfs"} / node_filesystem_size_bytes{mountpoint="/data01",fstype=~"ext4|xfs"} * 100)
下载带宽:(指定某一个网卡)
irate(node_network_receive_bytes_total{device='eth0'}[5m])
上传带宽:(指定某一个网卡)
irate(node_network_transmit_bytes_total{device='eth0'}[5m])
服务器负载:
node_load1
node_load5
node_load15