CAPITULO 4.- CARACTERISTICAS DE DISEÑO DEL MOTOR DE TURBINA DE GAS GE90-94B
4.8 Sistema de control y combustible del motor
Registrator⁸⁷ automatically registers and deregisters services by inspecting containers as they are brought online or stopped. It currently supports etcd, Consul and SkyDNS 2.
Setting up Registrator with etcd registry is easy. We can simply run the Docker container as follows (please do not run it yourself).
⁸⁷https://github.com/gliderlabs/registrator
1 docker run -d --name registrator \
2 -v /var/run/docker.sock:/tmp/docker.sock \ 3 -h serv-disc-01 \
4 gliderlabs/registrator \
5 -ip 10.100.194.201 etcd://10.100.194.201:2379
With this command we are sharing /var/run/docker.sock as Docker volume. Registrator will monitor and intercept Docker events and, depending on the event type, put or remove service information to/from etcd. With the -h argument we are specifying the hostname. Finally, we are passing two arguments to Registrator. The first one is the -ip and represents the IP of the host and the second one is the protocol (etcd), the IP (serv-disc-01) and the port (2379) of the registration service.
Before we proceed, let’s create a new Ansible role called registrator and deploy it to all nodes inside the cluster. Theroles/registrator/tasks/main.yml⁸⁸file is as follows.
1 - name: Container is running 2 docker:
3 name: "{{ registrator_name }}"
4 image: gliderlabs/registrator 5 volumes:
6 - /var/run/docker.sock:/tmp/docker.sock 7 hostname: "{{ ansible_hostname }}"
8 command: -ip {{ facter_ipaddress_eth1 }} {{ registrator_protocol }}://{{ fac\
9 ter_ipaddress_eth1 }}:2379 10 tags: [etcd]
This Ansible role is equivalent to the manual command we saw earlier. Please note that we changed the hard-coded etcd protocol with a variable. That way we can reuse this role with other registries as well. Keep in mind that having quotes is not mandatory in Ansible except when value starts with {{ as in the case of the hostname value.
Let’s take a look at theregistrator-etcd.yml⁸⁹playbook.
⁸⁸https://github.com/vfarcic/ms-lifecycle/blob/master/ansible/roles/registrator/tasks/main.yml
⁸⁹https://github.com/vfarcic/ms-lifecycle/blob/master/ansible/registrator-etcd.yml
1 - hosts: all
Most of the playbook is similar to those we used before except the vars key. In this case, we’re using it to define the Registrator protocol as etcd and port of the registry as 2379.
With everything in place, we can run the playbook.
1 ansible-playbook \
2 /vagrant/ansible/registrator-etcd.yml \ 3 -i /vagrant/ansible/hosts/serv-disc
Once the playbook is finished executing, Registrator will be running on all three nodes of our cluster.
Let’s give Registrator a spin and run one container inside one of the three cluster nodes.
1 export DOCKER_HOST=tcp://serv-disc-02:2375
We exported the DOCKER_HOST variable so that Docker commands are sent to the cluster node 2 (serv-disc-02) and run the nginx container exposing port 1234. We’ll use nginx later on, and there will be plenty of opportunities to get familiar with it. For now, we are not interested in what nginx does, but that Registrator detected it and stored the information in etcd. In this case, we put a few environment variables (SERVICE_NAME and SERVICE_ID) that Registrator can use to identify better the service.
Let us take a look at Registrator’s log.
1 docker logs registrator
The output should be similar to the following.
1 2015/08/30 19:18:12 added: 5cf7dd974939 nginx
2 2015/08/30 19:18:12 ignored: 5cf7dd974939 port 443 not published on host
We can see that Registrator detected nginx container with the ID 5cf7dd974939. We can also see that it ignored the port 443. The nginx container internally exposes ports 80 and 443. However, we exposed only 80 to the outside world, so Registrator decided to ignore the port 443. After all, why would we store the information about the port not accessible to anyone?
Now, let us take a look at data stored in etcd.
1 curl http://serv-disc-01:2379/v2/keys/ | jq '.' 2
3 curl http://serv-disc-01:2379/v2/keys/nginx/ | jq '.' 4
5 curl http://serv-disc-01:2379/v2/keys/nginx/nginx | jq '.'
The output of the last command is as follows.
1 {
2 "node": {
3 "createdIndex": 13, 4 "modifiedIndex": 13,
5 "value": "10.100.194.202:1234", 6 "key": "/nginx/nginx"
7 },
8 "action": "get"
9 }
The first command listed all keys at the root, the second listed all those inside nginx and the last one retrieved the final value. Registrator stored values in the format <NAME>/<ID> that matches environment variables we used when running the container. The value that Registrator put corresponds with the IP of the host where the container is running and the port that we exposed.
Please note that even though the container is run on the node 2, we queried etcd running on the node 1. It was yet another demonstration that data is replicated across all nodes etcd is running on.
What happens when we remove the container?
1 docker rm -f nginx 2
3 docker logs registrator
The output of Registrator logs should be similar to the following.
1 ...
2 2015/08/30 19:32:31 removed: 5cf7dd974939 nginx
Registrator detected that we removed the container and sent a request to etcd to remove correspond-ing values. We can confirm that with the followcorrespond-ing command.
1 curl http://serv-disc-01:2379/v2/keys/nginx/nginx | jq '.'
The output is as follows.
1 {
2 "index": 14,
3 "cause": "/nginx/nginx", 4 "message": "Key not found", 5 "errorCode": 100
6 }
The service with the ID nginx/nginx disappeared.
Registrator combined with etcd is a powerful, yet simple, combination that will allow us to practice many advanced techniques. Whenever we bring up a container, data will be stored in etcd and propagated to all nodes in the cluster. What we’ll do with that information will be the subject of the next chapter.
Figure 8-7: Multiple nodes with Docker containers, etcd and Registrator
There is one more piece of the puzzle missing. We need a way to create configuration files with data stored in etcd as well as run some commands when those files are created.