How to fix HA Proxy when my Docker images are frequently recreated?
I wrote some weeks ago about how to configure HA Proxy to fix address resolution and avoid startup failure. It usualy works when server addresses do not change often.
Yet, we have the case of a development server on which Gitlab CI pipelines
frequently remove and recreate containers. In this situation setting
init-addr
won't work as it fails to track the link between the
container name and its ip address.
The solution here is to add a resolver in configuration file. If your HA Proxy container is deployed within a docker network, it can call docker's internal resolver that listens on 127.0.0.11:53
Add a resolvers section in the configuration file
resolvers docker_resolver
nameserver dns 127.0.0.11:53
Then indicate which resolver to use in you backend definitions
server server1 container_name:service_port resolvers docker_resolver resolve-prefer ipv4
If HA Proxy routes the trafic only to containers on the same bridge, you can define the resolvers in the default-server
declaration.
default-server resolvers docker_resolver resolve-prefer ipv4