Search code examples
kubernetestraefiktraefik-ingresstraefik-middlewaretraefik-routers

How do I automatically retry a request in traefik when the downstream service isn't yet ready


I've configured Traefik within a Kubernetes (k8s) cluster as the ingress. However, I have some legacy containers that are being exposed that don't behave as well as one would want from a modern containerised application. I would like to be able to configure a Traefik middleware such that it will retry when the downstream service isn't yet ready.


Solution

  • One would naively have expected the retry middleware to satisfy this requirement. Unfortunately (and by design) this does not work as it appears to receive a 503 status code from the backend service and, as is clearly stated in the documentation, treats any response whatsoever from downstream services as a non-retryable event.

    To navigate around this, I used the error middleware instead. With this, I also provided a deployment (with associated service) in my cluster/namespace that was capable of serving a static html page that automatically refreshed e.g. some html that contained:

    <meta http-equiv="refresh" content="5">
    

    My middleware configuration looked like:

    apiVersion: traefik.io/v1alpha1
    kind: Middleware
    metadata:
      name: retry-on-503
    spec:
      errors:
        status:
          - "503"
        query: /retry.html
        service:
          name: staticsite
          port: 80
    

    Whilst not ideal as the retry is exposed to the client, this works for my specific needs.