Prometheus从二进制部署迁移Docker中更新到v3.0.0版本
一 Prometheus的迁移
1.备份数据
找到二进制安装的目录
mkdir -p ~/prometheus_backup
cp -r /usr/local/prometheus ~/prometheus_backup/
创建挂载目录
mkdir -p ~/docker/prometheus/data
mkdir -p ~/docker/prometheus/config
2.复制配置文件
cp ~/prometheus_backup/prometheus/prometheus.yml ~/docker/prometheus/config/
3.修改用户权限
Prometheus 容器通常以 nobody
用户运行,需要确保目录权限允许读写:
chown -R 65534:65534 ~/docker/prometheus/data
4.编写docker-compose
version: '3.8'
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
volumes:
- ./config/prometheus.yml:/etc/prometheus/prometheus.yml
- ./data:/prometheus
ports:
- "9090:9090"
restart: unless-stopped
5.停止Prometheus,启动容器
systemctl stop prometheus
cd ~/docker/prometheus/
docker-compose up -d
直接启动的话会显示端口已被占用,当然也可以修改端口
6.迁移数据
docker-compose down
cp -r ~/prometheus_backup/prometheus/* ~/docker/prometheus/data/
sudo chown -R 65534:65534 ~/docker/prometheus/data
docker-compose up -d
7.rules挂载
因之前设置的rules目录迁移后,配置文件中并没有修改,之前迁移数据将rules也迁移到挂载目录
docker exec -it prometheus /bin/sh
查看rules文件位置
修改配置文件
vim ~/docker/prometheus/config/prometheus.yml
rule_files:
- '/prometheus/rules/base_rules.yml'
- '/prometheus/rules/mysql_rule.yml'
在配置文件中可以看到之前的job-name设置的抓取node都还在,登录界面查看变化
二 Prometheus新旧界面对比
1.UI界面的更新
旧:
新
2.菜单更新
旧:
新
3.添加解释功能
旧:
新
三 迁移 Alertmanager
1.备份配置文件
mkdir -p ~/backup/alertmanager
mkdir -p ~/backup/alertmanager/templates
cp /usr/local/alertmanager/alertmanager.yml ~/backup/alertmanager/
cp -r /etc/alertmanager/templates/*.tmpl ~/backup/alertmanager/templates/
2.准备docker挂载目录
mkdir -p ~/docker/alertmanager/config
mkdir -p ~/docker/alertmanager/data
mkdir -p ~/docker/alertmanager/templates
3.迁移配置文件
cp ~/backup/alertmanager/alertmanager.yml ~/docker/alertmanager/config/
4.编写docker-compose文件
vim ~/docker/alertmanager/docker-compose.yml
version: '3.8'
services:
alertmanager:
image: prom/alertmanager:latest
container_name: alertmanager
volumes:
- ./config/alertmanager.yml:/etc/alertmanager/alertmanager.yml
- ./templates:/etc/alertmanager/templates
- ./data:/alertmanager
ports:
- "9093:9093"
restart: unless-stopped
5.停止服务,启动容器
systemctl stop alertmanager
cd ~/docker/alertmanager
docker-compose up -d
6.权限设置
sudo chown -R 65534:65534 ~/docker/alertmanager/data
7.配置Prometheus的热加载
docker exec -it prometheus kill -HUP 1
四 grafana迁移
1.备份数据
mkdir -p ~/backup/grafana
cp -r /etc/grafana ~/backup/grafana/config
cp -r /var/lib/grafana ~/backup/grafana/data
cp -r /var/log/grafana ~/backup/grafana/logs
2.创建数据目录
mkdir -p ~/docker/grafana/config
mkdir -p ~/docker/grafana/data
mkdir -p ~/docker/grafana/logs
3.创建挂载
cp -r ~/backup/grafana/config/* ~/docker/grafana/config/
cp -r ~/backup/grafana/data/* ~/docker/grafana/data/
cp -r ~/backup/grafana/logs/* ~/docker/grafana/logs/
4.配置grafana用户权限
sudo chown -R 472:472 ~/docker/grafana/data
sudo chown -R 472:472 ~/docker/grafana/logs
5.创建docker-compose文件
vim ~/docker/grafana/docker-compose.yaml
version: '3.8'
services:
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000" # Grafana 默认端口
volumes:
- ./config:/etc/grafana
- ./data:/var/lib/grafana
- ./logs:/var/log/grafana
environment:
- GF_SECURITY_ADMIN_USER=admin # 默认管理员用户名
- GF_SECURITY_ADMIN_PASSWORD=admin # 默认管理员密码
restart: unless-stopped
6.停止服务,启动容器
systemctl stop grafana-server
cd ~/docker/grafana
docker-compose up -d
7.热加载
docker exec -it grafana kill -HUP 1
五 部署容器监控
cAdvisor (Container Advisor) 是一个开源工具,主要用于监控和分析容器(特别是 Docker 容器)的性能和资源使用情况。它由 Google 开发,能够实时收集、存储和展示容器的各种资源数据,帮助管理员监控容器的运行状态。具体来说,cAdvisor 监控的内容包括:容器资源使用情况、容器资源使用情况、容器资源使用情况、可视化界面。
1.编写docker-compose.yaml文件
version: '3'
services:
cadvisor:
image: google/cadvisor:latest
container_name: cadvisor
restart: unless-stopped
ports:
- "8088:8080" # cAdvisor Web UI
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
- /dev/disk/:/dev/disk:ro
devices:
- /dev/kmsg:/dev/kmsg
cap_add:
- SYS_ADMIN
networks:
monitoring:
driver: bridge
特别注意,如果没设置SYS_ADMIN
,可能会出现以下报错,说没有权限,如果添加privileged: true ,我的网页全都打不开了,可能权限直接赋予root会对网络访问造成影响,查看应用日志没有什么变化。
I1126 07:06:31.292029 1 cadvisor.go:212] Exiting given signal: terminated
W1126 07:06:31.766250 1 manager.go:349] Could not configure a source for OOM detection, disabling OOM events: open /dev/kmsg: operation not permitted
启动,登录指定的8088端口查看网页情况
docker-compose up -d
2.配置到Prometheus中添加
- job_name: 'cadvisor'
static_configs:
- targets: ['172.16.208.12:8088'] # 指定 cAdvisor 容器地址