etcd集群管理
作者:互联网
一、成员管理
1. 添加成员
a. 执行增加节点操作
etcdctl member add etcd-04 --peer-urls="http://172.20.1.29:2380"
Member 158ed98009d1a70d added to cluster 7191d024b8a252eb
## 下面这些需要写到新节点的配置文件中,添加完成之后,需要修改所有节点配置文件中的 ETCD_INITIAL_CLUSTER 参数
ETCD_NAME="etcd-04" #
ETCD_INITIAL_CLUSTER="etcd-02=http://172.20.1.27:2380,etcd-04=http://172.20.1.29:2380,etcd-01=http://172.20.1.26:2380,etcd-03=http://172.20.1.28:2380"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.20.1.29:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"
b. 在新添加的节点启动 etcd
服务
root@etcd-04:~# systemctl start etcd
root@etcd-02:~# etcdctl --endpoints="http://172.20.1.26:2379,http://172.20.1.27:2379,http://172.20.1.28:2379,http://172.20.1.29:2379" member list --write-out table
+------------------+---------+---------+-------------------------+-------------------------+------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER |
+------------------+---------+---------+-------------------------+-------------------------+------------+
| 11582579cb716706 | started | etcd-02 | http://172.20.1.27:2380 | http://172.20.1.27:2379 | false |
| 3909c7e83049bd5d | started | etcd-01 | http://172.20.1.26:2380 | http://172.20.1.26:2379 | false |
| 483c82031f3eb55c | started | etcd-04 | http://172.20.1.29:2380 | http://172.20.1.29:2379 | false |
| a10314259291b7b2 | started | etcd-03 | http://172.20.1.28:2380 | http://172.20.1.28:2379 | false |
+------------------+---------+---------+-------------------------+-------------------------+------------+
2. 移除成员
a. 选择一个节点执行移除成员命令
root@etcd-01:~# etcdctl --endpoints="http://172.20.1.26:2379,http://172.20.1.27:2379,http://172.20.1.28:2379,http://172.20.1.29:2379" member remove 483c82031f3eb55c
Member 483c82031f3eb55c removed from cluster 7191d024b8a252eb
root@etcd-01:~# etcdctl --endpoints="http://172.20.1.26:2379,http://172.20.1.27:2379,http://172.20.1.28:2379" member list --write-out table
+------------------+---------+---------+-------------------------+-------------------------+------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER |
+------------------+---------+---------+-------------------------+-------------------------+------------+
| 11582579cb716706 | started | etcd-02 | http://172.20.1.27:2380 | http://172.20.1.27:2379 | false |
| 3909c7e83049bd5d | started | etcd-01 | http://172.20.1.26:2380 | http://172.20.1.26:2379 | false |
| a10314259291b7b2 | started | etcd-03 | http://172.20.1.28:2380 | http://172.20.1.28:2379 | false |
+------------------+---------+---------+-------------------------+-------------------------+------------+
b. 在被移除的节点查看 etcd
日志
root@etcd-04:~# systemctl status etcd.service
● etcd.service - Etcd Server
Loaded: loaded (/etc/systemd/system/etcd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"info","ts":"2022-07-30T11:45:31.549+0800","caller":"rafthttp/peer.go:335","msg":"stopped remote peer","remote-peer-id":"11582579cb716706"}
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"info","ts":"2022-07-30T11:45:31.549+0800","caller":"rafthttp/peer.go:330","msg":"stopping remote peer","remote-peer-id":"3909c7e83049bd5d"}
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"warn","ts":"2022-07-30T11:45:31.556+0800","caller":"rafthttp/stream.go:286","msg":"closed TCP streaming connection with remote peer","stream-writer-type":"stream MsgApp v2","remote-
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"info","ts":"2022-07-30T11:45:31.556+0800","caller":"rafthttp/stream.go:294","msg":"stopped TCP streaming connection with remote peer","stream-writer-type":"stream MsgApp v2","remote
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"warn","ts":"2022-07-30T11:45:31.557+0800","caller":"rafthttp/stream.go:286","msg":"closed TCP streaming connection with remote peer","stream-writer-type":"stream Message","remote-pe
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"info","ts":"2022-07-30T11:45:31.557+0800","caller":"rafthttp/stream.go:294","msg":"stopped TCP streaming connection with remote peer","stream-writer-type":"stream Message","remote-p
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"info","ts":"2022-07-30T11:45:31.557+0800","caller":"rafthttp/pipeline.go:85","msg":"stopped HTTP pipelining with remote peer","local-member-id":"483c82031f3eb55c","remote-peer-id":"
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"info","ts":"2022-07-30T11:45:31.557+0800","caller":"rafthttp/stream.go:442","msg":"stopped stream reader with remote peer","stream-reader-type":"stream MsgApp v2","local-member-id":
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"info","ts":"2022-07-30T11:45:31.558+0800","caller":"rafthttp/stream.go:442","msg":"stopped stream reader with remote peer","stream-reader-type":"stream Message","local-member-id":"4
Jul 30 11:45:31 etcd-04 etcd[1077]: {"level":"info","ts":"2022-07-30T11:45:31.558+0800","caller":"rafthttp/peer.go:335","msg":"stopped remote peer","remote-peer-id":"3909c7e83049bd5d"}
3. 设置Leader节点
a. 查看集群中成员状态
root@etcd-01:~# etcdctl --endpoints="http://172.20.1.26:2379,http://172.20.1.27:2379,http://172.20.1.28:2379" endpoint status --write-out table
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://172.20.1.26:2379 | 3909c7e83049bd5d | 3.5.3 | 20 kB | false | false | 4 | 23 | 23 | |
| http://172.20.1.27:2379 | 11582579cb716706 | 3.5.3 | 20 kB | true | false | 4 | 23 | 23 | |
| http://172.20.1.28:2379 | a10314259291b7b2 | 3.5.3 | 20 kB | false | false | 4 | 23 | 23 | |
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
b. 设置新的Leader节点
root@etcd-01:~# etcdctl --endpoints="http://172.20.1.26:2379,http://172.20.1.27:2379,http://172.20.1.28:2379" move-leader 3909c7e83049bd5d
Leadership transferred from 11582579cb716706 to 3909c7e83049bd5d
root@etcd-01:~# etcdctl --endpoints="http://172.20.1.26:2379,http://172.20.1.27:2379,http://172.20.1.28:2379" endpoint status --write-out table
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://172.20.1.26:2379 | 3909c7e83049bd5d | 3.5.3 | 20 kB | true | false | 5 | 24 | 24 | |
| http://172.20.1.27:2379 | 11582579cb716706 | 3.5.3 | 20 kB | false | false | 5 | 24 | 24 | |
| http://172.20.1.28:2379 | a10314259291b7b2 | 3.5.3 | 20 kB | false | false | 5 | 24 | 24 | |
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
二、备份恢复
1. 数据备份
etcdctl snapshot save /mnt/etcd_snapshot.db
{"level":"info","ts":"2022-07-30T12:09:34.474+0800","caller":"snapshot/v3_snapshot.go:65","msg":"created temporary db file","path":"/mnt/etcd_snapshot.db.part"}
{"level":"info","ts":"2022-07-30T12:09:34.481+0800","logger":"client","caller":"v3/maintenance.go:211","msg":"opened snapshot stream; downloading"}
{"level":"info","ts":"2022-07-30T12:09:34.482+0800","caller":"snapshot/v3_snapshot.go:73","msg":"fetching snapshot","endpoint":"127.0.0.1:2379"}
{"level":"info","ts":"2022-07-30T12:09:34.489+0800","logger":"client","caller":"v3/maintenance.go:219","msg":"completed snapshot read; closing"}
{"level":"info","ts":"2022-07-30T12:09:34.491+0800","caller":"snapshot/v3_snapshot.go:88","msg":"fetched snapshot","endpoint":"127.0.0.1:2379","size":"20 kB","took":"now"}
{"level":"info","ts":"2022-07-30T12:09:34.491+0800","caller":"snapshot/v3_snapshot.go:97","msg":"saved","path":"/mnt/etcd_snapshot.db"}
Snapshot saved at /mnt/etcd_snapshot.db
2. 数据恢复
a. 恢复备份到数据目录
root@etcd-01:~# etcdutl snapshot restore /mnt/etcd_snapshot.db \
--data-dir /apps/etcd/ \
--name etcd-01 \
--initial-cluster etcd-02=http://172.20.1.27:2380,etcd-01=http://172.20.1.26:2380,etcd-03=http://172.20.1.28:2380 \
--initial-cluster-token etcd-cluster-1 \
--initial-advertise-peer-urls http://172.20.1.26:2380
root@etcd-02:~# etcdutl snapshot restore /mnt/etcd_snapshot.db \
--data-dir /apps/etcd/ \
--name etcd-02 \
--initial-cluster etcd-02=http://172.20.1.27:2380,etcd-01=http://172.20.1.26:2380,etcd-03=http://172.20.1.28:2380 \
--initial-cluster-token etcd-cluster-1 \
--initial-advertise-peer-urls http://172.20.1.27:2380
root@etcd-03:~# etcdutl snapshot restore /mnt/etcd_snapshot.db \
--data-dir /apps/etcd/ \
--name etcd-03 \
--initial-cluster etcd-02=http://172.20.1.27:2380,etcd-01=http://172.20.1.26:2380,etcd-03=http://172.20.1.28:2380 \
--initial-cluster-token etcd-cluster-1 \
--initial-advertise-peer-urls http://172.20.1.28:2380
b. 启动etcd服务
root@etcd-01:~# systemctl status etcd
root@etcd-02:~# systemctl status etcd
root@etcd-03:~# systemctl status etcd
c. 验证各成员状态
root@etcd-01:~# etcdctl --endpoints="http://172.20.1.26:2379,http://172.20.1.27:2379,http://172.20.1.28:2379" endpoint health --write-out table
+-------------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+-------------------------+--------+------------+-------+
| http://172.20.1.26:2379 | true | 5.506461ms | |
| http://172.20.1.27:2379 | true | 8.588275ms | |
| http://172.20.1.28:2379 | true | 9.009899ms | |
+-------------------------+--------+------------+-------+
root@etcd-01:~# etcdctl --endpoints="http://172.20.1.26:2379,http://172.20.1.27:2379,http://172.20.1.28:2379" endpoint status --write-out table
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://172.20.1.26:2379 | 3909c7e83049bd5d | 3.5.3 | 20 kB | true | false | 2 | 15 | 15 | |
| http://172.20.1.27:2379 | 11582579cb716706 | 3.5.3 | 20 kB | false | false | 2 | 15 | 15 | |
| http://172.20.1.28:2379 | 48396dd3bf139ba4 | 3.5.3 | 20 kB | false | false | 2 | 15 | 15 | |
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
d. 验证数据
etcdctl --endpoints="http://172.20.1.26:2379,http://172.20.1.27:2379,http://172.20.1.28:2379" get /liytest --print-value-only=true
liytest
标签:http,管理,--,2380,集群,2379,etcd,172.20 来源: https://www.cnblogs.com/liy36/p/16534747.html