Search code examples
kubernetestraefikkubernetes-helmkubernetes-ingress

traefik ingress custom error in kubernetes


I need to set a custom error in traefik ingress on kubernetes so that when there is no endpoint or when the status is "404", or "[500-600]" it redirects to another error service or another custom error message I used the annotation as it's in the documentation in the ingress file as this (Note: this a helm template output of passing the annotation as a yaml in the values.yaml file)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: frontend
  namespace: "default"
  annotations:
      external-dns.alpha.kubernetes.io/target: "domain.com"
      kubernetes.io/ingress.class: "traefik"
      traefik.ingress.kubernetes.io/error-pages: "map[/:map[backend:hello-world status:[502 503]]]"
spec:
  rules:
  - host: frontend.domain.com
    http:
      paths:
      - backend:
          serviceName: frontend
          servicePort: 3000
        path: /

Solution

  • The answer by ldez is correct, but there are a few caveats:

    • First off, these annotations only work for traefik >= 1.6.x (earlier versions may support error pages, but not for the kubernetes backend)
    • Second, the traefik backend must be configured through kubernetes. You cannot create a backend in a config file and use it with kubernetes, at least not in traefik 1.6.x

    Here's how the complete thing looks like. foo is just a name, as explained in the other answer, and can be anything:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: frontend
      namespace: "default"
      annotations:
          external-dns.alpha.kubernetes.io/target: "domain.com"
          kubernetes.io/ingress.class: "traefik"
          traefik.ingress.kubernetes.io/error-pages: |-
            foo:
              status:
              - "404"
              - "500"
              # See below on where "error-pages" comes from
              backend: error-pages
              query: "/{{status}}.html"
    spec:
      rules:
       # This creates an ingress on an non-existing host name,
       # which binds to a service. As part of this a traefik
       # backend "error-pages" will be created, which is the one
       # we use above
       - host: error-pages
         http:
           paths:
           - backend:
             serviceName: error-pages-service
             servicePort: https
    - host: frontend.domain.com
        http:
        # The configuration for your "real" Ingress goes here
    
    # This is the service to back the ingress defined above
    # Note that you can use anything for this, including an internal app
    # Also: If you use https, the cert on the other side has to be valid
    ---
    kind: Service
    apiVersion: v1
    metadata:
      name: error-pages-service
      namespace: default
    spec:
      ports:
      - name: https
        port: 443
      type: ExternalName
      externalName: my-awesome-errors.mydomain.test
    

    If you use this configuration, and your app sends a 404, then https://my-awesome-errors.mydomain.test/404.html would be shown as the error page.