docker部署etcd集群

引言

单机部署三节点。机器ip:192.168.159.131

执行命令

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.159.131
HOST_2=192.168.159.131
HOST_3=192.168.159.131
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:12380,${NAME_3}=http://${HOST_3}:22380
DATA_DIR1=/var/lib/etcd1
DATA_DIR2=/var/lib/etcd2
DATA_DIR3=/var/lib/etcd3
# 以上变量,三个节点相同

THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
docker run \
   -p 2379:2379 \
   -p 2380:2380 \
   --volume=${DATA_DIR1}:/etcd-data \
   --name etcd1 ${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_1}
THIS_IP=${HOST_1}
docker run \
   -p 2379:2379 \
   -p 12380:2380 \
   --volume=${DATA_DIR2}:/etcd-data \
   --name etcd2 ${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_1}
THIS_IP=${HOST_1}
docker run \
   -p 2379:2379 \
   -p 22380:2380 \
   --volume=${DATA_DIR3}:/etcd-data \
   --name etcd3 ${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}

国内镜像

上面的镜像是redhat,不好拉取。可以使用下面的命令

REGISTRY=xieyanze/etcd3
ETCD_VERSION=latest
TOKEN=my-etcd-token
CLUSTER_STATE=new
NAME_1=node1
NAME_2=node2
NAME_3=node3
HOST_1=192.168.159.131
HOST_2=192.168.159.131
HOST_3=192.168.159.131
CLUSTER=${
     NAME_1}=http://${
     HOST_1}:2380,${
     NAME_2}=http://${
     HOST_2}:12380,${
     NAME_3}=http://${
     HOST_3}:22380
DATA_DIR1=/var/lib/etcd1
DATA_DIR2=/var/lib/etcd2
DATA_DIR3=/var/lib/etcd3
# 以上变量,三个节点相同

THIS_NAME=${
     NAME_1}
THIS_IP=${
     HOST_1}
docker run -d \
   -p 2379:2379 \
   -p 2380:2380 \
   --volume=${
     DATA_DIR1}:/etcd-data \
   --name etcd1 ${
     REGISTRY}:${
     ETCD_VERSION} \
   --data-dir=/etcd-data --name ${
     THIS_NAME} \
   --initial-advertise-peer-urls http://${
     THIS_IP}:2380 \
   --listen-peer-urls http://0.0.0.0:2380 \
   --initial-cluster ${
     CLUSTER} \
   --initial-cluster-state ${
     CLUSTER_STATE} --initial-cluster-token ${
     TOKEN}

   THIS_NAME=${
     NAME_1}
THIS_IP=${
     HOST_1}
docker run -d \
   -p 12379:2379 \
   -p 12380:2380 \
   --volume=${
     DATA_DIR2}:/etcd-data \
   --name etcd2 ${
     REGISTRY}:${
     ETCD_VERSION} \
   --data-dir=/etcd-data --name ${
     THIS_NAME} \
   --initial-advertise-peer-urls http://${
     THIS_IP}:2380 \
   --listen-peer-urls http://0.0.0.0:2380 \
   --initial-cluster ${
     CLUSTER} \
   --initial-cluster-state ${
     CLUSTER_STATE} --initial-cluster-token ${
     TOKEN}

   THIS_NAME=${
     NAME_1}
THIS_IP=${
     HOST_1}
docker run -d \
   -p 22379:2379 \
   -p 22380:2380 \
   --volume=${
     DATA_DIR3}:/etcd-data \
   --name etcd3 ${
     REGISTRY}:${
     ETCD_VERSION} \
   --data-dir=/etcd-data --name ${
     THIS_NAME} \
   --initial-advertise-peer-urls http://${
     THIS_IP}:2380 \
   --listen-peer-urls http://0.0.0.0:2380 \
   --initial-cluster ${
     CLUSTER} \
   --initial-cluster-state ${
     CLUSTER_STATE} --initial-cluster-token ${
     TOKEN}

你可能感兴趣的:(etcd)