Consul 简明教程
Consul - Working with Microservices
在本章中,我们将了解微服务如何使用 Consul。我们还将学习以下组件如何影响 Consul。
In this chapter, we will understand how Microservices work with Consul. We will also learn how the following components affect Consul.
-
Using docker
-
Building Registrator for Service Discovery
-
Using rkt and Nomad
让我们现在详细讨论每一个这些。
Let us now discuss each of these in detail.
Using Docker
在开始之前, please do not use this setup in production ,因为它仅用于演示目的。Docker 是一种基于容器的服务,我们可以使用它轻松部署我们的应用程序。对于使用 Consul,我们将在下面链接的映像中使用 −0
Before starting, please do not use this setup in production as it is used for demo purposes only. Docker is a container based service using which we can easily deploy our applications. For using Consul, we are going to use the image at the following link –0
我们假定你的系统已经安装并正确配置了 Docker。让我们尝试从 Docker 中心提取映像,方法是运行以下命令 −
It is being assumed that your system has Docker installed and properly configured. Let us try pulling down the image from the Docker hub, by running the following command −
$ docker pull progrium/consul
输出将如以下屏幕截图所示。
The output would be as shown in the following screenshot.

我们将以以下方式发布一些与其端口的接口(使用 Docker 上的 -p 选项)。
We are going to publish some interfaces with their ports (using -p option on Docker) in the following manner.
-
8400 (RPC)
-
8500 (HTTP)
-
8600 (DNS)
同样,根据拉取的结果,我们准备将主机名设置为 node1 。你可以使用 -h flag 随你自己的主机名将其更改为你想要的任何内容,如下所示。
Also as per the pull made, we are going to set the name of the hostname as node1.You can change it to anything you want by using the -h flag with some hostname of your own as shown below.
$ docker run -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h node1 progrium/consul
-server -bootstrap
输出将如以下屏幕截图所示。
The output would be as shown in the following screenshot.

你还可以使用 − 为 Consul 启用 UI 模式
You can also enable the UI mode for the Consul using −
$ docker run -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h node1 progrium/consul
-server -bootstrap -ui-dir /ui
你可以在 http://localhost:8500 上查看基于 UI 的输出。以下屏幕快照让你更好地了解基于 UI 的输出。
You can check the UI based output on http://localhost:8500. The following screenshot gives you a better idea regarding the UI based output.

若要通过不同节点上的各种 docker 容器使用 consul,我们可以在不同的节点上运行以下命令 −
For using consul over various docker containers on different nodes, we can run the following commands on different nodes −
On Node1
$ docker run -d --name node1 -h node1 progrium/consul -server -bootstrap-expect 3
其中, -bootstrap-expect 3 表示在自引导并成为一个工作集群之前,这个 consul 服务器将等待连接 3 个对等方。
Where, -bootstrap-expect 3 means that the consul server will wait until there are 3 peers connected before self-bootstrapping and becoming a working cluster.
在继续之前,我们需要通过检查容器来获取容器的内部 IP。为了我们的使用目的,我们将声明 $ JOIN_IP 。
Before going any further, we need to get the container’s internal IP by inspecting the container. For our use, case purpose, we are going to declare the $ JOIN_IP.
$ JOIN_IP = "$(docker inspect -f '{{.NetworkSettings.IPAddress}}' node1)"
Building Registrator for Service Discovery
Registrator 会通过检查容器自动为任何 Docker 容器注册和注销服务,因为容器会在线。我们即将使用的 Registrator 当前支持可插拔的服务注册表,其中目前包括 Consul, Etcd 和 SkyDNS2 。当我们在网络上与不同的服务交互时,强烈建议使用 Registrator。
Registrator automatically registers and deregisters services for any Docker container by inspecting containers as they come online. The Registrator we are about to use currently supports pluggable service registries, which currently includes Consul, Etcd and SkyDNS2. The usage of Registrator is highly recommended when we are interacting with different services over the network.
$ docker pull gliderlabs/registrator:latest
输出将如以下屏幕截图所示。
The output would be as shown in the following screenshot.

$ docker run -d \
--name = registrator \
--net = host \
--volume = /var/run/docker.sock:/tmp/docker.sock \
gliderlabs/registrator:latest \
consul://localhost:8500
输出将如以下屏幕截图所示。
The output would be as shown in the following screenshot.

你收到的输出是你刚启动的 Docker 容器的 ID。你可以使用命令检查容器是否正在运行:
The output you have received is the ID of the Docker Container that you have just started. You can check whether the container is running or not by using the command −
$ docker ps -a
输出将如以下屏幕截图所示。
he output would be as shown in the following screenshot.

你还可以使用以下命令查看 Registrator 的日志:
You can also view the logs of Registrator by using the following command.
$ docker logs registrator
Using rkt and Nomad
rkt 是另一项基于容器的服务,你可以在你的环境中使用它。它由 CoreOS 构建。构建 rkt 的主要原因是提高安全性,而安全性是 Docker 在 2013-14 年仍处于开发时面临的危机问题之一。
The rkt is another container-based service, which you can use in your environment. It is built by CoreOS. The main reason for building rkt was to improve the security that was one of the crisis issues for Docker back when it was still in development in 2013-14.
对于 Consul,我们可以使用 Rkt Registrator 来处理与 Consul 的服务发现。涵盖 rkt 的此特定 Registrator 项目正在开发中,并且是 not recommended for production level use 。
As for Consul, we can use the Rkt Registrator for working on service discovery with Consul. This particular Registrator project, which is covered for rkt is under development and is not recommended for production level use.
你可以通过转到其路径并运行以下命令来检查 rkt 是否已安装:
You can check if rkt is installed or not, by going to its path and running the following command.
$ ./rkt
你可以检查输出以验证是否正确安装了它,如以下屏幕截图所示。
You can check the output to check, if it is correctly installed or not as shown in the following screenshot.

如需试用 rkt 和 Consul,请查看 https://github.com/r3boot/rkt-registrator 。
For trying out rkt and Consul please check out − https://github.com/r3boot/rkt-registrator.
Nomad Tool
最常用和最受欢迎的选择之一是 Nomad 工具。Nomad 是一个用于管理机器集群并在其上运行应用程序的工具。它类似于 Mesos 或 Kubernetes 。默认情况下,Nomad 本身涵盖了 Docker 和 rkt 驱动程序。所以,如果你正在寻找使用 Consul 大规模部署容器。Nomad 可能是很好的解决方案。查看 https://www.nomadproject.io/docs/drivers/rkt.html 了解更多有关 Nomad 的信息。
One of the most commonly used and a favorite option is the Nomad tool. Nomad is a tool for managing a cluster of machines and running applications on them. It is similar to Mesos or Kubernetes. By default, Nomad covers the Docker and rkt driver within itself. So, if you are looking for a large-scale deployment of containers with Consul. Nomad might be a good solution to it. Check out − https://www.nomadproject.io/docs/drivers/rkt.html for further information on Nomad.