k8s lifecycle 之 preStop
环境: 因容器里服务不能直接被kill -9 强制杀掉,切pid 不为1 ,因此需要在容器关掉之前进行服务关闭操作,用到k8s 功能 prestop 关闭服务脚本: $ cat stop.sh #!/bin/sh echo "" echo "begin to shutdown..." #根据端口号查一下pid PID=$(ps -ef |grep java |grep -v "grep" |awk '{print $2}') if [ ! -n "$PID" ]; then echo "" echo "pid is not exist" exit 1 fi #do not use "kill -9" for stoppint service kill $PID #看看进程还在吗 UP_STATUS=$(ps -ef |grep java |grep -v "grep" |awk '{print $2}' | wc -l) if (($UP_STATUS <= 0)); then echo "shutdown succesful..." else echo "" echo "pid = $PID" TIME_OUT=90 TIME_COUNT=0 while [ $TIME_COUNT -le $TIME_OUT ] do UP_STATUS=$(ps -ef |grep java |grep -v "grep" |awk '{print $2}' | wc -l) if (($UP_STATUS <= 0)) then TIME_COUNT=999 else TIME_COUNT=$(($TIME_COUNT+1)) fi echo "waiting for shutdown ..." sleep 1 done if (($UP_STATUS <= 0)) then echo "" echo "shutdown successful..." else echo "" ps -ef | grep "$PID" | grep -v grep | grep "$SERVER_NAME" echo "" echo "shutdown failed..." fi fi 用法: 在容器deployment 中添加: lifecycle: preStop: exec: command: - /bin/sh - /tmp/stop.sh 可解决此问题。