using consul to configure task in nomad

424 views
Skip to first unread message

dma...@istreamplanet.com

unread,
Sep 11, 2018, 3:09:06 PM9/11/18
to Nomad
This seems like a pretty basic use case, so I'm surprised I haven't found documentation for it yet.  Sorry if this is something covered elsewhere.

I have a job with two tasks where one needs the other to work.  One is kafka-manager which needs zookeeper to work.  I've got zookeeper registered with consul.  How do I tell nomad to use the info from consul during the launch of kafka-manager?  EG how do I zookeeper service info from consul into the nomad env section?

Thanks!
-Dylan 

kunal kaul

unread,
Sep 11, 2018, 3:14:34 PM9/11/18
to Nomad
You can use consultemplate notation in the template stanza of the nomad job file to get the info into a file which would be available to your application to read.

Sébastien Portebois

unread,
Sep 12, 2018, 9:34:41 AM9/12/18
to Nomad
Hi Dylan

The template stanza, relying on Consul Template, seems to be your exact use case. (Look for {{ key ..}} sample in the docs)
Here's a job using the sample http-echo server and Consul to return the content of the configs/test-echo/test key stored in Consul:

job "echo-consul" {
  type = "service"
  datacenters = ["dc1"]

  group "echo" {
    count = 1

    task "http-server" {

      driver = "docker"
      config {
        image = "hashicorp/http-echo:latest"

        args = [
          "-text", "'hello '${ECHO_VALUE}'",
          "-listen", ":${NOMAD_PORT_http}",
        ]

        port_map {
          http = "${NOMAD_PORT_http}"
        }
      }

      resources {
        network {
           port "http" {
            static = 80
          }
        }
      }

      template {
        env         = true
        data = <<EOH
ECHO_VALUE="{{key "configs/test-echo/test"}}"
EOH
        destination = "secrets/config.env"
        change_mode = "restart"
      }


      service {
        port = "http"

dma...@istreamplanet.com

unread,
Sep 12, 2018, 1:11:18 PM9/12/18
to Nomad
Awesome!  Thanks!  Does nomad know to restart the dependant service if the values in consul change?  Does it know to hold off starting if the consul value isn't there?  Thanks again!

Sébastien Portebois

unread,
Sep 12, 2018, 1:24:59 PM9/12/18
to Nomad
Yes, template watches for changes in the Consul KV store.
In fact that's why I left the explicit  change_mode = "restart" (which is the default, so this isn't required, but it makes the restart trigger obvious). You can also have a look at the splay argument (to control how fast it reacts to changes). With this example, it's quite easy to test it with a simple curl in a loop to query your echo server, and in the meantime you update the value in Consul, and you can observe how the allocations change, or what is the returned value by the small echo service (i.e. when the allocation of the update is running)

Re the missing value: the job will be "blocked" in pending mode because of the missing value 
IN Nomad UI, for this new allocation you'll see in the recent event a 
Template   => Missing: kv.block(missing)
event entry

Which you'll also see with the the nomad alloc status: (bold and underline added for emphasis)

$ nomad alloc status e68373f4
ID                  = e68373f4
Eval ID             = e4d63ffe
Name                = echo-consul.echo[0]
Node ID             = bbd6bc91
Job ID              = echo-consul
Job Version         = 1
Client Status       = pending
Client Description  = <none>
Desired Status      = run
Desired Description = <none>
Created             = 2m36s ago
Modified            = 2m24s ago

Task "http-server" is "pending"
Task Resources
CPU      Memory   Disk     IOPS  Addresses
100 MHz  300 MiB  300 MiB  0     http: 10.255.103.106:80

Task Events:
Started At     = N/A
Finished At    = N/A
Total Restarts = 0
Last Restart   = N/A

Recent Events:
Time                       Type        Description
2018-09-12T13:21:04-04:00  Template    Missing: kv.block(missing)
2018-09-12T13:21:01-04:00  Task Setup  Building Task Directory
2018-09-12T13:21:01-04:00  Received    Task received by client

dma...@istreamplanet.com

unread,
Sep 12, 2018, 3:17:16 PM9/12/18
to Nomad
Perfect!  Thanks so much!


On Tuesday, September 11, 2018 at 12:09:06 PM UTC-7, dma...@istreamplanet.com wrote:
Reply all
Reply to author
Forward
0 new messages