|
| 1 | +--- |
| 2 | +title: Ciclos de Vida do Contêiner |
| 3 | +content_type: concept |
| 4 | +weight: 30 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- overview --> |
| 8 | + |
| 9 | +Essa página descreve como os contêineres gerenciados pelo _kubelet_ podem usar a estrutura de _hook_ de ciclo de vida do contêiner para executar código acionado por eventos durante seu ciclo de vida de gerenciamento. |
| 10 | + |
| 11 | + |
| 12 | +<!-- body --> |
| 13 | + |
| 14 | +## Visão Geral |
| 15 | + |
| 16 | +Análogo a muitas estruturas de linguagem de programação que tem _hooks_ de ciclo de vida de componentes, como angular, |
| 17 | +o Kubernetes fornece aos contêineres _hooks_ de ciclo de vida. |
| 18 | +Os _hooks_ permitem que os contêineres estejam cientes dos eventos em seu ciclo de vida de gerenciamento |
| 19 | +e executem código implementado em um manipulador quando o _hook_ de ciclo de vida correspondente é executado. |
| 20 | + |
| 21 | +## Hooks do contêiner |
| 22 | + |
| 23 | +Existem dois _hooks_ que são expostos para os contêiners: |
| 24 | + |
| 25 | +`PostStart` |
| 26 | + |
| 27 | +This hook is executed immediately after a container is created. |
| 28 | +However, there is no guarantee that the hook will execute before the container ENTRYPOINT. |
| 29 | +No parameters are passed to the handler. |
| 30 | + |
| 31 | +`PreStop` |
| 32 | + |
| 33 | +This hook is called immediately before a container is terminated due to an API request or management |
| 34 | +event such as a liveness/startup probe failure, preemption, resource contention and others. A call |
| 35 | +to the `PreStop` hook fails if the container is already in a terminated or completed state and the |
| 36 | +hook must complete before the TERM signal to stop the container can be sent. The Pod's termination |
| 37 | +grace period countdown begins before the `PreStop` hook is executed, so regardless of the outcome of |
| 38 | +the handler, the container will eventually terminate within the Pod's termination grace period. No |
| 39 | +parameters are passed to the handler. |
| 40 | + |
| 41 | +A more detailed description of the termination behavior can be found in |
| 42 | +[Termination of Pods](/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination). |
| 43 | + |
| 44 | +### Hook handler implementations |
| 45 | + |
| 46 | +Containers can access a hook by implementing and registering a handler for that hook. |
| 47 | +There are two types of hook handlers that can be implemented for Containers: |
| 48 | + |
| 49 | +* Exec - Executes a specific command, such as `pre-stop.sh`, inside the cgroups and namespaces of the Container. |
| 50 | +Resources consumed by the command are counted against the Container. |
| 51 | +* HTTP - Executes an HTTP request against a specific endpoint on the Container. |
| 52 | + |
| 53 | +### Hook handler execution |
| 54 | + |
| 55 | +When a Container lifecycle management hook is called, |
| 56 | +the Kubernetes management system execute the handler according to the hook action, |
| 57 | +`httpGet` and `tcpSocket` are executed by the kubelet process, and `exec` is executed in the container. |
| 58 | + |
| 59 | +Hook handler calls are synchronous within the context of the Pod containing the Container. |
| 60 | +This means that for a `PostStart` hook, |
| 61 | +the Container ENTRYPOINT and hook fire asynchronously. |
| 62 | +However, if the hook takes too long to run or hangs, |
| 63 | +the Container cannot reach a `running` state. |
| 64 | + |
| 65 | +`PreStop` hooks are not executed asynchronously from the signal to stop the Container; the hook must |
| 66 | +complete its execution before the TERM signal can be sent. If a `PreStop` hook hangs during |
| 67 | +execution, the Pod's phase will be `Terminating` and remain there until the Pod is killed after its |
| 68 | +`terminationGracePeriodSeconds` expires. This grace period applies to the total time it takes for |
| 69 | +both the `PreStop` hook to execute and for the Container to stop normally. If, for example, |
| 70 | +`terminationGracePeriodSeconds` is 60, and the hook takes 55 seconds to complete, and the Container |
| 71 | +takes 10 seconds to stop normally after receiving the signal, then the Container will be killed |
| 72 | +before it can stop normally, since `terminationGracePeriodSeconds` is less than the total time |
| 73 | +(55+10) it takes for these two things to happen. |
| 74 | + |
| 75 | +If either a `PostStart` or `PreStop` hook fails, |
| 76 | +it kills the Container. |
| 77 | + |
| 78 | +Users should make their hook handlers as lightweight as possible. |
| 79 | +There are cases, however, when long running commands make sense, |
| 80 | +such as when saving state prior to stopping a Container. |
| 81 | + |
| 82 | +### Hook delivery guarantees |
| 83 | + |
| 84 | +Hook delivery is intended to be *at least once*, |
| 85 | +which means that a hook may be called multiple times for any given event, |
| 86 | +such as for `PostStart` or `PreStop`. |
| 87 | +It is up to the hook implementation to handle this correctly. |
| 88 | + |
| 89 | +Generally, only single deliveries are made. |
| 90 | +If, for example, an HTTP hook receiver is down and is unable to take traffic, |
| 91 | +there is no attempt to resend. |
| 92 | +In some rare cases, however, double delivery may occur. |
| 93 | +For instance, if a kubelet restarts in the middle of sending a hook, |
| 94 | +the hook might be resent after the kubelet comes back up. |
| 95 | + |
| 96 | +### Debugging Hook handlers |
| 97 | + |
| 98 | +The logs for a Hook handler are not exposed in Pod events. |
| 99 | +If a handler fails for some reason, it broadcasts an event. |
| 100 | +For `PostStart`, this is the `FailedPostStartHook` event, |
| 101 | +and for `PreStop`, this is the `FailedPreStopHook` event. |
| 102 | +You can see these events by running `kubectl describe pod <pod_name>`. |
| 103 | +Here is some example output of events from running this command: |
| 104 | + |
| 105 | +``` |
| 106 | +Events: |
| 107 | + FirstSeen LastSeen Count From SubObjectPath Type Reason Message |
| 108 | + --------- -------- ----- ---- ------------- -------- ------ ------- |
| 109 | + 1m 1m 1 {default-scheduler } Normal Scheduled Successfully assigned test-1730497541-cq1d2 to gke-test-cluster-default-pool-a07e5d30-siqd |
| 110 | + 1m 1m 1 {kubelet gke-test-cluster-default-pool-a07e5d30-siqd} spec.containers{main} Normal Pulling pulling image "test:1.0" |
| 111 | + 1m 1m 1 {kubelet gke-test-cluster-default-pool-a07e5d30-siqd} spec.containers{main} Normal Created Created container with docker id 5c6a256a2567; Security:[seccomp=unconfined] |
| 112 | + 1m 1m 1 {kubelet gke-test-cluster-default-pool-a07e5d30-siqd} spec.containers{main} Normal Pulled Successfully pulled image "test:1.0" |
| 113 | + 1m 1m 1 {kubelet gke-test-cluster-default-pool-a07e5d30-siqd} spec.containers{main} Normal Started Started container with docker id 5c6a256a2567 |
| 114 | + 38s 38s 1 {kubelet gke-test-cluster-default-pool-a07e5d30-siqd} spec.containers{main} Normal Killing Killing container with docker id 5c6a256a2567: PostStart handler: Error executing in Docker Container: 1 |
| 115 | + 37s 37s 1 {kubelet gke-test-cluster-default-pool-a07e5d30-siqd} spec.containers{main} Normal Killing Killing container with docker id 8df9fdfd7054: PostStart handler: Error executing in Docker Container: 1 |
| 116 | + 38s 37s 2 {kubelet gke-test-cluster-default-pool-a07e5d30-siqd} Warning FailedSync Error syncing pod, skipping: failed to "StartContainer" for "main" with RunContainerError: "PostStart handler: Error executing in Docker Container: 1" |
| 117 | + 1m 22s 2 {kubelet gke-test-cluster-default-pool-a07e5d30-siqd} spec.containers{main} Warning FailedPostStartHook |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +## {{% heading "whatsnext" %}} |
| 123 | + |
| 124 | + |
| 125 | +* Learn more about the [Container environment](/docs/concepts/containers/container-environment/). |
| 126 | +* Get hands-on experience |
| 127 | + [attaching handlers to Container lifecycle events](/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/). |
| 128 | + |
0 commit comments