services:
  #  Metrics collector.
  #  It scrapes targets defined in --promscrape.config
  #  And forward them to --remoteWrite.url
  vmagent:
    image: victoriametrics/vmagent:v1.121.0
    depends_on:
      - "vmauth"
    ports:
      - 8429:8429
    volumes:
      - vmagentdata:/vmagentdata
      - ./prometheus-vm-cluster.yml:/etc/prometheus/prometheus.yml
    command:
      - "--promscrape.config=/etc/prometheus/prometheus.yml"
      - "--remoteWrite.url=http://vmauth:8427/insert/0/prometheus/api/v1/write"
    restart: always

  grafana:
    image: grafana/grafana:12.0.2
    depends_on:
      - "vmauth"
    ports:
      - 3000:3000
    restart: always
    volumes:
      - grafanadata:/var/lib/grafana
      - ./provisioning/datasources/prometheus-datasource/cluster.yml:/etc/grafana/provisioning/datasources/cluster.yml
      - ./provisioning/dashboards:/etc/grafana/provisioning/dashboards
      - ./../../dashboards/victoriametrics-cluster.json:/var/lib/grafana/dashboards/vm.json
      - ./../../dashboards/vmagent.json:/var/lib/grafana/dashboards/vmagent.json
      - ./../../dashboards/vmalert.json:/var/lib/grafana/dashboards/vmalert.json
      - ./../../dashboards/vmauth.json:/var/lib/grafana/dashboards/vmauth.json

  # vmstorage shards. Each shard receives 1/N of all metrics sent to vminserts,
  # where N is number of vmstorages (2 in this case).
  vmstorage-1:
    image: victoriametrics/vmstorage:v1.121.0-cluster
    volumes:
      - strgdata-1:/storage
    command:
      - "--storageDataPath=/storage"
    restart: always
  vmstorage-2:
    image: victoriametrics/vmstorage:v1.121.0-cluster
    volumes:
      - strgdata-2:/storage
    command:
      - "--storageDataPath=/storage"
    restart: always

  # vminsert is ingestion frontend. It receives metrics pushed by vmagent,
  # pre-process them and distributes across configured vmstorage shards.
  vminsert-1:
    image: victoriametrics/vminsert:v1.121.0-cluster
    depends_on:
      - "vmstorage-1"
      - "vmstorage-2"
    command:
      - "--storageNode=vmstorage-1:8400"
      - "--storageNode=vmstorage-2:8400"
    restart: always
  vminsert-2:
    image: victoriametrics/vminsert:v1.121.0-cluster
    depends_on:
      - "vmstorage-1"
      - "vmstorage-2"
    command:
      - "--storageNode=vmstorage-1:8400"
      - "--storageNode=vmstorage-2:8400"
    restart: always

  # vmselect is a query fronted. It serves read queries in MetricsQL or PromQL.
  # vmselect collects results from configured `--storageNode` shards.
  vmselect-1:
    image: victoriametrics/vmselect:v1.121.0-cluster
    depends_on:
      - "vmstorage-1"
      - "vmstorage-2"
    command:
      - "--storageNode=vmstorage-1:8401"
      - "--storageNode=vmstorage-2:8401"
      - "--vmalert.proxyURL=http://vmalert:8880"
    restart: always
  vmselect-2:
    image: victoriametrics/vmselect:v1.121.0-cluster
    depends_on:
      - "vmstorage-1"
      - "vmstorage-2"
    command:
      - "--storageNode=vmstorage-1:8401"
      - "--storageNode=vmstorage-2:8401"
      - "--vmalert.proxyURL=http://vmalert:8880"
    restart: always

  # vmauth is a router and balancer for HTTP requests.
  # It is configured via --auth.config and balances
  # read requests from Grafana, vmui, vmalert among vmselects.
  # It can be used as an authentication proxy.
  vmauth:
    image: victoriametrics/vmauth:v1.121.0
    depends_on:
      - "vmselect-1"
      - "vmselect-2"
    volumes:
      - ./auth-vm-cluster.yml:/etc/auth.yml
    command:
      - "--auth.config=/etc/auth.yml"
    ports:
      - 8427:8427
    restart: always

  # vmalert executes alerting and recording rules
  vmalert:
    image: victoriametrics/vmalert:v1.121.0
    depends_on:
      - "vmauth"
    ports:
      - 8880:8880
    volumes:
      - ./rules/alerts-cluster.yml:/etc/alerts/alerts.yml
      - ./rules/alerts-health.yml:/etc/alerts/alerts-health.yml
      - ./rules/alerts-vmagent.yml:/etc/alerts/alerts-vmagent.yml
      - ./rules/alerts-vmalert.yml:/etc/alerts/alerts-vmalert.yml
    command:
      - "--datasource.url=http://vmauth:8427/select/0/prometheus"
      - "--remoteRead.url=http://vmauth:8427/select/0/prometheus"
      - "--remoteWrite.url=http://vmauth:8427/insert/0/prometheus"
      - "--notifier.url=http://alertmanager:9093/"
      - "--rule=/etc/alerts/*.yml"
      # display source of alerts in grafana
      - "-external.url=http://127.0.0.1:3000" #grafana outside container
      - '--external.alert.source=explore?orgId=1&left={"datasource":"VictoriaMetrics","queries":[{"expr":{{.Expr|jsonEscape|queryEscape}},"refId":"A"}],"range":{"from":"{{ .ActiveAt.UnixMilli }}","to":"now"}}'
    restart: always

  # alertmanager receives alerting notifications from vmalert
  # and distributes them according to --config.file.
  alertmanager:
    image: prom/alertmanager:v0.28.0
    volumes:
      - ./alertmanager.yml:/config/alertmanager.yml
    command:
      - "--config.file=/config/alertmanager.yml"
    ports:
      - 9093:9093
    restart: always

volumes:
  vmagentdata: {}
  strgdata-1: {}
  strgdata-2: {}
  grafanadata: {}
