简体   繁体   中英

traefik not respecting frontend rule

I am trying to deploy multiple apps on my docker host and have traefik route traffic based on hostnames to the different apps

I am using docker-compose for all my docker containers

Here is my traeffik.yaml file

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

here is my wpapp1.yaml file

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

and here is my wpapp2.yaml file

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

So now i expect traefik to route based on the hostnames wpapp1.example.com and wpapp2.example.com BUT traefik is loadbalancing traffic!!!

So when i go to http:/wpapp1.example.com , traefik is loadbalancing it between the two apps and same for the other hostnames. Now sure what is going on here since i specifically add the traefik.frontend.rule

I mean how in the hell is that happening? I have spent hours to figure what is going on and before i go insane i decided to some here to get some help on what is going on here.

Put your database on a different network. Otherwise WordPress will RR load balance to the two mysql instances in the same docker network (that's the expected behavior when you have two containers with the same alias on the same network). You can do that with the default network:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM