nomad job spec 'count', how to reference in instance?

609 views
Skip to first unread message

Greg Fausak

unread,
Sep 13, 2018, 3:48:07 PM9/13/18
to Nomad
Probably a dumb question :-) If I am running a job with count=2, from the job instance point of view, how do i know which job i am (1 or 2)??

-g

Sébastien Portebois

unread,
Sep 13, 2018, 4:20:32 PM9/13/18
to Nomad
Hi Greg

I'm not sure about the job count. What I use is the group count (how many replicas of this group should run)
For that group, each group allocation got an index, which you can access from the NOMAD_ALLOC_INDEX env variable. 

Here's a simple http-echo job which would echo them (because I'm too lazy to use an exec then fetch the logs)

job "show-count" {

  type = "service"

  datacenters = ["us-east-1a", "us-east-1b"]

  group "show-count" {
    count = 2

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

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

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

      resources {
        network {
          mbits = 1

          port "http" {
            static = 80
          }
        }
      }

      service {
        name = "show-count"
        port = "http"
      }
    }
  }
}

Greg Fausak

unread,
Sep 13, 2018, 8:22:01 PM9/13/18
to Nomad
Perfect! thx,
-g
Reply all
Reply to author
Forward
0 new messages