一、成员管理
1. 添加成员
a. 执行增加节点操作
etcdctl member add etcd-04 --peer-urls="http://172.20.1.29:2380"
Member 158ed98009d1a70d added to cluster 7191d024b8a252eb
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:~
root@etcd-02:~
+------------------+---------+---------+-------------------------+-------------------------+------------+
| 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:~
Member 483c82031f3eb55c removed from cluster 7191d024b8a252eb
root@etcd-01:~
+------------------+---------+---------+-------------------------+-------------------------+------------+
| 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:~
● 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:~
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| 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:~
Leadership transferred from 11582579cb716706 to 3909c7e83049bd5d
root@etcd-01:~
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| 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:~
--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:~
--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:~
--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:~
root@etcd-02:~
root@etcd-03:~
c. 验证各成员状态
root@etcd-01:~
+-------------------------+--------+------------+-------+
| 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:~
+-------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」