一. 准备工作
准备三台虚拟机,我这里用的是 vagrant + virtualbox
配置三台虚拟机ip为
node1 = 192.168.33.11
node2 = 192.168.33.12
node3 = 192.168.33.13
二. 安装docker
PS. 参考地址为:https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce
sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo sudo yum-config-manager --disable docker-ce-edge sudo yum install docker-ce -y sudo systemctl start docker
三. 创建集群容器
PS. etcd cluster有三种启动集群的机制这里使用第二种etcd Discovery
discovery URL有二种创建方法,分别是Custom etcd Discovery Service和Public etcd Discovery Service,因为我们是首次搭建集群,无现成的etcd集群,所以无法使用第一种,因此使用第二种Public etcd Discovery Service
curl https://discovery.etcd.io/new?size=3#返回 https://discovery.etcd.io/e8a57bd1131c6b466d32989e81e441e3
根据上面命令返回的Discovery URL编写创建容器脚本
node1:
# For each machineETCD_VERSION=v3.0.0 TOKEN=etcd-token CLUSTER_STATE=new DISCOVERY=https://discovery.etcd.io/e8a57bd1131c6b466d32989e81e441e3# For node 1THIS_NAME=etcd-node-1 THIS_IP=192.168.33.11 sudo docker run -d --net=host --name etcd quay.io/coreos/etcd:${ETCD_VERSION} \ /usr/local/bin/etcd \ --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,http://127.0.0.1:2379 \ --initial-cluster-state ${CLUSTER_STATE} \ --initial-cluster-token ${TOKEN} \ --discovery ${DISCOVERY}
node2:
# For each machineETCD_VERSION=v3.0.0 TOKEN=etcd-token CLUSTER_STATE=new DISCOVERY=https://discovery.etcd.io/e8a57bd1131c6b466d32989e81e441e3# For node 2THIS_NAME=etcd-node-2 THIS_IP=192.168.33.12 sudo docker run -d --net=host --name etcd quay.io/coreos/etcd:${ETCD_VERSION} \ /usr/local/bin/etcd \ --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,http://127.0.0.1:2379 \ --initial-cluster-state ${CLUSTER_STATE} \ --initial-cluster-token ${TOKEN} \ --discovery ${DISCOVERY}
node3:
# For each machineETCD_VERSION=v3.0.0 TOKEN=etcd-token CLUSTER_STATE=new DISCOVERY=https://discovery.etcd.io/e8a57bd1131c6b466d32989e81e441e3# For node 3THIS_NAME=etcd-node-3 THIS_IP=192.168.33.13 sudo docker run -d --net=host --name etcd quay.io/coreos/etcd:${ETCD_VERSION} \ /usr/local/bin/etcd \ --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,http://127.0.0.1:2379 \ --initial-cluster-state ${CLUSTER_STATE} \ --initial-cluster-token ${TOKEN} \ --discovery ${DISCOVERY}
创建容器的注意事项:
因为该容器是基于alpine的系统镜像,不包含ca证书,因为Discovery URL为https,所以需要下载
sudo docker exec -it etcd sh apk update apk add ca-certificates#然后重启容器docker restart etcd
关于集群的问题节点node3删除和新增节点node4代替node3
①. 新增节点,得到新增节点信息
#新增节点etcdctl member add etcd-node-4 http://192.168.33.14#返回的新增节点信息#Added member named etcd-node-5 with ID b6cf922403228631 to cluster#ETCD_NAME="etcd-node-5"#ETCD_INITIAL_CLUSTER="etcd-node-2=http://192.168.33.12:2380,etcd-#node-5=http://192.168.33.15:2380,=http://192.168.33.13:2380,etcd-node-#4=http://192.168.33.14:2380,etcd-node-1=http://192.168.33.11:2380"#ETCD_INITIAL_CLUSTER_STATE="existing"
②. 新增一台虚拟主机,ip分配为:192.168.33.14,并安装docker
③. 根据返回的新增节点信息,编写新增节点容器脚本
# For each machineETCD_VERSION=v3.0.0 TOKEN=etcd-token CLUSTER_STATE=existing NAME_1=etcd-node-1 NAME_2=etcd-node-2 NAME_3=etcd-node-3 NAME_4=etcd-node-4 HOST_1=192.168.33.11 HOST_2=192.168.33.12 HOST_3=192.168.33.13 HOST_4=192.168.33.14 CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380,${NAME_4}=http://${HOST_4}:2380# For node 1sudo docker run -d --net=host --name etcd quay.io/coreos/etcd:${ETCD_VERSION} \ /usr/local/bin/etcd \ --name ${NAME_4} \ --initial-advertise-peer-urls http://${HOST_4}:2380 --listen-peer-urls http://${HOST_4}:2380 \ --advertise-client-urls http://${HOST_4}:2379 --listen-client-urls http://${HOST_4}:2379,http://127.0.0.1:2379 \ --initial-cluster ${CLUSTER} \ --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
④:删除问题节点node3
etcdctl member remove 问题节点ID
作者:Steven_coder
链接:https://www.jianshu.com/p/d72c64e80499