Ingress 自定义错误页:404、502、503 页面接管方案

来自AI助手的总结
通过自定义后端服务和配置,ingress-nginx可返回定制错误页面。
Ingress 自定义错误页:404、502、503 页面接管方案

一、打开https://github.com/kubernetes/ingress-nginx/blob/main/docs/examples/customization/custom-errors/custom-default-backend.yaml链接地址,下载custom-default-backend.yaml文件

# 下载后需要更换镜像为国内镜像
[root@k8s-master01 ~]# vim custom-default-backend.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-errors
  labels:
    app.kubernetes.io/name: nginx-errors
    app.kubernetes.io/part-of: ingress-nginx
spec:
  selector:
    app.kubernetes.io/name: nginx-errors
    app.kubernetes.io/part-of: ingress-nginx
  ports:
  - port: 80
    targetPort: 8080
    name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-errors
  labels:
    app.kubernetes.io/name: nginx-errors
    app.kubernetes.io/part-of: ingress-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: nginx-errors
      app.kubernetes.io/part-of: ingress-nginx
  template:
    metadata:
      labels:
        app.kubernetes.io/name: nginx-errors
        app.kubernetes.io/part-of: ingress-nginx
    spec:
      containers:
      - name: nginx-error-server
        image: registry.cn-hangzhou.aliyuncs.com/github_images1024/custom-error-pages:v1.1.1
        ports:
        - containerPort: 8080
        # Setting the environment variable DEBUG we can see the headers sent
        # by the ingress controller to the backend in the client response.
        # env:
        # - name: DEBUG
        #   value: "true"

        # Mounting custom error page from configMap
        # volumeMounts:
        # - name: custom_error_pages
        #   mountPath: /www

      # Mounting custom error page from configMap
      # volumes:
      # - name: custom_error_pages
      #   configMap:
      #     name: custom_error_pages
      #     items:
      #     - key: "404"
      #       path: "404.html"
      #     - key: "503"
      #       path: "503.html"

二、创建错误页面服务

[root@k8s-master01 ~]# kaf custom-default-backend.yaml -n ingress-nginx

# 验证
[root@k8s-master01 day012]# kgp -n ingress-nginx | grep nginx-errors
nginx-errors-788dcc78fc-fldn9          1/1     Running     0          31s

三、更改 ingress-nginx 的启动参数,支持自定义错误页

说明:修改完退出会自动重启

[root@k8s-master01 day012]# kg ds -n ingress-nginx
NAME                       DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                         AGE
ingress-nginx-controller   1         1         1       1            1           ingress=true,kubernetes.io/os=linux   4h54m

# 第50行下面添加- --default-backend-service=ingress-nginx/nginx-errors
...
...
- --default-backend-service=ingress-nginx/nginx-errors
...
...

四、配置 ConfigMap,定义哪些错误码被重定向到自定义错误页

# 添加custom-http-errors: "404,502,503"这行内容
[root@k8s-master01 ~]# k edit cm ingress-nginx-controller -n ingress-nginx
...
...
data:
    allow-snippet-annotations: "true"
    annotations-risk-level: Critical
    custom-http-errors: "404,502,503"
...
...

五、创建一个测试ingress

[root@k8s-master01 ~]# vim ingress-error.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  namespace: study-ingress
spec:
  ingressClassName: nginx # for k8s >= 1.22+
  rules:
  - host: test.zhang-qing.com
    http:
      paths:
      - backend:
          service:
            name: nginx
            port:
              number: 80
        path: /
        pathType: ImplementationSpecific

六、打开浏览器输入test.zhang-qing.com/adadsasdasdsadas,进行错误页面验证,观察到返回内容如下

image-20250319161339469

七、环境还原

k delete -f ingress-error.yaml
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容