Consul 简明教程

Consul - Bootstrapping & DNS

在本章中,我们将讨论如何在 Consul 中使用以下组件。

In this chapter, we will discuss how the following components are used in Consul.

  1. Automatic bootstrapping

  2. Manual bootstrapping

  3. Using DNS Forwarding

  4. DNS Caching

让我们现在详细讨论每一个这些。

Let us now discuss each of these in detail.

Automatic Bootstrapping

引导是 Consul 的核心功能之一。当您第一次安装 Consul 时,它会自动配置为检测、识别和加入它碰到的节点。在其集群形成期间,自动引导是 Consul 的内置功能。要获得更多有关 Consul 的信息,最好的方法是使用以下命令:

Bootstrapping is one of the core features of Consul. When you install consul for the first time, it is automatically configured to detect, identify and join nodes it comes across. During its cluster formation, automatic bootstrapping is a built-in feature of Consul. To gain more information on consul, the best way is to use the command below −

$ sudo consul info

输出将如以下屏幕截图所示。

The output would be as shown in the following screenshot.

automatic bootstrapping

此命令将显示 Consul 在 real working scenarios 中的实际工作原理。它将显示 Consul 中正在工作的 Raft 算法。可以使用以下命令显示自动引导命令:

This command will show the actual working of consul in real working scenarios. It will display the Raft Algorithm working in Consul. The automatic bootstrapping command can be shown using the following command −

$ consul agent -server -data-dir = ”/tmp/consul” -bootstrap-expect 3

Automatic bootstrapping cannot be done in -dev mode.

Automatic bootstrapping cannot be done in -dev mode.

此选项通知 Consul 预期的服务器节点数,并在服务器可用时自动引导。

This option informs Consul of the expected number of server nodes and automatically bootstraps when servers are available.

Manual Bootstrapping

手动引导是 Consul 的一项古老而有用的功能。实际上,在 Consul 的早期版本中,在首次安装和使用 consul 时必须手动进行引导。后来,人们意识到无法在不同的时间执行此命令行操作。因此,引入了自动引导。您始终可以使用以下命令手动进行引导。

Manual Bootstrapping is an old and useful feature of Consul. Actually, during Consul’s earlier version, bootstrapping has to be done manually when installing and using consul for the first time. Later, it was realized that it was not possible to perform this command line operation at different times. Hence, automatic bootstrapping was introduced. You can always use bootstrapping manually by using the following commands.

In this case, we will assume that a 3-node consul cluster is to be built.

In this case, we will assume that a 3-node consul cluster is to be built.

有两种手动引导选项:

There are two options to do manual bootstrapping

  1. Running commands over 2 nodes: On Node B and Node C you can do the following −

$ consul join <Node A Address>
  1. Running command over 1 node −

$ consul join <Node B Address> <Node C Address>

Using DNS Forwarding

DNS 从 port 53 提供。DNS 转发可以使用 BIND, dnsmasqiptables 完成。默认情况下,Consul 代理运行在端口 8600 上监听的 DNS 服务器。通过向 Consul 代理的 DNS 服务器提交 DNS 请求,您可以获取您感兴趣的服务正在运行的节点的 IP 地址。

DNS is served from port 53. The DNS forwarding can be done using BIND, dnsmasq and iptables. By default, the Consul agent runs a DNS server listening on port 8600. By submitting DNS requests to the Consul agent’s DNS server, you can get the IP address of a node running the service in which you are interested.

Consul DNS 接口通过 SRV records 使服务可用的端口信息。在您的代码中手动添加逻辑的情况下,通常仅限于您正在查询的服务的 IP 地址信息(即记录)。

The Consul DNS interface makes the port information for a service available via the SRV records. Without manually adding logic in your code, you are generally limited just to the IP address info (i.e. a record) of the service you are querying.

最佳选择是让每个本地运行 Consul 代理的多个 BIND 服务器。BIND 服务器接收的任何查询都将转发到其本地 Consul 代理 DNS 服务器。

The best option is to have multiple BIND servers each running a Consul agent locally. Any queries received by a BIND server would be forwarded to its local Consul Agent DNS Server.

Using Bind

我们可以使用 Bind 函数使用 DNS 转发。它可以通过使用以下命令完成。

We can use DNS Forwarding using the Bind function. It can be done by using the following command.

$ sudo apt-get install bind9 bind9utils bind9-doc

输出将如以下屏幕截图所示。

The output would be as shown in the following screenshot.

bind function

让我们使用以下命令编辑 /etc/bind/named.conf 文件。

Let us edit the /etc/bind/named.conf file with the following command.

$ sudo vim /etc/bind/named.conf

在该文件中,请在代码的最后一行下方添加以下行。

In the file, please add the following lines below the last line of the code.

options {
   directory "/var/cache/bind";
   recursion yes;
   allow-query { localhost; };

   forwarders {
      8.8.8.8;
      8.8.4.4;
   };
   dnssec-enable no;
   dnssec-validation no;
   auth-nxdomain no; # conform to RFC1035
   listen-on-v6 { any; };
};
include "/etc/bind/consul.conf";

输出将如以下屏幕截图所示。

The output would be as shown in the following screenshot.

bind command

您可以使用以下 Bind 命令配置 Consul。

You can take the following Bind command to configure Consul.

$ sudo vim /etc/bind/consul.conf

创建文件时添加以下行:

Add the following lines when you create the file −

zone "consul" IN {
   type forward;
   forward only;
   forwarders { 127.0.0.1 port 8600; };
};

现在,您可以使用以下命令启动 consul 代理。(请记得同时重新启动 bind9 服务。)

Now you can start running your consul agent by using the following command. (Remember to restart the bind9 service as well.)

$ sudo service bind9 restart
$ consul agent -server -bootstrap-expect 1 -data-dir = /tmp/consul -configdir = [Path]

需要将系统配置为将查询发送到本地 Consul 代理的 DNS 服务器。可以通过更新系统上的 resolv.conf 文件指向 127.0.0.1 来完成此操作。在大多数情况下,需要将 Consul 配置为在端口 53 上运行。

The system needs to be configured to send queries to the local Consul agent’s DNS server. This is done by updating the resolv.conf file on the system to point to 127.0.0.1. In most cases, Consul will need to be configured to run on port 53.

您可以将以下信息添加到 /etc/resolv.conf:

You can add the following information to the /etc/resolv.conf:

nameserver 127.0.0.1

DNS Caching

Consul 以“0 TTL”(生存时间)值提供所有 DNS 结果。这将阻止任何缓存。但是,由于 TTL 值,可以将其设置为允许将 DNS 结果缓存在 Consul 的下游。较高的 TTL 值减少了 Consul 服务器上的查找次数并加快了客户端的查找速度,而代价是结果逐渐过时。

Consul serves all DNS results with a ‘0 TTL’ (Time to Live) value. This prevents any caching. However, due to the TTL values, it can be set to allow DNS results to be cached with downstream of Consul. Higher TTL values reduce the number of lookups on the Consul servers and speed lookups for clients, at the cost of increasingly stale results.

为此,我们准备使用以下方法使用 DNS 缓存:

For this purpose, we are going to use DNS caching using the method below −

$ sudo apt-get install dnsmasq

输出将如以下屏幕截图所示。

The output would be as shown in the following screenshot.

dnsmasq

现在,我们可以进行一项非常简单的配置:

Now, we can do a very simple configuration −

$ echo "server = /consul/127.0.0.1#8600" > /etc/dnsmasq.d/10-consul

我们这里所做的只是指定要由 127.0.0.1 上端口 8600 的 DNS 服务器处理的 Consul 服务的 DNS 请求。除非您更改 consul 的默认设置,否则这应该会奏效。

All we are doing here is specifying that DNS requests for consul services, which are to be dealt with by the DNS server at 127.0.0.1 on port 8600. Unless you change the consul defaults, this should work.

在正常情况下,应该使用以下命令。

In normal cases, the following command should be used.

$ dig @127.0.0.1 -p 8600 web.service.consul

在使用 Dnsmasq 时,您应该使用以下命令。

With Dnsmasq, you should use the following command.

$ dig web.service.consul

输出将如以下屏幕截图所示。

The output would be as shown in the following screenshot.

web service