Consul 入门-运行
HashiCorp
Consul 是由 HashiCorp 公司开发的,它是一家专注于 DevOps 工具链的公司,旗下的明星级产品包括 Vagrant、Terraform、Vault、Nomad 以及 Consul,这些工具贯穿了持续交付的整个流程。
下载
在Consul 官网可以知道不同系统对应的下载方式
macOS
$ brew tap hashicorp/tap
$ brew install hashicorp/tap/consu
Windows
提供可二进制可执行程序的压缩包,分32位和64位
Linux系
提供相应包管理器的下载方式
- Ubuntu/Debian
$ curl -fsSL //apt.releases.hashicorp.com/gpg | sudo apt-key add -
$ sudo apt-add-repository "deb [arch=amd64] //apt.releases.hashicorp.com $(lsb_release -cs) main"
$ sudo apt-get update && sudo apt-get install consul
- CentOS/RHEL
$ sudo yum install -y yum-utils
$ sudo yum-config-manager --add-repo //rpm.releases.hashicorp.com/RHEL/hashicorp.repo
$ sudo yum -y install consul
- Fedora
$ sudo dnf install -y dnf-plugins-core
$ sudo dnf config-manager --add-repo //rpm.releases.hashicorp.com/fedora/hashicorp.repo
$ sudo dnf -y install consul
- Amazon Linux
$ sudo yum install -y yum-utils
$ sudo yum-config-manager --add-repo //rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
$ sudo yum -y install consul
- Homebrew
$ brew tap hashicorp/tap
$ brew install hashicorp/tap/consul
也提供对应操作架构的二进制下载链接
另外,Consul 是通过 Golang 编写的开源软件,可以从 Github 上找到源码自行编译。
$ git clone //github.com/hashicorp/consul.git -depth=1
$ cd consul
$ make tools
$ make dev
运行 Server&Client
回顾上一篇《Consul 入门-初识》,我们知道 Consul 是 C/S 架构以及各端能力
运行 Server
$ consul agent --server=true --ui=true --data-dir=s-data --node=server1 --dev
==> Starting Consul agent...
Version: '1.9.4'
Node ID: '34d5bfeb-3ed4-6aa1-f68d-d30950450fc9'
Node name: 'server1'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
==> Log data will now stream in as it occurs:
...
可以从输出信息里面看到 Server 节点的一些关键信息以及竞选 Leader 成功(毕竟只有它一个人)
运行 Client
$ consul agent --data-dir=c-data --join=127.0.0.1:8301 --serf-lan-port=8303 --serf-wan-port=8305 --dns-port=8601 --server-port=8304 --http-port=8503 --server=false --enable-script-checks --node=client1 --config-dir=config
==> Starting Consul agent...
Version: '1.9.4'
Node ID: '2b05ee18-1b09-3b0b-7431-ef79783d9758'
Node name: 'client1'
Datacenter: 'dc1' (Segment: '')
Server: false (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8503, HTTPS: -1, gRPC: -1, DNS: 8601)
Cluster Addr: 10.8.124.122 (LAN: 8303, WAN: 8305)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
...
程序同样会显示 Client 的一些相信息,由于Server 和 Client 是运行在同一台机器上,默认端口与 Server 的冲突了,所以通过参数--serf-lan-port=8303 --serf-wan-port=8305 --dns-port=8601 --server-port=8304 --http-port=8503
进行了指定。
此时我们观察 Server 端的程序输出内容,可以看到 Client1 与 Server 的连接信息:
2021-08-29T16:47:10.385+0800 [DEBUG] agent.router.manager: Rebalanced servers, new active server: number_of_servers=1 active_server="server1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)"
2021-08-29T16:47:10.385+0800 [DEBUG] agent.router.manager: Rebalanced servers, new active server: number_of_servers=1 active_server="server1.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)"
2021-08-29T16:48:04.265+0800 [DEBUG] agent: Skipping remote check since it is managed automatically: check=serfHealth
2021-08-29T16:48:04.265+0800 [DEBUG] agent: Node info in sync
2021-08-29T16:49:18.849+0800 [DEBUG] agent: Skipping remote check since it is managed automatically: check=serfHealth
2021-08-29T16:49:18.849+0800 [DEBUG] agent: Node info in sync
2021-08-29T16:49:18.993+0800 [DEBUG] agent.server.memberlist.lan: memberlist: Stream connection from=127.0.0.1:54711
2021-08-29T16:49:18.993+0800 [INFO] agent.server.serf.lan: serf: EventMemberJoin: client1 10.8.124.122
2021-08-29T16:49:18.993+0800 [INFO] agent.server: member joined, marking health alive: member=client1
2021-08-29T16:49:19.176+0800 [DEBUG] agent.server.serf.lan: serf: messageJoinType: client1
2021-08-29T16:49:19.192+0800 [DEBUG] agent.server.serf.lan: serf: messageJoinType: client1
2021-08-29T16:49:19.294+0800 [DEBUG] agent.server.serf.lan: serf: messageJoinType: client1
2021-08-29T16:49:19.393+0800 [DEBUG] agent.server.serf.lan: serf: messageJoinType: client1
通过 Consul 的命令可以查看现在的成员情况:
$ consul members
Node Address Status Type Build Protocol DC Segment
server1 127.0.0.1:8301 alive server 1.9.4 2 dc1 <all>
client1 10.8.124.122:8303 alive client 1.9.4 2 dc1 <default>
注册一个服务
我们先用Ctrl-C
暂停 Client 端代码,在配置文件夹config
中增加文件redis.json
{
"service": [
{
"name": "redis",
"tags": ["master"],
"address": "127.0.0.1",
"port": 6379,
}
]
}
然后重新运行,可以看到服 redis 服务已经被注册
2021-08-29T17:03:04.556+0800 [INFO] agent: Synced node info
2021-08-29T17:03:04.556+0800 [INFO] agent: Synced service: service=redis
来验证一下是否可以通过 Consul 的dns服务找到
$ dig @127.0.0.1 -p 8601 redis.sevice.consul
; <<>> DiG 9.10.6 <<>> @127.0.0.1 -p 8601 redis.sevice.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 39225
;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;redis.sevice.consul. IN A
;; AUTHORITY SECTION:
consul. 0 IN SOA ns.consul. hostmaster.consul. 1630227874 3600 600 86400 0
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8601(127.0.0.1)
;; WHEN: Sun Aug 29 17:04:34 CST 2021
;; MSG SIZE rcvd: 98
除了通过 DNS 服务获取,还可以通过 HTTP 服务访问//localhost:8503/v1/health/service/redis
总结
本篇讲述了 Consul 背后的公司信息、Consul 的安装方式以及 Consul 的运行方式,最后通过注册 redis 服务的例子,体验了一把简单的服务注册和发现的流程。