为haproxy loadbalancer提供多个cfg文件

为haproxy loadbalancer提供多个cfg文件

我的 haproxy 负载均衡器当前使用单个配置文件 /etc/haproxy/haproxy.cfg 运行:

ExecStart=/usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid $OPTIONS

我想将 haproxy.cfg 拆分为几个内容较少的文件,因为一些前端和后端定义可以使用 Ansible 和模板生成。我可以简单地将参数修改-f/etc/haproxy/*.cfg吗?或者我应该将其指向专用目录,例如/etc/haproxy/conf.d

还有一个附加问题。鉴于上述情况是可能的,并且我最终得到了多个 cfg 文件,其中包含不同的前端和后端节,我是否仍应在每个 cfg 文件中包括全局和默认节?

答案1

您可以指定 HAProxy 从目录加载配置,具体取决于您的 HAProxy 版本。此外,您无需在单独的配置文件中输入多个全局部分。

查看HAProxy 管理文档针对您的 HAProxy 服务器版本进行-f命令行切换。版本 1.6 及更低版本只能加载一个文件,但您可以添加多个-f <cfg1.cfg -f cfg2.cfg选项。例如,这是针对版本 1.8 的:

  -f <cfgfile|cfgdir> : adds <cfgfile> to the list of configuration files to be
    loaded. If <cfgdir> is a directory, all the files (and only files) it
    contains are added in lexical order (using LC_COLLATE=C) to the list of
    configuration files to be loaded ; only files with ".cfg" extension are
    added, only non hidden files (not prefixed with ".") are added.
    Configuration files are loaded and processed in their declaration order.
    This option may be specified multiple times to load multiple files. See
    also "--". The difference between "--" and "-f" is that one "-f" must be
    placed before each file name, while a single "--" is needed before all file
    names. Both options can be used together, the command line ordering still
    applies. When more than one file is specified, each file must start on a
    section boundary, so the first keyword of each file must be one of
    "global", "defaults", "peers", "listen", "frontend", "backend", and so on.
    A file cannot contain just a server list for example.

但要注意例如。多个默认部分工作

A "defaults" section sets default parameters for all other sections following
its declaration. Those default parameters are reset by the next "defaults"
section. 

相关内容