侧边栏壁纸
博主头像
往事随风博主等级

当你感到悲哀痛苦时,最好是去学些什么东西。学习会使你永远立于不败之地!

  • 累计撰写 20 篇文章
  • 累计创建 6 个标签
  • 累计收到 2 条评论
标签搜索

目 录CONTENT

文章目录

Kubernetes 中进行 ingress-nginx 配置优化以及HTTP请求速率限制

往事随风
2022-11-29 / 0 评论 / 1 点赞 / 589 阅读 / 1,981 字 / 正在检测是否收录...

Kubernetes中ingress-nginx优化配置

描述: 在K8s集群中部署安装 ingress-nginx 后默认并未进行相应的优化配置,本小结将针对于生产环境的中的 ingress-nginx 控制器进行优化。

我们可以从 ingress-nginx-controller 资源的 Pod 、ConfigMap 、以及业务的 ingress 规则入手。

ingress-nginx-controller Pod

我们需要针对承载 ingress-nginx 的相关 Pod 容器进行内核参数优化。

$ kubectl get deployments.apps -n ingress-nginx ingress-nginx-controller
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
ingress-nginx-controller   9/9     9            9           5d20h

$ kubectl get deployments.apps -n ingress-nginx ingress-nginx-controller  -o yaml
# 在 spec.template.spec 对象下添加一个初始化 initContainers 容器
  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
      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

ingress-nginx-controller ConfigMap

我们需要按照需要将下述K/V配置项插入到 ingress-nginx 的 configMap 里的 data 对象下。
ingress-nginx 资源查看

# 查看 Ingress-nginx 全局配置参数:
kubectl get cm -n ingress-nginx nginx-ingress-controller -o yaml

# 修改 Ingress-nginx 全局配置参数:
kubectl edit cm -n ingress-nginx nginx-ingress-controller

优化配置

# 负载工作机制,轮询
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,任选一个)
enable-brotli: "true"
brotli-level: 5
brotli-min-length: 3072
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 每个工作进程可以打开的最大文件数与同时打开最大连接数设置
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"

# upstream-keepalive 与上游Pod连接超时与最大请求数调整
upstream-keepalive-time: "30m"
upstream-keepalive-timeout: "60"
upstream-keepalive-requests: "10000"
upstream-keepalive-connections: "512"

# proxy-connect 设置 ingress-nginx 与 pstream pod 之间连接请求超时实践。
# 设置与代理服务器建立连接的超时时间(不能超过75s)
proxy-connect-timeout: "30"
# 设置将请求传输到代理服务器的超时时间(以秒为单位)(超时仅在两个连续的写操作之间设置,而不是为整个请求的传输设置)
proxy-send-timeout: "120"
# 设置从代理服务器读取响应的超时时间(以秒为单位)
proxy-read-timeout: "120"

安全配置

# 启用 modsecurity waf模块拦截常规Web攻击
enable-modsecurity: "true"

开发测试:

# 启用 nginx Opentracing 扩展, 默认值:禁用
enable-opentracing: true
  • 在 Nginx 中的 upstream
    主要是配置均衡池和调度方法.

  • 在 Nginx 中的 proxy_pass
    主要是配置代理服务器ip或服务器组的名字.

温馨提示: 在高并发场景下,我们需配置upstream-keepalive-*
相关参数, 使得 nginx 尽可能快速处理 HTTP 请求(尽量少释放并重建 TCP 连接),同时控制 nginx 内存使用量。

温馨提示: ingress nginx 与 upstream pod 建立 TCP 连接并进行通信,其中涉及 3 个超时配置我们需要重点关注。

  • proxy-read-timeout 选项 设置 nginx 与 upstream pod 之间读操作的超时时间,默认设置为 60s,当业务方服务异常导致响应耗时飙涨时,异常请求会长时间夯住 ingress 网关,我们在拉取所有服务正常请求的 P99.99 耗时之后,将网关与 upstream pod 之间读写超时均缩短到 3s,使得 nginx 可以及时掐断异常请求,避免长时间被夯住。

  • proxy-connect-timeout 选项 设置 nginx 与 upstream pod 连接建立的超时时间,默认设置为 5s,由于在 nginx 和业务均在内网同机房通信,我们将此超时时间缩短到 1s。

ingress Rule

描述: 通常我们需要为单个业务进行相应配置, 此时我们便需要再业务的ingress部署清单中进行修正。

例如: 编辑 blog.weiyigeek.top 虚拟主机站点的 ingress 规则
(kubectl edit ingress -n weiyigeek blog.weiyigeek.top)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    # 解决: 413 Request Entity Too Large
    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服务器传输
      }

Kubernetes 中 ingress-nginx 上的 HTTP 的速率限制请求

描述: 在某些情况我们可以使用ingress-nginx
针对请求速率进行请求限制。

通常我们使用如下两种方式管理通过配置映射和注释调整来完成。

# 配置映射-ConfigMap
$ kubectl get cm -n ingress-nginx ingress-nginx-controller -o yaml
data:
  http-snippet: |
    limit_req_zone $http_authorization zone=my-zone:20m rate=5r/s;
    limit_req_zone $binary_remote_addr zone=my-zone:20m rate=10r/s;
    limit_req_zone $http_someheader zone=my-zone:20m rate=20r/s;

# 配置注释-annotations
# - 入口资源中的注释:
metadata:
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      limit_req zone=my-zone-1 burst=10 nodelay;
      limit_req_log_level notice;
      limit_req_status 429;

# - 一个入口定义的不同位置的不同节流示例:
metadata:
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
      location content/images/ {
        limit_req zone=my-zone-2 burst=50 nodelay;
      }
      location content/texts/ {
        limit_req zone=my-zone-3 burst=50 nodelay;
      }
    nginx.ingress.kubernetes.io/configuration-snippet: |
      limit_req zone=my-zone-1 burst=10 nodelay;
      limit_req_log_level notice;
      limit_req_status 429;

温馨提示: 请注意 http-snippet 不允许作为注释,而只能在ConfigMap中进行映射配置!

原文地址:https://www.modb.pro/db/405641

1

评论区