Consul - 服务注册和健康检查

服务注册

定义一个服务

  1. 为Consul配置创建一个目录/etc/consul.d。Consul装载配置目录中所有的配置文件。[root@Automation bin]# sudo mkdir /etc/consul.d
  2. 我们将创建一个服务定义配置文件。echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}'>/etc/consul.d/web.json
  3. 用配置目录对数重启代理:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    [root@Automation bin]# curl http://10.24.33.75:8500/v1/catalog/service/web
    curl: (7) couldn't connect to host
    [root@Automation bin]# consul agent -dev -config-dir /etc/consul.d
    ==> Starting Consul agent...
    ==> Consul agent running!
    Version: 'v1.0.2'
    Node ID: '8f55dc0e-5765-5941-e566-5a4f1166645f'
    Node name: 'Automation'
    Datacenter: 'dc1' (Segment: '<all>')
    Server: true (Bootstrap: false)
    Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
    Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
    Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

    ==> Log data will now stream in as it occurs:

    2017/12/25 15:46:02 [DEBUG] Using random ID "8f55dc0e-5765-5941-e566-5a4f1166645f" as node ID
    2017/12/25 15:46:02 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:8f55dc0e-5765-5941-e566-5a4f1166645f Address:127.0.0.1:8300}]
    2017/12/25 15:46:02 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
    2017/12/25 15:46:02 [INFO] serf: EventMemberJoin: Automation.dc1 127.0.0.1
    2017/12/25 15:46:02 [INFO] serf: EventMemberJoin: Automation 127.0.0.1
    2017/12/25 15:46:02 [INFO] consul: Adding LAN server Automation (Addr: tcp/127.0.0.1:8300) (DC: dc1)
    2017/12/25 15:46:02 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
    2017/12/25 15:46:02 [INFO] consul: Handled member-join event for server "Automation.dc1" in area "wan"
    2017/12/25 15:46:02 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
    2017/12/25 15:46:02 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
    2017/12/25 15:46:02 [INFO] agent: started state syncer
    2017/12/25 15:46:02 [WARN] raft: Heartbeat timeout from "" reached, starting election
    2017/12/25 15:46:02 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
    2017/12/25 15:46:02 [DEBUG] raft: Votes needed: 1
    2017/12/25 15:46:02 [DEBUG] raft: Vote granted from 8f55dc0e-5765-5941-e566-5a4f1166645f in term 2. Tally: 1
    2017/12/25 15:46:02 [INFO] raft: Election won. Tally: 1
    2017/12/25 15:46:02 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
    2017/12/25 15:46:02 [INFO] consul: cluster leadership acquired
    2017/12/25 15:46:02 [DEBUG] consul: Skipping self join check for "Automation" since the cluster is too small
    2017/12/25 15:46:02 [INFO] consul: member 'Automation' joined, marking health alive
    2017/12/25 15:46:02 [INFO] consul: New leader elected: Automation
    2017/12/25 15:46:02 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically
    2017/12/25 15:46:02 [INFO] agent: Synced service "web"

你会注意到在输出中”synced service ‘web’”。这意味着代理已经从配置文件中装载了该服务定义,并且已经成功注册该服务到服务目录中。

如果你想注册多个服务,你可以在Consul配置目录中创建多个服务定义文件。

查询服务

这里只试了HTTP API:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@Automation ~]# curl http://localhost:8500/v1/catalog/service/web
[
{
"ID": "8f55dc0e-5765-5941-e566-5a4f1166645f",
"Node": "Automation",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "web",
"ServiceName": "web",
"ServiceTags": [
"rails"
],
"ServiceAddress": "",
"ServicePort": 80,
"ServiceEnableTagOverride": false,
"CreateIndex": 6,
"ModifyIndex": 6
}
]

健康检查

定义检查

定义check的位置为/etc/consul.d/,格式跟定义服务一样,也是JSON。
下边我们定义两个check,分别为ping.json和web.json。内容如下:

1
2
3
4
5
6
7
{
"check": {
"name": "ping",
"script": "ping -c1 google.com >/dev/null",
"interval": "30s"
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"service": {
"name": "web",
"tags": [
"springCloud"
],

"port": 80,
"check": {
"script": "curl localhost >/dev/null 2>&1",
"interval": "10s"
}

}

}

第一个定义增加了一个主机级别的检测,名为”ping”。每隔30秒检查一次。在一个基于脚本的健康检测中,该检测使用启动Consul进程的用户来启动该检测。如果检测命令返回一个非0的返回码,那么该节点将被标记为不健康。
第二个命令修改名为web的服务,增加了一个检测,每隔10秒执行一次。

检查健康状态

修改后,需要重启一下代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[root@Automation consul.d]# consul agent -dev -config-dir /etc/consul.d
==> Starting Consul agent...
==> Error starting agent: Failed to register service "web": Scripts are disabled on this agent; to enable, configure 'enable_script_checks' to true
[root@Automation consul.d]# consul agent -dev -enable-script-checks -config-dir /etc/consul.d
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.0.2'
Node ID: '33b66ccd-d48f-283d-c9ec-94f2f677c1af'
Node name: 'Automation'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

2017/12/25 16:16:34 [DEBUG] Using random ID "33b66ccd-d48f-283d-c9ec-94f2f677c1af" as node ID
2017/12/25 16:16:34 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:33b66ccd-d48f-283d-c9ec-94f2f677c1af Address:127.0.0.1:8300}]
2017/12/25 16:16:34 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2017/12/25 16:16:34 [INFO] serf: EventMemberJoin: Automation.dc1 127.0.0.1
2017/12/25 16:16:34 [INFO] serf: EventMemberJoin: Automation 127.0.0.1
2017/12/25 16:16:34 [WARN] agent: check "service:web" has the 'script' field, which has been deprecated and replaced with the 'args' field. See https://www.consul.io/docs/agent/checks.html
2017/12/25 16:16:34 [WARN] agent: check "ping" has the 'script' field, which has been deprecated and replaced with the 'args' field. See https://www.consul.io/docs/agent/checks.html
2017/12/25 16:16:34 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
2017/12/25 16:16:34 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
2017/12/25 16:16:34 [INFO] consul: Adding LAN server Automation (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2017/12/25 16:16:34 [INFO] consul: Handled member-join event for server "Automation.dc1" in area "wan"
2017/12/25 16:16:34 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
2017/12/25 16:16:34 [INFO] agent: started state syncer
2017/12/25 16:16:34 [WARN] raft: Heartbeat timeout from "" reached, starting election
2017/12/25 16:16:34 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2017/12/25 16:16:34 [DEBUG] raft: Votes needed: 1
2017/12/25 16:16:34 [DEBUG] raft: Vote granted from 33b66ccd-d48f-283d-c9ec-94f2f677c1af in term 2. Tally: 1
2017/12/25 16:16:34 [INFO] raft: Election won. Tally: 1
2017/12/25 16:16:34 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2017/12/25 16:16:34 [INFO] consul: cluster leadership acquired
2017/12/25 16:16:34 [INFO] consul: New leader elected: Automation
2017/12/25 16:16:34 [DEBUG] consul: Skipping self join check for "Automation" since the cluster is too small
2017/12/25 16:16:34 [INFO] consul: member 'Automation' joined, marking health alive
2017/12/25 16:16:34 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically
2017/12/25 16:16:34 [INFO] agent: Synced service "web"
2017/12/25 16:16:34 [DEBUG] agent: Check "service:web" in sync
2017/12/25 16:16:34 [INFO] agent: Synced check "ping"

检查:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@Automation ~]# curl http://localhost:8500/v1/health/state/critical
[
{
"Node": "Automation",
"CheckID": "ping",
"Name": "ping",
"Status": "critical",
"Notes": "",
"Output": "",
"ServiceID": "",
"ServiceName": "",
"ServiceTags": [],
"Definition": {},
"CreateIndex": 7,
"ModifyIndex": 7
}
]
You have new mail in /var/spool/mail/root
[root@Automation ~]# curl http://localhost:8500/v1/health/state/critical
[]

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!