为何设置worker_processes auto的Nginx在低CPU配额下创建50+工作进程?
当设置
worker_processes auto;且CPU请求/限制仅为200m/375m时,Nginx为何会创建50多个工作进程? 环境信息
Pod资源规格
resources: requests: memory: '512Mi' cpu: '200m' limits: memory: '768Mi' cpu: '375m'
Nginx配置
worker_processes auto; events { worker_connections 1024; }
Nginx启动日志
2024/02/15 12:17:15 [notice] 32#32: start worker processes 2024/02/15 12:17:15 [notice] 32#32: start worker process 51 2024/02/15 12:17:15 [notice] 32#32: start worker process 52 2024/02/15 12:17:15 [notice] 32#32: start worker process 56 2024/02/15 12:17:15 [notice] 32#32: start worker process 57 2024/02/15 12:17:15 [notice] 32#32: start worker process 58 2024/02/15 12:17:15 [notice] 32#32: start worker process 59 2024/02/15 12:17:15 [notice] 32#32: start worker process 60 2024/02/15 12:17:15 [notice] 32#32: start worker process 61 2024/02/15 12:17:15 [notice] 32#32: start worker process 62 2024/02/15 12:17:15 [notice] 32#32: start worker process 63 2024/02/15 12:17:15 [notice] 32#32: start worker process 64 2024/02/15 12:17:15 [notice] 32#32: start worker process 65 2024/02/15 12:17:15 [notice] 32#32: start worker process 66 2024/02/15 12:17:15 [notice] 32#32: start worker process 67 2024/02/15 12:17:15 [notice] 32#32: start worker process 68 2024/02/15 12:17:15 [notice] 32#32: start worker process 69 2024/02/15 12:17:15 [notice] 32#32: start worker process 70 2024/02/15 12:17:15 [notice] 32#32: start worker process 71 2024/02/15 12:17:15 [notice] 32#32: start worker process 72 2024/02/15 12:17:15 [notice] 32#32: start worker process 73 2024/02/15 12:17:15 [notice] 32#32: start worker process 74 2024/02/15 12:17:15 [notice] 32#32: start worker process 75 2024/02/15 12:17:15 [notice] 32#32: start worker process 76 2024/02/15 12:17:15 [notice] 32#32: start worker process 77 2024/02/15 12:17:15 [notice] 32#32: start worker process 78 2024/02/15 12:17:15 [notice] 32#32: start worker process 79 2024/02/15 12:17:15 [notice] 32#32: start worker process 80 2024/02/15 12:17:15 [notice] 32#32: start worker process 81 2024/02/15 12:17:15 [notice] 32#32: start worker process 82 2024/02/15 12:17:15 [notice] 32#32: start worker process 83 2024/02/15 12:17:15 [notice] 32#32: start worker process 84 2024/02/15 12:17:15 [notice] 32#32: start worker process 85 2024/02/15 12:17:15 [notice] 32#32: start worker process 86 2024/02/15 12:17:15 [notice] 32#32: start worker process 87 2024/02/15 12:17:15 [notice] 32#32: start worker process 88 2024/02/15 12:17:15 [notice] 32#32: start worker process 89 2024/02/15 12:17:15 [notice] 32#32: start worker process 90 2024/02/15 12:17:15 [notice] 32#32: start worker process 91 2024/02/15 12:17:15 [notice] 32#32: start worker process 92 2024/02/15 12:17:15 [notice] 32#32: start worker process 93 2024/02/15 12:17:15 [notice] 32#32: start worker process 94 2024/02/15 12:17:15 [notice] 32#32: start worker process 95 2024/02/15 12:17:15 [notice] 32#32: start worker process 96 2024/02/15 12:17:15 [notice] 32#32: start worker process 97 2024/02/15 12:17:15 [notice] 32#32: start worker process 98 2024/02/15 12:17:15 [notice] 32#32: start worker process 99 2024/02/15 12:17:15 [notice] 32#32: start worker process 100 2024/02/15 12:17:15 [notice] 32#32: start worker process 101 2024/02/15 12:17:15 [notice] 32#32: start worker process 102 2024/02/15 12:17:15 [notice] 32#32: start worker process 103 2024/02/15 12:17:15 [notice] 32#32: start worker process 104 2024/02/15 12:17:15 [notice] 32#32: start worker process 105 2024/02/15 12:17:15 [notice] 32#32: start worker process 106 2024/02/15 12:17:15 [notice] 32#32: start worker process 107 2024/02/15 12:17:15 [notice] 32#32: start worker process 108 2024/02/15 12:17:15 [notice] 32#32: start worker process 109 2024/02/15 12:17:15 [notice] 32#32: start worker process 110 2024/02/15 12:17:15 [notice] 32#32: start worker process 111 2024/02/15 12:17:15 [notice] 32#32: start worker process 112 2024/02/15 12:17:15 [notice] 32#32: start worker process 113 2024/02/15 12:17:15 [notice] 32#32: start worker process 114 2024/02/15 12:17:15 [notice] 32#32: start worker process 115 2024/02/15 12:17:15 [notice] 32#32: start worker process 116 2024/02/15 12:17:15 [notice] 32#32: start worker process 117 2024/02/15 12:17:15 [notice] 32#32: start cache manager process 118 2024/02/15 12:17:15 [notice] 32#32: start cache loader process 119
原因分析
Nginx设置worker_processes auto;时,默认会读取宿主机的物理CPU核心数来创建工作进程,完全无视Kubernetes给Pod设置的CPU请求和限制。
从日志里的进程数量来看,你的宿主机应该是一台64核左右的机器——去掉缓存管理和加载进程后,工作进程数正好匹配这个核心数。Nginx启动时直接读取宿主机的/proc/cpuinfo文件获取核心数,根本不会识别Kubernetes的CPU配额。
这种情况会引发两个严重问题:
- 大量Nginx工作进程在只有0.375核CPU配额的Pod内争抢资源,进程频繁被调度,性能大幅下降
- 超出Pod的CPU限制后,Kubernetes会触发CPU节流(throttling),进一步恶化性能
解决办法
- 手动设置
worker_processes为合理值,比如根据Pod的CPU限制设置为1或2:worker_processes 1; - 如果需要动态适配,可以通过启动脚本读取Kubernetes的CPU配额环境变量(比如
KUBERNETES_CPU_LIMIT),在启动Nginx前自动生成配置文件,设置对应的worker进程数
内容的提问来源于stack exchange,提问作者bhantol
相关产品推荐
相关产品推荐

