如何使用 K8s HPA 自动伸缩 PHP-FPM?
解读
在国内云原生面试中,“PHP-FPM 怎么横向扩” 早已不是简单“改 replica”就能过关。
面试官真正想听的是:
- 你是否理解 PHP-FPM 进程模型 与 Kubernetes 资源模型 之间的语义鸿沟;
- 能否把 业务指标(请求延迟、队列长度) 而非单纯 CPU 暴露给 HPA;
- 是否具备 生产级灰度、兜底、冷启动 的落地经验,而不是 Demo 级 YAML。
答不到这三层,基本会被追问“如果 CPU 60% 但请求全堵在 FPM 队列里,HPA 还缩容怎么办?”
知识点
- PHP-FPM 池子状态页(pm.status_path)给出的 listen queue len、active processes 是黄金指标;
- K8s 1.23+ 的 HPA v2 支持 custom metrics + external metrics,需 Prometheus Adapter 或 Kube-Prometheus-Stack 把 FPM 指标注册到 metrics.k8s.io / custom.metrics.k8s.io;
- Exporter 选型:官方 php-fpm_exporter(Go)或 nginx-lua-prometheus 二次开发,必须 ServiceMonitor 让 Prometheus 自动发现;
- 扩容阈值需按 request_per_pod * 0.7 设计,避免 Pod 冷启动 5~8 s 期间 502;
- 缩容行为要加 stabilizationWindowSeconds=300 与 selectPolicy: Min,防止 流量毛刺 把刚起的 Pod 立刻杀掉;
- HPA 与 VPA 不能同时作用于同一容器;若内存随并发线性涨,优先 VPA 更新 request/limit,HPA 只负责副本数;
- PodDisruptionBudget 与 preStop hook(sleep 15 + php-fpm -t)保证 滚动发布 不断连接;
- 国内云厂商(阿里云 ACK、腾讯云 TKE)均支持 CronHPA 与 HPA 同时生效,CronHPA 优先级更高,可用于 晚高峰提前预热;
- 压测工具 用 wrk2 + Lua 脚本 模拟 WordPress 登录态 WooCommerce 购物车,比 ab 更真实;
- 合规要求:若客户为 政府/金融,需通过 等保 2.0 镜像安全基线,php-fpm_exporter 必须 multi-arch 镜像签名,避免 “不受信镜像” 被扫描扣分。
答案
-
暴露指标
在 WordPress 容器 内启 PHP-FPM 状态页(pm.status_path = /fpm-status),Nginx location 只允许集群内 IP 访问;
Sidecar 注入 php-fpm_exporter:2.2.0,监听 9253 端口,把 fpm_active_processes、fpm_listen_queue 转成 Prometheus 格式。 -
注册自定义指标
通过 Helm 安装 kube-prometheus-stack,新增 ServiceMonitor 匹配标签 app=wordpress-phpfpm;
在 Prometheus Adapter 的 configMap 增加规则:- seriesQuery: 'fpm_active_processes{pod!=""}' resources: {template: "<<.Resource>>"} name: {matches: "fpm_active_processes", as: "fpm_active_processes"} metricsQuery: 'avg(<<.Series>>{<<.LabelMatchers>>})' -
创建 HPA
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: wordpress-phpfpm spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: wordpress-phpfpm minReplicas: 3 maxReplicas: 50 metrics: - type: Pods pods: metric: name: fpm_active_processes target: type: AverageValue averageValue: "20" # 每个 Pod 平均 20 活跃进程即扩容 behavior: scaleUp: stabilizationWindowSeconds: 60 policies: - type: Pods value: 5 periodSeconds: 30 scaleDown: stabilizationWindowSeconds: 300 policies: - type: Pods value: 1 periodSeconds: 60 -
冷启动优化
- WordPress 主题 用 Composer autoload --optimize 提前生成 classmap;
- OPcache 预加载脚本挂载到 initContainer,fpm 启动即命中缓存;
- HPA 扩容触发后,** readinessProbe** 检测 /healthz 接口返回 200 且 FPM 状态页 active < 80% 才标记为 Ready,防止流量瞬间打入半初始化 Pod。
-
灰度与兜底
- 通过 Argo Rollouts 做 canary 10% -> 30% -> 100%,HPA 仅作用于 stable ReplicaSet,避免 金丝雀被误杀;
- PDB 设置 minAvailable: 50%,集群节点故障 时 Eviction API 受控;
- HPA 失败场景(Prometheus 失联)fallback 用 KEDA 的 Cron 触发器 保持 最小副本数 = 晚高峰均值。
拓展思考
- 纵向扩容 vs 横向扩容:若 WordPress 使用 WooCommerce + object cache,单请求内存峰值 256 MB,此时 VPA 上调 memory request 比 HPA 盲目加 Pod 更省成本;
- 指标维度升级:把 “当前等待 SQL 连接数” 也写进 php-fpm_exporter,HPA 双指标(fpm_listen_queue > 8 且 mysql_threads_running > 30)才扩容,避免数据库瓶颈被掩盖;
- Serverless 化:阿里云 ACK 虚拟节点 支持 ECI 冷启动 2 s,HPA 扩容直接调度到 ECI,高峰过后缩容到 0,节省 70% 费用,但需解决 WordPress 上传目录 的 持久化(OSS + Fluid 缓存);
- 安全红线:HPA 控制循环 需要 kube-system 级权限,RBAC 必须 least-privilege,禁止把 metrics-reader 绑定到 system:anonymous,否则 等保测评 直接开 高风险整改单。