Run etcd clusters inside containers

    The following guide shows how to run etcd with rkt and Docker using the .

    The following rkt run command will expose the etcd client API on port 2379 and expose the peer API on port 2380.

    Use the host IP address when configuring etcd.

    Trust the CoreOS App Signing Key.

    1. # gpg key fingerprint is: 18AD 5014 C99E F7E3 BA5F 6CE9 50BD D3E0 FC8A 365E

    Run the v3.2 version of etcd or specify another release version.

    1. sudo rkt run --net=default:IP=${NODE1} quay.io/coreos/etcd:v3.2 -- -name=node1 -advertise-client-urls=http://${NODE1}:2379 -initial-advertise-peer-urls=http://${NODE1}:2380 -listen-client-urls=http://0.0.0.0:2379 -listen-peer-urls=http://${NODE1}:2380 -initial-cluster=node1=http://${NODE1}:2380
    1. etcdctl --endpoints=http://192.168.1.21:2379 member list

    Setup a 3 node cluster with rkt locally, using the -initial-cluster flag.

    1. export NODE1=172.16.28.21
    2. export NODE2=172.16.28.22
    3. export NODE3=172.16.28.23

    Verify the cluster is healthy and can be reached.

    1. ETCDCTL_API=3 etcdctl --endpoints=http://172.16.28.21:2379,http://172.16.28.22:2379,http://172.16.28.23:2379 endpoint health

    Production clusters which refer to peers by DNS name known to the local resolver must mount the .

    In order to expose the etcd API to clients outside of Docker host, use the host IP address of the container. Please see docker inspect for more detail on how to get the IP address. Alternatively, specify --net=host flag to docker run command to skip placing the container inside of a separate network stack.

    Use the host IP address when configuring etcd:

    1. export NODE1=192.168.1.21
    1. docker volume create --name etcd-data
    2. export DATA_DIR="etcd-data"

    Run the latest version of etcd:

    1. REGISTRY=quay.io/coreos/etcd
    2. # available from v3.2.5
    3. REGISTRY=gcr.io/etcd-development/etcd
    4. docker run \
    5. -p 2379:2379 \
    6. -p 2380:2380 \
    7. --volume=${DATA_DIR}:/etcd-data \
    8. --name etcd ${REGISTRY}:latest \
    9. /usr/local/bin/etcd \
    10. --data-dir=/etcd-data --name node1 \
    11. --initial-advertise-peer-urls http://${NODE1}:2380 --listen-peer-urls http://0.0.0.0:2380 \
    12. --advertise-client-urls http://${NODE1}:2379 --listen-client-urls http://0.0.0.0:2379 \
    13. --initial-cluster node1=http://${NODE1}:2380

    List the cluster member:

    1. REGISTRY=quay.io/coreos/etcd
    2. # available from v3.2.5
    3. REGISTRY=gcr.io/etcd-development/etcd
    4. # For each machine
    5. TOKEN=my-etcd-token
    6. CLUSTER_STATE=new
    7. NAME_1=etcd-node-0
    8. NAME_3=etcd-node-2
    9. HOST_1=10.20.30.1
    10. HOST_2=10.20.30.2
    11. HOST_3=10.20.30.3
    12. CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
    13. DATA_DIR=/var/lib/etcd
    14. # For node 1
    15. THIS_NAME=${NAME_1}
    16. THIS_IP=${HOST_1}
    17. docker run \
    18. -p 2379:2379 \
    19. -p 2380:2380 \
    20. --volume=${DATA_DIR}:/etcd-data \
    21. --name etcd ${REGISTRY}:${ETCD_VERSION} \
    22. /usr/local/bin/etcd \
    23. --data-dir=/etcd-data --name ${THIS_NAME} \
    24. --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://0.0.0.0:2380 \
    25. --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://0.0.0.0:2379 \
    26. --initial-cluster ${CLUSTER} \
    27. --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
    28. # For node 2
    29. THIS_NAME=${NAME_2}
    30. THIS_IP=${HOST_2}
    31. docker run \
    32. -p 2379:2379 \
    33. -p 2380:2380 \
    34. --volume=${DATA_DIR}:/etcd-data \
    35. --name etcd ${REGISTRY}:${ETCD_VERSION} \
    36. /usr/local/bin/etcd \
    37. --data-dir=/etcd-data --name ${THIS_NAME} \
    38. --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://0.0.0.0:2380 \
    39. --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://0.0.0.0:2379 \
    40. --initial-cluster ${CLUSTER} \
    41. # For node 3
    42. THIS_IP=${HOST_3}
    43. docker run \
    44. -p 2379:2379 \
    45. -p 2380:2380 \
    46. --volume=${DATA_DIR}:/etcd-data \
    47. --name etcd ${REGISTRY}:${ETCD_VERSION} \
    48. /usr/local/bin/etcd \
    49. --data-dir=/etcd-data --name ${THIS_NAME} \
    50. --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://0.0.0.0:2380 \
    51. --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://0.0.0.0:2379 \
    52. --initial-cluster ${CLUSTER} \
    53. --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}

    To run etcdctl using API version 3:

    1. docker exec etcd /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl put foo bar"

    To provision a 3 node etcd cluster on bare-metal, the examples in the may be useful.

    The etcd release container does not include default root certificates. To use HTTPS with certificates trusted by a root authority (e.g., for discovery), mount a certificate directory into the etcd container:

    1. REGISTRY=quay.io/coreos/etcd
    2. # available from v3.2.5
    3. REGISTRY=docker://gcr.io/etcd-development/etcd
    4. rkt run \
    5. --insecure-options=image \
    6. --volume etcd-ssl-certs-bundle,kind=host,source=/etc/ssl/certs/ca-certificates.crt \
    7. --mount volume=etcd-ssl-certs-bundle,target=/etc/ssl/certs/ca-certificates.crt \
    8. ${REGISTRY}:latest -- --name my-name \
    9. --initial-advertise-peer-urls http://localhost:2380 --listen-peer-urls http://localhost:2380 \
    10. --advertise-client-urls http://localhost:2379 --listen-client-urls http://localhost:2379 \
    11. --discovery https://discovery.etcd.io/c11fbcdc16972e45253491a24fcf45e1
    1. REGISTRY=quay.io/coreos/etcd
    2. # available from v3.2.5
    3. REGISTRY=gcr.io/etcd-development/etcd
    4. docker run \
    5. -p 2379:2379 \
    6. -p 2380:2380 \
    7. --volume=/etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt \
    8. ${REGISTRY}:latest \
    9. /usr/local/bin/etcd --name my-name \
    10. --initial-advertise-peer-urls http://localhost:2380 --listen-peer-urls http://localhost:2380 \