【问题标题】:Traefik not routing to other containersTraefik 没有路由到其他容器
【发布时间】:2020-09-12 02:52:40
【问题描述】:

我已经在我的前室运行 Ubuntu 20.04 的服务器上设置了 Traefik 和 Portainer(我使用了本指南和本指南,但没有按照我的意愿在第二个教程中设置默认 IP 白名单成为可公开访问的网络服务器)。这两个应用程序都可以正常工作,并且似乎正在使用 HTTPS。我可以在 Portainer 中管理和创建容器。

为了测试我的配置,我添加了两个容器 - MySQL 和 Wordpress。我在上面的教程中添加了 Traefik 标签,就像我设置 Traefik 时一样,我在 Portainer 中设置了 Wordpress 容器的域名,但是每当我尝试访问该域的 Wordpress 站点时,我都会收到一个 Bad Gateway 错误(只是词'Bad Gateway',甚至不是状态代码)。

我不确定我哪里出错了。这是我的配置文件:

traefik.yml:

api:
  dashboard: true

entryPoints:
  http:
    address: ":80"
  https:
    address: ":443"

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
  file:
    filename: /config.yml
version: '3'

services:
  traefik:
    image: traefik:v2.0
    container_name: traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./data/traefik.yml:/traefik.yml:ro
      - ./data/acme.json:/acme.json
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.entrypoints=http"
      - "traefik.http.routers.traefik.rule=Host(`traefik.mywebsite.com`)"
      - "traefik.http.middlewares.traefik-auth.basicauth.users=michael:$$apr1$$.m1mfSB0$$6Ypx6rfih8y.vHkNQe9rJ0"
      - "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
      - "traefik.http.routers.traefik-secure.entrypoints=https"
      - "traefik.http.routers.traefik-secure.rule=Host(`traefik.mywebsite.com`)"
      - "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
      - "traefik.http.routers.traefik-secure.tls=true"
      - "traefik.http.routers.traefik-secure.tls.certresolver=http"
      - "traefik.http.routers.traefik-secure.service=api@internal"

networks:
  proxy:
    external: true
certificatesResolvers:
  http:
    acme:
      email: me@myemail.com
      storage: acme.json
      httpChallenge:
        entryPoint: http

config.yml:

http:
  middlewares:
    https-redirect:
      redirectScheme:
        scheme: https

docker-compose.yml:

version: '3'

services:
  traefik:
    image: traefik:v2.0
    container_name: traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./data/traefik.yml:/traefik.yml:ro
      - ./data/acme.json:/acme.json
      - ./data/config.yml:/config.yml:ro
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.entrypoints=http"
      - "traefik.http.routers.traefik.rule=Host(`traefik.mywebsite.com`)"
      - "traefik.http.middlewares.traefik-auth.basicauth.users=michael:$$apr1$$.m1mfSB0$$6Ypx6rfih8y.vHkNQe9rJ0"
      - "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
      - "traefik.http.routers.traefik-secure.entrypoints=https"
      - "traefik.http.routers.traefik-secure.rule=Host(`traefik.mywebsite.com`)"
      - "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
      - "traefik.http.routers.traefik-secure.tls=true"
      - "traefik.http.routers.traefik-secure.tls.certresolver=http"
      - "traefik.http.routers.traefik-secure.service=api@internal"

networks:
  proxy:
    external: true

Wordpress/MySQL docker-compose.yml:

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: admin
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_NAME: wordpressdb
    volumes:
      - wordpress:/var/www/html
    networks:
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wordpress.entrypoints=http"
      - "traefik.http.routers.wordpress.rule=Host(`myblog.com`)"
      - "traefik.http.routers.wordpress.middlewares=https-redirect@file"
      - "traefik.http.routers.wordpress-secure.entrypoints=https"
      - "traefik.http.routers.wordpress-secure.rule=Host(`myblog.com`)"
      - "traefik.http.routers.wordpress-secure.tls=true"
      - "traefik.http.routers.wordpress-secure.tls.certresolver=http"
      - "traefik.http.routers.wordpress-secure.service=wordpress"
      - "traefik.http.services.wordpress.loadbalancer.server.port=9000"
      - "traefik.docker.network=proxy"

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: username
      MYSQL_PASSWORD: password
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
    networks:
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.mysql.entrypoints=http"
      - "traefik.http.routers.mysql.middlewares=https-redirect@file"
      - "traefik.http.routers.mysql-secure.entrypoints=https"
      - "traefik.http.routers.mysql-secure.tls=true"
      - "traefik.http.routers.mysql-secure.tls.certresolver=http"
      - "traefik.http.routers.mysql-secure.service=mysql"
      - "traefik.http.services.mysql.loadbalancer.server.port=9000"
      - "traefik.docker.network=proxy"

volumes:
  wordpress:
  db:

networks:
  proxy:
    external: true

如果需要,我也可以提供 Portainer docker-compose.yml 文件,但我认为没有必要。这里的任何帮助都会很棒!

【问题讨论】:

  • 这个有消息吗?

标签: wordpress docker traefik


【解决方案1】:

对于不同应用程序之间的网络连接,您必须在其中一个应用程序中创建网络。我会在你的 traefik docker-compose.yml 中这样做

意思是,在您的 traefik 撰写文件中,您不能将代理网络指定为外部,因为您在该应用程序中像这样在内部创建它:

networks:
  proxy:

在您的 Wordpress/MySQL docker-compose.yml 中,您必须为外部网络指定一个名称,如下所示:

networks:
  proxy:
    external:
      name: "traefik_proxy"

当您使用 compose 创建一个新应用程序时,应用程序中的所有内容都有一个前缀,即 compose 文件所在的目录名。

意思是上面的例子只有在你的 traefik compose 文件被放置在一个名为“traefik”的目录中时才有效

这应该可以解决您的连接问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多