k8s tomcat 进程 killed 访问 nginx 502 问题

2021-10-18 15:48:24 +08:00
 dunhanson

外部 nginx->ingress->service->pod

dockerfile 启动 catalina.sh 就不会生成 catalina.out 问题

所以第一版的 dockerfile 是这样的

第一版会存在可能 tomcat 进程挂了,但是容器没有挂导致流量会正常过来,导致用户访问返回 nginx 502 错误

sh /usr/local/tomcat/bin/startup.sh
tee /opt/health.sh <<-'EOF'
while true
do
 if test $( ps -aux | grep java | grep tomcat | wc -l ) -eq 0
 then
  ps -aux | grep catalina.out | grep -v grep | awk '{print$2}' | xargs kill -9
 fi
done
EOF
sh /opt/health.sh &
tail -f /usr/local/tomcat/logs/catalina.out

第二版做了改进,增加了一个脚本去进行健康检查(如果 tomcat 进程挂了就结束 tail 进程)

sh /usr/local/tomcat/bin/startup.sh
tee /opt/health.sh <<-'EOF'
while true
do
 if test $( ps -aux | grep java | grep tomcat | wc -l ) -eq 0
 then
  ps -aux | grep catalina.out | grep -v grep | awk '{print$2}' | xargs kill -9
 fi
done
EOF
sh /opt/health.sh &
tail -f /usr/local/tomcat/logs/catalina.out

第二版暂时没发现什么问题,不晓得还有没有其他更简便的方式?

2087 次点击
所在节点    Kubernetes
29 条回复
liuhan907
2021-10-18 22:22:14 +08:00
@dunhanson 你的探针效果很差的原因是你把间隔和超时时间设置的太长了。我设置探针一般一秒一次,超时 1s,五次就算挂了。
dunhanson
2021-10-19 09:10:03 +08:00
@liuhan907 因为负载的时候,确实有时候会存在超时的情况,不可能设置超时 1s
liuhan907
2021-10-19 13:15:49 +08:00
@dunhanson 负载高到已经能让 health check 接口都超时了,说明你的容量已经不够了,该加机器了,而不是拉大延迟。
dunhanson
2021-10-19 14:54:38 +08:00
探针会有延迟的,就算设置每次 1 秒,失败 1,超时 1s 。
经过测试,好几秒后容器才会重启
直接自己脚本编写 do while,可以马上检测到 tomcat 进程不存在,然后 kill 掉 tail 进程,容器直接重启
dunhanson
2021-10-19 14:55:04 +08:00
@salmon5 老项目呀,说改就改?
dunhanson
2021-10-19 14:55:40 +08:00
@liuhan907 生产环境很复杂的,有可能各种原因会导致超过 1s
liuhan907
2021-10-19 15:27:06 +08:00
@dunhanson 本来上了 k8s 之后你想立即就检测错误是很难的。另外健康检查接口不应该使用业务接口,另外写一个接口来做才是正确的模式。
salmon5
2021-10-19 15:29:47 +08:00
salmon5
2021-10-19 15:31:19 +08:00
readinessprobe 和 livenessprobe 是不够的,你需要 startupprobe

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/808560

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX