docker-compose.yml

version: '3'
services:
  prometheus:
    image: prom/prometheus
    ports:
      - 9090:9090
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
    depends_on:
      - influxdb
      - telegraf

  telegraf:
    image: telegraf
    volumes:
      - ./telegraf.conf:/etc/telegraf/telegraf.conf:ro
    depends_on:
      - influxdb

  influxdb:
    image: influxdb:2.7.1
    ports:
      - 38086:8086
    volumes:
      - ./influxdb/data:/var/lib/influxdb2
      - ./influxdb/backup:/root/backup
    environment:
      - DOCKER_INFLUXDB_INIT_MODE=setup 
      - DOCKER_INFLUXDB_INIT_USERNAME=admin
      - DOCKER_INFLUXDB_INIT_PASSWORD=adminpassword
      - DOCKER_INFLUXDB_INIT_ORG=myorg 
      - DOCKER_INFLUXDB_INIT_BUCKET=mybucket 
      - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=my-super-secret-auth-token 

新建promethues配置文件,命名为prometheus.yml。这里除了抓取prometheus自身的数据,还抓取了宿主机(MacOS)的node_exporter数据。


global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['prometheus:9090']

  - job_name: 'node_exporter'
    static_configs:
    - targets: ['host.docker.internal:9100']

remote_write:
  - url: "http://telegraf:31234/receive"

新建telegraf的配置文件telegraf.conf,负责将数据转发给influxdb

[[inputs.http_listener_v2]]
## Address and port to host HTTP listener on
service_address = ":31234"
## Path to listen to.
path = "/receive"
## Data format to consume.
data_format = "prometheusremotewrite"

[[outputs.influxdb_v2]]
urls = ["http://influxdb:8086"]
token = "my-super-secret-auth-token"
organization = "myorg"
bucket = "mybucket"