五 pod钩子 pod:pod hook和优雅的关闭nginx pod( 二 )

运行一段时间后,删除pod,在容器关闭之前执行preStop的命令,preStop执行完成之后,主程序还要运行10000秒,但是宽限期terminationGracePeriodSeconds只有600s,所以600秒之后pod被删除
[root@k8scloude1 pod]# kubectl delete pod pod3pod "pod3" deleted^C#强制删除pod[root@k8scloude1 pod]# kubectl delete pod pod3 --forcewarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "pod3" force deleted四.如何优雅的关闭nginx pod说明: 当一个 Pod 被删除时,执行kubectl get pod 命令会展示这个 Pod 的状态为 Terminating(终止) 。这个 Terminating 状态并不是 Pod 阶段之一 。Pod 被赋予一个可以体面终止的期限,默认为 30 秒 。你可以使用 --force 参数来强制终止 Pod 。
由于nginx默认是fast shutdown,关闭的时间一般小于30秒,如果想优雅的关闭nginx,可以在关闭容器之前运行nginx -s quit ,达到优雅的关闭nginx的效果
[root@k8scloude1 pod]# vim pod5.yaml #preStop处理函数指定在容器关闭之前执行"/bin/sh","-c","/usr/sbin/nginx -s quit"[root@k8scloude1 pod]# cat pod5.yamlapiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: pod5name: pod5spec:terminationGracePeriodSeconds: 600containers:- image: nginxcommand: ["sh","-c","date > /tmp/aa.txt ; sleep 10000"]imagePullPolicy: IfNotPresentname: n1resources: {}lifecycle:postStart:exec:command: ["/bin/sh","-c","date >> /tmp/bb.txt"]preStop:exec:command: ["/bin/sh","-c","/usr/sbin/nginx -s quit"]dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}[root@k8scloude1 pod]# kubectl apply -f pod5.yamlpod/pod5 created[root@k8scloude1 pod]# kubectl get podNAMEREADYSTATUSRESTARTSAGEpod51/1Running05s[root@k8scloude1 pod]# kubectl delete pod pod5pod "pod5" deleted^C[root@k8scloude1 pod]# kubectl delete pod pod5 --forcewarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "pod5" force deleted

经验总结扩展阅读