traefik ingress custom error in kubernetes

5/20/2018

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: /
-- yara mohamed
kubernetes
kubernetes-helm
kubernetes-ingress
traefik

3 Answers

7/5/2018

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.

-- averell
Source: StackOverflow

5/20/2018

The correct syntax is:

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"
          backend: bar
          query: /bar
        fii:
          status:
          - "500-600"
          backend: bar
          query: /bir
spec:
  rules:
  - host: frontend.domain.com
    http:
      paths:
      - backend:
          serviceName: frontend
          servicePort: 3000
        path: /

https://docs.traefik.io/v1.6/configuration/backends/kubernetes/#general-annotations

Note that, currently, the Helm Charts doesn't support this feature.

-- ldez
Source: StackOverflow

9/15/2018

Ingress does not support that annotations that you guys are using there! That annotations are supported with Service only, Ingress is using host section.

-- Dušan Šušić
Source: StackOverflow