Consul 简明教程

Consul - Installation

为了演示的目的,我们将在开发人员模式中使用 Consul 代理,使用 -dev 模式。只针对本地计算机设置,我们将执行单一系统 Consul 设置。 Please do not use this single node consul cluster in your production 。正如 Hashicorp 在单节点 Consul 群集的情况场景中提到的, the data loss is inevitable

For demonstration purposes, we are going to use consul agent in the developer mode using the -dev mode. Just for the local machine setup, we are going to do a single system consul setup. Please do not use this single node consul cluster in your production. As Hashicorp already mentions it in the case scenario of a single node consul cluster, the data loss is inevitable.

Installing Consul

可以通过以下位置的“下载”页面安装 Consul: www.consul.io/downloads.html

Consul can be installed via the Downloads page at www.consul.io/downloads.html

installing consul

你可以将二进制包解压到计算机上的“下载”部分。

You can extract the binary package in your Downloads section of your machine.

$ cd Downloads
$ chmod +x consul
$ sudo mv consul /usr/bin/

现在,让我们开始使用以下内容使用 Consul -dev flag

Now let us start using consul using the -dev flag.

$ consul agent -dev -data-dir=/tmp/consul

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

The output would be as shown in the following screenshot.

dev flag

现在,你可以使用以下命令检查你的 Consul 成员。

Now you can check your consul members using the following command.

$ consul members

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

The output would be as shown in the following screenshot.

memebers

如果你想将其他节点加入这个节点 −

If you want to join other nodes to this node −

$ consul join <Node 2> <Node 3>

或者,你可以在节点 2 和 3 上运行以下命令 −

Alternatively, you can run the following command on Node 2 & 3 −

$ consul join <Node 1>

Using the Command Line

Consul 的命令行包含几个不同的选项,其中一些最常用的选项如下 −

The command line of consul consists of several different options, some of the most commonly used ones are as follows −

  1. agent − which runs a Consul agent.

  2. configtest − to validate a config file.

  3. event − to start up a new event.

  4. exec − to execute a command on Consul nodes.

  5. force-leave − forcing a member of the cluster to leave the cluster.

  6. info − it provides us the debugging information for operators.

  7. join − to make a Consul agent join the cluster.

  8. keygen − to generate a new encryption key.

  9. keyring − to manage gossip layer encryption keys.

  10. *kv * − to interact with the key-value store.

  11. leave − to leave the Consul cluster and shut it down without force.

  12. lock − to execute a command to hold down a lock.

  13. maint − to control node or service maintenance mode.

  14. members − it lists the members of a Consul cluster.

  15. monitor − it streams logs from a Consul agent.

  16. operator − it provides us a cluster of tools for Consul operators.

  17. reload − it triggers the agent to reload configuration files.

  18. rtt − it estimates network round trip time between nodes.

  19. snapshot − it saves, restores and inspects snapshots of Consul server state.

  20. version − to print the current Consul version.

  21. watch − to Watch out for changes in the Consul.

Consul Template

consul-template 为我们提供了一个守护程序,它会查询 Consul 实例并在文件系统上更新任何指定数目的模板。consul-template 可以选择在更新过程完成后运行任意的命令。此选项帮助我们设置 Consul 集群,而无需手动执行所有操作。

The consul-template provides us a daemon that queries the Consul instance and updates any number of specified templates on the file system. The consul-template can optionally run arbitrary commands when the update process completes. This option helps us to setup the consul cluster without manually doing everything on our own.

consul 模板将在 /tmp/<name-of-file>.conf.tmpfl 处形成。该模板所用语言根据 Hashicorp Configuration Language (HCL)编写。

The consul template is to be formed at /tmp/<name-of-file>.conf.tmpfl. The language in which the template is written according to Hashicorp Configuration Language (HCL).

你可以在此 page 处下载 consul-template。

You can download the consul-template from this page.

template

使用以下命令进行尝试 −

Try it out by using the following command −

$ ./consul-template -h

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

The output would be as shown in the following screenshot.

template output

如果你希望将此二进制文件移动到更加突出的空间,以便用户在任何时候都可以使用,则可以输入以下命令 −

If you wish to move this binary to a more prominent space, so that it is available for the user every time. You can type in the following commands −

$ chmod +x consul-template
$ sudo mv consul-template /usr/share/bin/

用于演示,我们将使用 nginx 的示例配置作为服务。你可以在 https://github.com/hashicorp/consul-template/tree/master/examples 处尝试更多演示,或更好地写下你自己的模板。

For demo purposes, we are going to use a sample configuration of nginx to be used as our service. You can try out more demos at https://github.com/hashicorp/consul-template/tree/master/examples or better write down your own template.

$ vim /tmp/nginx.conf.ctmpl

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

The output would be as shown in the following screenshot.

nginx

配置文件可能如下所示 −

The config file may look like −

{{range services}} {{$name := .Name}} {{$service := service .Name}}

upstream {{$name}} {
   zone upstream-{{$name}} 64k;
   {{range $service}}server {{.Address}}:{{.Port}} max_fails = 3 fail_timeout = 60
   weight = 1;
   {{else}}server 127.0.0.1:65535; # force a 502{{end}}
} {{end}}

server {
   listen 80 default_server;
   location / {
      root /usr/share/nginx/html/;
      index index.html;
   }
   location /stub_status {
      stub_status;
   }
   {{range services}} {{$name := .Name}}
   location /{{$name}} {
      proxy_pass http://{{$name}};
   }
   {{end}}
}

现在,使用 consul 模板二进制文件,请运行以下命令 −

Now using the consul template binary file, please run the following commands −

$ consul-template \
 -template = "/tmp/nginx.conf.ctmpl:/etc/nginx/conf.d/default.conf"

通过前一个命令,进程已经启动。你可以稍后打开另一个终端,并使用以下命令查看已经完全呈现的 nginx.conf 文件。

With the previous command the process has started. You can later open up another terminal and view the nginx.conf file being completely rendered using the following command.

$ cat /etc/nginx/conf.d/default.conf

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

The output would be as shown in the following screenshot.

output