|
| 1 | +--- |
| 2 | +title: Attaching Handlers to Container Lifecycle Events |
| 3 | +--- |
| 4 | + |
| 5 | +{% capture overview %} |
| 6 | + |
| 7 | +This page shows how to attach handlers to Container lifecycle events. Kubernetes supports |
| 8 | +the postStart and preStop events. Kubernetes sends the postStart event immediately |
| 9 | +after a Container is started, and it sends the preStop event immediately before the |
| 10 | +Container is terminated. |
| 11 | + |
| 12 | +{% endcapture %} |
| 13 | + |
| 14 | + |
| 15 | +{% capture prerequisites %} |
| 16 | + |
| 17 | +{% include task-tutorial-prereqs.md %} |
| 18 | + |
| 19 | +{% endcapture %} |
| 20 | + |
| 21 | + |
| 22 | +{% capture steps %} |
| 23 | + |
| 24 | +### Defining postStart and preStop handlers |
| 25 | + |
| 26 | +In this exercise, you create a Pod that has one Container. The Container has handlers |
| 27 | +for the postStart and preStop events. |
| 28 | + |
| 29 | +Here is the configuration file for the Pod: |
| 30 | + |
| 31 | +{% include code.html language="yaml" file="lifecycle-events.yaml" ghlink="/docs/tasks/configure-pod-container/lifecycle-events.yaml" %} |
| 32 | + |
| 33 | +In the configuration file, you can see that the postStart command writes a `message` |
| 34 | +file to the Container's `/usr/share` directory. The preStop command shuts down |
| 35 | +nginx gracefully. This is helpful if the Container is being terminated because of a failure. |
| 36 | + |
| 37 | +Create the Pod: |
| 38 | + |
| 39 | + kubectl create -f http://k8s.io/docs/tasks/configure-pod-container/lifecycle-events.yaml |
| 40 | + |
| 41 | +Verify that the Container in the Pod is running: |
| 42 | + |
| 43 | + kubectl get pod lifecycle-demo |
| 44 | + |
| 45 | +Get a shell into the Container running in your Pod: |
| 46 | + |
| 47 | + kubectl exec -it lifecycle-demo -- /bin/bash |
| 48 | + |
| 49 | +In your shell, verify that the `postStart` handler created the `message` file: |
| 50 | + |
| 51 | + root@lifecycle-demo:/# cat /usr/share/message |
| 52 | + |
| 53 | +The output shows the text written by the postStart handler: |
| 54 | + |
| 55 | + Hello from the postStart handler |
| 56 | + |
| 57 | +{% endcapture %} |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +{% capture discussion %} |
| 62 | + |
| 63 | +### Discussion |
| 64 | + |
| 65 | +Kubernetes sends the postStart event immediately after the Container is created. |
| 66 | +There is no guarantee, however, that the postStart handler is called before |
| 67 | +the Container's entrypoint is called. The postStart handler runs asynchronously |
| 68 | +relative to the Container's code, but Kubernetes' management of the container |
| 69 | +blocks until the postStart handler completes. The Container's status is not |
| 70 | +set to RUNNING until the postStart handler completes. |
| 71 | + |
| 72 | +Kubernetes sends the preStop event immediately before the Container is terminated. |
| 73 | +Kubernetes' management of the Container blocks until the preStop handler completes, |
| 74 | +unless the Pod's grace period expires. For more details, see |
| 75 | +[Termination of Pods](/docs/user-guide/pods/#termination-of-pods). |
| 76 | + |
| 77 | +{% endcapture %} |
| 78 | + |
| 79 | + |
| 80 | +{% capture whatsnext %} |
| 81 | + |
| 82 | +* Learn more about [Container lifecycle hooks](/docs/user-guide/container-environment/.) |
| 83 | +* Learn more about the [lifecycle of a Pod](https://kubernetes.io/docs/user-guide/pod-states/). |
| 84 | + |
| 85 | + |
| 86 | +#### Reference |
| 87 | + |
| 88 | +* [Lifecycle](https://kubernetes.io/docs/resources-reference/1_5/#lifecycle-v1) |
| 89 | +* [Container](https://kubernetes.io/docs/resources-reference/1_5/#container-v1) |
| 90 | +* See `terminationGracePeriodSeconds` in [PodSpec](/docs/resources-reference/v1.5/#podspec-v1) |
| 91 | + |
| 92 | +{% endcapture %} |
| 93 | + |
| 94 | +{% include templates/task.md %} |
0 commit comments