etcd跨机房部署方案
使用ETCD做为元数据方便快捷,但是谈到跨机房灾备可能就迷糊了,我们在做节日灾备的时候同样遇到了问题, 通过查阅官方文档找到了解决方案,官方提供make-mirror方法,提供数据镜像服务
注意: make-mirror 的使用需要依赖于API版本3, 使用API2的无法通过该工具做数据同步
有关ETCD的编译安装这里就不在说明了, 不明白的可以参考官方说明:https://github.com/coreos/etcd
1. 启动集群1
#!/bin/bash nohup etcd --name infra1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof & nohup etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof & nohup etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof &
通过命令向集群1中写入数据
export ETCDCTL_API=3 etcdctl put t1 v1 --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379 etcdctl put t2 v2 --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379 etcdctl put t3 v3 --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379 etcdctl put t4 v4 --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379
2. 启动集群2
#!/bin/bash nohup etcd --name infra4 --listen-client-urls http://127.0.0.1:12479 --advertise-client-urls http://127.0.0.1:12479 --listen-peer-urls http://127.0.0.1:12480 --initial-advertise-peer-urls http://127.0.0.1:12480 --initial-cluster-token etcd-cluster-2 --initial-cluster 'infra4=http://127.0.0.1:12480,infra5=http://127.0.0.1:22480,infra6=http://127.0.0.1:32480' --initial-cluster-state new --enable-pprof & nohup etcd --name infra5 --listen-client-urls http://127.0.0.1:22479 --advertise-client-urls http://127.0.0.1:22479 --listen-peer-urls http://127.0.0.1:22480 --initial-advertise-peer-urls http://127.0.0.1:22480 --initial-cluster-token etcd-cluster-2 --initial-cluster 'infra4=http://127.0.0.1:12480,infra5=http://127.0.0.1:22480,infra6=http://127.0.0.1:32480' --initial-cluster-state new --enable-pprof & nohup etcd --name infra6 --listen-client-urls http://127.0.0.1:32479 --advertise-client-urls http://127.0.0.1:32479 --listen-peer-urls http://127.0.0.1:32480 --initial-advertise-peer-urls http://127.0.0.1:32480 --initial-cluster-token etcd-cluster-2 --initial-cluster 'infra4=http://127.0.0.1:12480,infra5=http://127.0.0.1:22480,infra6=http://127.0.0.1:32480' --initial-cluster-state new --enable-pprof &
3. 使用 make-mirror同步数据
#export ETCDCTL_API=3;
#etcdctl make-mirror --no-dest-prefix=true 127.0.0.1:12479 --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379 --insecure-skip-tls-verify=true
4. 查看集群2的数据
export ETCDCTL_API=3;etcdctl get t1 t99 --endpoints=127.0.0.1:12479,127.0.0.1:22479,127.0.0.1:32479
通过以上命令发现集群1和集群2的数据已经完全同步了, etcdctl make-mirror 代码里面数据同步打印的周期是30s一次, 可以通过日志查看已经同步数据的数量。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现