为haproxy负载均衡器提供多个cfg文件

Tom*_*Tom 4 load-balancing haproxy

我的 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 文件中包含全局和默认节?

小智 5

您可以指示 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.
Run Code Online (Sandbox Code Playgroud)

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

A "defaults" section sets default parameters for all other sections following
its declaration. Those default parameters are reset by the next "defaults"
section. 
Run Code Online (Sandbox Code Playgroud)