Consul 简明教程

Consul - Querying Nodes

在本章中,我们将学习如何使用以下功能对节点进行查询 −

In this chapter, we will learn how to query nodes with the following functions −

  1. Using dig

  2. Using the Monitor command

  3. Using the Watch command

  4. By Registering External Services

让我们详细了解每个功能。

Let us understand each of these functions in detail.

Using Dig

Consul 在 consul 的 127.0.0.1:8600 端口上侦听 DNS 查询。它确定哪些节点可用于提供服务的方式是使用可以是以下内容的检查 −

Consul listens on 127.0.0.1:8600 for DNS queries in the consul. The way it determines which nodes are available to provide a service is using checks that can be either −

  1. A Script that is executed and it returns a nagios compliant code.

  2. An HTTP check that returns an HTTP response code.

  3. A TCP check that is checking if a port is open.

尝试 dig 的一般命令是 −

The general command for trying out dig is −

$ dig @127.0.0.1 -p <port> <service-name>.consul

现在,让我们尝试一个样本 dig 命令 −

Now, let us try out a sample dig command −

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

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

The output would be as shown in the following screenshot.

dig

Using the Monitor Command

用于连接正在运行的 Consul agent 并显示其日志。此命令将显示最近的日志。它还允许你在较高的日志级别记录 agent。它包含你可以关注的各种日志级别,例如 - Trace、Debug、Info、Warn 和 Err。

It is used to connect and show the logs of a running Consul agent. This command will show the recent logs. It also allows you to log the agent at a relatively high log level. It consists of various log levels, which you can follow, such as – Trace, Debug, Info, Warn and Err.

让我们尝试以下命令 −

Let us try out the following command −

$ consul monitor

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

The output would be as shown in the following screenshot.

monitor command

你还可以使用 -log-level 和 -rpc-address 等子命令设置监视器命令。默认情况下,RPC 的地址为 127.0.0.1:8400。有关更多信息,请点击 here

You can also set the monitor command using the sub-commands such as -log-level and -rpc-address. By default, the address of the RPC is 127.0.0.1:8400. For more info, click here.

Using the Watch Command

此命令为我们提供了监视节点列表、服务成员、键值等的变化的机制。它还用视图的最新值调用一个进程。如果未指定进程,当前值将处理为 STDOUT (一种检查 Consul 中数据的有用方法)。Consul Watch 命令帮助有各种不同的选项,如下面的屏幕快照所示 −

This command provides us with a mechanism to watch for changes in the list of nodes, service members, key value, etc. It also invokes a process with the latest values of the view. If no process is specified, the current values are processed to STDOUT, which can be a useful way to inspect data in Consul. The Consul Watch Command help has a variety of different options as shown in the following screenshot −

watch command

我们尝试使用 -type = service 进行演示,如下面的命令所示。

Let us try out a demo with -type = service as shown in the following command.

$ consul watch -type = service -service = consul
watch type

有关此主题的更多信息,你可以点击 here

For more information on this topic, you can click here.

By Registering External Services

注册后,DNS 接口将能够为服务返回适当的“A 记录”或 CNAME 记录。让我们注册一个外部服务,例如亚马逊,如下面的代码块和屏幕快照所示。

Once registered, the DNS interface will be able to return the appropriate ‘A Records’ or CNAME Records for the service. Let us register an external service, such as Amazon as shown in the following code block and the screenshot as well.

$ sudo curl -X PUT -d '{"Datacenter": "dc1", "Node": "amazon",
"Address": "www.amazon.com",
"Service": {"Service": "shop", "Port": 80}}'
http://127.0.0.1:8500/v1/catalog/register
registering external services

上面的命令指定了一个称为 shop 的服务。这个节点称为 amazon,其 URL 可在 www.amazon.com 上的端口 80 获得。让我们检查 consul 上的输出以确保我们正确安装了此服务。为此,请在 localhost:8500 打开浏览器窗口。

The above command specifies a service called as shop. This Node is called as amazon with its URL available at www.amazon.com on Port 80. Let us check the output on consul to make sure we have correctly installed this service. For this, please open the browser window at localhost:8500.

registering external services1

若要删除服务,我们可以简单使用以下命令。

To remove the service, we can simply use the following command.

$ curl -X PUT -d '{"Datacenter": "dc1", "Node": "amazon"}'
http://127.0.0.1:8500/v1/catalog/deregister
remove service

让我们检查它的 UI,如下面的屏幕快照所示。

Let us check the UI for it as shown in the following screenshot.

remove service1