Redundancy failover with Traefik
What is Traefik?
Traefik is a modern, open-source reverse proxy and load balancer for microservice-oriented architectures. It supports multiple backends such as Docker and Kubernetes, offers automatic service discovery, SSL/TLS encryption and easy management via a dashboard or API.
There are several ways to configure Traefik, as different "providers" can be used for a configuration, e.g. files and Docker.
In the following, the configuration of Traefik with files is discussed first, as this type of configuration can be used universally. Later, an example will show how Traefik can be configured in Docker Compose.
Configuration of failover with files
The general configuration of Traefik for the use of configuration files could look like this. In this example, all configuration files in the /etc/traefik/traefik_configs
folder are used to configure Traefik.
api:
dashboard: true
insecure: true
ping: {}
providers:
file:
# Specifies the directory in which the configuration files are located.
directory: /etc/traefik/traefik_configs
entryPoints:
web:
address: ":80"
A failover configuration file for an OPC router redundancy pair could look like this. This file must be in the configured directory for configuration files.
http:
routers:
api-router:
# Responds to the prefix /api
rule: "PathPrefix(`/api`)"
# Sends requests to opcrouter-service
service: opcrouter-service
entryPoints:
- web
middlewares:
- api-rewrite
middlewares:
api-rewrite:
# Replaces /api/ with /services/MyRestServer/api/
replacePathRegex:
regex: "^/api/(.*)"
replacement: "/services/MyRestServer/api/$1"
services:
opcrouter-service:
loadBalancer:
# Server of the redundancy pair
servers:
- url: "http://opcrouter-primary:8080"
- url: "http://opcrouter-secondary:8080"
# Sends requests to the specified path to test availability
healthCheck:
path: "/health/runtime/ready"
interval: "5s"
timeout: "2s"