Docker 搭建 etcd 集群及管理
环境
host1 10.1.99.13
host2 10.1.99.14
host3 10.1.99.15
host4 10.1.99.12(用于测试添加删除节点)
初始化集群
host1
$ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \ --name etcd quay.io/coreos/etcd \ etcd \ --name etcd0 \ --advertise-client-urls http: //10.1.99.13:2379,http://10.1.99.13:4001 \ --listen-client-urls http: //0.0.0.0:2379,http://0.0.0.0:4001 \ --initial-advertise-peer-urls http: //10.1.99.13:2380 \ --listen-peer-urls http: //0.0.0.0:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster etcd0=http: //10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd2=http://10.1.99.15:2380 \ --initial-cluster-state new |
host2
1 2 3 4 5 6 7 8 9 10 11 | $ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \ --name etcd quay.io/coreos/etcd \ etcd \ --name etcd1 \ --advertise-client-urls http: //10.1.99.14:2379,http://10.1.99.14:4001 \ --listen-client-urls http: //0.0.0.0:2379,http://0.0.0.0:4001 \ --initial-advertise-peer-urls http: //10.1.99.14:2380 \ --listen-peer-urls http: //0.0.0.0:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster etcd0=http: //10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd2=http://10.1.99.15:2380 \ --initial-cluster-state new |
host3
1 2 3 4 5 6 7 8 9 10 11 | $ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \ --name etcd quay.io/coreos/etcd \ etcd \ -name etcd2 \ --advertise-client-urls http: //10.1.99.15:2379,http://10.1.99.15:4001 \ --listen-client-urls http: //0.0.0.0:2379,http://0.0.0.0:4001 \ --initial-advertise-peer-urls http: //10.1.99.15:2380 \ --listen-peer-urls http: //0.0.0.0:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster etcd0=http: //10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd2=http://10.1.99.15:2380 \ --initial-cluster-state new |
验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #选择任意一个节点 进入 etcd shell $ docker exec -it etcd bin/sh # 查看节点状态 $ etcdctl member list 52a25183c1fa5a39: name=etcd0 peerURLs=http: //10.1.99.13:2380 clientURLs=http://10.1.99.13:2379,http://10.1.99.13:4001 isLeader=false 69aa775a244f2954: name=etcd1 peerURLs=http: //10.1.99.14:2380 clientURLs=http://10.1.99.14:2379,http://10.1.99.14:4001 isLeader=true f18991cb367747f3: name=etcd2 peerURLs=http: //10.1.99.15:2380 clientURLs=http://10.1.99.15:2379,http://10.1.99.15:4001 isLeader=false #查看集群状态 etcdctl cluster-health member 52a25183c1fa5a39 is healthy: got healthy result from http: //10.1.99.13:2379 member 69aa775a244f2954 is healthy: got healthy result from http: //10.1.99.14:2379 member f18991cb367747f3 is healthy: got healthy result from http: //10.1.99.15:2379 cluster is healthy #插入一条记录 $ etcdctl set /home/etcdtest value value #读取插入记录 在其他节点执行获取到相同结果 $ etcdctl get /home/etcdtest value |
集群管理在集群中加入一个节点
#在现有的集群中执行以下命令 $ etcdctl member add etcd3 http://10.1.99.12:2380 Added member named etcd3 with ID 7b33d7070ed90c25 to cluster ETCD_NAME="etcd3" ETCD_INITIAL_CLUSTER="etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd3=http://10.1.99.12:2380,etcd2=http://10.1.99.15:2380" ETCD_INITIAL_CLUSTER_STATE="existing" #在对应的待加入的节点上 $ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \ --name etcd quay.io/coreos/etcd \ etcd \ --name etcd3 \ --advertise-client-urls http://10.1.99.12:2379,http://10.1.99.12:4001 \ --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \ --initial-advertise-peer-urls http://10.1.99.12:2380 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd3=http://10.1.99.12:2380,etcd2=http://10.1.99.15:2380 \ --initial-cluster-state existing 注意事项: 1 使用上面生成的配置内容分别修改对应的属性 2 设置 --initial-cluster-state existing 删除节点, #在现有的集群中执行以下命令 #显示当前集群中的所用节点信息 $ etcdctl member list 52a25183c1fa5a39: name=etcd0 peerURLs=http://10.1.99.13:2380 clientURLs=http://10.1.99.13:2379,http://10.1.99.13:4001 isLeader=false 69aa775a244f2954: name=etcd1 peerURLs=http://10.1.99.14:2380 clientURLs=http://10.1.99.14:2379,http://10.1.99.14:4001 isLeader=true 7b33d7070ed90c25: name=etcd3 peerURLs=http://10.1.99.12:2380 clientURLs=http://10.1.99.12:2379,http://10.1.99.12:4001 isLeader=false f18991cb367747f3: name=etcd2 peerURLs=http://10.1.99.15:2380 clientURLs=http://10.1.99.15:2379,http://10.1.99.15:4001 isLeader=false #在集群中剔除待删除节点 $ etcdctl member remove 7b33d7070ed90c25
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2014-12-07 一、创建线程