[关闭]
@nalan90 2019-10-27T18:29:47.000000Z 字数 12456 阅读 807

etcd集群部署(二)

自动化运维


简介

etcd集群的组建有三种方式

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


环境准备
  1. ## 安装etcd
  2. work:~ ys$ for item in 161 162 163 164 165; do ssh root@dev-$item 'yum install -y etcd';done
  3. Loaded plugins: fastestmirror
  4. Loading mirror speeds from cached hostfile
  5. * base: mirrors.aliyun.com
  6. * epel: mirrors.tuna.tsinghua.edu.cn
  7. * extras: mirrors.aliyun.com
  8. * updates: mirrors.aliyun.com
  9. Resolving Dependencies
  10. --> Running transaction check
  11. ---> Package etcd.x86_64 0:3.2.15-1.el7 will be installed
  12. --> Finished Dependency Resolution
  13. Installed:
  14. etcd.x86_64 0:3.2.15-1.el7
  15. Complete!
  16. ......
  17. ## 创建etcd目录
  18. work:~ ys$ for item in 161 162 163 164 165; do ssh root@dev-$item 'mkdir -p /opt/data/etcd/data && mkdir -p /opt/data/etcd/wal && mkdir -p /opt/data/etcd/logs && chown -R etcd:etcd /opt/data/etcd';done

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

目标

修改配置文件(/etc/etcd/etcd.conf)
  1. ETCD_DATA_DIR="/opt/data/etcd/data"
  2. ETCD_WAL_DIR="/opt/data/etcd/wal"
  3. ETCD_LISTEN_PEER_URLS="http://172.16.1.161:2380"
  4. ETCD_LISTEN_CLIENT_URLS="http://172.16.1.161:2379,http://127.0.0.1:2379"
  5. ETCD_NAME="infra0"
  6. #[Clustering]
  7. ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.16.1.161:2380"
  8. ETCD_ADVERTISE_CLIENT_URLS="http://172.16.1.161:2379"
  9. ETCD_INITIAL_CLUSTER="infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380"
  10. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  11. ETCD_INITIAL_CLUSTER_STATE="new"
  1. ETCD_DATA_DIR="/opt/data/etcd/data"
  2. ETCD_WAL_DIR="/opt/data/etcd/wal"
  3. ETCD_LISTEN_PEER_URLS="http://172.16.1.162:2380"
  4. ETCD_LISTEN_CLIENT_URLS="http://172.16.1.162:2379,http://127.0.0.1:2379"
  5. ETCD_NAME="infra1"
  6. #[Clustering]
  7. ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.16.1.162:2380"
  8. ETCD_ADVERTISE_CLIENT_URLS="http://172.16.1.162:2379"
  9. ETCD_INITIAL_CLUSTER="infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380"
  10. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  11. ETCD_INITIAL_CLUSTER_STATE="new"
  1. ETCD_DATA_DIR="/opt/data/etcd/data"
  2. ETCD_WAL_DIR="/opt/data/etcd/wal"
  3. ETCD_LISTEN_PEER_URLS="http://172.16.1.163:2380"
  4. ETCD_LISTEN_CLIENT_URLS="http://172.16.1.163:2379,http://127.0.0.1:2379"
  5. ETCD_NAME="infra2"
  6. #[Clustering]
  7. ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.16.1.163:2380"
  8. ETCD_ADVERTISE_CLIENT_URLS="http://172.16.1.163:2379"
  9. ETCD_INITIAL_CLUSTER="infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380"
  10. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  11. ETCD_INITIAL_CLUSTER_STATE="new"
  1. [root@dev-161 ~]# systemctl start etcd
  2. [root@dev-161 ~]# systemctl enable etcd
  3. [root@dev-162 ~]# systemctl start etcd
  4. [root@dev-162 ~]# systemctl enable etcd
  5. [root@dev-163 ~]# systemctl start etcd
  6. [root@dev-163 ~]# systemctl enable etcd

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

  1. [root@dev-161 ~]# etcdctl -v
  2. etcdctl version: 3.2.15
  3. API version: 2
  4. ## 设置ETCDCTL_API环境变量
  5. [root@dev-162 ~]$ echo "export ETCDCTL_API=2" >> ~/.bash_profile
  6. [root@dev-162 ~]$ source ~/.bash_profile
  7. [root@dev-162 ~]$ echo $ETCDCTL_API
  8. 2
  9. [root@dev-161 ~]# etcdctl member list
  10. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=true
  11. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=false
  12. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  13. [root@dev-161 ~]# 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. [root@dev-161 ~]# etcdctl member add infra3 http://172.16.1.164:2379
  2. Added member named infra3 with ID 12ceea0b7048b928 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. [root@dev-161 ~]# etcdctl member list
  8. 12ceea0b7048b928[unstarted]: peerURLs=http://172.16.1.164:2379
  9. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=true
  10. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=false
  11. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  12. [root@dev-161 ~]# etcdctl cluster-health
  13. member 12ceea0b7048b928 is unreachable: no available published client urls
  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
  1. ## 修改配置如下
  2. ETCD_DATA_DIR="/opt/data/etcd/data"
  3. ETCD_WAL_DIR="/opt/data/etcd/wal"
  4. ETCD_LISTEN_PEER_URLS="http://172.16.1.164:2380"
  5. ETCD_LISTEN_CLIENT_URLS="http://172.16.1.164:2379,http://127.0.0.1:2379"
  6. ETCD_NAME="infra3"
  7. #[Clustering]
  8. ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.16.1.164:2380"
  9. ETCD_ADVERTISE_CLIENT_URLS="http://172.16.1.164:2379"
  10. ## 上面生成的环境变量
  11. 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"
  12. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  13. ETCD_INITIAL_CLUSTER_STATE="existing"
  14. ## 启动etcd
  15. [root@dev-164 ~]# systemctl start etcd
  16. [root@dev-164 ~]# systemctl enable etcd
  17. Created symlink from /etc/systemd/system/multi-user.target.wants/etcd.service to /usr/lib/systemd/system/etcd.service.
  18. [root@dev-164 ~]# systemctl status etcd
  19. etcd.service - Etcd Server
  20. Loaded: loaded (/usr/lib/systemd/system/etcd.service; enabled; vendor preset: disabled)
  21. Active: active (running) since Wed 2018-03-21 09:53:01 CST; 52s ago
  22. Main PID: 3047 (etcd)
  23. CGroup: /system.slice/etcd.service
  24. └─3047 /usr/bin/etcd --name=infra3 --data-dir=/opt/data/etcd/data --listen-client-urls=http://172.16.1.164:2379,http://127.0.0.1:2379
  1. [root@dev-161 ~]# etcdctl member add infra4 http://172.16.1.165:2379
  2. Added member named infra4 with ID 7ca9bd7d5389fec7 to cluster
  3. ETCD_NAME="infra4"
  4. ETCD_INITIAL_CLUSTER="infra3=http://172.16.1.164:2379,infra4=http://172.16.1.165:2379,infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380"
  5. ETCD_INITIAL_CLUSTER_STATE="existing"
  6. [root@dev-161 ~]# etcdctl member list
  7. 12ceea0b7048b928: name=infra3 peerURLs=http://172.16.1.164:2379 clientURLs=http://172.16.1.164:2379 isLeader=false
  8. 7ca9bd7d5389fec7[unstarted]: peerURLs=http://172.16.1.165:2379
  9. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=true
  10. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=false
  11. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  12. [root@dev-161 ~]# etcdctl cluster-health
  13. member 12ceea0b7048b928 is healthy: got healthy result from http://172.16.1.164:2379
  14. member 7ca9bd7d5389fec7 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. ETCD_DATA_DIR="/opt/data/etcd/data"
  3. ETCD_WAL_DIR="/opt/data/etcd/wal"
  4. ETCD_LISTEN_PEER_URLS="http://172.16.1.165:2380"
  5. ETCD_LISTEN_CLIENT_URLS="http://172.16.1.165:2379,http://127.0.0.1:2379"
  6. ETCD_NAME="infra4"
  7. #
  8. #[Clustering]
  9. ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.16.1.165:2380"
  10. ETCD_ADVERTISE_CLIENT_URLS="http://172.16.1.165:2379"
  11. ETCD_INITIAL_CLUSTER="infra3=http://172.16.1.164:2379,infra4=http://172.16.1.165:2379,infra0=http://172.16.1.161:2380,infra1=http://172.16.1.162:2380,infra2=http://172.16.1.163:2380"
  12. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  13. ETCD_INITIAL_CLUSTER_STATE="existing"
  14. ## 启动etcd
  15. [root@dev-165 ~]# systemctl start etcd
  16. [root@dev-165 ~]# systemctl enable etcd
  17. Created symlink from /etc/systemd/system/multi-user.target.wants/etcd.service to /usr/lib/systemd/system/etcd.service.
  18. [root@dev-165 ~]# systemctl status etcd
  19. etcd.service - Etcd Server
  20. Loaded: loaded (/usr/lib/systemd/system/etcd.service; enabled; vendor preset: disabled)
  21. Active: active (running) since Wed 2018-03-21 10:01:17 CST; 7s ago
  22. Main PID: 3073 (etcd)
  23. CGroup: /system.slice/etcd.service
  24. └─3073 /usr/bin/etcd --name=infra4 --data-dir=/opt/data/etcd/data --listen-client-urls=http://172.16.1.165:2379,http://127.0.0.1:2379
  1. [root@dev-161 ~]# etcdctl member list
  2. 12ceea0b7048b928: name=infra3 peerURLs=http://172.16.1.164:2379 clientURLs=http://172.16.1.164:2379 isLeader=false
  3. 7ca9bd7d5389fec7: name=infra4 peerURLs=http://172.16.1.165:2379 clientURLs=http://172.16.1.165:2379 isLeader=false
  4. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=true
  5. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=false
  6. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  7. [root@dev-161 ~]# etcdctl cluster-health
  8. member 12ceea0b7048b928 is healthy: got healthy result from http://172.16.1.164:2379
  9. member 7ca9bd7d5389fec7 is healthy: got healthy result from http://172.16.1.165: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. [root@dev-161 ~]# etcdctl member list
  14. 12ceea0b7048b928: name=infra3 peerURLs=http://172.16.1.164:2379 clientURLs=http://172.16.1.164:2379 isLeader=false
  15. 7ca9bd7d5389fec7: name=infra4 peerURLs=http://172.16.1.165:2379 clientURLs=http://172.16.1.165:2379 isLeader=false
  16. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=true
  17. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=false
  18. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  19. [root@dev-161 ~]# etcdctl cluster-health
  20. member 12ceea0b7048b928 is healthy: got healthy result from http://172.16.1.164:2379
  21. member 7ca9bd7d5389fec7 is healthy: got healthy result from http://172.16.1.165:2379
  22. member 88e89ef57491e822 is healthy: got healthy result from http://172.16.1.161:2379
  23. member 905994ad8f9fbbcd is healthy: got healthy result from http://172.16.1.162:2379
  24. member cf5f03d58c1b363b is healthy: got healthy result from http://172.16.1.163:2379
  25. cluster is healthy
  26. ## 删除infra3、infra4两个节点
  27. [root@dev-161 ~]# etcdctl member remove 7ca9bd7d5389fec7
  28. Removed member 7ca9bd7d5389fec7 from cluster
  29. [root@dev-161 ~]# etcdctl member remove 12ceea0b7048b928
  30. Removed member 12ceea0b7048b928 from cluster
  31. [root@dev-161 ~]# etcdctl member list
  32. 88e89ef57491e822: name=infra0 peerURLs=http://172.16.1.161:2380 clientURLs=http://172.16.1.161:2379 isLeader=true
  33. 905994ad8f9fbbcd: name=infra1 peerURLs=http://172.16.1.162:2380 clientURLs=http://172.16.1.162:2379 isLeader=false
  34. cf5f03d58c1b363b: name=infra2 peerURLs=http://172.16.1.163:2380 clientURLs=http://172.16.1.163:2379 isLeader=false
  35. ## 查看infra3、infra4两个节点的状态
  36. [root@dev-164 ~]# systemctl status etcd
  37. etcd.service - Etcd Server
  38. Loaded: loaded (/usr/lib/systemd/system/etcd.service; enabled; vendor preset: disabled)
  39. Active: inactive (dead) since Thu 2018-03-22 09:20:36 CST; 16min ago
  40. Main PID: 3047 (code=exited, status=0/SUCCESS)
  41. [root@dev-165 ~]# systemctl status etcd
  42. etcd.service - Etcd Server
  43. Loaded: loaded (/usr/lib/systemd/system/etcd.service; enabled; vendor preset: disabled)
  44. Active: inactive (dead) since Thu 2018-03-22 09:20:28 CST; 14min ago
  45. Main PID: 3073 (code=exited, status=0/SUCCESS)

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

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