0

I have multiple microservices running on my AWS ECS Fargate cluster. Inside a task (pod), there will be multiple containers (1 container for core-business service and additional 3 sidecar containers). Here are the list of those containers:

  • core-business service container (runs the core business service on specific port)
  • consul-agent container (runs consul-agent and joins it with the consul-master)
  • consul-template container (gets the service information from consul and updates the haproxy.cfg)
  • haproxy container (takes the haproxy.cfg from consul-template and runs)

All these containers are up and running fine as well. The problem is to reload the haproxy. Since the consul-template is responsible for updating the haproxy.cfg file, Should I need to add some configuration on consul-template itself to update the haproxy?

Here is the command I am currently using for 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

What can I try to achieve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Deependra Dangal
  • 1,145
  • 1
  • 13
  • 36

1 Answers1

1

I am currently having the exact same issue, but for Nginx.

From my research, there is no good solution. The one that seems to work is to mount the volume

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

on the consul-template container.

I see you are using :

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

in order to run a command after the consul-template renders a new config file, use something like:

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

HINT

I find it more readable to use consul-template config files, the language is very well specified here: https://github.com/hashicorp/consul-template/blob/master/docs/configuration.md .

Using that approach you would have a config file (for example consul_template_config.hcl) for consul-template like:

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"
}

Then, you run the consul-template container using

consul-template -config /path/to/consul_template_config.hcl

or using

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)

I know it is not a good solution to use this docker volume (security warnings), but it was what I could find about our use case.

dans4b
  • 11
  • 3
  • Noob question: How to trigger this `consul-template -config /path/to/consul_template_config.hcl` if one of the service goes down ? – Chang Zhao Nov 11 '21 at 18:57
  • Not noob at all, we are all learning ! 2 options: 1) Setup a Consul Watcher so that when something in the Consul cluster changes, you trigger the consul-template command (https://www.consul.io/docs/dynamic-app-config/watches). This should require minimal setup, since consul Watches are already part of the Consul Agents and Servers. The setup is similar to setting up an healthcheck. 2) Your command actually already does that, it starts consul-template process and listens to the Consul cluster, according to the changes on the *.hcl file. Actually I am using this option inside an OS service. – dans4b Nov 27 '21 at 10:01