如何在 2 个数据中心配置 consul-replicate 以进行灾难恢复?

How to configure consul-replicate in 2 datacenters for disaster recovery?

我正在尝试为我的应用程序配置灾难恢复。我们有一个有状态的 Consul,这意味着将有对 consul kv 的写操作,这应该在数据中心之间保持一致。换句话说,如果我在 dc1 上进行写操作,而在 dc2 上进行读操作,我必须获取该键的最新值。 这是我的思考过程:我将通过 wan join 加入两个数据中心。请注意每个数据中心有 4 台服务器。并且 dc1 上的任何写操作都将通过 consul-replicate 工具复制到 dc2。我尝试了 ACL 复制,但它似乎很复杂。 我还在网上搜索了 consul-replicate 配置示例,但找不到任何有用的信息。有人可以指导我这样做吗? 提前致谢。

您可以使用 consul-replicate 使 KV 数据在 Consul 数据中心之间保持同步,但您需要牢记有关设置的几点注意事项。

Consul replicate 使用 blocking queries 来观察配置的 KV 前缀下的变化。今天阻塞查询的工作方式是,如果在你监视的前缀下更新了一个键,阻塞查询将 return 该前缀下 all 键的数据——即使只有更新了一个密钥——导致 Consul 将远程 DC 中的所有监视密钥复制到 PUT/update。根据您要复制的密钥数量以及更新这些密钥的频率,您可能会看到更高的带宽利用率并遇到性能问题。

为了减轻性能问题,您可以 运行 多个 consul-replicate 进程,每个进程负责从 KV 树(例如,/env/prod/env/dev) 而不是整棵树。 https://github.com/hashicorp/consul/issues/2791 是一项改进 watch 行为的功能请求,因此它只有 returns 更改键的数据,而不是所有被监视的键​​。

此外,Consul 复制在 Consul 集群中不提供与 Raft 相同的数据保证。我的意思是,由于复制是在 Consul 外部进行的,当 user/service 写入 DC1 中的 KV 时,如果该 KV 未成功复制到 DC2,Consul 无法 return 出错. Consul 完全不知道数据正在被复制。 consul-replicate 执行的复制是异步的,基本上是尽力而为。

就设置这一切而言,README for Consul replicate 相当详细,并提供了对每个可用配置选项的解释。我想象从 DC1 复制的 DC2 中的 consul-replicate 守护进程 运行ning 的最小配置可能看起来像这样。

# This denotes the start of the configuration section for Consul. All values
# contained in this section pertain to Consul.
consul {
  address = "127.0.0.1:8500"
  token = "<token>"
}

# This is the path to store a PID file which will contain the process ID of the
# Consul Replicate process. This is useful if you plan to send custom signals
# to the process.
pid_file = "/var/run/consul-replicate/pid"

# This is the prefix and datacenter to replicate and the resulting destination.
prefix {
  source      = "env/prod"
  datacenter  = "dc1"
  destination = "env/prod"
}