Consul-template: Query multiple DataCenters

Created on 24 Aug 2015  路  8Comments  路  Source: hashicorp/consul-template

Our Consul deployment spread across 4 different DataCenters but I don't see a way to have consul-template to query multiple instances of Consul that would be needed to configure an application that has to load balance across DataCenters, am I missing something?

Most helpful comment

Thanks!
I did find printf was giving me some extra quotes but after looking in the template package doc I found print does what I need:

{{range $dc := datacenters}}{{$dc}}
{{$service := print "<service name>@" $dc}}{{range service $service"}}{{.Node}}
{{end}}{{end}}

All 8 comments

Hi @Telmo

Are your data centers linked via WAN? It's possible to query services in multiple data centers, but it is not possible to query multiple KV stores since each KV store is data-center specific. We recommend using something like consul-replicate if you want to replicate KV across multiple data centers.

Does that make sense?

I am only trying to query the services, not the KV store, all my datacenters are linked through WAN but I only get the list of services for a given datacenter.

[root@docker-wc-1p ~]# consul members -wan
Node                               Address              Status  Type    Build  Protocol  DC
docker-as-3p.as    <ip>:8302   alive   server  0.5.2  2         as
docker-wc-1p.wc   <ip>:8302    alive   server  0.5.2  2         wc
docker-ch2-3p.ch2  <ip>:8302  alive   server  0.5.2  2         ch2
docker-as-2p.as    <ip>:8302   alive   server  0.5.2  2         as
docker-as-1p.as    <ip>:8302   alive   server  0.5.2  2         as
docker-wc-3p.wc    <ip>:8302    alive   server  0.5.2  2         wc
docker-ch2-1p.ch2  <ip>:8302  alive   server  0.5.2  2         ch2
docker-ch2-2p.ch2  <ip>:8302   alive   server  0.5.2  2         ch2
docker-wc-2p..wc    <ip>:8302    alive   server  0.5.2  2         wc

However the query for services only returns services for a given DC.

curl docker-as-4p:8500/v1/catalog/services
{"autofluentd-24220":[],"autofluentd-8888":[],"consul":[],"foreman":[],"weave-6783":["udp"],"weavedns":["udp"]}

curl docker-wc-1p:8500/v1/catalog/services
{"consul":[],"magneto":["puppetmaster","3.3.1"],"magneto38":["puppetmaster","magneto38"]}

Is there any specific config option I have to pass to consul-template to query all services?

Hi @Telmo

Have a look at the service documentation in the README: https://github.com/hashicorp/consul-template#service.

I think you want to pass the @datacenter attribute when querying for a service. By default, it will only return services in the current datacenter. Please note there is no way to query _all_ datacenters at this time, but you can get around that by iterating over each datacenter using datacenters and then find the services in that datacenter.

Does that answer your question?

this is exactly what I needed thanks! Is there a list of fields that can be used? I am having a difficult time accessing the ServiceAddress field (different from Address)

I'm probably just missing something in the documentation, but is there a way to pass the datacenter to the service when iterating through datacenters?

For example I want to list each datacenter, and then list each node for a given service in those datacenters. In a single datacenter this works fine:

{{range service "<service name>@<datacenter name>"}}
{{.Node}}
{{end}}

but I can't find a working syntax for something like this:

{{range $dc := datacenters}}
{{$dc}}
{{range service "<service name>@{{$dc}}"}}
{{.Node}}
{{end}}
{{end}}

@moss04 you will need to use printf: https://golang.org/pkg/text/template/

Thanks!
I did find printf was giving me some extra quotes but after looking in the template package doc I found print does what I need:

{{range $dc := datacenters}}{{$dc}}
{{$service := print "<service name>@" $dc}}{{range service $service"}}{{.Node}}
{{end}}{{end}}
Was this page helpful?
0 / 5 - 0 ratings