Scaling a Docker application across multiple nodes requires leveraging additional tools. Here I outline one approach using Docker Compose, Swarm, Consul and Registrator.
26. Consul - Service Discovery
Register service with consul
$ curl -X PUT
-d "{"ID": "app001", "Name": "app", "Tags": [], "Port": 5000}"
0.0.0.0:8500/v1/agent/services/register
List of services can now be queried via API
$ curl 0.0.0.0:8500/v1/catalog/services
{"app": [], "consul": []}
27. Consul - Service Discovery
A single service definition via API
$ curl 0.0.0.0:8500/v1/catalog/service/app
[
{
"ServicePort": 49153,
"ServiceAddress": "",
"ServiceTags": null,
"ServiceName": "app",
"ServiceID": "swarm-0:dockeransible_app_1:5000",
"Address": "10.129.127.240",
"Node": "swarm-0"
}
]
28. Consul - DNS
Service can now be also queried by DNS*
$ dig A app.service.consul
; <<>> DiG 9.8.3-P1 <<>> A app.service.consul
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55035
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;app.service.consul. IN A
;; ANSWER SECTION:
app.service.consul. 300 IN A 192.168.0.12
*Consul DNS runs on a nonstandard port so install DNSmasq to query this by default.
29. Consul - Health Checks
Health checks monitor service
Remove a service from DNS queries if it fails a
health check
30. Consul - Key/Value Store
Store application configuration data in the cluster
Store a value
$ curl -X PUT -d "bar" 0.0.0.0:8500/v1/kv/foo
Retrieve a value
$ curl 0.0.0.0:8500/v1/kv/foo?raw
32. Consul - Watches
Using a watch we can perform a deployment across the cluster
$ consul watch -type key -key app/sha /usr/local/bin/deploy.sh
On Swarm Manager node add a watch for the key
When this changes run a script
In the script loop over the app containers stopping
them one by one and starting the new container