Repository navigation
Service Routing
Traefik serves as the API gateway and load balancer in this architecture. This page explains how to configure routing for your services using Traefik Docker labels.
Traefik uses Docker labels to create routing rules. Here are the basic labels you need to add to your service:
services:
my-service:
# ... other configuration
labels:
- "traefik.enable=true" # Enable Traefik for this container
- "traefik.http.routers.my-service.rule=PathPrefix(`/my-service`)" # Route based on path
- "traefik.http.services.my-service.loadbalancer.server.port=8080" # Container port
- "traefik.docker.network=microservices_template_traefik-net" # Docker networkOften, you want to strip the prefix before forwarding the request to your service:
labels:
- "traefik.http.middlewares.strip-my-service-prefix.stripprefix.prefixes=/my-service"
- "traefik.http.routers.my-service.middlewares=strip-my-service-prefix"With this configuration:
- A request to
/my-service/api/userswill be forwarded to your service as/api/users - Your service doesn't need to know about the
/my-serviceprefix
Middlewares modify requests before they reach your service or responses before they're sent back to the client.
labels:
- "traefik.http.routers.my-service.middlewares=rate-limit@consul,secure-headers@consul"The template pre-configures several middlewares in Consul:
-
rate-limit@consul: Limits the number of requests per time period -
secure-headers@consul: Adds security headers to responses -
compress@consul: Compresses responses
You can define custom middlewares directly in your Docker labels:
labels:
# Basic Authentication
- "traefik.http.middlewares.my-auth.basicauth.users=admin:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/"
# Redirect HTTP to HTTPS
- "traefik.http.middlewares.https-redirect.redirectscheme.scheme=https"
- "traefik.http.middlewares.https-redirect.redirectscheme.permanent=true"
# Apply the middlewares
- "traefik.http.routers.my-service.middlewares=my-auth,https-redirect"To enable HTTPS for your service:
labels:
- "traefik.http.routers.my-service.tls=true"
- "traefik.http.routers.my-service.tls.certresolver=letsencrypt"The template is pre-configured to use Let's Encrypt for automatic certificate issuance.
Traefik supports complex routing rules:
labels:
- "traefik.http.routers.my-service.rule=PathPrefix(`/api`) || PathPrefix(`/docs`)"labels:
- "traefik.http.routers.my-service.rule=Host(`api.example.com`)"labels:
- "traefik.http.routers.my-service.rule=Host(`example.com`) && PathPrefix(`/api`)"labels:
- "traefik.http.routers.my-service.rule=Headers(`X-Custom-Header`, `value`)"labels:
- "traefik.http.routers.my-service.rule=Query(`version`, `v1`)"Traefik automatically load balances between multiple instances of the same service:
services:
my-service:
# ... other configuration
deploy:
replicas: 3Prevent cascading failures with circuit breakers:
# Define circuit breaker in Consul
curl -X PUT -d '{
"circuitBreaker": {
"expression": "NetworkErrorRatio() > 0.5"
}
}' http://consul-server:8500/v1/kv/traefik/http/middlewares/my-circuit-breaker/
# Reference in your service
labels:
- "traefik.http.routers.my-service.middlewares=my-circuit-breaker@consul"Test new service versions without affecting users:
# Mirror traffic to a canary service
labels:
- "traefik.http.middlewares.mirror-to-canary.mirror.service=my-service-canary"
- "traefik.http.middlewares.mirror-to-canary.mirror.maxBodySize=5M"
- "traefik.http.routers.my-service.middlewares=mirror-to-canary"Create complex request processing pipelines:
# Define a chain of middlewares
labels:
- "traefik.http.middlewares.auth-chain.chain.middlewares=rate-limit@consul,secure-headers@consul,my-auth"
- "traefik.http.routers.my-service.middlewares=auth-chain"To verify your routing configuration, check the Traefik dashboard at http://localhost:8080. It shows:
- Configured routers
- Middleware chains
- Service endpoints
- Health status
- Use namespaced router names: Include your service name in router names to avoid conflicts
- Keep middleware chains short: Long chains can impact performance
- Test routing rules thoroughly: Ensure rules match the expected paths
- Use circuit breakers for critical services: Prevent cascading failures
- Monitor Traefik metrics: Watch for unusual patterns or errors
- Add your services (see [Adding Services](Adding-Services)) [Architecture Overview](Architecture-Overview) - Detailed explanation of the architecture design [Core Components](Core-Components) - Information about Consul, Traefik, and Vault [Detailed Setup](Detailed-Setup) - Step-by-step setup instructions [Adding Services](Adding-Services) - How to add microservices to the architecture [Service Registration](Service-Registration) - Methods for registering services with Consul [Service Routing](Service-Routing) - Configuring Traefik for routing to services [Secret Management](Secret-Management) - Working with Vault for secrets [Health Checks](Health-Checks) - Implementing and monitoring service health [Advanced Features](Advanced-Features) - Advanced capabilities of each component [Production Deployment](Production-Deployment) - Checklist and best practices for production