K8s 1.27 的集群,订单服务三个副本,某天下午全部重启了两次。kubectl get events 里一排 Unhealthy,Liveness probe failed: Get "http://10.244.1.31:8080/actuator/health": context deadline exceeded。三个副本同时被判死,重启期间 502 堆了四千多条。
探针当时是这么配的:initialDelaySeconds 10,periodSeconds 5,timeoutSeconds 1,failureThreshold 3。应用启动要 38 秒,前 30 秒端口都没监听,liveness 连失败三次就杀掉重启,重启后又慢,正好卡成循环。
第一次改把 initialDelaySeconds 提到 60,启动期过了,高峰期又出问题。Full GC 一次停 1.2 秒,timeoutSeconds 1 必然超时,连续三次就重启,一个实例重启把流量压到另外两个,连锁反应。
正确的拆法是三个探针各管各的。startupProbe 只管启动,failureThreshold 30、periodSeconds 5,给足 150 秒;liveness 管死锁,timeout 3 秒、failureThreshold 3;readiness 管摘流量,timeout 2 秒、failureThreshold 2,失败只摘不重启。
startupProbe:
httpGet: { path: /actuator/health/liveness, port: 8080 }
failureThreshold: 30
periodSeconds: 5
livenessProbe:
httpGet: { path: /actuator/health/liveness, port: 8080 }
timeoutSeconds: 3
failureThreshold: 3
readinessProbe:
httpGet: { path: /actuator/health/readiness, port: 8080 }
timeoutSeconds: 2
failureThreshold: 2
健康检查的端点也换了。原来 /actuator/health 带数据库和 Redis 检查,DB 抖一下三个副本一起被摘,整站不可用。liveness 只用 /actuator/health/liveness,只查进程和线程池,外部依赖放 readiness。
验证方法是对着 events 的时间戳看。容器重启时间如果和 Unhealthy 事件差不到 5 秒,就是探针杀的;差得多才是应用自己崩。改成现在这套之后,GC 停顿 1.5 秒只触发一次 readiness 失败,实例摘掉 10 秒又回来,用户侧没有感知。
