使用Docker方式创建3节点的Etcd集群
一、简要说明
二、运行容器
三、验证集群
四、运行截图
五、参考链接
一、简要说明
参考etcd官网文档, 在node1、node2、node3三个节点上,分别运行etcd容器,创建etcd集群,并通过另一台机器验证集群的可用性。集群信息如下:
机器名 IP地址 角色
node1 192.168.3.161 etcd节点
node2 192.168.3.162 etcd节点
node3 192.168.3.163 etcd节点
nfs 192.168.3.167 验证节点
二、运行容器
# 在节点 node1 上运行:
REGISTRY=quay.io/coreos/etcd
ETCD_VERSION=v3.3.8
TOKEN=my-etcd-token
CLUSTER_STATE=new
NAME_1=node1
NAME_2=node2
NAME_3=node3
HOST_1=192.168.3.161
HOST_2=192.168.3.162
HOST_3=192.168.3.163
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
DATA_DIR=/var/lib/etcd
# 以上变量,三个节点相同
THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
docker run \
-p 2379:2379 \
-p 2380:2380 \
--volume=${DATA_DIR}:/etcd-data \
--name etcd ${REGISTRY}:${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://0.0.0.0:2380 \
--advertise-client-urls http://${THIS_IP}:2379 \
--listen-client-urls http://0.0.0.0:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
# 在节点 node2 上运行:
REGISTRY=quay.io/coreos/etcd
ETCD_VERSION=v3.3.8
TOKEN=my-etcd-token
CLUSTER_STATE=new
NAME_1=node1
NAME_2=node2
NAME_3=node3
HOST_1=192.168.3.161
HOST_2=192.168.3.162
HOST_3=192.168.3.163
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
DATA_DIR=/var/lib/etcd
# 以上变量,三个节点相同
THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
docker run \
-p 2379:2379 \
-p 2380:2380 \
--volume=${DATA_DIR}:/etcd-data \
--name etcd ${REGISTRY}:${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://0.0.0.0:2380 \
--advertise-client-urls http://${THIS_IP}:2379 \
--listen-client-urls http://0.0.0.0:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
# 在节点 node3 上运行:
REGISTRY=quay.io/coreos/etcd
ETCD_VERSION=v3.3.8
TOKEN=my-etcd-token
CLUSTER_STATE=new
NAME_1=node1
NAME_2=node2
NAME_3=node3
HOST_1=192.168.3.161
HOST_2=192.168.3.162
HOST_3=192.168.3.163
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
DATA_DIR=/var/lib/etcd
# 以上变量,三个节点相同
THIS_NAME=${NAME_3}
THIS_IP=${HOST_3}
docker run \
-p 2379:2379 \
-p 2380:2380 \
--volume=${DATA_DIR}:/etcd-data \
--name etcd ${REGISTRY}:${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://0.0.0.0:2380 \
--advertise-client-urls http://${THIS_IP}:2379 \
--listen-client-urls http://0.0.0.0:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} \
--initial-cluster-token ${TOKEN}
三、验证集群
#在测试验证节点NFS上,运行另一个etcd容器,
#以便使用其中的etcdctl命令行程序进行验证
REGISTRY=quay.io/coreos/etcd
ETCD_VERSION=v3.3.8
docker run \
--name etcd ${REGISTRY}:${ETCD_VERSION} \
/usr/local/bin/etcd
#进入容器,查询集群状态,
#验证存入数据、读取数据
docker exec etcd /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://192.168.3.161:2379,http://192.168.3.162:2379,http://192.168.3.163:2379 --write-out=table member list"
docker exec etcd /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://192.168.3.161:2379,http://192.168.3.162:2379,http://192.168.3.163:2379 --write-out=table endpoint status"
docker exec etcd /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://192.168.3.161:2379,http://192.168.3.162:2379,http://192.168.3.163:2379 --write-out=table endpoint health"
docker exec etcd /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://192.168.3.161:2379,http://192.168.3.162:2379,http://192.168.3.163:2379 put url http://www.sina.com.cn"
docker exec etcd /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://192.168.3.161:2379,http://192.168.3.162:2379,http://192.168.3.163:2379 get url"
四、运行截图
图1-节点node1上etcd容器运行日志1
图2-节点node1上etcd容器运行日志2
图3-节点node1上etcd容器运行日志3
图4-节点node1上etcd容器运行日志4
图5-在测试节点上,验证集群的状态,存入、获取数据,一切正常
五、参考链接:
ETCD版本(v3.1.18/v3.2.23/v3.3.8)
https://quay.io/repository/coreos/etcd?tag=latest&tab=tags
ETCD文档(Run etcd clusters inside containers)
https://github.com/coreos/etcd/blob/master/Documentation/op-guide/container.md#docker