Redundancy and 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 an API.
There are several ways to configure Traefik, as different providers can be used, such as files or Docker.
The following section first describes how to configure Traefik using files, as this method is generally applicable. An example then shows how Traefik can be configured in Docker Compose.
Traefik does not replace the internal OPC Router redundancy. The Traefik configuration only uses the provided health endpoints to forward requests to the currently active OPC Router.
Configuring Failover Using Files
The general basic configuration of Traefik for using configuration files might look like this, for example. In this example, all configuration files are loaded from the /etc/traefik/traefik_configs directory.
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"
The insecure: true setting is intended here only for a simple example with local access. In production environments, the Traefik dashboard should not be published unprotected; instead, it should be secured or completely disabled.
A possible configuration file for the failover of an OPC router redundancy pair might look like this. This file must be located 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:
# 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"