使用docker部署3节点etcd集群

在每个节点输入以下变量,根据实际情况修改主机名和IP
为了将etcd API暴露给docker主机外的客户端,必须使用docker inspect命令获得容器的IP来配置,这里使用了–net=host主机网络的方式来简化这一步骤。

ETCD_VERSION=latest
TOKEN=my-etcd-token
CLUSTER_STATE=new
NAME_1=etcd-node1
NAME_2=etcd-node2
NAME_3=etcd-node3
HOST_1=192.168.1.101
HOST_2=192.168.1.102
HOST_3=192.168.1.103
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
DATA_DIR=/var/lib/etcd

在节点1输入以下命令

THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
docker run -d \
  --net=host \
  --volume=${DATA_DIR}:/etcd-data \
  --name etcd quay.io/coreos/etcd:${ETCD_VERSION} \
  /usr/local/bin/etcd \
  --data-dir=/etcd-data --name ${THIS_NAME} \
  --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
  --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
  --initial-cluster ${CLUSTER} \
  --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}

在节点2输入以下命令

THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
docker run -d \
  --net=host \
  --volume=${DATA_DIR}:/etcd-data \
  --name etcd quay.io/coreos/etcd:${ETCD_VERSION} \
  /usr/local/bin/etcd \
  --data-dir=/etcd-data --name ${THIS_NAME} \
  --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
  --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
  --initial-cluster ${CLUSTER} \
  --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}

在节点3输入以下命令

THIS_NAME=${NAME_3}
THIS_IP=${HOST_3}
docker run -d \
  --net=host \
  --volume=${DATA_DIR}:/etcd-data \
  --name etcd quay.io/coreos/etcd:${ETCD_VERSION} \
  /usr/local/bin/etcd \
  --data-dir=/etcd-data --name ${THIS_NAME} \
  --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
  --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
  --initial-cluster ${CLUSTER} \
  --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}

全部执行完后使用以下命令查看集群节点信息

docker exec etcd /usr/local/bin/etcdctl --endpoints=http://${HOST_1}:2379 member list
20c4dbd9ca01c9fc: etcd-node-2 peerURLs=http://192.168.1.103:2380 clientURLs=http://192.168.1.103:2379 isLeader=false
52b6c5eaedead574: etcd-node-1 peerURLs=http://192.168.1.102:2380 clientURLs=http://192.168.1.102:2379 isLeader=false
7623946005cf410f: etcd-node-0 peerURLs=http://192.168.1.101:2380 clientURLs=http://192.168.1.101:2379 isLeader=true

发表评论

电子邮件地址不会被公开。 必填项已用*标注