Skip to content

Container lifecycle hooks

Container lifecycle hooks are mechanisms triggered by the kubelet at specific points during a container's existence, allowing for the execution of custom logic before a container fully starts or before it terminates[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md].[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md]

These hooks are configured within the lifecycle field of a container specification and are managed by the container runtime[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md].[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md].

Types of Hooks

Kubernetes provides two primary lifecycle hooks:

PostStart

This hook executes immediately after the container is created[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md].[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md]

Its primary characteristics are: * Execution Timing: It runs as soon as the container is created, though it does not strictly guarantee execution before the container's ENTRYPOINT[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md][400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md].^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md]. * Purpose: It is often used for resource deployment or environment preparation^[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md]. * Failure Handling: If the PostStart hook hangs or takes too long to execute, the container will never reach the Running state^[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md].

PreStop

This hook is invoked immediately before a container is terminated[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md][400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md].

Its primary characteristics are: * Synchronous Execution: The hook is synchronous, meaning it blocks the container termination process until the hook completes[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md][400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md]. * Purpose: It is mainly used to gracefully shut down applications or notify other systems^[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md]. * Failure Handling: If the PreStop hook hangs during execution, the Pod phase will remain in the Running state and will never transition to Failed^[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md].

Configuration

Lifecycle hooks are defined in the Pod specification under spec.containers.lifecycle.^[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md].

A common handler type is exec, which runs a specific command inside the container^[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md].

Example:

apiVersion: v1
kind: [Pod](<./pod.md>)
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"]
^[400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md]

Sources

  • 400-devops__06-Kubernetes__k8s-ithelp__Day10__README.md
  • 400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes基本概念.md