Implement Traefik for Loading balancing Wordpress application

Implement Traefik for Loading balancing Wordpress application

Overview of Traefik

Traefik is a modern and dynamic reverse proxy and load balancer that provides automatic discovery and configuration for microservices architecture. It is open-source, designed for simplicity and ease of use, and works seamlessly with popular container orchestration platforms like Kubernetes, Docker Swarm, and Mesos.

Features

Traefik provides the following features:

  • Automatic discovery and configuration of microservices

  • Supports multiple protocols, including HTTP, TCP, UDP, and WebSockets

  • Multiple load-balancing strategies, including round-robin, least connections, IP hash, and sticky sessions

  • Automatic SSL/TLS certificate generation and renewal with Let's Encrypt integration

  • SSL termination and redirection

  • Flexible and intuitive routing system with path-based routing, host-based routing, and header-based routing

  • Custom middleware and plugins

  • Web-based dashboard for monitoring and managing traffic

  • Dynamic Configuration

One of the key features of Traefik is its dynamic configuration. Traefik can automatically discover and adapt to changes in your infrastructure. This means that you don't have to manually update your configuration every time you add or remove a service.

Load Balancing

Traefik supports multiple load-balancing strategies, including round-robin, least connections, IP hash, and sticky sessions. This allows you to choose the strategy that best fits your use case and traffic requirements.

Automatic SSL/TLS Certificate Generation

Traefik can automatically generate and renew SSL/TLS certificates through its integration with Let's Encrypt. This provides secure communication between clients and your microservices.

Routing

Traefik has a flexible and intuitive routing system that allows you to define rules for how traffic is routed to your services. It supports path-based routing, host-based routing, and header-based routing, as well as custom middleware and plugins to handle more complex scenarios.

Dashboard

Traefik provides a web-based dashboard for monitoring and managing your traffic. The dashboard provides real-time metrics, logs, and health checks for your services, making it easy to troubleshoot issues and optimize your infrastructure.

Implementing Traefik on Docker

Prerequisites

  • Docker and Docker-compose should be already installed

  • You should have some experience in WordPress and it's configuration

Docker Compose File

Create a Docker Compose file named docker-compose.yml and add the following template code, you can make changes if you like, I have kept it simple :

version: "3"

services:
  reverse-proxy:
    image: traefik:v2.9
    platform: linux/amd64
    command: --api.insecure=true --providers.docker
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      - "traefik.http.services.reverse-proxy.loadbalancer.strategy=rr"

  db1:
    image: mysql:5.7
    platform: linux/amd64
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  db2:
    image: mysql:5.7
    platform: linux/amd64
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress1:
    depends_on:
      - db1
    image: wordpress:latest
    platform: linux/amd64
    restart: always
    environment:
      WORDPRESS_DB_HOST: db1:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    ports:
      - "8001:80"
    labels:
      - "traefik.http.routers.wordpress.rule=Host(`localhost`) && Path(`/`)"
      - "traefik.http.services.wordpress.loadbalancer.server.port=80"

  wordpress2:
    depends_on:
      - db2
    image: wordpress:latest
    platform: linux/amd64
    restart: always
    environment:
      WORDPRESS_DB_HOST: db2:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    ports:
      - "8002:80"
    labels:
      - "traefik.http.routers.wordpress.rule=Host(`localhost`) && Path(`/`)"
      - "traefik.http.services.wordpress.loadbalancer.server.port=80"

At first, the endpoint will return 404 so please go to each WordPress site and configure it. Once both the sites are configured then Traefik will work perfectly well. I got the following result.

Check out the results:

As you can see our load balancer is diverting the traffic between two different WordPress sites hosted on Docker.