Prometheus监控系统
https://blog.csdn.net/qq_39578545/article/details/108754585
docker-compose将所管理的容器分为三层, 分别是工程(project),服务(service)以及容器(containner)
需要一台docker 宿主机,docker 容器安装prometheus服务
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@docker1 ~]# chmod +x /usr/local/bin/docker-compose
[root@docker1 ~]# which docker-compose
/usr/local/bin/docker-compose
[root@docker1 ~]# docker compose version
Docker Compose version v2.7.0
[roott@docker1 ~]# mkdir /prom_docker
[roott@docker1 ~]# cd /prom_docker/
[roott@docker1 prom_docker]#
[root@docker1 prom_docke]# vim prometheus.yml
[root@docker1 prom_docker]# cat prometheus.yml
scrape_configs:
- job_name: cadvisor
scrape_interval: 5s
static_configs:
- targets:
- cadvisor:8080
[root@docker1 prom_docke]# vim docker-compose.yaml
[root@docker1 prom_docker]# 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
cadvisor相当于node_exporter
docker pull gcr.io/cadvisor/cadvisor:latest
由于下载该镜像需访问谷歌,因此国内无法成功下载,有条件可以使用香港的服务器里去下载镜像,或者通过其他途径获取该镜像
https://download.csdn.net/download/qq_39578545/87613018
iptables -F
真正删除规则需要重启机器
cAdvisor(需要监控的主机都要安装)
官方地址:https://github.com/google/cadvisor
CAdvisor是谷歌开发的用于分析运行中容器的资源占用和性能指标的开源工具。CAdvisor是一个运行时的守护进程,负责收集、聚合、处理和输出运行中容器的信息。
docker监控也需要用一个exporter,这个exporter就是cAdvisor,类似zabbix的各种agent
[root@localhost prom_docker]# pwd
/prom_docker
[root@localhost prom_docker]# ls
cadvisor.tar docker-compose.yml prometheus.yml
docker tag 68c29634fe49 gcr.io/cadvisor/cadvisor:latest
[root@docker1 prom_docker]# docker compose up -d
[+] Running 4/4
⠿ Network prom_docker_default Created 0.3s
⠿ Container redis Started 1.1s
⠿ Container cadvisor Started 2.3s
⠿ Container prometheus Started 3.5s
[root@docker1 prom_docker]# docker compose ps
NAME COMMAND SERVICE STATUS PORTS
cadvisor "/usr/bin/cadvisor -…" cadvisor running (starting) 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
prometheus "/bin/prometheus --c…" prometheus running 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp
redis "docker-entrypoint.s…" redis running 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp
如启动有问题注意检查配置文件的格式问题,查看配置文件中各个端口本机是否已经占用等
8080 端口访问 cadvisor
9090 端口访问 prometheus
http://192.168.48.129:8080
[root@docker1 prom_docker]# docker run -d -p 3001:3000 --name sc-grafana-1 grafana/grafana
Unable to find image 'grafana/grafana:latest' locally
latest: Pulling from grafana/grafana
9621f1afde84: Pull complete
37c977d2b27c: Pull complete
c808de7a697e: Pull complete
86659f76fb09: Pull complete
747aee88ceca: Pull complete
99d875e11915: Pull complete
3fb2327ae1ed: Pull complete
ac9523800f50: Pull complete
e15e29cdce8f: Pull complete
Digest: sha256:980ff2697655a0aa5718e40bbda6ac52299d2f3b1584d0081152e2d0a4742078
Status: Downloaded newer image for grafana/grafana:latest
c4aae27eb48ce7be7add6af0d8073aed15c305de81ec94e788ebdfc529836353
http://192.168.48.129:3001/
首次进入时,需要输入用户名和密码,皆为admin
1、添加数据源 datasource
2、导入图标模板
https://grafana.com/grafana/dashboards
该网站有许多已经搭建好了的grafana模板,每个模板都有一个id,也可以下载成json文件,
scrape_configs:
- job_name: cadvisor
scrape_interval: 5s
static_configs:
- targets:
- cadvisor:8080
- job_name: 'prometheus'
static_configs:
- targets: ['127.0.0.1:9090']
- job_name: 'job1'
static_configs:
- targets: ['192.168.48.129:9100']
- job_name: 'job2'
static_configs:
- targets: ['192.168.48.129:6379']
Node-exporter 可以采集机器(物理机、虚拟机、云主机)的监控指标数据,能够采集到的指标包括cpu、内存、磁盘、网络、文件数等信息。
各种各样的exporter下载地址:https://prometheus.io/download/
1、下载安装
curl -O -L https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
# 解压
tar -zxvf node_exporter-1.5.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/node_exporter-1.5.0.linux-amd64/ /usr/local/node_exporter-1.5.0
# 创建软连接
ln -s /usr/local/node_exporter-1.5.0/ /usr/local/node_exporter
2、添加到系统服务
sudo tee /usr/lib/systemd/system/node_exporter.service << EOF
[Unit]
Description=Prometheus node_exporter
[Service]
User=nobody
ExecStart=/usr/local/node_exporter/node_exporter --log.level=error
ExecStop=/usr/bin/killall node_exporter
[Install]
WantedBy=default.target
EOF
3、启动
# 启动
systemctl start node_exporter
# 查看服务状态
systemctl status node_exporter
访问地址:http://192.168.48.129:9100/metrics,可以看到监控的指标信息
4、配置Prometheus
global:
scrape_interval: 5s
evaluation_interval: 5s
scrape_timeout: 5s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'linux-exporter'
metrics_path: /metrics
static_configs:
- targets: ['localhost:9100']
5、grafana DashBoard添加
https://grafana.com/grafana/dashboards/12884-1-node-exporter-for-prometheus-dashboard-cn-v20200628/
https://grafana.com/grafana/dashboards