Etcd in Docker Compose

Posted on Jan 2, 2023

Prepare data persistent directories on Host machine.

sudo mkdir -p /data/docker-vol/etcd/etcd{01,02,03}
sudo chown -R 1001:1001 /data/docker-vol/etcd

Run docker compose

docker-compose.yml

version: '3.1'

services:
  etcd01:
    image: 'bitnami/etcd:3.5'
    container_name: etcd01
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd01
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_ADVERTISE_CLIENT_URLS=http://hostname-of-host-machine-1:2379
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://hostname-of-host-machine-1:2380
      - ETCD_INITIAL_CLUSTER=etcd01=http://hostname-of-host-machine-1:2380,etcd02=http://hostname-of-host-machine-2:12380,etcd03=http://hostname-of-host-machine-3:22380
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER_STATE=new
      - ETCD_QUOTA_BACKEND_BYTES=5368709120
      - ETCD_AUTO_COMPACTION_RETENTION=1
      - ETCD_MAX_REQUEST_BYTES=33554432
      - ETCDCTL_API=3
    ports:
      - 2379:2379
      - 2380:2380
    volumes:
      - /data/docker-vol/etcd/etcd01:/bitnami/etcd/data

  etcd02:
    image: 'bitnami/etcd:3.5'
    container_name: etcd02
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd02
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:12380
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:12379
      - ETCD_ADVERTISE_CLIENT_URLS=http://hostname-of-host-machine-2:12379
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://hostname-of-host-machine-2:12380
      - ETCD_INITIAL_CLUSTER=etcd01=http://hostname-of-host-machine-1:2380,etcd02=http://hostname-of-host-machine-2:12380,etcd03=http://hostname-of-host-machine-3:22380
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER_STATE=new
      - ETCD_QUOTA_BACKEND_BYTES=5368709120
      - ETCD_AUTO_COMPACTION_RETENTION=1
      - ETCD_MAX_REQUEST_BYTES=33554432
      - ETCDCTL_API=3
    ports:
      - 12379:12379
      - 12380:12380
    volumes:
      - /data/docker-vol/etcd/etcd02:/bitnami/etcd/data

  etcd03:
    image: 'bitnami/etcd:3.5'
    container_name: etcd03
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd03
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:22380
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:22379
      - ETCD_ADVERTISE_CLIENT_URLS=http://hostname-of-host-machine-3:22379
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://hostname-of-host-machine-3:22380
      - ETCD_INITIAL_CLUSTER=etcd01=http://hostname-of-host-machine-1:2380,etcd02=http://hostname-of-host-machine-2:12380,etcd03=http://hostname-of-host-machine-3:22380
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER_STATE=new
      - ETCD_QUOTA_BACKEND_BYTES=5368709120
      - ETCD_AUTO_COMPACTION_RETENTION=1
      - ETCD_MAX_REQUEST_BYTES=33554432
      - ETCDCTL_API=3
    ports:
      - 22379:22379
      - 22380:22380
    volumes:
      - /data/docker-vol/etcd/etcd03:/bitnami/etcd/data
docker compose up

Misc

Add handy alias in your .bash_rc

export ETCDCTL_API=3
HOST_1=hostname-of-host-machine-1
HOST_2=hostname-of-host-machine-2
HOST_3=hostname-of-host-machine-3
export ETCD_ENDPOINTS=$HOST_1:2379,$HOST_2:12379,$HOST_3:22379
alias etcd_status=$'docker run --rm --runtime=runsc bitnami/etcd:3.5 etcdctl --write-out=table --endpoints=$ETCD_ENDPOINTS endpoint status'
alias etcd_health=$'docker run --rm --runtime=runsc bitnami/etcd:3.5 etcdctl --write-out=table --endpoints=$ETCD_ENDPOINTS endpoint health'