You can't really have useful service discovery without health checking. Consul gives you two ways to register healthchecks:
- check definition (config) files, and
- the HTTP API (see the python applications used in this course)
There are a bunch of different kinds of checks types that you can register, depending on what you're checking and how you want to do it:
- Script
- HTTP
- TCP
- TTL
- Docker
- gRPC
- Alias
Docs: https://www.consul.io/docs/agent/checks.html
Check your config!
mkdir -p /etc/consul.d/checks
cat <<EOF > '/etc/consul.d/checks/nginx.json'
{
"service": {
"name": "nginx",
"tags": [
"web"
],
"port": 80,
"check": {
"args": ["curl", "localhost"],
"interval": "10s"
}
}
}
EOF
systemctl reload consul
curl 'http://localhost:8500/v1/health/service/web?passing'
dig nginx.service.consul
Turn off nginx:
systemctl stop nginx
Consul will mark it as unhealthy:
curl 'http://localhost:8500/v1/health/service/web?passing'
dig nginx.service.consul