我一直在尝试将Traefik配置为我的dev docker swarm的反向代理,该swarm在与外部世界隔离的独立VPN上运行。 群由3个管理器节点和3个工作器节点组成,它们之间没有用于通信的TLS。 我设法正确设置了许多前端(带有用于主机的规则)和后端,这些前端指向群集中运行的服务。 下面是我的docker-compose文件,用于在集群上为traefik部署docker堆栈。
version: '3.6'
networks:
traefik:
driver: overlay
configs:
traefik:
file: ./config.dev.toml
services:
traefik:
image: traefik:1.7
command:
- --docker
- --docker.swarmMode
- --docker.domain=traefik.dev.pap
- --docker.exposedByDefault=false
- --entryPoints=Name:http Address::80 Redirect.EntryPoint:https
- --entryPoints=Name:https Address::443 TLS:/certs/certfile.cert,/certs/keyfile.key
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
configs:
- source: traefik
target: /traefik.toml
networks:
- traefik
ports:
- target: 80
published: 80
mode: host
- target: 443
published: 443
mode: host
- target: 8080
published: 8080
mode: host
- target: 8081
published: 8081
mode: host
- target: 8082
published: 8082
mode: host
deploy:
mode: global
placement:
constraints:
- node.role == manager
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
traefik的Toml文件
defaultEntryPoints = ["http", "https"]
logLevel = "WARN"
insecureSkipVerify = true
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.auth.basic]
users = ["traefik:$2y$05$ZF08gDSv0f88vEUVzmwuxu.9seYIAZVzTMXnrETBDhQT30dxu4vOu"]
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[[entryPoints.https.tls.certificates]]
certFile = """-----BEGIN CERTIFICATE-----
MIICsjCCAZoCCQDP9WReX5fgQjANBgkqhkiG9w0BAQsFADAbMRkwFwYDVQQDDBAq
dHJhZWZpay5kZXYucGFwMB4XDTE5MDUyNDIwMTYzMloXDTIwMDUyMzIwMTYzMlow
GzEZMBcGA1UEAwwQKnRyYWVmaWsuZGV2LnBhcDCCASIwDQYJKoZIhvcNAQEBBQAD
ggEPADCCAQoCggEBA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
xmBjApqWt1hm9c9lDZIqC0cX1/al6sgd2fZczFcyFN2dAOTneHCbr47FDvNniAO5
Bu+obbuxjALvjELvOr+63qm/43M8P8gvnNQYKHgoIQKBgQCQJrT3z27O/hRVUIGg
bwAmzwGExOC39uLHNWmOcVmw36oV592sVw1K9pRvPFVJp956T63lhcGv4t0ki2DE
gBN/V1jhkAw33qRDd5bbYmIutf6RO7HeewwFHpbaLw0FULe5UlsziE9AbTHmddqU
GTcyajXnqimXvSu6AC9XFA9s4g==
-----END PRIVATE KEY-----
"""
[entryPoints.api]
address = ":8081"
[entryPoints.ping]
address = ":8082"
[api]
entryPoint = "api"
dashboard = true
[ping]
entryPoint = "ping"
[retry]
logLevel = "DEBUG"
[traefikLog]
logLevel = "DEBUG"
format = "json"
[accessLog]
logLevel = "DEBUG"
format = "json"
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "traefik.dev.pbp"
swarmmode = true
watch = true
exposedByDefault = false
以上证书是使用以下命令在我的笔记本电脑上生成的:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./backend.key -out ./backend.cert
和公用名:*traefik.dev.pap
。
我要公开的2个服务具有以下标签:
labels:
- "traefik.enable=true"
- "traefik.docker.network=traefik_traefik"
# Public segment
- "traefik.public.protocol=h2c"
- "traefik.public.port=50051"
- "traefik.public.frontend.rule=Host:serviceA-grpc.traefik.dev.pbp"
- "traefik.public.frontend.auth.forward.tls.insecureSkipVerify=true"
- "traefik.public.backend.loadbalancer.method=drr"
- "traefik.public.frontend.passHostHeader=true"
labels:
- "traefik.enable=true"
- "traefik.docker.network=traefik_traefik"
# Public segment
- "traefik.public.protocol=http"
- "traefik.public.port=8080"
- "traefik.public.frontend.rule=Host:serviceB-gateway.traefik.dev.pbp"
- "traefik.public.frontend.passHostHeader=true"
- "traefik.public.backend.loadbalancer.method=drr"
第一个是我正在运行的grpc服务,第二个是grpc服务的HTTP网关。
第二个按预期方式工作。除了证书不安全之外,没有什么奇怪的了。
但是,我发现很难让Traefik公开grpc服务。
如果执行grpcurl -plaintext tag-service-grpc.traefik.dev.pbp:443 list
,我将得到结果Failed to list services: rpc error: code = Unknown desc = Internal Server Error
和Traefic日志是
{"BackendAddr":"","BackendName":"Traefik","BackendURL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"ClientAddr":"x.x.x.x:64391","ClientHost":"x.x.x.x","ClientPort":"64391","ClientUsername":"-","DownstreamContentSize":0,"DownstreamStatus":500,"DownstreamStatusLine":"500 Internal Server Error","Duration":426232,"FrontendName":"Auth for frontend-public-tag-service-public","OriginContentSize":0,"OriginDuration":222656,"OriginStatus":500,"OriginStatusLine":"500 Internal Server Error","Overhead":203576,"RequestAddr":"serviceA-grpc.traefik.dev.pap:443","RequestContentSize":0,"RequestCount":14,"RequestHost":"serviceA-grpc.traefik.dev.pap","RequestLine":"POST /grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo HTTP/2.0","RequestMethod":"POST","RequestPath":"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo","RequestPort":"443","RequestProtocol":"HTTP/2.0","RetryAttempts":0,"StartLocal":"2019-05-24T20:24:24.216358162Z","StartUTC":"2019-05-24T20:24:24.216358162Z","level":"info","msg":"","request_Content-Type":"application/grpc","request_Te":"trailers","request_User-Agent":"grpc-go/1.19.0","time":"2019-05-24T20:24:24Z"}
{"BackendAddr":"","BackendName":"Traefik","BackendURL":{"Scheme":"","Opaque":"","User":null,"Host":"","Path":"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":""},"ClientAddr":"x.x.x.x:64391","ClientHost":"x.x.x.x","ClientPort":"64391","ClientUsername":"-","DownstreamContentSize":0,"DownstreamStatus":500,"DownstreamStatusLine":"500 Internal Server Error","Duration":398352,"FrontendName":"Auth for frontend-public-tag-service-public","OriginContentSize":0,"OriginDuration":142857,"OriginStatus":500,"OriginStatusLine":"500 Internal Server Error","Overhead":255495,"RequestAddr":"serviceA-grpc.traefik.dev.pap:443","RequestContentSize":0,"RequestCount":15,"RequestHost":"serviceA-grpc.traefik.dev.pap","RequestLine":"POST /grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo HTTP/2.0","RequestMethod":"POST","RequestPath":"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo","RequestPort":"443","RequestProtocol":"HTTP/2.0","RetryAttempts":0,"StartLocal":"2019-05-24T20:24:24.297632103Z","StartUTC":"2019-05-24T20:24:24.297632103Z","level":"info","msg":"","request_Content-Type":"application/grpc","request_Te":"trailers","request_User-Agent":"grpc-go/1.19.0","time":"2019-05-24T20:24:24Z"}
如果执行grpcurl -plaintext docker-swarm-worker-01.dev.pap:50061 list
,我会得到可以调用的服务列表。
有谁可以帮助我吗?
答案 0 :(得分:1)
我设法使其正常运行!看来问题出在将HTTP重定向到https以及我正在使用的基本http身份验证。
Traefik.toml配置文件现在更简单:
defaultEntryPoints = ["http", "https"]
[entryPoints]
[entryPoints.http]
address = ":80"
compress = true
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[[entryPoints.https.tls.certificates]]
certFile = "/certs/certfile.cert"
keyFile = "/certs/keyfile.key"
[entryPoints.api]
address = ":8081"
[entryPoints.ping]
address = ":8082"
[api]
entryPoint = "api"
dashboard = true
[ping]
entryPoint = "ping"
[retry]
[traefikLog]
format = "json"
[accessLog]
format = "json"
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "traefik.dev.pbp"
swarmmode = true
watch = true
exposedByDefault = false
insecureSkipVerify = true
现在,我可以通过Traefik连接到在Docker swarm上运行的grpc服务!