Dynamic port mapping with consul connect

I was following the demo nomad-connect from the documentation

but I couldn’t make it work with assigning dynamic ports to my containers, the above config only works for 1 container, what if I need hundreds of containers, I will need a dynamic port assigning which nomad does perfectly but, using the connect config, it doesn’t allow me.

I am getting the following error

Recent Events:
Time                  Type            Description
2019-10-06T18:08:22Z  Killing         Sent interrupt. Waiting 5s before force killing
2019-10-06T18:08:22Z  Not Restarting  Error was unrecoverable
2019-10-06T18:08:22Z  Driver Failure  Failed to create container configuration for image "crizstian/payment-service-go:v0.1" ("sha256:d21a9f457bbcb1cf94d4c1817ef9ef57e8066c747a93e78f1fc6d85f722c9c84"): Trying to map ports but no network interface is available

I have also tried with this config

job "countdash" {
  datacenters = ["dc1-ncv"]
  region      = "dc1-region"
  type        = "service"

   group "api" {
    count = 3

     network {
       mode = "bridge"
     }

     service {
       name = "count-api"
       port = "9001"

       connect {
         sidecar_service {}
       }
     }

     task "web" {
       driver = "docker"
       config {
         image = "hashicorpnomad/counter-api:v1"
       }

       resources {
        cpu    = 50
        memory = 10
      }
     }
   }

   group "dashboard" {
    count = 3
  
     network {
       mode = "bridge"
       port "http" {
       #  static = 9002
       #  to     = 9002
       }
     }

     service {
       name = "count-dashboard"
       port = "http"

       connect {
         sidecar_service {
           proxy {
             upstreams {
               destination_name = "count-api"
               local_bind_port = 8080
             }
           }
         }
       }
     }

     task "dashboard" {
       driver = "docker"
       env {
         COUNTING_SERVICE_URL = "http://${NOMAD_UPSTREAM_ADDR_count_api}"
       }
       config {
          image = "hashicorpnomad/counter-dashboard:v1"
          port_map {
            http = 3000
          }
       }

        resources {
          cpu    = 50
          memory = 10
        }
      }
   }
 }

and what I have found is that using port_map is not able to allocated

Recent Events:
Time                  Type            Description
2019-10-06T19:06:28Z  Killing         Sent interrupt. Waiting 5s before force killing
2019-10-06T19:06:28Z  Not Restarting  Error was unrecoverable
2019-10-06T19:06:28Z  Driver Failure  Failed to create container configuration for image "hashicorpnomad/counter-dashboard:v1" ("sha256:729a950cfecc3f00d00013e90d9d582a6c72fc5806376b1a342c34270ffd8113"): Trying to map ports but no network interface is available
2019-10-06T19:06:28Z  Task Setup      Building Task Directory
2019-10-06T19:06:27Z  Received        Task received by client

I ran into a similar issue. Try adding a network to your resource stanza.

To make this work with dynamic ports, simply remove the static line, but leave the to line

Before

     network {
       mode = "bridge"
       port "http" {
        #static = 9002 Removed
        to     = 9002
       }
     }

This will let the job use dynamic ports. You can then use a consul aware loadbalancer like fabio/nginx/haproxy to route to the services like normal.

1 Like

Hey @spuder thanks for the response

now it is working