这个问题的解决方案是使用实际的服务发现而不是静态目标。这样,Prometheus 将在每次迭代期间抓取每个副本。
如果只是 docker-compose(我的意思不是 Swarm),您可以使用 DNS 服务发现(dns_sd_配置) 获取属于某个服务的所有 IP:
# docker-compose.yml
version: "3"
services:
prometheus:
image: prom/prometheus
test-service: # <- this
image: nginx
deploy:
replicas: 3
---
# prometheus.yml
scrape_configs:
- job_name: test
dns_sd_configs:
- names:
- test-service # goes here
type: A
port: 80
这是启动和运行最简单的方法。
接下来,您可以使用专用的 Docker 服务发现:docker_sd_配置。除了目标列表之外,它还为您提供更多标签数据(例如容器名称、镜像版本等),但它还需要连接到 Docker 守护进程才能获取这些数据。在我看来,这对于开发环境来说是一种矫枉过正,但在生产中可能是必不可少的。这是一个示例配置,大胆复制粘贴自https://github.com/prometheus/prometheus/blob/release-2.33/documentation/examples/prometheus-docker.yml :
# A example scrape configuration for running Prometheus with Docker.
scrape_configs:
# Make Prometheus scrape itself for metrics.
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
# Create a job for Docker daemon.
#
# This example requires Docker daemon to be configured to expose
# Prometheus metrics, as documented here:
# https://docs.docker.com/config/daemon/prometheus/
- job_name: "docker"
static_configs:
- targets: ["localhost:9323"]
# Create a job for Docker Swarm containers.
#
# This example works with cadvisor running using:
# docker run --detach --name cadvisor -l prometheus-job=cadvisor
# --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock,ro
# --mount type=bind,src=/,dst=/rootfs,ro
# --mount type=bind,src=/var/run,dst=/var/run
# --mount type=bind,src=/sys,dst=/sys,ro
# --mount type=bind,src=/var/lib/docker,dst=/var/lib/docker,ro
# google/cadvisor -docker_only
- job_name: "docker-containers"
docker_sd_configs:
- host: unix:///var/run/docker.sock # You can also use http/https to connect to the Docker daemon.
relabel_configs:
# Only keep containers that have a `prometheus-job` label.
- source_labels: [__meta_docker_container_label_prometheus_job]
regex: .+
action: keep
# Use the task labels that are prefixed by `prometheus-`.
- regex: __meta_docker_container_label_prometheus_(.+)
action: labelmap
replacement: $1
最后,有dockerswarm_sd_config显然,它是与 Docker Swarm 一起使用的。这是三者中最复杂的,因此,有全面的官方设置指南。像docker_sd_config
它在标签中包含有关容器的附加信息,甚至更多(例如,它可以告诉容器在哪个节点上)。此处提供了示例配置:https://github.com/prometheus/prometheus/blob/release-2.33/documentation/examples/prometheus-dockerswarm.yml,但您应该真正阅读文档才能理解它并自行调整。