环境
3台机器验证集群的可用性。集群信息如下:
机器名 IP地址 角色
node1 10.0.102.214 etcd节点
node2 10.0.102.175 etcd节点
node3 10.0.102.191 etcd节点
步骤
设置环境变量(三个节点都要运行)
REGISTRY=k8s.gcr.io/etcd
ETCD_VERSION=3.2.24
TOKEN=my-etcd-token
NAME_1=k8s-node-1
NAME_2=k8s-node-2
NAME_3=k8s-node-3
HOST_1=10.0.102.218
HOST_2=10.0.102.151
HOST_3=10.0.102.162
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
DATA_DIR=/var/lib/etcd
在node1运行
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 new --initial-cluster-token ${TOKEN}
在 node2 运行
THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
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 existing --initial-cluster-token ${TOKEN}
在 node3 运行:
THIS_NAME=${NAME_3}
THIS_IP=${HOST_3}
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 existing --initial-cluster-token ${TOKEN}
验证集群
在测试验证节点NFS上,运行另一个etcd容器,
以便使用其中的etcdctl命令行程序进行验证
REGISTRY=k8s.gcr.io/etcd
ETCD_VERSION=3.2.24
docker run -d \
--name etcd-client ${REGISTRY}:${ETCD_VERSION} \
/usr/local/bin/etcd
进入容器,查询集群状态
验证存入数据、读取数据
docker exec etcd-client /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://10.0.102.214:2379,http://10.0.102.175:2379,http://10.0.102.191:2379 --write-out=table member list"
docker exec etcd-client /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://10.0.102.214:2379,http://10.0.102.175:2379,http://10.0.102.191:2379 --write-out=table endpoint status"
docker exec etcd-client /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://10.0.102.214:2379,http://10.0.102.175:2379,http://10.0.102.191:2379 --write-out=table endpoint health"
docker exec etcd-client /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://10.0.102.214:2379,http://10.0.102.175:2379,http://10.0.102.191:2379 put url http://www.sina.com.cn"
docker exec etcd-client /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl --endpoints=http://10.0.102.214:2379,http://10.0.102.175:2379,http://10.0.102.191:2379 get url"
或者
docker exec -it 5dd66ba70de2 /bin/bash
/#etcdctl --endpoints=http://10.0.102.218:2379 member list
etcdctl cluster-health
网友评论