好吧,在 nomad 中运行 traefik 和容器之间的负载平衡的最合适的选项是使用consul 目录(服务发现所必需的)。
要运行它,您必须在启动 nomad 时配置consule 连接。如果您想在本地进行测试,只需运行sudo nomad agent -dev-connect
. Consul 可以从consul agent -dev -client="0.0.0.0"
.
现在您可以简单地使用标签提供您的 traefik 配置,如此处所示。
如果您确实需要(这肯定会在集群设置中导致问题)使用 docker 提供程序在 nomad 中运行 traefik,您可以执行以下操作:
首先,您需要在 docker 插件中启用主机路径挂载。看到这个和这个。您可以将配置放在一个额外的文件中,例如extra.hcl,如下所示:
plugin "docker" {
config {
volumes {
enabled = true
}
}
}
现在你可以用这个额外的设置开始 nomad 了sudo nomad agent -dev-connect -config=extra.hcl
。现在您可以在config/labels块中提供您的 traefik 设置,例如(完整):
job "traefik" {
region = "global"
datacenters = ["dc1"]
type = "service"
group "traefik" {
count = 1
task "traefik" {
driver = "docker"
config {
image = "traefik:v2.3"
//network_mode = "host"
volumes = [
"local/traefik.yaml:/etc/traefik/traefik.yaml",
"/var/run/docker.sock:/var/run/docker.sock"
]
labels {
traefik.enable = true
traefik.http.routers.from-docker.rule = "Host(`docker.loris.mydomain`)"
traefik.http.routers.from-docker.entrypoints = "web"
traefik.http.routers.from-docker.service = "api@internal"
}
}
template {
data = <<EOF
log:
level: DEBUG
entryPoints:
traefik:
address: ":8080"
web:
address: ":80"
api:
dashboard: true
insecure: true
accessLog: {}
providers:
docker:
exposedByDefault: false
consulCatalog:
prefix: "traefik"
exposedByDefault: false
endpoint:
address: "10.0.0.20:8500"
scheme: "http"
datacenter: "dc1"
EOF
destination = "local/traefik.yaml"
}
resources {
cpu = 100
memory = 128
network {
mbits = 10
port "http" {
static = 80
}
port "traefik" {
static = 8080
}
}
}
service {
name = "traefik"
tags = [
"traefik.enable=true",
"traefik.http.routers.from-consul.rule=Host(`consul.loris.mydomain`)",
"traefik.http.routers.from-consul.entrypoints=web",
"traefik.http.routers.from-consul.service=api@internal"
]
check {
name = "alive"
type = "tcp"
port = "http"
interval = "10s"
timeout = "2s"
}
}
}
}
}
(可能有一个设置要绑定到0.0.0.0
我在我定义的那些域/etc/hosts
指向我的主接口 IP)。
您可以使用修改后的 webapp 规范对其进行测试(我不知道如何正确映射端口,例如container:80 -> host:<random>
,但我认为这足以说明它变得多么复杂 :)):
job "demo-webapp" {
datacenters = ["dc1"]
group "demo" {
count = 3
task "server" {
env {
// "${NOMAD_PORT_http}"
PORT = "80"
NODE_IP = "${NOMAD_IP_http}"
}
driver = "docker"
config {
image = "hashicorp/demo-webapp-lb-guide"
labels {
traefik.enable = true
traefik.http.routers.webapp-docker.rule = "Host(`docker.loris.mydomain`) && Path(`/myapp`)"
traefik.http.services.webapp-docker.loadbalancer.server.port = 80
}
}
resources {
network {
// Used for docker provider
mode ="bridge"
mbits = 10
port "http"{
// Used for docker provider
to = 80
}
}
}
service {
name = "demo-webapp"
port = "http"
tags = [
"traefik.enable=true",
"traefik.http.routers.webapp-consul.rule=Host(`consul.loris.mydomain`) && Path(`/myapp`)",
]
check {
type = "http"
path = "/"
interval = "2s"
timeout = "2s"
}
}
}
}
}
我希望这能以某种方式回答你的问题。