基于动态发现的ETCD集群的节点迁移

基于docker-compose编排了一组etcd集群,如果想把其中一个节点替换掉应该怎么操作呢,网上有一些质量非常高的技术文章,但是有一些细节地方没有说清楚。

先把我的docker-compose.yml帖上来。

version: '3'
services:
  etcd1:
    image: "quay.io/coreos/etcd:latest"
    network_mode: "host"
    volumes:
      - /data0/etcd/etcd1/:/etcd_data/:rw
    command:
      - etcd
      - --name
      - etcd1
      - --data-dir
      - /etcd_data
      - --listen-peer-urls
      - http://${DOCKER_IIP}:2380
      - --listen-client-urls
      - http://${DOCKER_IIP}:2379,http://127.0.0.1:2379
      - --advertise-client-urls
      - http://${DOCKER_IIP}:2379
      - --initial-advertise-peer-urls
      - http://${DOCKER_IIP}:2380
      - --discovery
      - http://discovery.etcd.io/TTTTTTTTTTTTTTTTTTTTTTT

下面来进行替换节点的操作,先查看一下节点列表

/ # etcdctl member list
1ca4fc0d4b2c9fa2: name=etcd1 peerURLs=http://192.168.56.42:2380 clientURLs=http://192.168.56.42:2379 isLeader=true
4cf04322e0117c69: name=etcd4 peerURLs=http://192.168.56.33:2380 clientURLs=http://192.168.56.33:2379 isLeader=false
6dc5120e6c9ba574: name=etcd3 peerURLs=http://192.168.56.42:2382 clientURLs=http://192.168.56.42:2381 isLeader=false

先拿掉etcd4。

/ # etcdctl member remove 4cf04322e0117c69
Removed member 4cf04322e0117c69 from cluster

再进行添加操作。

/ # etcdctl member add etcd2 http://192.168.56.33:2380
Added member named etcd2 with ID d401a8b3607279fc to cluster

ETCD_NAME="etcd2"
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.56.42:2380,etcd3=http://192.168.56.42:2382,etcd2=http://192.168.56.33:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"

接下来是重点了,把这几个环境变量记录一下,修改一下原来的docker-compose.yml,进行新节点启动。

version: '3'
services:
  etcd2:
    image: "quay.io/coreos/etcd:latest"
    network_mode: "host"
    volumes:
      - /data0/etcd/etcd2/:/etcd_data/:rw
    command:
      - etcd
      - --name
      - etcd2
      - --data-dir
      - /etcd_data
      - --listen-peer-urls
      - http://${DOCKER_IIP}:2380
      - --listen-client-urls
      - http://${DOCKER_IIP}:2379,http://127.0.0.1:2379
      - --advertise-client-urls
      - http://${DOCKER_IIP}:2379
      - --initial-advertise-peer-urls
      - http://${DOCKER_IIP}:2380
      #- --discovery
      #- http://discovery.etcd.io/df65ece6e8eef0db7548c7a51adb894e
      - --initial-cluster-state
      - existing
      - --initial-cluster
      - etcd1=http://192.168.56.42:2380,etcd2=http://192.168.56.33:2380,etcd3=http://192.168.56.42:2382

将原来的–discovery临时注释,添加下面几行,然后进行节点启动,节点启动成功之后,把上面这一次临时修改还原就可以。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Captcha Code