name: kv-store-cluster

x-etcd-common: &etcd-common
  image: quay.io/coreos/etcd:v3.5.15
  restart: unless-stopped
  networks:
    - kv-net
  healthcheck:
    test: ["CMD", "etcdctl", "endpoint", "health", "--endpoints=http://127.0.0.1:2379"]
    interval: 10s
    timeout: 5s
    retries: 5
    start_period: 20s

services:
  etcd-1:
    <<: *etcd-common
    container_name: etcd-1
    hostname: etcd-1
    ports:
      - "2379:2379"
      - "2380:2380"
    environment:
      ETCD_NAME: etcd-1
      ETCD_DATA_DIR: /etcd-data
      ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
      ETCD_ADVERTISE_CLIENT_URLS: http://etcd-1:2379
      ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
      ETCD_INITIAL_ADVERTISE_PEER_URLS: http://etcd-1:2380
      ETCD_INITIAL_CLUSTER: etcd-1=http://etcd-1:2380,etcd-2=http://etcd-2:2380,etcd-3=http://etcd-3:2380
      ETCD_INITIAL_CLUSTER_STATE: new
      ETCD_INITIAL_CLUSTER_TOKEN: kv-prod-cluster-01
      ETCD_AUTO_COMPACTION_MODE: periodic
      ETCD_AUTO_COMPACTION_RETENTION: 1h
      ETCD_QUOTA_BACKEND_BYTES: "8589934592"
    volumes:
      - etcd-1-data:/etcd-data

  etcd-2:
    <<: *etcd-common
    container_name: etcd-2
    hostname: etcd-2
    ports:
      - "2479:2379"
      - "2480:2380"
    environment:
      ETCD_NAME: etcd-2
      ETCD_DATA_DIR: /etcd-data
      ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
      ETCD_ADVERTISE_CLIENT_URLS: http://etcd-2:2379
      ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
      ETCD_INITIAL_ADVERTISE_PEER_URLS: http://etcd-2:2380
      ETCD_INITIAL_CLUSTER: etcd-1=http://etcd-1:2380,etcd-2=http://etcd-2:2380,etcd-3=http://etcd-3:2380
      ETCD_INITIAL_CLUSTER_STATE: new
      ETCD_INITIAL_CLUSTER_TOKEN: kv-prod-cluster-01
      ETCD_AUTO_COMPACTION_MODE: periodic
      ETCD_AUTO_COMPACTION_RETENTION: 1h
      ETCD_QUOTA_BACKEND_BYTES: "8589934592"
    volumes:
      - etcd-2-data:/etcd-data

  etcd-3:
    <<: *etcd-common
    container_name: etcd-3
    hostname: etcd-3
    ports:
      - "2579:2379"
      - "2580:2380"
    environment:
      ETCD_NAME: etcd-3
      ETCD_DATA_DIR: /etcd-data
      ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
      ETCD_ADVERTISE_CLIENT_URLS: http://etcd-3:2379
      ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
      ETCD_INITIAL_ADVERTISE_PEER_URLS: http://etcd-3:2380
      ETCD_INITIAL_CLUSTER: etcd-1=http://etcd-1:2380,etcd-2=http://etcd-2:2380,etcd-3=http://etcd-3:2380
      ETCD_INITIAL_CLUSTER_STATE: new
      ETCD_INITIAL_CLUSTER_TOKEN: kv-prod-cluster-01
      ETCD_AUTO_COMPACTION_MODE: periodic
      ETCD_AUTO_COMPACTION_RETENTION: 1h
      ETCD_QUOTA_BACKEND_BYTES: "8589934592"
    volumes:
      - etcd-3-data:/etcd-data

networks:
  kv-net:
    driver: bridge

volumes:
  etcd-1-data:
  etcd-2-data:
  etcd-3-data: