首先在主机或者虚拟机下设置变量:
REGISTRY=quay.io/coreos/etcd
ETCD_VERSION=v3.3.8
TOKEN=my-etcd-token
CLUSTER_STATE=new
NAME_1=node1 //节点一名称
NAME_2=node2 //节点二名称
HOST_1=192.168.50.250 \
HOST_2=172.16.196.129 \ //两个主机必须能ping通,且开放了2379和2380端口
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380 //集群
DATA_DIR=/home/mok/etcd/data //这个目录必须是主机中存在的
节点1:
THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
docker run \
-d \
-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}
同理节点二设置只需要修改
THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
然后集群就创建结束了
在浏览器中访问:http://127.0.0.1:2379/v2/members可以得到集群的信息:
{"members":
[
{"id":"455687b21ff942af","name":"node2","peerURLs":["http://172.16.196.129:2380"],"clientURLs":["http://172.16.196.129:2379"]},
{"id":"8afdec76e5748137","name":"node1","peerURLs":["http://192.168.50.250:2380"],"clientURLs":["http://192.168.50.250:2379"]}
]
}