etcd跨机房部署方案

2022-12-09,,,

使用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=
etcdctl put t1 v1 --endpoints=127.0.0.1:,127.0.0.1:,127.0.0.1:
etcdctl put t2 v2 --endpoints=127.0.0.1:,127.0.0.1:,127.0.0.1:
etcdctl put t3 v3 --endpoints=127.0.0.1:,127.0.0.1:,127.0.0.1:
etcdctl put t4 v4 --endpoints=127.0.0.1:,127.0.0.1:,127.0.0.1:

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=;
#etcdctl make-mirror --no-dest-prefix=true 127.0.0.1: --endpoints=127.0.0.1:,127.0.0.1:,127.0.0.1: --insecure-skip-tls-verify=true

4. 查看集群2的数据  

export ETCDCTL_API=;etcdctl get t1 t99 --endpoints=127.0.0.1:,127.0.0.1:,127.0.0.1:

通过以上命令发现集群1和集群2的数据已经完全同步了, etcdctl make-mirror 代码里面数据同步打印的周期是30s一次, 可以通过日志查看已经同步数据的数量。

etcd跨机房部署方案的相关教程结束。

《etcd跨机房部署方案.doc》

下载本文的Word格式文档,以方便收藏与打印。