首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >码头Traefik未能路由websocket

问码头Traefik未能路由websocket
EN

Stack Overflow用户
提问于 2021-02-04 16:43:37
回答 1查看 1.6K关注 0票数 2

我有几个微型服务作为码头集装箱运行。所有web服务运行良好,路由正确。

唯一的问题是websocket服务。websocket服务本身使用python,并拥有自己的TLS证书。

尝试使用wss://websocket.localhost访问websocket失败,在下面的设置中它根本找不到页面。在我以前的配置中,它会导致坏网关错误。

显然,traefik是在没有附加配置的情况下使用websockets的。

情况似乎并非如此。有什么指示吗?

websocket连接在不涉及码头或traefik的情况下工作,所以我排除了这个问题。如果能在这方面提供任何帮助,我们将不胜感激。

docker-compose.yml

代码语言:javascript
复制
version: "3.7"

networks:
  web:
    external: true
  internal:
    external: false

volumes:
  mysql_data:

services:
  traefik:
    image: traefik:v2.2.1
    container_name: traefik
    restart: always
    ports:
      - "80:80"
      - "443:443"
    expose:
      - 8080
    environment:
      - /var/run/docker.sock:/var/run/docker.sock
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./config/:/config
      - ./traefik.yml:/traefik.yml
    networks:
      - web
      - internal
    labels:
      - traefik.http.routers.traefik.tls=true
      - traefik.http.routers.traefik.entrypoints=secure
      - traefik.http.routers.traefik.rule=Host(`traefik.localhost`)
      - traefik.http.routers.traefik.service=api@internal

  dozzle:
    image: amir20/dozzle:latest
    container_name: dozzle
    restart: always
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    expose:
      - 8080
    labels:
      - traefik.http.routers.dozzle.tls=true
      - traefik.http.routers.dozzle.entrypoints=secure
      - traefik.http.routers.dozzle.rule=Host(`dozzle.localhost`) || Host(`logs.localhost`)
    networks:
      - internal

  db:
    image: mysql:latest
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: ########
    restart: always
    healthcheck:
      test: "exit 0"
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - '3306:3306'
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - internal

  websocket:
    image: local-websocket-image
    container_name: websocket-stage
    restart: on-failure
    command: python server.py
    depends_on:
      db:
        condition: service_healthy
    expose:
      - 8080
    networks:
      - web
      - internal
    environment:
      - PATH_TO_CONFIG=/src/setup.cfg
    volumes:
      - ${PWD}/docker-config:/src
      - ${PWD}/config/certs/socket:/var
    labels:
      - traefik.http.routers.core-socket-stage-router.tls=true
      - traefik.http.routers.core-socket-stage-router.entrypoints=secure
      - traefik.http.routers.core-socket-stage-router.rule=Host(`websocket.localhost`)

traefik.yml

代码语言:javascript
复制
entryPoints:
  insecure:
    address: :80
    http:
      redirections:
        entryPoint:
          to: secure
          scheme: https
  secure:
    address: :443

log:
  level: INFO
 
accessLog:
  filePath: "traefik-access.log"
  bufferingSize: 100

api:
  dashboard: true
  insecure: true

ping: {}

providers:
  file:
    filename: /config/dynamic.yml # traefik dynamic configuration
    watch: true                   # everytime it changes, it will be reloaded 
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: true

配置

代码语言:javascript
复制
tls:
  stores:
    default:
      defaultCertificate:
        certFile: cert.crt
        keyFile: key.key

  certificates:
    - certFile: crt.crt
      keyFile: key.key
      stores:
        - default
 
  domains:
    - main: "localhost"
EN

回答 1

Stack Overflow用户

发布于 2021-02-13 23:36:22

在查看您的配置时,以下内容不适合:

  • ,码头-合成项目名称将是域名的一部分.默认情况下是使用您的docker的父文件夹名称-Compose.yaml。您没有在这里指定它,因此我假设它是由traefik指定的。您可以在带有docker-compose -p traefik up的docker-组合调用中或通过设置env变量PROJECT_NAME.

显式地设置此设置。

  • --您使用的是域名‘..localhost’,但是没有显式地定义域名。这意味着默认名称是从服务名称、项目名称(存储停靠-组合文件的文件夹)和与此模式附加的对接者-网络名称( servicename.projectname_networkname )派生的。使用属性hostname和domainname显式定义名称(仅适用于internal=false网络)。当有两个网络连接和一个域名定义时,您将得到以下域名:

代码语言:javascript
复制
1. db.traefik\_internal (only intern, db.localhost will not work)
2. dozzle.traefik\_internal (only intern, dozzle.localhost will not work)
3. traefik.localhost
4. traefik.traefik\_web
5. traefik.traefik\_internal
6. websocket.localhost
7. websocket.traefik\_web
8. websocket.traefik\_internal

  • external=true仅仅意味着网络是由docker network create或另一个对接者组成的项目外部创建的。其主要效果是,在执行docker-compose down时不受影响。这与与外界的联系无关。要获得独立的内部网络,必须使用选项internal: true

2.4不再支持

  • 选项condition: service_healthy,所以要么删除该选项(但它不像您预期的那样工作),要么将版本更改为version: "3.7"

在这里,我现在的版本是复合船坞。

代码语言:javascript
复制
version: "2.4"

networks:
  web:
  internal:
    internal: true

volumes:
  mysql_data:

services:
  traefik:
    image: traefik:v2.2.1
    container_name: traefik
    hostname: traefik
    domainname: localhost
    restart: always
    ports:
      - "80:80"
      - "443:443"
    expose:
      - 8080
    environment:
      - /var/run/docker.sock:/var/run/docker.sock
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./config/:/config
      - ./traefik.yml:/traefik.yml
    networks:
      - web
      - internal
    labels:
      - traefik.http.routers.traefik.tls=true
      - traefik.http.routers.traefik.entrypoints=secure
      - traefik.http.routers.traefik.rule=Host(`traefik.localhost`)
      - traefik.http.routers.traefik.service=api@internal

  dozzle:
    image: amir20/dozzle:latest
    container_name: dozzle
    hostname: dozzle
    domainname: localhost
    restart: always
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    expose:
      - 8080
    labels:
      - traefik.http.routers.dozzle.tls=true
      - traefik.http.routers.dozzle.entrypoints=secure
      - traefik.http.routers.dozzle.rule=Host(`dozzle.traefik_internal`) || Host(`logs.localhost`)
    networks:
      - internal

  db:
    image: mysql:latest
    container_name: db
    hostname: db
    domainname: localhost
    environment:
      MYSQL_ROOT_PASSWORD: ########
    restart: always
    healthcheck:
      test: "exit 0"
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - '3306:3306'
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - internal

  websocket:
    image: local-websocket-image
    container_name: websocket-stage
    hostname: websocket
    domainname: localhost
    restart: on-failure
    command: python server.py
    depends_on:
      db:
        condition: service_healthy
    expose:
      - 8080
    networks:
      - web
      - internal
    environment:
      - PATH_TO_CONFIG=/src/setup.cfg
    volumes:
      - ${PWD}/docker-config:/src
      - ${PWD}/config/certs/socket:/var
    labels:
      - traefik.http.routers.core-socket-stage-router.tls=true
      - traefik.http.routers.core-socket-stage-router.entrypoints=secure
      - traefik.http.routers.core-socket-stage-router.rule=Host(`websocket.localhost`)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66049995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档