Henry
发布于 2025-10-23 / 4 阅读
0
0

Docker - 部署 Prometheus

背景简介

Docker 部署 Prometheus 并搜集宿主机指标。

前置信息

  1. Docker 【Docker - Debian 安装

详细信息

文件夹结构

Prometheus/
  |-- docker-compose.yml
  |-- data
  |     |-- prometheus.yml
  |     |-- prometheus
  • 修改文件夹权限为 nobody
sudo chown -R 65534:65534 data

准备配置文件

  • prometheus.yml

global:
  scrape_interval: 15s # 全局默认的抓取间隔
  evaluation_interval: 15s # 评估规则的间隔

scrape_configs:
  # 任务1:抓取 Prometheus 自身的指标
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  # 任务2:抓取 node-exporter 的指标(用于监控宿主机)
  - job_name: 'node'
    static_configs:
      - targets: ['node-exporter:9100']

部署 Prometheus

  • 准备 docker-compose.yml 
services:
  prometheus:
    image: prom/prometheus:v3.0.0
    container_name: prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus' # 指定数据存储路径
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle' # 允许热重载配置
    ports:
      - "9090:9090"
    volumes:
      - ./data/prometheus.yml:/etc/prometheus/prometheus.yml:ro # 挂载配置文件
      - ./data/prometheus:/prometheus # 挂载数据卷以持久化数据
    restart: unless-stopped

  node-exporter:
    image: prom/node-exporter:v1.8.2
    container_name: node-exporter
    command:
      - '--path.rootfs=/hostfs' # 指定 node-exporter 在容器内读取宿主机文件系统的根路径
    ports:
      - "9100:9100"
    volumes:
      - '/:/hostfs:ro,rslave' # 将宿主机的根目录挂载到容器的 /host,并设置为只读
    restart: unless-stopped
    pid: "host" # 共享宿主机的 PID namespace,以获取更准确的进程信息
  • 启动服务
docker compose up -d
[+] Running 3/3
 ✔ Network prometheus_default  Created 
 ✔ Container node-exporter     Started 
 ✔ Container prometheus        Started 
  • 查看 Log
docker logs -f prometheus

time=2025-10-23T08:35:02.141Z level=INFO source=main.go:642 msg="No time or size retention was set so using the default time retention" duration=15d
time=2025-10-23T08:35:02.141Z level=INFO source=main.go:689 msg="Starting Prometheus Server" mode=server version="(version=3.0.0, branch=HEAD, revision=c5d009d57fcccb7247e1191a0b10d74b06295388)"
time=2025-10-23T08:35:02.141Z level=INFO source=main.go:694 msg="operational information" build_context="(go=go1.23.3, platform=linux/amd64, user=root@aa286d0eb00a, date=20241114-16:40:43, tags=netgo,builtinassets,stringlabels)" host_details="(Linux 6.8.0-86-generic #87-Ubuntu SMP PREEMPT_DYNAMIC Mon Sep 22 18:03:36 UTC 2025 x86_64 e8268424a16a (none))" fd_limits="(soft=1048576, hard=1048576)" vm_limits="(soft=unlimited, hard=unlimited)"
time=2025-10-23T08:35:02.143Z level=INFO source=main.go:770 msg="Leaving GOMAXPROCS=36: CPU quota undefined" component=automaxprocs
time=2025-10-23T08:35:02.145Z level=INFO source=web.go:650 msg="Start listening for connections" component=web address=0.0.0.0:9090
time=2025-10-23T08:35:02.145Z level=INFO source=main.go:1239 msg="Starting TSDB ..."
time=2025-10-23T08:35:02.147Z level=INFO source=tls_config.go:347 msg="Listening on" component=web address=[::]:9090
time=2025-10-23T08:35:02.147Z level=INFO source=tls_config.go:350 msg="TLS is disabled." component=web http2=false address=[::]:9090
time=2025-10-23T08:35:02.152Z level=INFO source=head.go:628 msg="Replaying on-disk memory mappable chunks if any" component=tsdb
time=2025-10-23T08:35:02.152Z level=INFO source=head.go:715 msg="On-disk memory mappable chunks replay completed" component=tsdb duration=1.94µs
time=2025-10-23T08:35:02.152Z level=INFO source=head.go:723 msg="Replaying WAL, this may take a while" component=tsdb
time=2025-10-23T08:35:02.153Z level=INFO source=head.go:795 msg="WAL segment loaded" component=tsdb segment=0 maxSegment=0
time=2025-10-23T08:35:02.153Z level=INFO source=head.go:832 msg="WAL replay completed" component=tsdb checkpoint_replay_duration=75.974µs wal_replay_duration=1.359896ms wbl_replay_duration=195ns chunk_snapshot_load_duration=0s mmap_chunk_replay_duration=1.94µs total_replay_duration=1.543306ms
time=2025-10-23T08:35:02.158Z level=INFO source=main.go:1260 msg="filesystem information" fs_type=EXT4_SUPER_MAGIC
time=2025-10-23T08:35:02.158Z level=INFO source=main.go:1263 msg="TSDB started"
time=2025-10-23T08:35:02.159Z level=INFO source=main.go:1446 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
time=2025-10-23T08:35:02.159Z level=INFO source=main.go:1485 msg="updated GOGC" old=100 new=75
time=2025-10-23T08:35:02.159Z level=INFO source=main.go:1495 msg="Completed loading of configuration file" db_storage=1.019µs remote_storage=1.435µs web_handler=415ns query_engine=852ns scrape=375.989µs scrape_sd=56.033µs notify=1.025µs notify_sd=688ns rules=1.376µs tracing=6.084µs filename=/etc/prometheus/prometheus.yml totalDuration=798.546µs
time=2025-10-23T08:35:02.159Z level=INFO source=main.go:1224 msg="Server is ready to receive web requests."
time=2025-10-23T08:35:02.159Z level=INFO source=manager.go:168 msg="Starting rule manager..." component="rule manager"

以上便是本文的全部内容,感谢您的阅读,如遇到任何问题,欢迎在评论区留言讨论。



评论