如何在多个实例中 运行 etcd?

How to run etcd in multiple instance?

我正在尝试 运行 在 AWS 中使用 3 个实例进行 etcd,但是当我尝试列出成员时出现以下错误:

[ec2-user@etcd1 ~]$ etcdctl member list
{"level":"warn","ts":"2021-10-19T04:50:01.981Z","logger":"etcd-client","caller":"v3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"etcd-endpoints://0xc0002e0a80/127.0.0.1:2379","attempt":0,"error":"rpc error: code = DeadlineExceeded desc = context deadline exceeded"}
Error: context deadline exceeded

我正在使用以下命令启动我的实例:

实例 1:

etcd --data-dir=data.etcd --name etcd1 \
    --initial-advertise-peer-urls http://localhost:2380 --listen-peer-urls http://localhost:2380 \
    --advertise-client-urls http://localhost:2379 --listen-client-urls http://localhost:2379 \
    --initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \
    --initial-cluster-state new --initial-cluster-token token-01

实例 2:

etcd --data-dir=data.etcd --name etcd2 \
    --initial-advertise-peer-urls http://localhost:2380 --listen-peer-urls http://localhost:2380 \
    --advertise-client-urls http://localhost:2379 --listen-client-urls http://localhost:2379 \
    --initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \
    --initial-cluster-state new --initial-cluster-token token-01

实例 3:

etcd --data-dir=data.etcd --name etcd3 \
    --initial-advertise-peer-urls http://localhost:2380 --listen-peer-urls http://localhost:2380 \
    --advertise-client-urls http://localhost:2379 --listen-client-urls http://localhost:2379 \
    --initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 \
    --initial-cluster-state new --initial-cluster-token token-01

如果你知道我做错了什么,请告诉我。

对我来说,您似乎遵循了 https://etcd.io/docs/v3.5/demo/#set-up-a-cluster,如果是这种情况,您应该更改两件事:

  1. 您使用 localhost 代替 ${THIS_IP} 变量,而它应该是集群中每个虚拟机都可以访问的虚拟机 IP 地址(它可以是 public 或私有地址只要它是可达的)
  2. 您在 --initial-cluster 选项中使用了 etcdX,这些应该是虚拟机的 IP 而不是 etcd 成员的名称

我建议在每个虚拟机中设置这些变量:

TOKEN=token-01
CLUSTER_STATE=new
NAME_1=etcd1
NAME_2=etcd2
NAME_3=etcd3
HOST_1=10.240.0.17 # <- should be ip of 1st vm
HOST_2=10.240.0.18 # <- should be ip of 2nd vm
HOST_3=10.240.0.19 # <- should be ip of 3rd vm
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380

然后使用以下命令为 etcd1 启动 etcd

THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
etcd --data-dir=data.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 \
    --initial-cluster ${CLUSTER} \
    --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}

下面是 etcd2 的命令

THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
etcd --data-dir=data.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 \
    --initial-cluster ${CLUSTER} \
    --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}

etcd3 及以下

THIS_NAME=${NAME_3}
THIS_IP=${HOST_3}
etcd --data-dir=data.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 \
    --initial-cluster ${CLUSTER} \
    --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}