HAProxy Use Multiple Config Files from a folder
Goodies, Goodies | HAProxyCreate a folder to contain the .cfg files
sudo mkdir -p /etc/haproxy/haproxy.d
Change the systemd file used for the HAProxy Service
sudo vim /etc/systemd/system/multi-user.target.wants/haproxy.service
Find the line that starts with Environment
Edit the line so it contains the -f /etc/haproxy/haproxy.d
In this case we are using the /etc/haproxy/haproxy.d folder for the files.
Example of the config file
[Unit]
Description=HAProxy Load Balancer
Documentation=man:haproxy(1)
After=syslog.target network.target
[Service]
EnvironmentFile=-/etc/sysconfig/haproxy
Environment="CONFIG=/etc/haproxy/haproxy.cfg -f /etc/haproxy/haproxy.d" "PIDFILE=/run/haproxy.pid" "EXTRAOPTS=-S /run/haproxy-master.sock"
ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecStart=/usr/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS
ExecReload=/usr/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
SuccessExitStatus=143
Type=notify
[Install]
WantedBy=multi-user.target
It should be noted, that with the above configuration, it is easy to misuse the haproxy configuration check:
haproxy -c -f /etc/haproxy/haproxy.cfg
will only check the haproxy config in this particular file. If there is an error in one of the config files under haproxy.d/*.cfg
, the config check will report the haproxy config as correct, but haproxy may fail to restart.
Of course, the correct way to use haproxy config check in such a case would be:
haproxy -c -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/haproxy.d/
Using the systemd unit file to include an additional directory for haproxy config files is clunky and prone to errors.