[关闭]
@nalan90 2018-03-15T09:14:56.000000Z 字数 11327 阅读 716

etcd集群部署

自动化运维


简介

etcd集群的组建有三种方式

本文以static的方式进行演示,通常集群的节点为奇数(3、5、7),一般部署3个即可。


环境准备

node及etcd对应关系
Node Name IP HostName Etcd Name
node1 172.16.1.161 dev-161 infra0
node2 172.16.1.162 dev-162 infra1
node3 172.16.1.163 dev-163 infra2
node4 172.16.1.164 dev-164 infra3
node5 172.16.1.165 dev-165 infra4

目标

安装脚本
  1. etcd --name infra0 --initial-advertise-peer-urls http://172.16.1.161:2380 \
  2. --data-dir /opt/data/etcd/data \
  3. --wal-dir /opt/data/etcd/wal \
  4. --listen-peer-urls http://172.16.1.161:2380 \
  5. --listen-client-urls http://172.16.1.161:2379,http://127.0.0.1:2379 \
  6. --advertise-client-urls http://172.16.1.161:2379 \
  7. --initial-cluster-token etcd-cluster \
  8. --initial-cluster infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380 \
  9. --initial-cluster-state new >> /opt/data/etcd/logs/infra0.log 2>&1 &
  1. etcd --name infra1 --initial-advertise-peer-urls http://172.16.1.162:2380 \
  2. --data-dir /opt/data/etcd/data \
  3. --wal-dir /opt/data/etcd/wal \
  4. --listen-peer-urls http://172.16.1.162:2380 \
  5. --listen-client-urls http://172.16.1.162:2379,http://127.0.0.1:2379 \
  6. --advertise-client-urls http://172.16.1.162:2379 \
  7. --initial-cluster-token etcd-cluster \
  8. --initial-cluster infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380 \
  9. --initial-cluster-state new >> /opt/data/etcd/logs/etcd.log 2>&1 &
  1. etcd --name infra2 --initial-advertise-peer-urls http://172.16.1.163:2380 \
  2. --data-dir /opt/data/etcd/data \
  3. --wal-dir /opt/data/etcd/wal \
  4. --listen-peer-urls http://172.16.1.163:2380 \
  5. --listen-client-urls http://172.16.1.163:2379,http://127.0.0.1:2379 \
  6. --advertise-client-urls http://172.16.1.163:2379 \
  7. --initial-cluster-token etcd-cluster \
  8. --initial-cluster infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380 \
  9. --initial-cluster-state new >> /opt/data/etcd/logs/etcd.log 2>&1 &

etcd API有v2、v3两个版本,v2提供更多的操作命令,因此我们通过v2进行集群的管理

  1. [test@dev-162 ~]$ etcdctl -v
  2. etcdctl version: 3.2.15
  3. API version: 2
  4. ## 设置ETCDCTL_API环境变量
  5. [test@dev-162 ~]$ echo "export ETCDCTL_API=2" >> ~/.bash_profile
  6. [test@dev-162 ~]$ source ~/.bash_profile
  7. [test@dev-162 ~]$ echo $ETCDCTL_API
  8. 2
  9. [test@dev-162 ~]$ etcdctl member list
  10. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=false
  11. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=true
  12. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  13. [test@dev-162 ~]$ etcdctl cluster-health
  14. member 88e89ef57491e822 is healthy: got healthy result from http://172.16.1.161:2379
  15. member 905994ad8f9fbbcd is healthy: got healthy result from http://172.16.1.162:2379
  16. member cf5f03d58c1b363b is healthy: got healthy result from http://172.16.1.163:2379
  17. cluster is healthy

也可以通过页面访问

image_1c8h5phaeno2smvrvle701joc9.png-74.8kB

image_1c8hal18pfc01kfhsn61go4qkm.png-19.7kB

到现在为止三个节点的etcd已经可以正常访问


添加新的节点
  1. [test@dev-162 ~]$ etcdctl member add infra3 http://172.16.1.164:2379
  2. Added member named infra3 with ID 206aaee1711610f7 to cluster
  3. ## 执行完成生成如下环境变量
  4. ETCD_NAME="infra3"
  5. ETCD_INITIAL_CLUSTER="infra3=http://172.16.1.164:2379,infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380"
  6. ETCD_INITIAL_CLUSTER_STATE="existing"
  7. [test@dev-162 ~]$ etcdctl member list
  8. ## 新添加的节点还处于未启动状态
  9. 5922e75caa279deb[unstarted]: peerURLs=http://172.16.1.164:2379
  10. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=false
  11. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=true
  12. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  13. [test@dev-162 ~]$ etcdctl cluster-health
  14. member 5922e75caa279deb is unreachable: no available published client urls
  15. member 88e89ef57491e822 is healthy: got healthy result from http://172.16.1.161:2379
  16. member 905994ad8f9fbbcd is healthy: got healthy result from http://172.16.1.162:2379
  17. member cf5f03d58c1b363b is healthy: got healthy result from http://172.16.1.163:2379
  18. cluster is healthy
  1. ## 导入以上生成的环境变量
  2. [test@dev-164 ~]$ export ETCD_NAME="infra3"
  3. [test@dev-164 ~]$ export ETCD_INITIAL_CLUSTER="infra3=http://172.16.1.164:2379,infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380"
  4. [test@dev-164 ~]$ export ETCD_INITIAL_CLUSTER_STATE="existing"
  5. [test@dev-164 ~]$ etcd --data-dir /opt/data/etcd/data \
  6. --wal-dir /opt/data/etcd/wal \
  7. --listen-client-urls http://172.16.1.164:2379,http://127.0.0.1:2379 \
  8. --advertise-client-urls http://172.16.1.164:2379 >> /opt/data/etcd/logs/etcd.log 2>&1 &
  9. [test@dev-164 ~]$ ps -ef | grep etcd
  10. test 5243 4999 7 13:17 pts/0 00:00:00 etcd --data-dir /opt/data/etcd/data --wal-dir /opt/data/etcd/wal --listen-client-urls http://172.16.1.164:2379,http://127.0.0.1:2379 --advertise-client-urls http://172.16.1.164:2379
  1. [test@dev-162 ~]$ etcdctl member add infra4 http://172.16.1.165:2379
  2. Added member named infra4 with ID 4598a311935f7513 to cluster
  3. ## 执行完成生成如下环境变量
  4. ETCD_NAME="infra4"
  5. ETCD_INITIAL_CLUSTER="infra4=http://172.16.1.165:2379,infra3=http://172.16.1.164:2379,infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380"
  6. ETCD_INITIAL_CLUSTER_STATE="existing"
  7. [test@dev-162 ~]$ etcdctl member list
  8. ## 新添加的节点还处于未启动状态
  9. 4598a311935f7513[unstarted]: peerURLs=http://172.16.1.165:2379
  10. 5922e75caa279deb: name=infra3 peerURLs=http://172.16.1.164:2379 clientURLs=http://172.16.1.164:2379 isLeader=false
  11. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=false
  12. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=true
  13. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  14. [test@dev-162 ~]$ etcdctl cluster-health
  15. member 4598a311935f7513 is unreachable: no available published client urls
  16. member 5922e75caa279deb is healthy: got healthy result from http://172.16.1.164:2379
  17. member 88e89ef57491e822 is healthy: got healthy result from http://172.16.1.161:2379
  18. member 905994ad8f9fbbcd is healthy: got healthy result from http://172.16.1.162:2379
  19. member cf5f03d58c1b363b is healthy: got healthy result from http://172.16.1.163:2379
  20. cluster is healthy
  1. ## 导入以上生成的环境变量
  2. [test@dev-165 ~]$ export ETCD_NAME="infra4"
  3. [test@dev-165 ~]$ export ETCD_INITIAL_CLUSTER="infra4=http://172.16.1.165:2379,infra3=http://172.16.1.164:2379,infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380"
  4. [test@dev-165 ~]$ export ETCD_INITIAL_CLUSTER_STATE="existing"
  5. [test@dev-165 ~]$ etcd --data-dir /opt/data/etcd/data \
  6. --wal-dir /opt/data/etcd/wal \
  7. --listen-client-urls http://172.16.1.165:2379,http://127.0.0.1:2379 \
  8. --advertise-client-urls http://172.16.1.165:2379 >> /opt/data/etcd/logs/etcd.log 2>&1 &
  9. [test@dev-165 ~]$ ps -ef | grep etcd
  10. test 2973 2810 14 13:58 pts/0 00:00:00 etcd --data-dir /opt/data/etcd/data --wal-dir /opt/data/etcd/wal --listen-client-urls http://172.16.1.165:2379,http://127.0.0.1:2379 --advertise-client-urls http://172.16.1.165:2379
  1. [test@dev-162 ~]$ etcdctl member list
  2. 4598a311935f7513: name=infra4 peerURLs=http://172.16.1.165:2379 clientURLs=http://172.16.1.165:2379 isLeader=false
  3. 5922e75caa279deb: name=infra3 peerURLs=http://172.16.1.164:2379 clientURLs=http://172.16.1.164:2379 isLeader=false
  4. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=false
  5. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=true
  6. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  7. [test@dev-162 ~]$ etcdctl cluster-health
  8. member 4598a311935f7513 is healthy: got healthy result from http://172.16.1.165:2379
  9. member 5922e75caa279deb is healthy: got healthy result from http://172.16.1.164:2379
  10. member 88e89ef57491e822 is healthy: got healthy result from http://172.16.1.161:2379
  11. member 905994ad8f9fbbcd is healthy: got healthy result from http://172.16.1.162:2379
  12. member cf5f03d58c1b363b is healthy: got healthy result from http://172.16.1.163:2379
  13. cluster is healthy

node4、node5两个节点正常添加至etcd集群,五个节点正常访问


删除节点
  1. [test@dev-162 ~]$ etcdctl member --help
  2. NAME:
  3. etcdctl member - member add, remove and list subcommands
  4. USAGE:
  5. etcdctl member command [command options] [arguments...]
  6. COMMANDS:
  7. list enumerate existing cluster members
  8. add add a new member to the etcd cluster
  9. remove remove an existing member from the etcd cluster
  10. update update an existing member in the etcd cluster
  11. OPTIONS:
  12. --help, -h show help
  13. ## 当前有五个节点
  14. [test@dev-162 ~]$ etcdctl member list
  15. 4598a311935f7513: name=infra4 peerURLs=http://172.16.1.165:2379 clientURLs=http://172.16.1.165:2379 isLeader=false
  16. 5922e75caa279deb: name=infra3 peerURLs=http://172.16.1.164:2379 clientURLs=http://172.16.1.164:2379 isLeader=false
  17. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=false
  18. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=true
  19. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  20. ## 删除infra4
  21. [test@dev-162 ~]$ etcdctl member remove 4598a311935f7513
  22. Removed member 4598a311935f7513 from cluster
  23. ## 再次查看仅剩四个节点
  24. [test@dev-162 ~]$ etcdctl member list
  25. 5922e75caa279deb: name=infra3 peerURLs=http://172.16.1.164:2379 clientURLs=http://172.16.1.164:2379 isLeader=false
  26. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=false
  27. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=true
  28. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  29. [test@dev-162 ~]$ etcdctl cluster-health
  30. member 5922e75caa279deb is healthy: got healthy result from http://172.16.1.164:2379
  31. member 88e89ef57491e822 is healthy: got healthy result from http://172.16.1.161:2379
  32. member 905994ad8f9fbbcd is healthy: got healthy result from http://172.16.1.162:2379
  33. member cf5f03d58c1b363b is healthy: got healthy result from http://172.16.1.163:2379
  34. cluster is healthy
  35. ## 删除infra3之前,etcd进程正在运行
  36. [test@dev-164 ~]$ ps -ef | grep etcd
  37. test 5243 4999 6 13:17 pts/0 00:03:02 etcd --data-dir /opt/data/etcd/data --wal-dir /opt/data/etcd/wal --listen-client-urls http://172.16.1.164:2379,http://127.0.0.1:2379 --advertise-client-urls http://172.16.1.164:2379
  38. ## 删除节点infra3
  39. [test@dev-162 ~]$ etcdctl member remove 5922e75caa279deb
  40. Removed member 5922e75caa279deb from cluster
  41. [test@dev-162 ~]$ etcdctl member list
  42. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=false
  43. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=true
  44. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  45. [test@dev-162 ~]$ etcdctl cluster-health
  46. member 88e89ef57491e822 is healthy: got healthy result from http://172.16.1.161:2379
  47. member 905994ad8f9fbbcd is healthy: got healthy result from http://172.16.1.162:2379
  48. member cf5f03d58c1b363b is healthy: got healthy result from http://172.16.1.163:2379
  49. cluster is healthy
  50. ## 删除infra3之后,node4上的etcd进程停止运行
  51. [test@dev-164 ~]$ ps -ef | grep etcd
  52. test 5351 5327 0 14:06 pts/1 00:00:00 grep --color=auto etcd

从集群中删除指定的etcd节点后,相应服务器上的etcd进程将会停止运行

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注