This is not a howto or guide for running service discovery in production. It’s only writedown of a small expirement to see how service discovery using consul can work. It assumes basic knowledge of how to use a shell, docker and aws.
Service Discovery within an AWS environment
AWS has a blogpost available on using consul with ECS: In this post, a cloudformation template is referenced. I would definitely run this template as an example to learn from, but I would do it in a “throw-away” aws environment. In other words, create a trial account, launch it, throw it away after use. It will “pollute” your exisiting AWS account with iam profiles, policies, sec groups etc.
References I used:
Consul is a distributed key–value store and service discovery layer. In the example below I use this consul-agent docker image. Hashicorp has published an official dockerimage, but that runs Consul as a docker user instead of the root user. This has as the side effect that the docker container can’t bind on port 53, but binds to port 8600 instead. You will haven to bind port 53 to port 8600 in the docker run command, but with the current beta release of docker for mac bridging networking mode seems to broken.
Consul Template provides generic template rendering and notifications with Consul. This interacts with consul to generate configuration templates for eg. HAProxy (it can be used ofc for other services as well).
Registrator is responsible for registering (and de-registering) containers to Consul. It receives signals through the Docker socket, which is exposed to it.
local test setup
You can now login into consul at http://localhost:8500 and into the stats page of haproxy on http://localhost:1936 (someuser:password) for docker-machine.
Now launch more clients:
If you now refresh the haproxy stats page you will see that the additional launched instances are added to haproxy automagically.
If you query port 80, you will get replies back from random nodes, so haproxy is doing it’s job.
Same applies when you kill some docker instances, those will be removed from your haproxy stats page.
This is a very simple hello world setup of service discovery, but it should allow you to see what moving parts are involved. I intend to post a more production ready setup using terraform and docker swarm soon.