Ingress nginx自定义错误页面的深度定制
1、错误页面状态码
网站运行过程中难免出现问题,为用户抛出一个错误页面,常见的错误页面包含403
、404
、500
、502
、503
、504
状态码,这些常见的错误页面状态码的含义如下
-
403 Forbidden
-
404 Not Found
-
500 Internal Server Eroor
-
502 Bad Gateway
-
503 Service Unavailable
-
504 Gateway Timeout
2、在k8s中模拟错误页面
本文中涉及到的的k8s集群
版本、Ingress nginx
版本如下
# kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.4", GitCommit:"b695d79d4f967c403a96986f1750a35eb75e75f1", GitTreeState:"clean", BuildDate:"2021-11-17T15:48:33Z", GoVersion:"go1.16.10", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.4", GitCommit:"b695d79d4f967c403a96986f1750a35eb75e75f1", GitTreeState:"clean", BuildDate:"2021-11-17T15:42:41Z", GoVersion:"go1.16.10", Compiler:"gc", Platform:"linux/amd64"}
# POD_NAME=$(kubectl get pods -l app.kubernetes.io/name=ingress-nginx -n ingress-nginx -o jsonpath='{.items[0].metadata.name}')
# kubectl exec -it $POD_NAME -n ingress-nginx -- /nginx-ingress-controller --version
-------------------------------------------------------------------------------
NGINX Ingress controller
Release: v1.11.1
Build: 7c44f992012555ff7f4e47c08d7c542ca9b4b1f7
Repository: https://github.com/kubernetes/ingress-nginx
nginx version: nginx/1.25.5
-------------------------------------------------------------------------------
对于错误页面状态码,为了方便,这里模拟出404
和503
两个错误状态码页面
-
404页面
解析一个路径不存在的域名到Ingress controller
所在的节点,进行访问,页面如下
-
503页面
在k8s
中创建一个如下的Ingress
资源
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingrss-nginx-503
namespace: ratel-test1
spec:
ingressClassName: nginx
rules:
- host: example.bar.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-503
port:
number: 80
修改nginx-503配置文件
##可用 hostPath:挂在配置文件到容器测试使用挂载到/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
return 503; #修改为这个
}
#error_page 404 /404.html;
同样将对应的域名解析到Ingress controller
所在的节点进行访问,由于该Ingress
的后端nginx做了配置,因此会返回默认的503
(服务暂时不可用)
3、部署默认后端
Ingress nginx
提供了默认的自定义后端供用户使用,yaml
如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-errors
namespace: ratel-test1
labels:
app: nginx-errors
spec:
replicas: 1
selector:
matchLabels:
app: nginx-errors
template:
metadata:
labels:
app: nginx-errors
annotations:
app: nginx-errors
spec:
containers:
- name: nginx-errors
image: nginx:1.15.2
command:
- sh
- '-c'
- >-
echo 404 503 > /usr/share/nginx/html/index.html && exec nginx -g #这里显示了默认后端要展示的内容
'daemon off;'
ports:
- name: nginxport
containerPort: 8080
protocol: TCP
检查创建的资源
~]# kubectl get deploy,svc -n ratel-test1
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-404 1/1 1 1 6h3m
deployment.apps/nginx-503 1/1 1 1 5h23m
deployment.apps/nginx-error 1/1 1 1 58s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-404 ClusterIP 10.97.88.255 <none> 80/TCP 6h3m
service/nginx-503 ClusterIP 10.102.193.125 <none> 80/TCP 5h23m
service/nginx-error ClusterIP 10.96.161.36 <none> 80/TCP 58s
3.1 配置启动参数
修改Ingress controller
控制器的启动参数,加入以下配置,通过--default-backend
标志的值设置为新创建的错误后端的名称
#kubectl edit -n ingress-nginx ds ingress-nginx-controller
containers:
- name: controller
image: >-
registry.cn-hangzhou.aliyuncs.com/jiuxiang-iimages/nginx-ingress-controller:v1.11.1
args:
- /nginx-ingress-controller
- '--publish-service=$(POD_NAMESPACE)/ingress-nginx-controller'
- '--election-id=ingress-nginx-leader'
- '--controller-class=k8s.io/ingress-nginx'
- '--ingress-class=nginx'
- '--configmap=$(POD_NAMESPACE)/ingress-nginx-controller'
- '--validating-webhook=:8443'
- '--validating-webhook-certificate=/usr/local/certificates/cert'
- '--validating-webhook-key=/usr/local/certificates/key'
- '--enable-metrics=false'
- '--default-backend-service=ratel-test1/nginx-error' # ratel-test1确保这里指定了正确的命名空间和服务
3.2 修改configmap
修改对应的configmap
指定要关联到默认后端服务的服务状态码,意味着如果状态码是配置项中的值,那么返回给客户端浏览器的就是默认后端服务
# kubectl edit -n ingress-nginx cm ingress-nginx-controller
data:
allow-snippet-annotations: 'false'
custom-http-errors: 403,404,500,502,503,504
3.4 测试
通过终端命令访问上面404
和503
页面的两个域名
404域名
503域名
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
k8s Ingress nginx错误页面的深度定制-腾讯云开发者社区-腾讯云
标签:Ingress,自定义,--,ingress,nginx,controller,503 From: https://www.cnblogs.com/sl08/p/18555341