Table of Contents
docker network create --subnet "172.20.0.0/16" etcd
export NODE1=172.20.1.1
docker run \
--rm \
--name node1 \
--network=etcd \
--ip=172.20.1.1 \
quay.io/coreos/etcd:v3.4.14 \
/usr/local/bin/etcd \
--data-dir=/etcd-data --name node1 \
--initial-advertise-peer-urls http://${NODE1}:2380 \
--listen-peer-urls http://${NODE1}:2380,http://localhost:2380 \
--advertise-client-urls http://${NODE1}:2379,http://localhost:2379 \
--listen-client-urls http://${NODE1}:2379,http://localhost:2379 \
--initial-cluster node1=http://${NODE1}:2380
"docker-compose docker-compose.yaml up" will launch a 3 nodes etcd cluster.
3node-etcd.yaml can be used to launch a ready environment to practice static way
docker exec -it etcd_etcd1_1 /bin/sh
etcd --name etcd1 --initial-advertise-peer-urls http://etcd1:2380 \
--listen-peer-urls http://0.0.0.0:2380 \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://etcd1:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \
--initial-cluster-state new
docker exec -it etcd_etcd2_1 /bin/sh
etcd --name etcd2 \
--initial-advertise-peer-urls http://etcd2:2380 \
--listen-peer-urls http://0.0.0.0:2380 \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://etcd2:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \
--initial-cluster-state new
docker exec -it etcd_etcd3_1 /bin/sh
etcd --name etcd3 \
--initial-advertise-peer-urls http://etcd3:2380 \
--listen-peer-urls http://0.0.0.0:2380 \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://etcd3:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \
--initial-cluster-state new
Working directory: labs/etcd
Env: docker-compose up
docker exec -it etcd-1 /bin/sh
etcdctl put foo1 bar1
etcdctl put foo2 bar2
etcdctl put foo3 bar3
etcdctl put foo4 bar4
etcdctl snapshot save /root/snapshot.db
exit
docker cp etcd-1:/root/snapshot.db /tmp/snapshot.db
docker-compose down
docker-compose -f 3node-etcd.yaml up
for i in {1..3}
do
docker cp snapshot.db etcd-${i}:/root/snapshot.db
done
docker exec -d etcd-1 etcdctl snapshot restore /root/snapshot.db \
--name m1 \
--initial-cluster m1=http://etcd-1:2380,m2=http://etcd-2:2380,m3=http://etcd-3:2380 \
--initial-cluster-token etcd-cluster-1 \
--initial-advertise-peer-urls http://etcd-1:2380
docker exec -d etcd-2 etcdctl snapshot restore /root/snapshot.db \
--name m2 \
--initial-cluster m1=http://etcd-1:2380,m2=http://etcd-2:2380,m3=http://etcd-3:2380 \
--initial-cluster-token etcd-cluster-1 \
--initial-advertise-peer-urls http://etcd-2:2380
docker exec -d etcd-3 etcdctl snapshot restore /root/snapshot.db \
--name m3 \
--initial-cluster m1=http://etcd-1:2380,m2=http://etcd-2:2380,m3=http://etcd-3:2380 \
--initial-cluster-token etcd-cluster-1 \
--initial-advertise-peer-urls http://etcd-3:2380
docker exec -it etcd-1 getent hosts etcd-{1..3}
docker exec -it etcd-1 etcd \
--name m1 \
--listen-client-urls http://etcd-1:2379 \
--advertise-client-urls http://etcd-1:2379 \
--listen-peer-urls http://etcd-1:2380
docker exec -it etcd-2 etcd \
--name m2 \
--listen-client-urls http://etcd-2:2379 \
--advertise-client-urls http://etcd-2:2379 \
--listen-peer-urls http://etcd-2:2380
docker exec -it etcd-3 etcd \
--name m3 \
--listen-client-urls http://etcd-3:2379 \
--advertise-client-urls http://etcd-3:2379 \
--listen-peer-urls http://etcd-3:2380
#check
docker exec -it etcd-1 etcdctl \
--endpoints=$(docker exec etcd-1 getent hosts etcd-{1..3} | awk '{print "http://" $1 ":2379"}' | paste -sd ',') \
member list -w table
Working directory: labs/etcd
Env: docker-compose up
docker run -d --name=etcd-4 \
--network=etcd_default \
--hostname=etcd-4 \
quay.io/coreos/etcd:v3.4.13 \
/bin/sh -c "sleep 1d"
docker exec -d etcd-1 etcdctl member add etcd-4 --peer-urls=http://etcd-4:2380
docker exec -it etcd-4 /bin/sh
export ETCD_NAME="etcd-4"
export ETCD_INITIAL_CLUSTER="etcd-2=http://etcd-2:2380,etcd-4=http://etcd-4:2380,etcd-1=http://etcd-1:2380,etcd-3=http://etcd-3:2380"
export ETCD_INITIAL_ADVERTISE_PEER_URLS="http://etcd-4:2380"
export ETCD_INITIAL_CLUSTER_STATE="existing"
export IP=$(getent hosts etcd-4 | awk '{print $1}')
etcd --listen-client-urls http://$IP:2379 \
--advertise-client-urls http://$IP:2379 \
--listen-peer-urls http://$IP:2380
# verify cluster status
export ENDPOINTS=$(getent hosts etcd-{1..4} | awk '{print "http://" $2 ":2379"}' | paste -sd',')
etcdctl member list --endpoints=$ENDPOINTS -w table
etcdctl endpoint status --endpoints=$ENDPOINTS -w table
#assume etcd-2 88d11e2649dad027 , not a leader
etcdctl member remove 88d11e2649dad027 --endpoints=$ENDPOINTS
#clean up
docker-compose down
docker rm -f etcd-4
docker-compose up
docker run -d --name=etcd-4 \
--network=etcd_default \
--hostname=etcd-4 \
-e 'ETCD_DATAT_DIR=/etcd_data' \
--mount source=etcd_etcd-3,target=/old-etcd-data \
quay.io/coreos/etcd:v3.4.13 \
/bin/sh -c "sleep 1d"
docker pause etcd-3
docker exec -it etcd-1 /bin/sh
# assume etcd-3's member id is c3697c3697a4, and etcd-4's IP is 172.26.0.5
etcdctl member update c3697a420dcd --peer-urls=http://172.26.0.5:2380
docker exec -it etcd-4 /bin/sh
cp -r /old-etcd-data /etcd_data
etcd -name etcd-3 \
-listen-peer-urls http://172.26.0.5:2380 \
-listen-client-urls http://172.26.0.5:2379,http://127.0.0.1:2379 \
-advertise-client-urls http://172.26.0.5:2379,http://127.0.0.1:2379
#clean up
docker rm -f etcd-4
docker-compose down