Can I split a large HAProxy config file into multiple smaller files? [closed]
Asked Answered
A

6

37

I'm building an haproxy config file that has multiple front and backends. It's going to be several hundred lines long and I'd rather split it up into separate files for each of the different websites that I want to loadbalance.

Does HAProxy offer the ability to link to partial config files from the main haproxy.cfg file?

Accouterment answered 10/9, 2014 at 21:52 Comment(0)
W
50

Configuration files can't be linked together from a configuration directive.

However HAProxy can load multiple configuration files from its command line, using the -f switch multiple times:

haproxy -f conf/http-defaults -f conf/http-listeners -f conf/tcp-defaults -f conf/tcp-listeners 

If you want to be flexible with the amount of config files you can even specify a directory like this: -f /etc/haproxy. The files will then be used in their lexical order, newer files overriding older files. See the mailing list for an example, if provides links to the documentation. This information can be found in the management guide, not the regular docs.

Wivina answered 12/9, 2014 at 6:29 Comment(2)
haproxy -f conf/http-defaults -f conf/http-listeners -f conf/tcp-defaults -f conf/tcp-listenersTarkany
Nice tip with the directory. I now use -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/haproxy.d. Additional configs can easily be added to the haproxy.d directory. This is extremely useful for splitting the configuration for dataplane, so dataplane can operate on only the configuration that it needs.Sporogonium
P
10

Stumbled on this answer where the author created scripts to imitate nginx disable enable sites functionality. In the haproxy init.d startup he uses script loop to build the haproxy -f commands concatenation.

/etc/init.d/haproxy:

EXTRAOPTS=`for FILE in \`find /etc/haproxy/sites-enabled -type l | sort
-n\`; do CONFIGS="$CONFIGS -f $FILE"; done; echo $CONFIGS`

haensite script:

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
  echo "You must be a root user" 2>&1
  exit 1
fi

if [ $# -lt 1 ]; then
  echo "Invalid number of arguments"
  exit 1
fi

echo "Enabling $1..."

cd /etc/haproxy/sites-enabled
ln -s ../sites-available/$1 ./

echo "To activate the new configuration, you need to run:"
echo "  /etc/init.d/haproxy restart"

hadissite script:

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
  echo "You must be a root user" 2>&1
  exit 1
fi

if [ $# -lt 1 ]; then
  echo "Invalid number of arguments"
  exit 1
fi

echo "Disabling $1..."

rm -f /etc/haproxy/sites-enabled/$1

echo "To activate the new configuration, you need to run:"
echo "  /etc/init.d/haproxy restart"
Prurigo answered 10/9, 2014 at 21:52 Comment(0)
D
4

This was a solution building off of @stephenmurdoch's answer which involved the use of multiple -f <conf file> arguments to the haproxy executable.

Using the stock CentOS 6.x RPM's included /etc/init.d/haproxy script you can amend it like so:

start() {
    $exec -c -q -f $cfgfile $OPTIONS
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi

    echo -n $"Starting $prog: "
    # start it up here, usually something like "daemon $exec"
    #daemon $exec -D -f $cfgfile -f /etc/haproxy/haproxy_ds.cfg -f /etc/haproxy/haproxy_es.cfg -f /etc/haproxy/haproxy_stats.cfg -p $pidfile $OPTIONS
    daemon $exec -D -f $cfgfile $(for i in /etc/haproxy/haproxy_*.cfg;do echo -n "-f $i ";done) -p $pidfile $OPTIONS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

With the above in place you can then create files such as haproxy_<X>.cfg and haproxy_<Y>.cfg using whatever names you want. The above for loop will include these files in an augmented daemon haproxy ... line if these files are present, otherwise the stock haproxy.cfg file will be used solely.

Within the haproxy_<...>.cfg files you need to make sure that your global and defaults are defined in the "toplevel" haproxy.cfg file. The rest of the files simply need to have frontend/backends and nothing more.

Dovetailed answered 29/8, 2016 at 18:6 Comment(0)
H
3

You can follow this simple step.

  1. Insert one line script (cat /etc/$BASENAME/conf.d/*.cfg > $CFG) in /etc/init.d/haproxy
    Here is position where you must insert line
    CFG=/etc/$BASENAME/$BASENAME.cfg cat /etc/$BASENAME/conf.d/*.cfg > $CFG [ -f $CFG ] || exit 1
  2. Reload daemon config with systemctl daemon-reload
  3. Make directory mkdir /etc/haproxy/conf.d
  4. Move default haproxy.cfg to conf.d as global.cfg mv /etc/haproxy/haproxy.cfg /etc/haproxy/conf.d/global.cfg
  5. Create your other .cfg file in conf.d directory
  6. Just restart your haproxy service systemctl restart haproxy
  7. NOTE: /etc/haproxy/haproxy.cfg will be automaticly created from all files in conf.d/
Hammad answered 16/7, 2018 at 12:13 Comment(1)
You can implement this solution using systemd. This question is closed for solutions so I apologise for the formatting - \n is a line break bash mkdir -p /etc/haproxy/conf.d\n cp /etc/haproxy/haproxy.cfg /etc/haproxy/conf.d/00-haproxy.cfg\n cp /lib/systemd/system/haproxy.service /etc/systemd/system/.\n mkdir -p /etc/systemd/system/haproxy.service.d\n cat <<EOF > /etc/systemd/system/haproxy.service.d/cfg_merge.conf\n [Service]\n ExecStartPre=/bin/bash -c 'cat /etc/haproxy/conf.d/*.cfg > /etc/haproxy/haproxy.cfg'\n EOF\n systemctl daemon-reload\n systemctl restart haproxy\n Phenomenalism
P
1

answer of @Bapstie memtioned that, a directory can be passed to haproxy as config file, and files inside will be loaded in alphabet order. It's correct.

But problem is, the package haproxy in CentOS 'base/7/x86_64' repository is so old that it does not support that.

So either do you need to write a wrapper to append -f <individual config file>to the command, or you need to install latest version of haproxy:

for package in centos-release-scl-rh rh-haproxy18-haproxy; do 
    yum install -y $package
done

and create a drop-in config for haproxy service:

[Service]
ExecStart=
ExecStart=/opt/rh/rh-haproxy18/root/sbin/haproxy -f /etc/haproxy-nutstore/ -p /run/haproxy.pid $OPTIONS
Pasturage answered 27/5, 2019 at 3:53 Comment(0)
P
1

If you use Ansible you can do a trick like this:

- name: haproxy configuration
  copy:
    content: >
      {{ lookup('template', haproxy_cfg_src_top) +
      lookup('template', haproxy_cfg_src_edge) +
      lookup('template', haproxy_cfg_src_bottom) }}

    dest: "{{ haproxy_cfg }}"
    owner: "{{ docker_user }}"
    group: "docker"
    mode: 0664
  register: haproxy_cfg_change
Protocol answered 17/12, 2020 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.