监控 Docker系统
Prometheus + Grafana 监控 Docker 系统搭建指南。
要实现对 Docker 的全面监控,需要按照以下步骤搭建 Prometheus 和 Grafana 并进行配置:
4.1、安装 Prometheus
-
下载并解压
Prometheusbashwget https://github.com/prometheus/prometheus/releases/download/v2.47.0/prometheus-2.47.0.linux-amd64.tar.gz tar xvfz prometheus-2.47.0.linux-amd64.tar.gz cd prometheus-2.47.0.linux-amd64 -
配置
Prometheus编辑
prometheus.yml添加Docker监控目标:yamlglobal: scrape_interval: 15s # 数据采集间隔 scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'docker' # Docker 监控任务 static_configs: - targets: ['localhost:9323'] # 对应 Docker metrics-addr 配置 -
启动
Prometheusbash./prometheus --config.file=prometheus.yml -
验证
Prometheus访问
http://localhost:9090,查看状态 → 目标,确保docker目标状态为UP
4.2、安装 Grafana
-
添加
Grafana仓库并安装bashsudo apt-get install -y apt-transport-https software-properties-common wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add - echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list sudo apt-get update sudo apt-get install grafana -
启动
Grafana服务bashsudo systemctl start grafana-server sudo systemctl enable grafana-server -
验证
Grafana访问
http://localhost:3000,默认用户名 / 密码:admin/admin
4.3、配置 Grafana 数据源与仪表盘
- 添加
Prometheus数据源 - 登录
Grafana,导航至 配置 → 数据源 → 添加数据源 - 选择
Prometheus
URL: http://localhost:9090 - 点击 "保存并测试"
- 导入
Docker监控仪表盘 - 导航至 仪表盘 → 管理 → 导入
- 输入仪表盘 ID: 11332 (Docker 综合监控) 或 893 (Docker 详细监控)
选择Prometheus数据源 - 点击 "导入"
4.4、高级配置:持久化与服务化
-
创建 Prometheus 服务文件
bashsudo tee /etc/systemd/system/prometheus.service <<EOF [Unit] Description=Prometheus Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/prometheus \ --config.file=/etc/prometheus/prometheus.yml \ --storage.tsdb.path=/var/lib/prometheus/ \ --web.console.templates=/etc/prometheus/consoles \ --web.console.libraries=/etc/prometheus/console_libraries [Install] WantedBy=multi-user.target EOF -
创建数据目录并设置权限
bashsudo mkdir -p /etc/prometheus /var/lib/prometheus sudo cp prometheus.yml /etc/prometheus/ sudo useradd -rs /bin/false prometheus sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus -
启动并验证服务
bashsudo systemctl daemon-reload sudo systemctl start prometheus sudo systemctl enable prometheus
4.5、关键监控指标说明
- 导入的仪表盘通常包含以下核心监控板块:
- 容器概览
- 运行中 / 暂停 / 停止的容器数量
- 容器创建 / 启动 / 停止操作频率
- 资源使用
- CPU 使用率(系统 / 容器)
- 内存使用率(总内存 / 容器内存)
- 网络流量(入站 / 出站)
- 磁盘 I/O 速率
- 性能分析
- 容器启动耗时分布
- 镜像拉取 / 构建时间
- 服务响应延迟