【问题标题】:Unable to access services running on Hashicorp Nomad via browser无法通过浏览器访问在 Hashicorp Nomad 上运行的服务
【发布时间】:2021-09-02 13:41:37
【问题描述】:

我在我的 MacOS 上的 Nomad dev 上开始了两个 Nomad 工作,一个用于 PostgreSQL,另一个用于 pgAdmin。工作规范如下:

## postgres.nomad
job "postgres" {
  datacenters = ["dc1"]
  type = "service"

  group "postgres" {
    count = 1

    task "postgres" {
      driver = "docker"
      config {
        image = "postgres"
        network_mode = "host"
        port_map {
          db = 5432
        }

      }
      env {
          POSTGRES_USER="postgres"
          POSTGRES_PASSWORD="postgres"
      }

      logs {
        max_files     = 5
        max_file_size = 15
      }

      resources {
        cpu = 1000
        memory = 1024
        network {
          mbits = 10
          port  "db"  {
            static = 5432
          }
        }
      }
      service {
        name = "postgres"
        tags = ["postgres for vault"]
        port = "db"

        check {
          name     = "alive"
          type     = "tcp"
          interval = "10s"
          timeout  = "2s"
        }
      }
    }
    restart {
      attempts = 10
      interval = "5m"
      delay = "25s"
      mode = "delay"
    }

  }

  update {
    max_parallel = 1
    min_healthy_time = "5s"
    healthy_deadline = "5m"
    auto_revert = false
    canary = 0
  }
}
## pgadmin.nomad
job "pgadmin4" {
  datacenters = ["dc1"]
  type = "service"

  group "pgadmin4" {
    count = 1

    task "pgadmin4" {
      driver = "docker"
      config {
        image = "dpage/pgadmin4"
        network_mode = "host"
        port_map {
          db = 8080
        }
        volumes = [
          "local/servers.json:/servers.json",
          "local/servers.passfile:/root/.pgpass"
        ]

      }
      template {
        perms = "600"
        change_mode = "noop"
        destination = "local/servers.passfile"
        data = <<EOH
postgres.service.consul:5432:postgres:postgres:postgres
EOH
      }
      template {
        change_mode = "noop"
        destination = "local/servers.json"
        data = <<EOH
{
  "Servers": {
    "1": {
      "Name": "Local Server",
      "Group": "Server Group 1",
      "Port": "5432",
      "Username": "root",
      "PassFile": "/root/.pgpass",
      "Host": "postgres.service.consul",
      "SSLMode": "disable",
      "MaintenanceDB": "postgres"
    }
  }
}
EOH
      }
      env {
        PGADMIN_DEFAULT_EMAIL="youremail@yourdomain.com"
        PGADMIN_DEFAULT_PASSWORD="yoursecurepassword"
        PGADMIN_LISTEN_PORT="5050"
        PGADMIN_CONFIG_ENHANCED_COOKIE_PROTECTION="False"
        PGADMIN_SERVER_JSON_FILE="/servers.json"
      }

logs {
        max_files     = 5
        max_file_size = 15
      }

      resources {
        cpu = 1000
        memory = 1024
        network {
          mbits = 10
          port  "ui"  {
            static = 5050
          }
        }
      }
      service {
        name = "pgadmin"
        tags = [ "urlprefix-/pgadmin strip=/pgadmin"]
        port = "ui"

        check {
          name     = "alive"
          type     = "tcp"
          interval = "10s"
          timeout  = "2s"
        }
      }
    }
    restart {
      attempts = 10
      interval = "5m"
      delay = "25s"
      mode = "delay"
    }

  }

  update {
    max_parallel = 1
    min_healthy_time = "5s"
    healthy_deadline = "3m"
    auto_revert = false
    canary = 0
  }
}

所有作业都已成功部署并处于运行状态。

从作业规范中可以看出,pgadmin 应该在 localhost:5050 中运行,但每当我尝试在浏览器中访问此地址时,我都会收到“无法连接到服务器”错误。是否缺少任何配置?

【问题讨论】:

    标签: nomad hashicorp


    【解决方案1】:
    1. 您暴露了错误的端口名称。
    2. 现在,网络只在job -&gt; group -&gt; network可用,见here
    3. 如果您只想公开单个端口,请避免使用network_mode = "host"

    这里是可用的配置

    job "postgres" {
      datacenters = ["dc1"]
      type = "service"
    
      group "postgres" {
        count = 1
    
        task "postgres" {
          driver = "docker"
          config {
            image = "postgres"
            ports = ["db"]
          }
          env {
              POSTGRES_USER="postgres"
              POSTGRES_PASSWORD="postgres"
          }
    
          logs {
            max_files     = 5
            max_file_size = 15
          }
    
          resources {
            cpu = 1000
            memory = 1024
          }
          service {
            name = "postgres"
            tags = ["postgres for vault"]
            port = "db"
    
            check {
              name     = "alive"
              type     = "tcp"
              interval = "10s"
              timeout  = "2s"
            }
          }
        }
        restart {
          attempts = 10
          interval = "5m"
          delay = "25s"
          mode = "delay"
        }
        network {
          mbits = 10
          port  "db"  {
            static = 5432
          }
        }
      }
      update {
        max_parallel = 1
        min_healthy_time = "5s"
        healthy_deadline = "5m"
        auto_revert = false
        canary = 0
      }
    }
    
    job "pgadmin4" {
      datacenters = ["dc1"]
      type = "service"
    
      group "pgadmin4" {
        count = 1
    
        task "pgadmin4" {
          driver = "docker"
          config {
            image = "dpage/pgadmin4"
            ports = ["ui"]
            volumes = [
              "local/servers.json:/servers.json",
              "local/servers.passfile:/root/.pgpass"
            ]
    
          }
          template {
            perms = "600"
            change_mode = "noop"
            destination = "local/servers.passfile"
            data = <<EOH
    postgres.service.consul:5432:postgres:postgres:postgres
    EOH
          }
          template {
            change_mode = "noop"
            destination = "local/servers.json"
            data = <<EOH
    {
      "Servers": {
        "1": {
          "Name": "Local Server",
          "Group": "Server Group 1",
          "Port": "5432",
          "Username": "root",
          "PassFile": "/root/.pgpass",
          "Host": "postgres.service.consul",
          "SSLMode": "disable",
          "MaintenanceDB": "postgres"
        }
      }
    }
    EOH
          }
          env {
            PGADMIN_DEFAULT_EMAIL="youremail@yourdomain.com"
            PGADMIN_DEFAULT_PASSWORD="yoursecurepassword"
            PGADMIN_LISTEN_PORT="5050"
            PGADMIN_CONFIG_ENHANCED_COOKIE_PROTECTION="False"
            PGADMIN_SERVER_JSON_FILE="/servers.json"
          }
    
    logs {
            max_files     = 5
            max_file_size = 15
          }
    
          resources {
            cpu = 1000
            memory = 1024
          }
          service {
            name = "pgadmin"
            tags = [ "urlprefix-/pgadmin strip=/pgadmin"]
            port = "ui"
    
            check {
              name     = "alive"
              type     = "tcp"
              interval = "10s"
              timeout  = "2s"
            }
          }
        }
        restart {
          attempts = 10
          interval = "5m"
          delay = "25s"
          mode = "delay"
        }
        network {
          mbits = 10
          port  "ui"  {
            static = 5050
          }
        }
      }
    
      update {
        max_parallel = 1
        min_healthy_time = "5s"
        healthy_deadline = "3m"
        auto_revert = false
        canary = 0
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 1970-01-01
      • 2013-02-14
      • 2015-10-18
      • 2013-09-28
      • 1970-01-01
      • 2015-06-27
      • 2018-04-01
      • 2017-06-01
      相关资源
      最近更新 更多