The semantics of Prestop are also very simple, and call before stopping.
But before what stops? Before the container stops? Before pod destruction?
The exact time of Prestop is called before Kubernetes sends top note to SIGTERM.
It should be noted that the SIGTERM signal is sent to the pod and the prestop hook is the container
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
lifecycle:
preStop:
exec:
command:
- sh
- -c
- sleep 30
- name: u1
command:
- sleep
- infinity
image: ubuntu
lifecycle:
preStop:
exec:
command:
- sh
- -c
- date "+%Y-%m-%d %H:%M:%S" >> /datetmp.txt && sleep 5
- name: u2
command:
- sleep
- infinity
image: ubuntu
lifecycle:
preStop:
exec:
command:
- sh
- -c
- date "+%Y-%m-%d %H:%M:%S" >> /datetmp.txt && sleep 10
In the script, nginx is blocked for 30 seconds, and finally it should be killed
There is a difference of 5 seconds between the blocking time of the two Ubuntu containers,
The observed result is that they all end together in the last 30 seconds, while the prestop script starts asynchronously at the same time