Split Haproxy configuration in multiple files working with Systemd

9 12 2016

Splitting Haproxy configuration in multiple files it’s a bit tricky due the Systemd startup behaviour. One workaround who works ok it’s the following:

1.- Split the Haproxy configuration file in multiple files.

1.1.- One file called “00-haproxy.conf” with the basic haproxy conf (in my case global, defaults and listen stats). This must have the 00- at the begining for listed it at first place in the script.

1.2.- One file for each listen section for the different services balanced, “some_name_a.conf”. Each new balanced service will have a new file.

Note: In this case, each balanced service is inside a listen section, not using fronted and backed.

# ls /etc/haproxy/
00-haproxy.conf service1.conf service2.conf

# cat /etc/haproxy/00-haproxy.conf
global
...
defaults
...
listen proxy-stats
...
# cat /etc/haproxy/corebalv1.conf
listen xxx
bind ...
server ...
server ...

2.- Create a small script into “/usr/local/bin/haproxy-multiconf” with this content:

#!/bin/bash
for file in /etc/haproxy/*.conf; do
test -f $file
CNF="$CNF -f $file"
done
echo "CONF='$CNF'" > /etc/haproxy/haproxy-multiconf.lst

3.- Create a new systemd unit for create the conf list before launching the main Haproxy unit.
# cat /etc/systemd/system/haproxy-multiconf.service
[Unit]
Description=HAProxy Load Balancer Multiconf
After=network.target
Before=haproxy.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/haproxy-multiconf
[Install]
WantedBy=multi-user.target

4.- Modify the default systemd unit file of Haproxy:
# cat haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=network.target
Requires=haproxy-multiconf.service
[Service]
EnvironmentFile=/etc/haproxy/haproxy-multiconf.lst
ExecStartPre=/usr/local/sbin/haproxy -c -q $CONF
ExecStart=/usr/local/sbin/haproxy-systemd-wrapper -p /run/haproxy.pid $CONF
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
[Install]
WantedBy=multi-user.target

5.- Refresh systemd and run it:

systemctl daemon-reload
systemctl restart haproxy.service

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: