« Back to home

Testing etcd on Docker

Posted on

I was messing around with etcd today on docker. Etcd has some simple instructions for setting up a cluster, but some minor tweaks are needed when you run etcd inside a docker container.

Within a docker container, the loopback address doesn’t connect back to the host. Instead you need to determine address that docker configures for the docker host. On my system the host IP is 172.16.42.1, but it seems that is not guaranteed. To check your system execute ip route, look for the docker line, the host address should be at the end of that line, or simply execute the following command:


ip route | grep docker | awk '{ print $NF }'

Once you have the docker host address, you can use it to configure and launch your etcd containers. I found it useful to launch them in seperate terminal windows.

# launch machine 1 at ports 4001 and 7001
docker run -p 4001:4001 -p 7001:7001 coreos/etcd -name machine1 -addr 172.17.42.1:4001 -peer-addr 172.17.42.1:7001

# launch machine 2 at ports 4002 and 7002
docker run -p 4002:4002 -p 7002:7002 coreos/etcd -name machine2 -addr 172.17.42.1:4002 -peer-addr 172.17.42.1:7002 -peers 172.17.42.1:7001,172.17.42.1:7003 

# launch machine 3 at ports 4003 and 7003
docker run -p 4003:4003 -p 7003:7003 coreos/etcd -name machine3 -addr 172.17.42.1:4003 -peer-addr 172.17.42.1:7003 -peers 172.17.42.1:7001,172.17.42.1:7002

Etcd uses the loopback 127.0.0.1 as the default address, so we need to override that with the docker host address. Also, the coreos/etcd dockerfile is configured with an ENTRYPOINT, so command line arguments after the image name are passed to the etcd process.

Once you have your etcd containers running, test it with curl. On the host you can use 127.0.0.1 or your docker host address (172.17.42.1):

# write the value to machine 1
curl -L http://127.0.0.1:4001/v2/keys/mykey -XPUT -d value="testing etcd in docker cluster."

# read the value from machine 1
curl -L http://127.0.0.1:4001/v2/keys/mykey

# read the value from machine 2
curl -L http://127.0.0.1:4002/v2/keys/mykey

# read the value from machine 3
curl -L http://127.0.0.1:4003/v2/keys/mykey

# read the value from machine 3 using host address
curl -L http://172.17.42.1:4003/v2/keys/mykey

Summary

Etcd was pretty easy to get running and I really like the http interface. As of this writing, the coreos/etcd image is at version 0.4.6 – the etcd team is actively working on 0.5 and they have a warning for anyone that wants to use etcd in production.