Wednesday, June 18, 2014

CoreOS Floating IP

Using fleetctl to run Docker containers in CoreOS will start the container on a random member of the cluster and then restart it attomatically on another member if that one becomes unavailable.  This works great for most containers but you need a consistent way to reach an entry point whether it is a load balancer or an apache front end.

We can use the ExeStartPre and ExeStopPost commands from the fleetctl unit file to add and delete a virtual IP when our service is started and stopped.

[Unit]
Description=Example
After=docker.service
Requires=docker.service

[Service]
Restart=always
ExecStartPre=/usr/bin/sudo /usr/bin/ip addr add 172.17.8.100/24 dev enp0s8
ExecStart=/usr/bin/docker run --rm=true --name webapp -p 80:80 coreos/apache /usr/sbin/apache2ctl -D FOREGROUND

ExecStop=/usr/bin/docker stop webapp
ExecStopPost=/usr/bin/sudo /usr/bin/ip addr del 172.17.8.100/24 dev enp0s8

[Install]
WantedBy=multi-user.target