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.
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=truelabel and Docker Compose is running correctly. - HTTPS access issues: Ensure
certificatesresolversis correctly configured and Let's Encrypt certificates are issued. - Incorrect traffic weighting: Review the
middlewaresandweightsettings in the routers.
References
- Traefik Documentation
- Xây dựng máy chủ tile geospatial tối ưu kết hợp OpenStreetMap và PostGIS trên VPS
- Building a High-Performance Time Series Data Warehouse for IoT Dashboards with VictoriaMetrics on VPS
Related infrastructure & engineering guides: NVMe Cloud VPS for Docker, Nginx & Databases
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.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
