traefik ingress自定义错误在kubernetes

时间:2018-05-20 21:47:29

标签: kubernetes traefik kubernetes-helm kubernetes-ingress

我需要在kubernetes的traefik ingress中设置自定义错误,以便在没有端点或状态为" 404"或" [500-600]"它重定向到另一个错误服务或另一个自定义错误消息我在入口文件的文档中使用了注释,因为它(注意:这是一个将注释作为yaml传递给values.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: /

3 个答案:

答案 0 :(得分:2)

ldez的答案是正确的,但有一些警告:

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

这是完整的外观。 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 只是一个名字,如另一个答案中所述,可以是任何东西:

https://my-awesome-errors.mydomain.test/404.html

如果使用此配置,并且您的应用发送了404,则from itertools import accumulate, chain s = "0030108102017033119080001010048000000" n = [7, 2, 8, 6, 2, 2, 5, 5] ranges = list(accumulate(n)) list(map(lambda i: s[i[0]:i[1]], zip(chain([0], ranges), ranges)) # ['0030108', '10', '20170331', '190800', '01', '01', '00480', '00000'] 将显示为错误页面。

答案 1 :(得分:1)

正确的语法是:

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并不支持此功能。

答案 2 :(得分:-2)

Ingress不支持你们在那里使用的注释! 注释仅受Service支持,Ingress正在使用主机部分。