iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
DevOps

Hashicorp Jot Notes系列 第 5

Day 5. Hashicorp Nomad: How to configure one service advertise multiple ports

Hashicorp Nomad: How to configure one service advertise multiple port

之前在Hashicorp forum看到一個發問 Can a service advertise multiple ports? 所以記錄下來。

情境

User在一個task裡使用2個port, port name分別是http, metrics, 再向Consul註冊一個service, service name為awesome-api, 想要達到2個port註冊相同的service,

一般來說會為註冊不同的service name如:

...
service {
      name    = "awesome-api-http"
      port    = "http"
    }
    
service {
      name    = "awesome-api-metrics"
      port    = "metrics"
    }
...

User的寫法如下:

...
network {
      port "http"    { host_network = "private" }
      port "metrics" {
        host_network = "private"
        static       = "9090"
      }
      //port "metrics" { host_network = "private" }
    }
    
service {
      name    = "awesome-api"
      port    = "http"
      //port  = "metrics"
    }
...

把註解拿掉會報錯

Dimension network: reserved port collision metrics=9090 exhausted on [x] nodes

解法

有網友回復可以使用tag來達到這需求:

    service {
      name = "awesome-api"
      port = "http"
      tags = ["http"]
    }

    service {
      name = "awesome-api"
      port = "metrics"
      tags = ["metrics"]
    }

訪問的endpoint會變成 http.awesome-api.service.consulmetrics.awesome-api.service.consul
這樣即可達成一個service多個port的需求


上一篇
Day 4. Hashicorp Nomad: resources
下一篇
Day 6. Hashicorp Nomad: Submit a Job
系列文
Hashicorp Jot Notes30

尚未有邦友留言

立即登入留言