kubernetes 中的 traefik ingress 自定义错误

traefik ingress custom error in kubernetes

我需要在 kubernetes 上的 traefik ingress 中设置自定义错误,以便在没有端点或状态为“404”或“[500-600]”时重定向到另一个错误服务或另一个自定义错误错误消息我在入口文件的文档中使用了注释,因为它是这样的(注意:这是在 values.yaml 文件中将注释作为 yaml 传递的 helm 模板输出)

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: /

正确的语法是:

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

请注意,目前 Helm Charts 不支持此功能。

ldez 的回答是正确的,但有几点需要注意:

  • 首先,这些注释仅适用于 traefik >= 1。6.x(早期版本可能支持错误页面,但不支持 kubernetes 后端)
  • 其次,traefik后端必须通过kubernetes配置。您不能在配置文件中创建后端并将其与 kubernetes 一起使用,至少不能在 traefik 1 中使用。6.x

这是完整的样子。 foo 只是一个名称,如其他答案中所述,可以是任何名称:

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

如果您使用此配置,并且您的应用发送 404,则 https://my-awesome-errors.mydomain.test/404.html 将显示为错误页面。

Ingress 不支持你们在那里使用的注解! 只有服务支持注释,入口正在使用主机部分。