Back to articles
DevOps & Systems

Configure Traefik v3 as API Gateway and Load Balancer for Microservices Cluster with Canary Deployment

Guide to configure Traefik v3 as API Gateway and Load Balancer for a microservices cluster, integrating Canary Deployment for safe and controlled updates.

DPTCloud Engineering4 min read
Configure Traefik v3 as API Gateway and Load Balancer for Microservices Cluster with Canary Deployment

Image credit: LubosHouska · Pixabay Content License

Configure Traefik v3 as API Gateway and Load Balancer for Microservices Cluster

In modern environments, deploying microservices requires a robust and flexible traffic management system. Traefik v3 is a popular solution for serving as an API Gateway and Load Balancer, offering automatic service discovery, URL-based routing, and integration with Docker, Kubernetes, and Consul. This guide walks through configuring Traefik v3 as an API Gateway and Load Balancer for a microservices cluster, while implementing Canary Deployment to safely roll out new versions with controlled risk.

Prerequisites

  • A server or virtual machine with Docker and Docker Compose installed.
  • Docker Compose version 2.0 or higher.
  • Microservices with HTTP endpoints already set up.
  • A domain name or IP address for system access.

Basic Traefik v3 Configuration with Docker Compose

Start by creating a docker-compose.yml file to launch Traefik v3 and the microservices.

version: '3.8'

services:
  traefik:
    image: traefik:latest
    container_name: traefik
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik.yml:/traefik.yml:ro
    command:
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --certificatesresolvers.myresolver.acme.tlschallenge=true
      - --certificatesresolvers.myresolver.acme.email=your-email@example.com
      - --certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json
    restart: unless-stopped

  microservice-v1:
    image: your-microservice:v1
    container_name: microservice-v1
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.microservice-v1.rule=Host(`api.example.com`) && PathPrefix(`/v1`)"

  microservice-v2:
    image: your-microservice:v2
    container_name: microservice-v2
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.microservice-v2.rule=Host(`api.example.com`) && PathPrefix(`/v2`)"

  # Microservice for Canary Deployment
  microservice-canary:
    image: your-microservice:canary
    container_name: microservice-canary
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.canary.rule=Host(`api.example.com`) && PathPrefix(`/canary`)"`

Set Up Canary Deployment with Traefik

Canary Deployment allows rolling out a new application version to a small subset of users before full rollout. Traefik supports this via weighted routing.

Configure Traefik to route traffic between the old and canary versions using traefik.http.routers.router.middlewares.

version: '3.8'

services:
  traefik:
    image: traefik:latest
    container_name: traefik
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik.yml:/traefik.yml:ro
    command:
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --certificatesresolvers.myresolver.acme.tlschallenge=true
      - --certificatesresolvers.myresolver.acme.email=your-email@example.com
      - --certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json
      - --providers.docker.watch=true
      - --providers.docker.constraint-label=traefik.enable
    restart: unless-stopped

  microservice-v1:
    image: your-microservice:v1
    container_name: microservice-v1
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.microservice-v1.rule=Host(`api.example.com`) && PathPrefix(`/v1`)"
      - "traefik.http.routers.microservice-v1.service=microservice-v1"
      - "traefik.http.services.microservice-v1.loadbalancer.server.port=80"

  microservice-canary:
    image: your-microservice:canary
    container_name: microservice-canary
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.canary.rule=Host(`api.example.com`) && PathPrefix(`/canary`)"
      - "traefik.http.routers.canary.service=canary"
      - "traefik.http.services.canary.loadbalancer.server.port=80"
      - "traefik.http.routers.canary.middlewares=canary-weight"

Verification and Testing

After configuration, verify Traefik is running correctly by accessing the admin interface:

curl http://localhost:8080/api

To test traffic distribution, use curl or ab to send requests:

curl -H "Host: api.example.com" http://localhost/v1
curl -H "Host: api.example.com" http://localhost/canary

Ensure both versions are accessible and functioning as expected.

Troubleshooting

  • Services not visible in Traefik: Check that services have the traefik.enable=true label and Docker Compose is running correctly.
  • HTTPS access issues: Ensure certificatesresolvers is correctly configured and Let's Encrypt certificates are issued.
  • Incorrect traffic weighting: Review the middlewares and weight settings in the routers.

References

Related infrastructure & engineering guides: NVMe Cloud VPS for Docker, Nginx & Databases

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478