consul introduction
CP model.
consul deployment on k8s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: consul
name: consul
spec:
selector:
matchLabels:
app: consul
template:
metadata:
labels:
app: consul
spec:
containers:
- args:
- agent
- -enable-script-checks
- -dev
- -client
- 0.0.0.0
image: consul:1.8.4
name: consul
|
Service registration
Configuration file method
Specify the configuration directory via -config-dir
.
1
|
consul agent -data-dir=/consul/data -config-dir=/consul/config -dev -client 0.0.0.0
|
Create a configuration file in the directory. As follows, create a service named web.
1
2
3
4
5
6
7
8
9
|
{
"service": {
"name": "web",
"tags": [
"rails"
],
"port": 80
}
}
|
consul does not automatically monitor the directory and needs to send a SIGHUP signal to the consul process (kill -HUP {pid}
) before it will reload.
The configuration can also be re-read via the command line consul reload
.
1
2
|
$ consul reload
Configuration reload triggered
|
Service discovery
DNS method
When consul is started, a dns server is started synchronously, listening on port 8600. registered services are added with the .service.consul
suffix.
A record (IP address) or SRV record (IP+Port) can be requested.
1
2
3
4
5
6
7
8
9
10
11
12
|
$ dig @10.244.6.25 -p 8600 web.service.consul
...
;; ANSWER SECTION:
web.service.consul. 0 IN A 127.0.0.1
$ dig @10.244.6.25 -p 8600 web.service.consul SRV
...
;; ANSWER SECTION:
web.service.consul. 0 IN SRV 1 1 80 consul-6c464f5d4b-gv52l.node.dc1.consul.
;; ADDITIONAL SECTION:
consul-6c464f5d4b-gv52l.node.dc1.consul. 0 IN A 127.0.0.1
consul-6c464f5d4b-gv52l.node.dc1.consul. 0 IN TXT "consul-network-segment="
|
API method
It can also be requested through the API method
Query all services
1
2
3
4
5
6
7
|
$ curl http://127.0.0.1:8500/v1/catalog/services
{
"consul": [],
"web": [
"rails"
]
}
|
Query the details of a service
1
2
3
4
5
6
7
8
|
$ curl http://127.0.0.1:8500/v1/catalog/service/web
[
{
"ID": "0b617f2a-58d6-3779-268a-8583f1d56e1f",
"Node": "consul-6c464f5d4b-gv52l",
"Address": "127.0.0.1",
"Datacenter": "dc1",
...
|
Query the health status of a service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$ curl 127.0.0.1:8500/v1/health/service/web
[
{
"Node": {
"ID": "291dec46-80c2-cfe4-e13e-10b4521e9ccc",
"Node": "consul-655bc59456-nj5l8",
"Address": "127.0.0.1",
"Datacenter": "dc1",
...
"Checks": [
{
"Node": "consul-655bc59456-nj5l8",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
...
}
]
|
Register service
student.json
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
|
{
"ID":"student-service-e5",
"Name":"student-service",
"Tags":[
],
"Meta":{
},
"Port":9098,
"Address":"127.0.0.1",
"TaggedAddresses":{
"lan_ipv4":{
"Address":"127.0.0.1",
"Port":9098
},
"wan_ipv4":{
"Address":"127.0.0.1",
"Port":9098
}
},
"Weights":{
"Passing":1,
"Warning":1
},
"EnableTagOverride":false
}
|
1
|
$ curl --request PUT --data @student.json "http://192.168.0.54:8500/v1/agent/service/register?replace-existing-checks=true"
|
Logout service
1
|
$ curl --request PUT http://192.168.0.54:8500/v1/agent/service/deregister/student-service
|
Health Detection
consul can configure health monitoring for the service. Note that -enable-script-checks
needs to be enabled.
As follows, the service is checked every 10 seconds to see if it is OK. If it is not normal, DNS resolution will not return an IP address.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
{
"service": {
"name": "web",
"tags": [
"rails"
],
"port": 80,
"check": {
"args": [
"curl",
"localhost"
],
"interval": "10s"
}
}
}
|
The status is already cirtical through API detection.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$ curl 127.0.0.1:8500/v1/health/service/web
[
{
"Node": {
"ID": "291dec46-80c2-cfe4-e13e-10b4521e9ccc",
"Node": "consul-655bc59456-nj5l8",
"Address": "127.0.0.1",
"Datacenter": "dc1",
{
"Node": "consul-655bc59456-nj5l8",
"CheckID": "service:web",
"Name": "Service 'web' check",
"Status": "critical",
...
$ curl http://10.244.6.25:8500/v1/health/service/web?passing
[]
|