HTTP(S)负载均衡器 - ingress
Ingress 是 kubernetes API 的标准资源类型之一, 其本质就是一组基于 DNS 名称 (host) 或 URL 路径把请求转发至指定的 Service 资源的规则, 用于将集群外的请求流量转发至集群内部完成服务发布.
Ingress 控制器 (Ingress Controller) 可以由任何具有反向代理 (HTTP/HTTPS) 功能的服务程序实现, 如 Nginx,Envoy,HAProxy,Vulcand 和 Traefik 等. Ingress 控制器本身也作为 Pod 对象与被代理的运行为 Pod 资源的应用运行于同一网络中. 我们在这里选择了 NGINX Ingress Controller, 由于对 NGINX 的配置较为熟悉, 同时我们使用的 kubernetes 是阿里云的容器服务, 构建集群的时候, 容器服务会自带 NGINX Ingress Controller.
根据实际情况 Ingress 调优
解决 400 Request Header Or Cookie Too Large 问题
现象: 微信小程序需要调用后端接口, 需要在 header 中传一段很长的 token 参数, 直接使用浏览器访问该端口可以访问通, 但是在加上 token 访问之后, 会报 "400 Request Header Or Cookie Too Large"
- <html>
- <head>
- <title>400 Request Header Or Cookie Too Large</title>
- </head>
- <body>
- <center>
- <h1>400 Bad Request</h1>
- </center>
- <center>Request Header Or Cookie Too Large</center>
- <hr>
- <center>nginx/1.15.6</center>
- </body>
- </HTML>
问题定位: 直接修改 Service 使用 nodeport 的形式访问, 则没有报错, 初步定位需要在 ingress 中 nginx 配置客户端的请求头, 进入 Ingress Controller 的 Pod 查询配置, 果然是请求头空间不足.
- www-data@nginx-ingress-controller-5vx7x:/etc/nginx$ cat nginx.conf | grep client_header_buffer_size
- client_header_buffer_size 1k;
- www-data@nginx-ingress-controller-5vx7x:/etc/nginx$ cat nginx.conf | grep large_client_header_buffers
- large_client_header_buffers 4 8k;
解决方法: 在 ingress 中添加注释
- nginx.ingress.kubernetes.io/server-snippet: client_header_buffer_size 2046k;
- Server snippet
- Using the annotation nginx.ingress.kubernetes.io/server-snippet it is possible to add custom configuration in the server configuration block.
该注释是将自定义配置加入 nginx 的 server 配置中
解决请求超时问题
现象: 有一个数据导出功能, 需要将大量数据进行处理, 然后以 Excel 格式返回, 在导出一个大约 3W 条数据的时候, 出现访问超时情况.
解决方法: 调整 proxy_read_timeout, 连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理
在 ingress 中添加注释
nginx.ingress.kubernetes.io/proxy-read-timeout: 600
这里需要注意的事该注释的 value 需要时 number 类型, 不能加 s, 否则将不生效
总结
使用 NGINX ingress controller 的好处就是对于 nginx 配置相对比较熟悉, 性能也不差. 相关 nginx 配置的对应的 ingress 可以在 上查到.
来源: https://yq.aliyun.com/articles/696551