Consul Service Data in Nomad Job

961 views
Skip to first unread message

dma...@istreamplanet.com

unread,
May 8, 2018, 3:12:50 PM5/8/18
to Nomad
Sorry if this is obvious.  I thought it would be but I haven't been able to find the answer yet.

I've got two services in docker, kafka & zookeeper,  and one (kafka) needs to know the IP address & port number of the other (zookeeper).  The kafka container expects the zookeeper details as environment variables.  I figure the zookeeper details are already in consul, so I should be able to feed that into the env vars in the job file.  Something like

env {
  KAFKA_ZK_HOST = "$consul.service.zookeeper.host"
  KAFKA_ZK_PORT = "$consul.service.zookeeper.port"
  ...
}

Am I totally barking up the wrong tree?

How do I get details about one job/task/allocation into the definition of another job/task?

Thanks!
-Dylan

Justin DynamicD

unread,
May 8, 2018, 9:46:50 PM5/8/18
to Nomad
What you want here is a template.  This basically leverages consul-template to create your variables for you ... and as consul-template can do consul lookups ... you're golden.  Try a stanza like this:

template {
data = <<EOH
KAFKA_ZK_HOST="{{range service "zookeeper"}}{{.Address}}{{end}}"
KAFKA_ZK_PORT="{{range service "zookeeper"}}{{.Port}}{{end}}"
EOH
destination = "secrets/file.env"
env = true
}

dma...@istreamplanet.com

unread,
May 9, 2018, 12:15:58 PM5/9/18
to Nomad
Cool!  Thanks!

Are you saying I use consul-template to build my job file?  And consul-template kicks the job if it detects a change?  Or are you saying if I put your example in my job file, nomad will know what it means?  If the latter, where can I find reference documentation to help me learn how that works?

Thanks again!
-Dylan

Justin DynamicD

unread,
May 9, 2018, 3:47:58 PM5/9/18
to Nomad
I'm saying the later:  nomad will know what that means.  It basically leverages consul-template to get the work done.  Basics can be found here:

https://www.nomadproject.io/docs/job-specification/template.html

Thing to keep in mind is this is basically using the consul-template code to generate the template data.  Because of that I've found reading the consul-template details to be useful:


Hope this helps.

dma...@istreamplanet.com

unread,
May 9, 2018, 6:40:39 PM5/9/18
to Nomad
Awesome!  Thanks!
Reply all
Reply to author
Forward
0 new messages