为 k8s nginx ingress controller添加错误页面

创建错误页面

kubectl apply -f error-page.yaml -n ingress-nginx

apiVersion: v1
kind: ConfigMap
metadata:
  name: error-page
  namespace: ingress-nginx
data:
  404.html: |-
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="UTF-8" http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>404-对不起!您访问的页面不存在</title>
    <style type="text/css">
    .head404{ width:580px; height:234px; margin:50px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/head404.png) no-repeat; }
    .txtbg404{ width:499px; height:169px; margin:10px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/txtbg404.png) no-repeat;}
    .txtbg404 .txtbox{ width:390px; position:relative; top:30px; left:60px;color:#eee; font-size:13px;}
    .txtbg404 .txtbox p {margin:5px 0; line-height:18px;}
    .txtbg404 .txtbox .paddingbox { padding-top:15px;}
    .txtbg404 .txtbox p a { color:#eee; text-decoration:none;}
    .txtbg404 .txtbox p a:hover { color:#FC9D1D; text-decoration:underline;}
    </style>
    </head>
    <body bgcolor="#494949">
    <div class="head404"></div>
    </body>
    </html>

可以将yaml中的html代码替换掉。

创建backend

kubectl apply -f backend.yaml -n ingress-nginx

---
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
  - port: 443
    targetPort: 8080
    name: https
---
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: quay.io/kubernetes-ingress-controller/custom-error-pages-amd64:0.3
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: error-page
          mountPath: /www/404.html
          subPath: 404.html
      volumes:
      - name: error-page
        configMap:
          name: error-page
        # 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"

修改ingress nginx配置

kubectl edit cm nginx-configuration -n ingress-nginx
添加 custom-http-errors: 404,503 , 意思是 404和503错误会定向的刚才定义的错误页面。

apiVersion: v1
data:
  custom-http-errors: 404,503
  fastcgi_connect_timeout: 600s
  fastcgi_read_timeout: 600s
  fastcgi_send_timeout: 600s
  proxy-body-size: 2048m
  proxy-protocol: "True"
  proxy_connect_timeout: 600s
  proxy_intercept_errors: "True"
  proxy_read_timeout: 600s
  proxy_send_timeout: 600s
  real-ip-header: X-Forwarded-For
  use-forwarded-headers: "true"
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"ConfigMap","metadata":{"annotations":{},"labels":{"app.kubernetes.io/name":"ingress-nginx","app.kubernetes.io/part-of":"ingress-nginx"},"name":"nginx-configuration","namespace":"ingress-nginx"}}
  creationTimestamp: "2019-12-23T13:06:06Z"
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  name: nginx-configuration
  namespace: ingress-nginx
  resourceVersion: "70153039"
  selfLink: /api/v1/namespaces/ingress-nginx/configmaps/nginx-configuration
  uid: 079c4b59-295b-4752-8911-a327e549fc6f

修改ingress ngixn args

编辑ingress部署文件 kubectl edit deploy nginx-ingress-controller -n ingress-nginx
在启动的args添加 --default-backend-service=ingress-nginx/nginx-errors

验证

执行 kubectl get pod -n ingress-nginx 查看pod是否已经重启过, 如果没有自动重启,需要杀掉pod。
打开一个不存在的链接, 查看是否显示的是定义的错误页面。
部分浏览器不支持页面显示,如谷歌浏览器会显示“无法显示此网站”

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Kubernetes 官方提供的 NGINX Ingress Controller 进行部署,具体步骤如下: 1. 创建一个名为 `nginx-ingress` 的命名空间: ``` kubectl create namespace nginx-ingress ``` 2. 添加官方的 NGINX Ingress Controller Helm Chart 仓库: ``` helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx helm repo update ``` 3. 安装 NGINX Ingress Controller: ``` helm install nginx-ingress ingress-nginx/ingress-nginx \ --namespace nginx-ingress \ --set controller.replicaCount=2 \ --set controller.nodeSelector."beta\.kubernetes\.io/os"=linux \ --set defaultBackend.nodeSelector."beta\.kubernetes\.io/os"=linux ``` 其中,`controller.replicaCount` 用于设置控制器的副本数量,`controller.nodeSelector."beta\.kubernetes\.io/os"=linux` 和 `defaultBackend.nodeSelector."beta\.kubernetes\.io/os"=linux` 用于设置控制器和默认后端 Pod 的 NodeSelector,确保它们只会在 Linux 节点上运行。 4. 验证 NGINX Ingress Controller 是否已经成功运行: ``` kubectl get pods -n nginx-ingress ``` 如果控制器和默认后端 Pod 的状态都显示为 Running,则表示已经成功部署 NGINX Ingress Controller。 5. 部署 NGINX Ingress: ``` apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: example-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: /$1 spec: rules: - host: example.com http: paths: - path: /example(/|$)(.*) pathType: Prefix backend: service: name: example-service port: name: http ``` 其中,`metadata.name` 用于设置 Ingress 的名称,`spec.rules.host` 用于设置 Ingress 的域名,`spec.rules.http.paths.path` 用于设置 Ingress 的路径,`spec.rules.http.paths.backend.service.name` 和 `spec.rules.http.paths.backend.service.port.name` 用于设置 Ingress 的后端服务和端口。 6. 验证 NGINX Ingress 是否已经成功部署: ``` kubectl get ingress example-ingress ``` 如果 Ingress 的状态显示为 Running,则表示已经成功部署 NGINX Ingress

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值