Brief introduction
This article describes how to add a statement to the processor cycle time container, Kubernetes support postStart
and preStop
events. Kubernetes sent immediately after the container start postStart
event, sent immediately before the container is terminated preStop
event.
NOTE: This document is to be understood with reference to their official documents, and. If misleading, please criticism.
And a processor preStop defined postStart
Create a container of Pod, container there postStart
and preStop
events. file name:lifecycle-events.yaml
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: lifecycle-demo-container
image: nginx
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
preStop:
exec:
command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]
In the configuration file, postStart
the command writes a message
file to the container /usr/share/message
directory. preStop
Command smooth stop nginx
. If the container is terminated due to a failure, it would be helpful.
# kubectl apply -f /root/k8s-example/pods/lifecycle-events.yaml
Pod confirm the vessel is running
# kubectl get pod lifecycle-demo
NAME READY STATUS RESTARTS AGE
lifecycle-demo 1/1 Running 0 26h
Into the vessel shell
# kubectl exec -it lifecycle-demo -- /bin/bash
Confirm the shell postStart
processor creates a message
file exists
root@lifecycle-demo:/# cat /usr/share/message
Hello from the postStart handler
After you create a container, Kubernetes sent immediately postStart
event. However, no guarantee that the calling Container entrypoint
before the first call postStart
handler. postStart
Handlers run asynchronously with respect to the code Container, but Kubernetes management of container blocks until postStart
the handler is completed. In the postStart
prior processing program is completed, the state is not set to the container RUNNING
.