Consul service registration

If I have two tasks in a job group how can I access the consul ID of each other services?

In other words: how can I create two tasks within a job, one java and one exec. In the exec one be able to run:
consul connect envoy --sidecar-for 123

Being 123 the consul service ID of the java task.

Until 0.9.1 I could query consul agent/services until some service with the ${NOMAD_PORT_javaservice_http} was registered. Now it seems that consul registration only happens after the whole group if running or some other state.

Thanks,
Rafael.

Hi Rafael,

The way I did service registration with envoy sidecar proxy in Nomad for my tasks is by having a second task that runs envoy and bootstraps it using consul connect. And then a 3rd task that registers it.

This contains a few work arounds for the way Nomad and Consul interact at the moment.

job "example" {
  datacenters = ["dc1"]
  type = "service"

  group "api" {
    count = 1

    restart {
      attempts = 2
      interval = "30m"
      delay = "15s"

      mode = "fail"
    }

    task "api" {
      driver = "exec"

      artifact {
        source = "https://example.com/api.zip"
      }

      config {
        command = "api"
        args = [
          "${NOMAD_ADDR_http}",
          "${NOMAD_ADDR_sidecar_upstream}"
        ]
      }

      env {
        PATH = "$PATH:${NOMAD_TASK_DIR}"
      }

      resources {
        cpu    = 500
        memory = 256
        network {
          mbits = 10
          port "http" {}
        }
      }
    }

    task "sidecar" {
      driver = "exec"

      config {
        command = "consul"
        args    = [
          "connect", "envoy",
          "-sidecar-for", "api-${NOMAD_ALLOC_ID}",
          "-admin-bind", "${NOMAD_ADDR_envoyadmin}"
        ]
      }

      artifact {
        source = "https://example.com/consul.zip"
      }

      artifact {
        source = "https://example.com/envoy.zip"
      }

      env {
        PATH="${PATH}:${NOMAD_TASK_DIR}"
      }

      resources {
        network {
          port "upstream" {}
          port "ingress" {}
          port "envoyadmin" {}
        }
      }
    }

    task "register" {
      driver = "exec"
      kill_timeout = "10s"

      artifact {
        source = "https://example.com/consul.zip"
      }

      config {
        command = "bash"
        args = [
          "local/init.sh"
        ]
      }

      env {
        PATH="${PATH}:${NOMAD_TASK_DIR}"
      }

      template {
        data = <<EOH
        {
          "service": {
            "name": "api",
            "ID": "api-{{ env "NOMAD_ALLOC_ID" }}",
            "port": {{ env "NOMAD_PORT_api_http" }},
            "connect": {
              "sidecar_service": {
                "port": {{ env "NOMAD_PORT_sidecar_ingress" }},
                "proxy": {
                  "local_service_address": "{{ env "NOMAD_IP_api_http" }}",
                  "upstreams": [
                    {
                      "destination_name": "database",
                      "local_bind_port": {{ env "NOMAD_PORT_sidecar_upstream"}}
                    }
                  ]
                }
              }
            }
          }
        }
        EOH
        destination = "local/service.json"
      }

      template {
        data = <<EOH
        #!/bin/bash
        set -x
        consul services register local/service.json
        trap "consul services deregister local/service.json" INT
        tail -f /dev/null &
        PID=$!
        wait $PID
        EOH

        destination = "local/init.sh"
      }

      resources {
        memory = 100
      }
    }
  }
}