traefik不遵守前端规则

时间:2018-11-10 15:59:05

标签: docker docker-compose reverse-proxy traefik

我试图在我的Docker主机上部署多个应用程序,并让traefik根据主机名将流量路由到不同的应用程序

我正在对所有docker容器使用docker-compose

这是我的traeffik.yaml文件

version: '3.5'

services:
  traefik:
    image: traefik
    container_name: traefik
    command: --api --docker
    networks:
      - traefik_network
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

networks:
  traefik_network:
     name: traefik_network

这是我的wpapp1.yaml文件

version: '3.5'

services:
   mysql:
     image: mysql:5.7
     volumes:
       - wpapp1_mysql:/var/lib/mysql
     restart: always
     container_name: wpapp1_mysql
     networks:
       - traefik_network
     environment:
       MYSQL_ROOT_PASSWORD: wpapp1
       MYSQL_DATABASE: wpapp1
       MYSQL_USER: wpapp1
       MYSQL_PASSWORD: wpapp1
   wordpress:
     depends_on:
       - mysql
     image: wordpress:latest
     volumes:
       - wpapp1_wordpress:/var/www/html
     restart: always
     container_name: wpapp1_wordpress
     networks:
       - traefik_network
     labels:
      - "traefik.frontend.rule=Host:wpapp1.example.com"
      - "traefik.port=80"
      - "traefik.docker.network=traefik_network"
     environment:
       WORDPRESS_DB_HOST: mysql:3306
       WORDPRESS_DB_USER: wpapp1
       WORDPRESS_DB_PASSWORD: wpapp1
volumes:
    wpapp1_mysql:
      name: wpapp1_mysql
    wpapp1_wordpress:
      name: wpapp1_wordpress
networks:
    traefik_network:
      external:
         name: traefik_network

这是我的wpapp2.yaml文件

version: '3.5'

services:
   mysql:
     image: mysql:5.7
     volumes:
       - wpapp2_mysql:/var/lib/mysql
     restart: always
     container_name: wpapp2_mysql
     networks:
       - traefik_network
     environment:
       MYSQL_ROOT_PASSWORD: wpapp2
       MYSQL_DATABASE: wpapp2
       MYSQL_USER: wpapp2
       MYSQL_PASSWORD: wpapp2
   wordpress:
     depends_on:
       - mysql
     image: wordpress:latest
     volumes:
       - wpapp2_wordpress:/var/www/html
     restart: always
     container_name: wpapp2_wordpress
     networks:
       - traefik_network
     labels:
      - "traefik.frontend.rule=Host:wpapp2.example.com"
      - "traefik.port=80"
      - "traefik.docker.network=traefik_network"
     environment:
       WORDPRESS_DB_HOST: mysql:3306
       WORDPRESS_DB_USER: wpapp2
       WORDPRESS_DB_PASSWORD: wpapp2
volumes:
    wpapp2_mysql:
      name: wpapp2_mysql
    wpapp2_wordpress:
      name: wpapp2_wordpress
networks:
    traefik_network:
      external:
         name: traefik_network

所以现在我希望traefik根据主机名wpapp1.example.comwpapp2.example.com进行路由,但是traefik正在负载均衡流量!

因此,当我转到http:/wpapp1.example.com时,traefik在两个应用程序之间进行了负载平衡,其他主机名也是如此。现在确定发生了什么,因为我专门添加了traefik.frontend.rule

我的意思是这到底是怎么回事? 我花了几个小时弄清楚发生了什么事,在发疯之前,我决定在这里找一些事情,以寻求帮助。

1 个答案:

答案 0 :(得分:1)

将数据库放置在其他网络上。否则,WordPress将RR负载均衡到同一docker网络中的两个mysql实例(当您在同一网络上有两个具有相同别名的容器时,这是预期的行为)。您可以使用默认网络来做到这一点:

version: '3.5'

services:
   mysql:
     image: mysql:5.7
     volumes:
       - mysql:/var/lib/mysql
     restart: unless-stopped
     networks:
       - db
     environment:
       MYSQL_ROOT_PASSWORD: wpapp
       MYSQL_DATABASE: wpapp
       MYSQL_USER: wpapp
       MYSQL_PASSWORD: wpapp
   wordpress:
     depends_on:
       - mysql
     image: wordpress:latest
     volumes:
       - wordpress:/var/www/html
     restart: unless-stopped
     networks:
       - traefik
       - db
     labels:
      - "traefik.frontend.rule=Host:wpapp1.example.com"
      - "traefik.port=80"
      - "traefik.docker.network=traefik_network"
     environment:
       WORDPRESS_DB_HOST: mysql:3306
       WORDPRESS_DB_USER: wpapp
       WORDPRESS_DB_PASSWORD: wpapp
volumes:
    mysql:
    wordpress:
networks:
    db:
    traefik:
      external:
         name: traefik_network