The Goal
Run a single haproxy container in the swarm with a static IP on my LAN that will be recreated if a node fails. In essence, it would be akin to a VIP pointing to haproxy but without requiring an external load balancer outside of the swarm.
What works - local macvlan
Example: Creating the macvlan networks on two nodes, running the container on node1, stopping the container, then running the container on node2. The haproxy container is created with the same static IP and is accessible on the LAN.
What doesn't work - swarm macvlan
Example: Setting the macvlan network scope to swarm, then deploying the stack. Container is not visible to the network.
Files used in example
docker-compose.yml:
version: "3.2"
networks:
vlan0:
external: true
services:
haproxy:
image: haproxy:2.3.2-alpine
container_name: haproxy
volumes:
- ./data:/usr/local/etc/haproxy:ro
environment:
- TZ=America/Los_Angeles
restart: always
networks:
vlan0:
ipv4_address: 192.168.0.201
localnet.sh (script to stop stack / remove network / recreate network as local / run container local):
#!/bin/bash
docker service rm haproxy_haproxy
docker-compose down
docker network rm vlan0
docker network create -o parent=eth0 --subnet 192.168.0.0/24 --gateway 192.168.0.1 --driver macvlan --scope local vlan0
docker-compose up
swarmnet.sh (script to remove container and network / recreate network as swarm / run as swarm stack):
#!/bin/bash
docker service rm haproxy_haproxy
docker-compose down
docker network rm vlan0
docker network create -o parent=eth0 --subnet 192.168.0.0/24 --gateway 192.168.0.1 --driver macvlan --scope swarm vlan0
docker stack deploy -c docker-compose.yml haproxy
Is it possible to run a single container with a static macvlan IP in swarm mode?
Any help is appreciated.
Edit: I have been able to get macvlan addresses working, but docker swarm does not obey the ipv4_address field to static the container. I understand the reason for this (replicas, etc. not going on the same IP) but in this scenario, it will not happen due to it being a single container. I've found this issue discussed here: https://forums.docker.com/t/docker-swarm-1-13-static-ips-for-containers/28060/
I've figured out how to bind a single static ip address (with a major caveat below).
Method
To do this, create a config only network on each host with a single number ip range (32 bit mask). I've created a script here to auto-generate the commands.
Example output:
So, on each node, I build the network config
Then I build the swarm interface
And in the applicable docker-compose.yaml file, add the appropriate lines:
Results
My container will now always be available at that single static IP address. If the node fails, it will restart on another node with the same IP, in essence creating a high-availability VIP for the swarm.
Caveat (Swarm macvlan bug?)
Docker swarm will throw errors trying to bind multiple macvlans to the same parent interface, like below:
Described here:
https://github.com/moby/libnetwork/issues/2384
https://github.com/moby/libnetwork/issues/1743
Conclusion
It is promising that it is actually possible to create a static macvlan IP in the swarm, but until the bug is fixed with binding multiple networks to a single parent interface, it is very limited.
If anyone knows how to reliably fix the problem above, or has a better method, please post.