如何使用Traefik路由docker(大量)容器需要一个端口,以连接到/ myapp之类的主机url的上下文?

时间:2019-04-09 11:10:00

标签: docker-swarm traefik

我的配置是Docker Swarm。

MyWebApp在容器中运行,并且可以访问端口8100。

出于某些原因,我需要URL访问不是使用Port,而是使用诸如myhost.com/mywebapp之类的URL上下文。

路由应该由Traefik完成。

我尝试使用Path,PathPrefix,PathPrefixStrip,用于配置Traefik的方法,无论如何在相同的结果下,我只能访问MyWebApp,因为URL中的端口无法使用上下文/ mywebapp。

#startscript.sh

docker swarm init
docker network create -d overlay proxy
docker stack deploy -c docker-compose.traefik.yml traefik
docker stack deploy -c docker-compose.webapps.yml webapps

traefik.toml

accessLogsFile = "/dev/stdout"

logLevel = "DEBUG"

defaultEntryPoints = ["http", "https"]

[entryPoints]
  [entryPoints.http]
  address = ":80"
#    [entryPoints.http.redirect]
#    entryPoint = "https"
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]
      [[entryPoints.https.tls.certificates]]
      CertFile = "/run/secrets/cert.pem"
      KeyFile = "/run/secrets/key.pem"

[web]
address = ":8085"

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "myhost.com"
watch = true
swarmmode = true
exposedbydefault = false

[file]

docker-compose-traefik.yml

version: '3.3'

networks:
  proxy:
    external:
      name: proxy

condigs:
  traefik_toml_v2:
    file: ./traefik.toml

secrets:
  traefik_cert:
    file: ./tls/cert.pem
  traefik_key:
    file: ./tls/key.pem

services:
  traefik:
    image: traefik
    deploy:
      replicas: 2
      placement:
        constraints:
        - node.role == manager
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    networks:
    - proxy
    ports:
    - target: 80
      protocol: tcp
      published: 80
      mode: ingress
    - target: 443
      protocol: tcp
      published: 443
      mode: ingress
    - target: 8085
      protocol: tcp
      published: 8085
      mode: ingress
    configs:
    - source: traefik_toml_v2
      target: /etc/traefik/traefik.toml
      mode: 444
    secrets:
    - source: traefik_cert
      target: cert.pem
      uid: "0"
      mode: 400
    - source: traefik_key
      target: key.pem
      uid: "0"
      mode: 400

docker-compose-webapps.yml

version: '3.3'

networks:
  proxy:
    external: true
  net:
    driver: overlay
    attachable: true

services:
  whoami:
    image: emilevauge/whoami
    networks:
    - proxy
    deploy:
      replicas: 2
      resources:
        limits:
          memory: 1G
      labels:
#This is working - i can access with: http://myhost/whoami
      - traefik.frontend.rule=PathPrefixStrip:/whoami
      - traefik.docker.network=proxy
      - traefik.port=80
      - traefik.enable=true

  mywebapp:
    image: myregistry/myrepos:my_image
    networks:
    - proxy
    - net
    ports:
    - 8100:8100
    volumes:
    - ~/dev/myconf:/home/developer/dev/myconf 
    command: mywebapp.bin --http-address=0.0.0.0 --http-port=8100
    deploy:
      replicas: 2
      resources:
        limits:
          memory: 1G
      labels:
      - traefik.enable=true
#This is NOT working - i canNOT access with: http://myhost/webapp
#Access is only possible with: http://myhost:8100
#WHAT I HAVE TO DO THAT i can forward/redirect http://myhost:8100 to http://myhost/webapp????
#      - traefik.frontend.rule=Host:myhost.com;Path:/mywebapp
#      - traefik.port=8100
#I tried both, with servicename and without servicename, in both cases access to http://myhost/webapp is not possible, only to http://myhost:8100
      - traefik.webapps_mywebapp.frontend.rule=Host:myhost.com;Path:/mywebapp
      - traefik.webapps_mywebapp.port=8100
      - traefik.docker.network=proxy

1 个答案:

答案 0 :(得分:0)

我要对此戳一击。

首先,您将docker容器端口打开到外部连接:

ports:
  - 8100:8100

这就是为什么您可以通过端口访问URL的原因。因此,当您通过http://url:8100访问它时,实际上是在绕开traefik并直接连接到容器。

如果将其设置为EXPOSE,则仅应打开docker网络。

expose:
  - "8100"

(在此引用此链接:What is the difference between docker-compose ports vs expose

现在开始您的Traefik连接问题, 我明白为什么无法访问的原因之一是因为您使用了错误的前端规则:

labels:    
  - traefik.frontend.rule=Host:myhost.com;Path:/mywebapp

具有主机规则将要求您在请求的标头中添加其他信息。您不能只单独调用URL。

对于Path,我不确定该怎么做,但是对我来说,它只会发送404。

您需要将PathPrefixStrip设置为唯一的前端规则:

labels:    
  - traefik.frontend.rule=PathPrefixStrip:/mywebapp

我遇到了同样的问题。这是一个为我工作的docker-compose块的示例:

cherrypy-helloworld:
  build:
    dockerfile: cherrypy_helloworld.Dockerfile
  expose:
    - "8080"
  volumes:
    - $PWD/cherrypy_helloworld:/pyapp
  labels:
    - traefik.frontend.rule=PathPrefixStrip:/apitest
    - traefik.enable=true

希望这会有所帮助。