The following documents a trial of using etcd, and confd to automatically configure a haproxy load balancer.
It is built using a combination of blogs, resources and experimentation, but provides a rough template of the approach that would allow a fully featured balancer to be configured from etcd keyvalues.
- Include systemd sidekick unit to automatically register the key's into ETCD based on a docker service (for example) being started
- A more complete haproxy template that builds a valid, complex haproxy that would be capable of dealing with Future PLC's balancer requirements
Follow instructions from https://github.com/coreos/etcd/releases then...
- mv /tmp/test-etcd/etcd* /usr/bin/
- Add etcd.system script shown above to correct location and follow instructions in the file
journal -xe -u etcd
should show logs that etcd is started- Confirm with
etcdctl cluster-health && etcdctl member list
wget https://github.com/kelseyhightower/confd/releases/download/v0.11.0/confd-0.11.0-linux-amd64
mv confd-0.11.0-linux-amd64 /usr/bin/confd
The follow the quick start guide to validate things are working; https://github.com/kelseyhightower/confd/blob/master/docs/quick-start-guide.md
(Based on https://github.com/Ventures/haproxy-confd )
Create a new confd configuration based on the haproxy.toml and haproxy.tmpl
Configure the ETCD keys using the following basic example;
etcdctl mkdir "/haproxy-etcd/services"
etcdctl set "/haproxy-etcd/services/etcd/domain" "etcd.lan"
etcdctl set "/haproxy-etcd/services/etcd/port" "80"
And run confd with the following for a one off test case
confd -onetime -backend etcd -node http://etcd.lan:2379 -log-level=debug
And use this to get confd to keep a watch on the etcd keys and rebuild the config if any of the values change;
confd -backend etcd -node http://etcd.lan:2379 -log-level=debug -watch=true
* WARNING: * - Running with watch on a production system might be a bad idea if all your balancers spontaniously reconfigure themselves concurrently.
confd -onetime -backend etcd -node http://etcd.lan:2379 -log-level=debug
2016-11-21T21:48:23Z haproxy confd[7569]: INFO Backend set to etcd
2016-11-21T21:48:23Z haproxy confd[7569]: INFO Starting confd
2016-11-21T21:48:23Z haproxy confd[7569]: INFO Backend nodes set to http://etcd.lan:2379
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Loading template resources from confdir /etc/confd
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Loading template resource from /etc/confd/conf.d/haproxy.toml
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Retrieving keys from store
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Key prefix set to /haproxy-etcd
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Using source template /etc/confd/templates/haproxy.tmpl
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Compiling source template /etc/confd/templates/haproxy.tmpl
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Comparing candidate config to /etc/haproxy/haproxy.conf
2016-11-21T21:48:23Z haproxy confd[7569]: INFO Target config /etc/haproxy/haproxy.conf out of sync
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Running /usr/sbin/haproxy -c -f /etc/haproxy/.haproxy.conf670147617
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG "Configuration file is valid\n"
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Overwriting target config /etc/haproxy/haproxy.conf
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG Running /usr/sbin/service haproxy reload
2016-11-21T21:48:23Z haproxy confd[7569]: DEBUG ""
2016-11-21T21:48:23Z haproxy confd[7569]: INFO Target config /etc/haproxy/haproxy.conf has been updated
Run this on your docker server and it'll ping back to etcd when services start...
docker run -d \
> --name=registrator \
> --net=host \
> --volume=/var/run/docker.sock:/tmp/docker.sock \
> gliderlabs/registrator:latest \
> etcd://etcd:2379/manager
Then run redis (for kicks and giggles)
docker run -d -P --name=redis redis
Then look at etcd
etcdctl ls /manager/redis
/manager/redis/manager:redis:6379
Thats right! Docker container started, registrator told ETCD and etcd can now create configuration for the backend redis instances automatically.
Magic!