【问题标题】:Haproxy reload from consul-template从领事模板重新加载 Haproxy
【发布时间】:2021-07-31 18:42:00
【问题描述】:

我的 AWS ECS Fargate 集群上运行了多个微服务。在一个任务(pod)内部,将有多个容器(1 个用于核心业务服务的容器和另外 3 个边车容器)。以下是这些容器的列表:

  • 核心业务服务容器(在特定端口上运行核心业务服务)
  • consul-agent 容器(运行 consul-agent 并将其与 consul-master 连接)
  • consul-template容器(从consul获取服务信息并更新haproxy.cfg)
  • haproxy 容器(从 consul-template 获取 haproxy.cfg 并运行)

所有这些容器都已启动并运行良好。问题是重新加载haproxy。由于 consul-template 负责更新 haproxy.cfg 文件,我是否需要在 consul-template 本身上添加一些配置来更新 haproxy?

这是我目前用于 consul-template 的命令:

consul-template -consul-addr=xx.xx.xx.xx:8500 -template /etc/consul-template/data/haproxy.cfg.ctmpl:/etc/consul-template/data/haproxy.cfg

我可以尝试什么来实现这一目标?

【问题讨论】:

    标签: docker kubernetes haproxy consul consul-template


    【解决方案1】:

    我目前遇到了完全相同的问题,但对于 Nginx。

    根据我的研究,没有好的解决方案。 似乎可行的方法是挂载卷

    -v /var/run/docker.sock:/var/run/docker.sock
    

    在领事模板容器上。

    我看到你正在使用:

    consul-template -consul-addr=xx.xx.xx.xx:8500 -template /etc/consul-template/data/haproxy.cfg.ctmpl:/etc/consul-template/data/haproxy.cfg
    

    为了在 consul-template 渲染一个新的配置文件后运行命令,使用类似:

    consul-template -consul-addr=xx.xx.xx.xx:8500 -template /etc/consul-template/data/haproxy.cfg.ctmpl:/etc/consul-template/data/haproxy.cfg:docker run haproxy-container command-to-reload-haproxy
    

    提示

    我发现使用 consul-template 配置文件更具可读性,这里的语言非常明确: https://github.com/hashicorp/consul-template/blob/master/docs/configuration.md.

    使用这种方法,您将拥有一个用于 consul-template 的配置文件(例如 consul_template_config.hcl),例如:

    consul {
      address = "consul-address:8500"
    }
    
    template {
      source = "/etc/consul-template/data/haproxy.cfg.ctmpl"
      destination = "/etc/consul-template/data/haproxy.cfg"
      command = "docker run haproxy-container command-to-reload-haproxy"
    }
    

    然后,您使用

    运行 consul-template 容器
    consul-template -config /path/to/consul_template_config.hcl
    

    或使用

    consul-template -config /path/to/folder/containing->consul_template_config.hcl (This approach is more advanced, and lets you have consul-template-configs across multiple files, and not only in 1 like consul_template_config.hcl)
    

    我知道使用这个 docker 卷(安全警告)不是一个好的解决方案,但这是我能找到的关于我们用例的内容。

    【讨论】:

    • 菜鸟问题:如果其中一项服务出现故障,如何触发consul-template -config /path/to/consul_template_config.hcl
    • 根本不是菜鸟,我们都在学习! 2 个选项:1) 设置一个 Consul Watcher,以便当 Consul 集群中的某些内容发生变化时,您会触发 consul-template 命令 (consul.io/docs/dynamic-app-config/watches)。这应该需要最少的设置,因为 consul Watches 已经是 Consul 代理和服务器的一部分。该设置类似于设置健康检查。 2)您的命令实际上已经这样做了,它会根据 *.hcl 文件的更改启动 consul-template 进程并监听 Co​​nsul 集群。实际上我在 OS 服务中使用了这个选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2012-08-05
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多