[resolved] Port labels not working intuitively

Here is a working wordpress/mysql set that goes through consul connect and works with -dev-connect.

job "wordpress" {
  datacenters = ["fin1"]

  group "database" {
    network {
      mode = "bridge"
    }

    service {
      name = "database"
      port = 3306
      tags = ["production", "mariadb"]

      connect {
        sidecar_service {}
      }
    }

    task "database" {
      driver = "docker"

      config {
        image = "mariadb"
      }

      env {
        MYSQL_RANDOM_ROOT_PASSWORD = "yes"
        MYSQL_INITDB_SKIP_TZINFO   = "yes"
        MYSQL_DATABASE             = "exampledb"
        MYSQL_USER                 = "exampleuser"
        MYSQL_PASSWORD             = "examplepass"
      }

      resources {
        cpu    = 100
        memory = 128
      }
    }
  }

  group "server" {
    network {
      mode = "bridge"

      port "http" {
        static = 8080
        to     = 80
      }
    }

    service {
      name = "wordpress"
      port = "8080"
      tags = ["production", "wordpress"]

      connect {
        sidecar_service {
          proxy {
            upstreams {
              destination_name = "database"
              local_bind_port  = 3306
            }
          }
        }
      }
    }


    task "server" {
      driver = "docker"

      config {
        image = "wordpress"
      }

      env {
        WORDPRESS_DB_HOST     = "${NOMAD_UPSTREAM_ADDR_database}"
        WORDPRESS_DB_USER     = "exampleuser"
        WORDPRESS_DB_PASSWORD = "examplepass"
        WORDPRESS_DB_NAME     = "exampledb"
      }

      resources {
        cpu    = 100
        memory = 64
      }
    }
  }
} 

Now, another one that does not work:

job "wordpress" {
  datacenters = ["fin1"]

  group "database" {
    network {
      mode = "bridge"
      port "db" {
        to = 3306
      }
    }

    service {
      name = "database"
      port = "db"
      tags = ["production", "mariadb"]

      connect {
        sidecar_service {}
      }
    }

    task "database" {
      driver = "docker"

      config {
        image = "mariadb"
      }

      env {
        MYSQL_RANDOM_ROOT_PASSWORD = "yes"
        MYSQL_INITDB_SKIP_TZINFO   = "yes"
        MYSQL_DATABASE             = "exampledb"
        MYSQL_USER                 = "exampleuser"
        MYSQL_PASSWORD             = "examplepass"
      }

      resources {
        cpu    = 100
        memory = 128
      }
    }
  }

  group "server" {
    network {
      mode = "bridge"

      port "http" {
        static = 8080
        to     = 80
      }
    }

    service {
      name = "wordpress"
      port = "8080"
      tags = ["production", "wordpress"]

      connect {
        sidecar_service {
          proxy {
            upstreams {
              destination_name = "database"
              local_bind_port  = 3306
            }
          }
        }
      }
    }


    task "server" {
      driver = "docker"

      config {
        image = "wordpress"
      }

      env {
        WORDPRESS_DB_HOST     = "${NOMAD_UPSTREAM_ADDR_database}"
        WORDPRESS_DB_USER     = "exampleuser"
        WORDPRESS_DB_PASSWORD = "examplepass"
        WORDPRESS_DB_NAME     = "exampledb"
      }

      resources {
        cpu    = 100
        memory = 64
      }
    }
  }
} 

For sake of readability, the only change affects the database. In particular, the non working example uses:

    network {
      mode = "bridge"
      port "db" {
        to = 3306
      }
    }

    service {
      name = "database"
      port = "db"
      tags = ["production", "mariadb"]

      connect {
        sidecar_service {}
      }
    }

while the working one has:

    network {
      mode = "bridge"
    }

    service {
      name = "database"
      port = 3306
      tags = ["production", "mariadb"]

      connect {
        sidecar_service {}
      }
    }

Why is this?

Seems related to Consul Connect service health checks not accessible? · Issue #9907 · hashicorp/nomad · GitHub

1 Like

Thanks for the link!