Skip to main content
Version: 5.6

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 configuration, e.g., files and Docker.

The following section first discusses the configuration of Traefik with files, as this type of configuration is universally applicable. Later, an example will be used to show how Traefik can be configured in Docker Compose.

Configuring failover with files

The general configuration of Traefik for using configuration files could look like this. In this example, all configuration files in the /etc/traefik/traefik_configs folder are used to configure Traefik.

traefik.yaml
api:
dashboard: true
insecure: true

ping: {}

providers:
file:
# Specifies the directory where the configuration files are located.
directory: /etc/traefik/traefik_configs

entryPoints:
web:
address: ":80"

A configuration file for failover for an OPC Router redundancy pair could look like this. This file must be located in the configured directory for configuration files.

redundancy_failover.yaml
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:
# Servers in 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"