主要从 ingress-nginx-controller 资源的 Pod 、ConfigMap以及业务的 ingress 规则入手。
1、ingress-nginx-controller Pod
1.1 主容器内核参数优化
# 在 values.yaml 文件中修改 extraInitContainers 添加一个初始化 initContainers 容器
- name: sysctl
image: alpine:3.10
imagePullPolicy: IfNotPresent
command:
- sh
- -c
-|
mount -o remount rw /proc/sys
sysctl -w net.core.somaxconn=65535 # 最大连接数
sysctl -w net.ipv4.tcp_tw_reuse=1 # 允许复用tw
sysctl -w net.ipv4.ip_local_port_range="1024 65535" # 指定源端口范围
sysctl -w fs.file-max=1048576 # 最大文件句柄数
sysctl -w fs.inotify.max_user_instances=16384
sysctl -w fs.inotify.max_user_watches=524288
sysctl -w fs.inotify.max_queued_events=16384
securityContext:
privileged: true
更新验证:
$ helm upgrade ingress-nginx -f values.yaml -n ingress-nginx .
查看 Ingress-nginx 全局配置参数:
$ kubectl get ds -n ingress-nginx ingress-nginx-controller -o yaml
2、ingress-nginx-controller ConfigMap
ConfigMap – Ingress-Nginx Controller (kubernetes.github.io)
将下述K/V配置项插入到 ingress-nginx 的 configMap 里的 data 对象下。
# 在 values.yaml 文件中修改 config 添加参数
config:
load-balance: "round_robin"
2.1 优化配置
# 负载工作机制,轮询
load-balance: "round_robin"
# 错误日志等级设置 (debug, info, notice, warn, error, crit, alert, or emerg)
error-log-level: "notice"
# 启用Gzip资源压缩 (3k以上)
use-gzip: "true"
gzip-level: "2"
gzip-min-length: "3072"
gzip-types: "text/html text/plain text/css text/javascript application/javascript application/x-javascript application/xml application/x-httpd-php application/x-font-ttf application/json image/x-icon image/svg+xml image/avif image/webp font/ttf font/opentype"
# 不建议进行照片压缩 image/jpeg image/gif image/png 可能反而会增加其体积
# 启用Brotli资源压缩(同等条件下优于Gzip,任选一个)
# 参考: https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#enable-brotli
enable-brotli: "true"
brotli-level: 5 # 范围1-11
brotli-types: "text/plain text/css text/javascript application/javascript application/x-javascript application/xml application/x-httpd-php application/x-font-ttf image/x-icon image/svg+xml image/avif image/webp font/ttf font/opentype"
# 不建议进行照片压缩 image/jpeg image/gif image/png 可能反而会增加其体积
# 启用http2支持(实际上默认是开启的,如果过关闭请将其设置为true)
use-http2: "true"
# ssl 会话复用
ssl_session_cache: "shared:SSL:10m;" ssl-session-timeout: "10m"
# worker 每个工作进程可以打开的最大文件数与同时打开最大连接数设置
# 参考: https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#max-worker-connections
worker-processes: "auto"
max-worker-open-files: "10240"
max-worker-connections: "32767"
# 连接复用
enable-multi-accept: "true"
# keep-alive 连接超时和最大请求数调整
keep-alive: "75"
keep-alive-requests: "10000"
# 参考: https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#upstream-keepalive-connections
# upstream-keepalive 与上游Pod连接超时与最大请求数调整
upstream-keepalive-time: "30m"
upstream-keepalive-timeout: "60"
upstream-keepalive-requests: "10000"
upstream-keepalive-connections: "512"
# 参考:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#proxy-read-timeout
# 参考:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#proxy-send-timeout
# proxy-connect 设置 ingress-nginx 与 upstream pod 之间连接请求超时实践。
# 设置与代理服务器建立连接的超时时间(不能超过75s)
proxy-connect-timeout: "1"
# 设置将请求传输到代理服务器的超时时间(以秒为单位)(超时仅在两个连续的写操作之间设置,而不是为整个请求的传输设置)
proxy-send-timeout: "3"
# 设置从代理服务器读取响应的超时时间(以秒为单位)
proxy-read-timeout: "3"
ingress-nginx 资源查看
# 查看 Ingress-nginx 全局配置参数:
kubectl get cm -n ingress-nginx ingress-nginx-controller -o yaml
Nginx 全局配置通过 configmap 配置(Nginx Ingress Controller 会 watch 并自动 reload 配置)。
2.2 安全配置
ModSecurity (http://modsecurity.org/) 是一个开源的Web Application防火墙, 可以为一组特定的入口位置启用它。
必须首先通过在 ConfigMap 中启用 ModSecurity 来启用 ModSecurity 模块。
建议的配置参考 :https://github.com/SpiderLabs/ModSecurity/blob/v3/master/modsecurity.conf-recommended
# 启用 modsecurity waf模块拦截常规Web攻击
enable-modsecurity: "true"
2.3 分布式跟踪
参考:https://kubernetes.github.io/ingress-nginx/user-guide/third-party-addons/opentracing/
启用 NGINX 服务的请求,通过 OpenTracing 项目进行分布式跟踪。
使用第三方模块 opentracing-contrib/nginx-opentracing(https://github.com/opentracing-contrib/nginx-opentracing) ,NGINX 入口控制器可以配置 NGINX 以启用 OpenTracing(http://opentracing.io/) 检测。 默认情况下,此功能处于禁用状态。
用法:
要启用检测,我们必须在配置 ConfigMap 中启用 OpenTracing:
data:
enable-opentracing: "true"
要为单个 Ingress 启用或禁用检测,请使用 enable-opentracing注解:
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/enable-opentracing: "true"
2.4 总结
- 在 Nginx 中的 upstream 主要是配置均衡池和调度方法.
- 在 Nginx 中的 proxy_pass 主要是配置代理服务器ip或服务器组的名字.
- 常用的压缩算法是 gzip(Ingress-nginx也是默认使用gzip),据说brotli要比gzip高出20%至30%的压缩率。默认的压缩算法是gzip,压缩级别为1,如需要启用 brotli,需要配置以下三个参数:
- enable-brotli: true 或 false,是否启用brotli压缩算法。
- brotli-level: 压缩级别,范围1~11,默认为4,级别越高,越消耗CPU性能。
- brotli-types: 由brotli即时压缩的MIME类型。
- 在高并发场景下,我们需配置
upstream-keepalive-*
相关参数, 使得 nginx 尽可能快速处理 HTTP 请求(尽量少释放并重建 TCP 连接),同时控制 nginx 内存使用量。 - Ingress nginx 与 upstream pod 建立 TCP 连接并进行通信,其中涉及 3 个超时配 置我们需要重点关注。
- proxy-read-timeout / proxy-send-timeout: 设置 nginx 与 upstream pod 之间读操作的超时时间,默认设置为 60s,当业务方服务异常导致响应耗时飙涨时,异常请求会长时间夯住 ingress 网关,我们在拉取所有服务正常请求的 P99.99 耗时之后,将网关与 upstream pod 之间读写超时均缩短到 3s,使得 nginx 可以及时掐断异常请求,避免长时间被夯住。
- proxy-connect-timeout: 设置 nginx 与 upstream pod 连接建立的超时时间,默认设置为 5s,由于在 nginx 和业务均在内网同机房通信,我们将此超时时间缩短到 1s。
3、ingress Rule
描述:通常我们需要为单个业务进行相应调优配置, 此时我们就需要在业务的ingress上做修改。
例如:编辑 java.kubernets.cn 虚拟主机站点的 ingress 规则 (kubectl edit ingress -n default java-ingress-nginx
)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
# 解决: 413 Request Entity Too Large
nginx.ingress.kubernetes.io/proxy-body-size: "50m"
# 解决:后端大文件上传问题
nginx.ingress.kubernetes.io/client-body-buffer-size: 50m
nginx.ingress.kubernetes.io/proxy-max-temp-file-size: 100m
# 解决: 上传文件较慢问题
nginx.ingress.kubernetes.io/proxy-buffer-size: 50m
nginx.ingress.kubernetes.io/proxy-buffering: "on"
nginx.ingress.kubernetes.io/proxy-buffers-number: "4"
# 解决: 与后端backend超时问题
nginx.ingress.kubernetes.io/proxy-connect-timeout: 300s
nginx.ingress.kubernetes.io/proxy-read-timeout: 180s
nginx.ingress.kubernetes.io/proxy-send-timeout: 180s
# 解决: 处理Nginx代理转发与后端服务文件上传缓存区设置(原生命令)
nginx.ingress.kubernetes.io/server-snippet: |
location ~ /fastfile {
client_max_body_size 1024m; # 允许客户端请求的最大单文件字节数,人话:能上传多大文件
client_body_buffer_size 10m; # 缓冲区代理缓冲用户端请求的最大字节数,人话:一次能接受多少文件,建议根据带宽上限设置,减少磁盘读写,加快速度
proxy_connect_timeout 300; # Nginx与后端代理连接超时时间
proxy_read_timeout 300; # 后端服务器响应时间(代理接收超时)时间
proxy_buffer_size 1024k; # 设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 6 500k; # proxy_buffers缓冲区,网页平均在32k以下的话>,这样设置
proxy_busy_buffers_size 1024k; # 高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 1024k; # 设定缓存文件夹大小,大于这个值将从upstream服务器传输
}