Introduction To Consul

官方参考文档

1. 安装consul

下载之后配置环境变量即可以运行

1
2
3
#consul
CONSUL_HOME=/Users/jay/opt/consul
PATH=$CONSUL_HOME:$PATH


2. What is Consul?


3. Basic Architecture of Consul

Every node that provides services to Consul runs a Consul agent. Running an agent is not required for discovering other services or getting/setting key/value data. The agent is responsible for health checking the services on the node as well as the node itself.

服务的提供者需要运行一个consul agent, 例如:

1
consul agent -dev

发现服务,使用服务就不需要运行consul agent


4. Run the Consul Agent

The agent can run either in server or client mode. Each datacenter must have at least one server.
From the log data, you can see that our agent is running in server mode and has claimed leadership of the cluster.

4.1 start the Consul agent in development mode

1
2
3
# This mode is useful for bringing up a single-node Consul environment quickly and easily.
# It is not intended to be used in production as it does not persist any state.
consul agent -dev

4.2 see the members of the Consul cluster

1
2
3
$ consul members
Node Address Status Type Build Protocol DC Segment
bogon 127.0.0.1:8301 alive server 1.0.1 2 dc1 <all>

4.3 use the HTTP API as it forwards the request to the Consul servers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ curl localhost:8500/v1/catalog/nodes
[
{
"ID": "f0d57963-617c-ef36-76ad-7b8298fe0a3a",
"Node": "bogon",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"Meta": {
"consul-network-segment": ""
},
"CreateIndex": 5,
"ModifyIndex": 6
}
]

4.4 In addition to the HTTP API, the DNS interface can be used to query the node

1
$ dig @127.0.0.1 -p 8600 bogon.node.consul


5. Registering Services

5.1 Define a Service

1
2
3
4
5
6
7
8
9
10
# make a configuration directory for consul
$ sudo mkdir /etc/consul.d

# write service defintion
$ echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' \
| sudo tee /etc/consul.d/web.json


#restart th agent
$ consul agent -dev -config-dir=/etc/consul.d


6. Query Services

Once the agent is started and the service is synced, we can query the service using either the DNS or HTTP API.

6.1 DNS
For the DNS API, the DNS name for services is NAME.service.consul. By default, all DNS names are always in the consul namespace, though this is configurable. The service subdomain tells Consul we’re querying services, and the NAME is the name of the service.

1
2
3
4
$ dig @localhost -p 8600 web.service.consul

#或者
$ dig @localhost -p 8600 rails.web.service.consul

6.2 HTTP API

1
2
3
4
5
6
7
8
#查询所有的services
$ curl http://localhost:8500/v1/catalog/services
{
"consul": [],
"web": [
"rails"
]
}

Catalog API给出所有的持有web这个服务的nodes.

The catalog API gives all nodes hosting a given service. As we will see later with health checks you’ll typically want to query just for healthy instances where the checks are passing.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#查询某个service
$ curl http://localhost:8500/v1/catalog/service/web
[
{
"ID": "a9b87a61-2b0e-9c62-90fd-23de60769fa5",
"Node": "bogon",
"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
}
]

#过滤不health的服务
$ curl 'http://localhost:8500/v1/health/service/web?passing'
[
{
"Node": {
"ID": "a9b87a61-2b0e-9c62-90fd-23de60769fa5",
"Node": "bogon",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"Meta": {
"consul-network-segment": ""
},
"CreateIndex": 5,
"ModifyIndex": 6
},
"Service": {
"ID": "web",
"Service": "web",
"Tags": [
"rails"
],
"Address": "",
"Port": 80,
"EnableTagOverride": false,
"CreateIndex": 6,
"ModifyIndex": 6
},
"Checks": [
{
"Node": "bogon",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"ServiceTags": [],
"Definition": {},
"CreateIndex": 5,
"ModifyIndex": 5
}
]
}
]

Alternatively, the HTTP API can be used to add, remove, and modify services dynamically.

常用命令

1
2
3
4
5
6
7
$ consul members

$ curl http://127.0.0.1:8500/v1/catalog/services

$ curl http://127.0.0.1:8500/v1/catalog/nodes

$ curl http://127.0.0.1:8500/v1/catalog/service/myservice