📜  领事-故障转移事件

📅  最后修改于: 2020-11-01 03:08:09             🧑  作者: Mango


在本章中,我们将学习有关Consul中的故障转移事件的信息。这将在以下功能的帮助下完成-

  • 单集群故障
  • 吉普车测试
  • 多集群故障
  • 拍摄快照

让我们详细了解每个。

单集群故障

在单个群集故障中,放置在一个数据中心中的群集开始发生故障。在每种情况下,重要的是要确保在发生故障转移的情况下,系统不仅可以防止故障转移,而且还具有可以依靠的备份。为了防止Consul故障转移事件,我们将使用称为Consul-alerts的东西。主要项目可在以下位置找到: https://github.com/AcalephStorage/consul-alerts

Consul-alerts是一个高度可用的守护程序,用于基于Consul Health检查发送通知和提醒。该项目在localhost:9000上运行守护程序和API,并使用默认数据中心(dc1)连接到本地领事代理(localhost:8500)。

有两种方法可以开始该项目。第一种方法是通过GO进行安装。对于已安装和配置GO的用户,他们可以按照以下步骤进行操作-

$ go get github.com/AcalephStorage/consul-alerts
$ go install
$ consul-alerts start

使用last命令可以轻松地覆盖consul-alert,datacenter选项,consul-acl令牌等的默认端口。该命令也可以编写如下:

$ consul-alerts start --alert-addr = localhost:9000 --consul-addr = localhost:8500
--consul-dc = dc1 --consul-acl-token = ""

第二种方法涉及用户使用Docker。两种方法在不同情况下都同样有用。为了在Docker上使用Consul-alerts,让我们使用以下命令从Docker Hub中拉取映像。

$ docker pull acaleph/consul-alerts

领事警报

进入Docker方法,我们可以考虑以下三个选项-

  • 使用容器本身内置的Consul Agent。
  • 使用在另一个Docker容器上运行的Consul代理。
  • 使用Consul-alerts通过远程Consul实例进行链接。

现在让我们详细讨论这两个方面。

使用容器本身内置的Consul Agent

让我们使用以下命令启动领事代理-

$ docker run -ti \
   --rm -p 9000:9000 \
   --hostname consul-alerts \
   --name consul-alerts \  
   --entrypoint = /bin/consul \
   acaleph/consul-alerts \
   agent -data-dir /data -server -bootstrap -client = 0.0.0.0

在这里,我们覆盖了–entrypoint标志所提到的Consul的入口。伴随着它,我们通过提及使用-p标志使用的端口,使用标志-data-dir标记数据目录/ data和使用0.0.0.0客户机来引导客户机。

入口点

在新的终端窗口上,让我们启动consul-alerts选项。

$ docker exec -ti consul-alerts /bin/consul-alerts start --alertaddr = 0.0.0.0:9000
--log-level = info --watch-events --watch-checks

在这里,在上述步骤中,我们正在执行领事警报,以交互方式启动。警报地址端口被标记为9000。监视功能将与领事检查一起检查领事代理是否已启用。

警报选项

我们可以清楚地看到,领事警报已轻松启动,并且已注册了新的运行状况检查,并添加了领事代理。数据中心取为dc1,可以根据用户进行更改。

使用在另一个Docker容器上运行的Consul Agent

在这里,您可以使用任何类型的领事映像在Docker容器上运行。使用consul-alerts图像,我们可以轻松地将consul容器与consul-alerts容器链接。这是使用–link标志完成的。

–使用以下命令之前,请确保领事容器已在另一个终端上运行。

$ docker run -ti \
   -p 9000:9000 \
   --hostname consul-alerts \
   --name consul-alerts \
   --link consul:consul \
   acaleph/consul-alerts start \
   --consul-addr=consul:8500 \
   --log-level = info --watch-events --watch-checks

使用领事警报通过远程领事实例链接

在这里,我们应该使用以下命令来使用Consul-alerts通过远程consul实例进行链接。

$ docker run -ti \
   -p 9000:9000 \
   --hostname consul-alerts \
   --name consul-alerts \
   acaleph/consul-alerts start \
   --consul-addr = remote-consul-server.domain.tdl:8500 \
   --log-level = info --watch-events --watch-checks

吉普车测试

Jespen是一种用于测试任何系统中的部分公差和联网的工具。它通过在系统上创建一些随机操作来测试系统。吉普森用Clojure写成。不幸的是,对于演示而言,Jepsen测试需要与数据库系统一起形成大量集群,因此不在本文讨论范围之内。

Jepsen通过在五台不同的主机上设置被测数据存储来工作。它为被测数据存储创建一个客户端,指向五个节点中的每个节点发送请求。它还创建了一个特殊的客户端系列,称为“ Nemesis”,它在集群中造成了严重破坏,例如,使用iptables切断了节点之间的链接。然后,它继续对不同的节点同时发出请求,同时交替分区和修复网络。

在测试运行结束时,它将修复群集,等待群集恢复,然后验证系统的中间状态和最终状态是否符合预期。一些摘录摘自这里

有关Jepsen测试的更多信息,请在此处检查。

多集群故障

在多群集故障转移事件期间,部署在多个数据中心中的群集无法支持客户支持的服务。 Consul使我们能够确保在其中一种情况发生时,Consul具有可帮助您在这种情况下启用服务的功能。

为此,我们将研究一个项目,该项目可帮助我们将Consul从一个群集复制到多个群集。该项目为我们提供了一种使用consul复制守护进程跨多个Consul数据中心复制K / V对的方法。您可以在− https://github.com/hashicorp/consul-replicate上查看此Hashicorp项目。尝试此项目的一些先决条件包括-

  • 高朗
  • 码头工人
  • 领事
  • 吉特

让我们开始使用以下命令-

-在运行以下命令之前,请确保已在计算机上正确安装并配置了Git。

$ git clonehttps://github.com/hashicorp/consul-replicate.git

输出将如以下屏幕快照所示。

Git克隆

$ cd consul-replicate
$ make

输出将如以下屏幕快照所示。

复制

如果您在构建二进制文件时遇到麻烦,也可以尝试使用以下命令手动提取Docker映像-

$ docker pull library/golang:1.7.4

上面提到的命令将创建bin / consul-replicate,可以将其作为二进制文件调用。下表显示了它涵盖的子命令的完整列表-

Option Description
auth The basic authentication username (and optional password), separated by a colon. There is no default value.
consul* The location of the consul instance to query (may be an IP address or FQDN) with port.
max-stale The maximum staleness of a query. If specified, Consule will distribute work among all servers instead of just the leader. The default value is 0 (none).
ssl Use HTTPS while talking to Consul. Requires the consule server to be configured to server secure connections. The default value is false.
ssl-verify Verify certificates when connecting via SSL. This requires the use of -ssl. The default value is true.
syslog Send log output to syslog (in addition to stdout and stderr). The default value is false
syslog-facility The facility to use when sending to syslog. This requires the use of -syslog. The default is LOCAL
token The Consul API token. There is no default value.
prefix* The source prefix including the, with options destination prefix, separated by a colon(:). This option is additive and may be specified multiple times for multiple prefixes to replicate.
exclude A prefix to exclude during replication. This option is additive and may be specified multiple times for multiple prefixes to exclude.
wait The minium(:maximum) to wait for stability before replicating, separated by a colon(:). If the optional maximum value is omitted, it is assumed to be 4x the required minimum value. There is no default value.
retry The amount to time to wait if Consule returns an error when communicating with the API. The default value is 5 seconds.
config The path to a configuration file or directory of configuration files on disk, relative to the current working directory. Values specified on the CLI take precedence over values specified in the configuration file. There is no default value.
log-level The log level for output. This applies to the stdout/stderr logging as well as syslog logging (if eneabled). Valid values are “debug”, “info”, “warn, and “err”. The default value is “warn”.
once Run Consule Replicate once and exit (as opposed to the default behavior of daemon). (CLI-only)
version Output version information and quit. (CLI-only)

拍摄快照

对于备份情况下的快照,快照是管理Consul群集的重要组成部分。默认情况下,Consul提供了一种保存Consul群集快照的方法。 Consul为我们提供了四个单独的子命令,使用它们可以使用consul创建快照,它们是-

  • 领事快照保存
  • 领事快照代理
  • 领事快照检查
  • 领事快照还原

让我们详细了解每个。

领事快照保存

设置此命令以获取Consul服务器状态的原子时间点快照,其中包括键/值条目,服务目录,准备好的查询,会话和ACL。快照将保存为上述文件名。

$ consul snapshot save .snap

输出将如以下屏幕快照所示。

快照保存

要检查当前目录中文件的存在,请通过在当前目录中运行文件进行检查。如果是非领导节点,请执行以下命令-

$ consul snapshot save -stale .snap

领事快照代理

此子命令启动一个过程,该过程将获取Consul服务器状态的快照并将其保存在本地,或将其推送到可选的远程存储服务。

快照代理

领事快照检查

它用于检查Consul服务器状态的时间点快照,其中包括键/值条目,服务目录,准备好的查询,会话和ACL。该命令可以执行如下-

–请记住,以下命令只能在保存快照的目录中运行。

$ consul snapshot save .snap

输出将如以下屏幕快照所示。

快照检查

领事快照还原

snapshot restore命令用于恢复Consul服务器状态的时间点快照,其中包括键/值条目,服务目录,准备好的查询,会话和ACL。从保存的备份文件中读取快照。

–请记住,以下命令只能在保存快照的目录中运行。

$ consul snapshot restore .snap

输出将如以下屏幕快照所示。

快照还原

如果您正在使用AWS使用Consul,则该项目可能会帮助您节省一些时间-https://github.com/pshima/consul-snapshot