32-容器的生命周期postStart和preStop

一、容器的生命周期postStart和preStop

[root@master231 probe]# cat 09-deploy-lifecycle-postStart-preStop.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-poststart-prestop-001
spec:
  volumes:
  - name: data
    hostPath:
      path: /dingzhiyan-linux
  # 在pod优雅终止时,定义延迟发送kill信号的时间,此时间可用于pod处理完未处理的请求等状况。
  # 默认单位是秒,若不设置默认值为30s。
  #terminationGracePeriodSeconds: 60
  terminationGracePeriodSeconds: 3
  containers:
  - name: c1
    image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
    volumeMounts:
    - name: data
      mountPath: /data
    # 定义容器的生命周期。
    lifecycle:
      # 容器启动之后做的事情,如果次函数未执行完成,容器始终处于:' ContainerCreating'状态。
      postStart:
        exec:
          command: 
          - "/bin/sh"
          - "-c"
          - "sleep 30;echo \"postStart at $(date +%F_%T)\" >> /data/postStart.log"
      # 容器停止之前做的事情
      preStop:
        exec:
         command: 
         - "/bin/sh"
         - "-c"
         - "sleep 20;echo \"preStop at $(date +%F_%T)\" >> /data/preStop.log"

二、kubelet启动容器的原理图解

1.kubelet创建Pod的全流程

  • 1.kubelet调用CRI接口创建容器,底层支持docker|containerd作为容器运行时;
  • 2.底层基于runc(符合OCI规范)创建容器:
  • 3.优先创建pause基础镜像;
  • 4.创建初始化容器
  • 5.业务容器,业务容器如果定义了优雅终止,探针则顺序如下:
    • 5.1 启动命令【COMMAND】
    • 5.2 启动postStart;
    • 5.3 Probe
      - StartupProbe
      - LivenessProbe | readinessProbe
    • 5.4 启动PreStop
      受限于优雅终止时间(默认30s)。

2.测试案例

[root@master231 pods]# cat 25-shaonao-workflow.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pods-workflow-001
spec:
  volumes:
  - name: data
    hostPath:
      path: /dingzhiyan-shaonao
  initContainers:
  - name: init01
    image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
    volumeMounts:
    - name: data
      mountPath: /dingzhiyan
    command:
    - "/bin/sh"
    - "-c"
    - "echo \"initContainer at $(date +%F_%T)\" > /dingzhiyan/haha.log"
  terminationGracePeriodSeconds: 3
  containers:
  - name: c1
    image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v3
    command:
    - /bin/sh
    - -c
    - "echo \"command at $(date +%F_%T)\" >> /usr/share/nginx/html/haha.log; sleep 600"
    volumeMounts:
    - name: data
      mountPath: /usr/share/nginx/html
    imagePullPolicy: IfNotPresent
    livenessProbe:
      exec:
        command:
        - "/bin/sh"
        - "-c"
        - "echo \"livenessProbe at $(date +%F_%T)\" >> /usr/share/nginx/html/haha.log"
      failureThreshold: 3
      initialDelaySeconds: 0
      periodSeconds: 3
      successThreshold: 1
      timeoutSeconds: 1
    readinessProbe:
      exec:
        command:
        - "/bin/sh"
        - "-c"
        - "echo \"readinessProbe at $(date +%F_%T)\" >> /usr/share/nginx/html/haha.log"
      failureThreshold: 3
      initialDelaySeconds: 0
      periodSeconds: 3
      successThreshold: 1
      timeoutSeconds: 1
    startupProbe:
      exec:
        command:
        - "/bin/sh"
        - "-c"
        - "echo \"startupProbe at $(date +%F_%T)\" >> /usr/share/nginx/html/haha.log"
      failureThreshold: 3
      initialDelaySeconds: 0
      periodSeconds: 3
      successThreshold: 1
      timeoutSeconds: 1
    lifecycle:
      postStart:
        exec:
          command: 
          - "/bin/sh"
          - "-c"
          - "sleep 10;echo \"postStart at $(date +%F_%T)\" >> /usr/share/nginx/html/haha.log"
      preStop:
        exec:
          command: 
          - "/bin/sh"
          - "-c"
          - "echo \"preStop at $(date +%F_%T)\" >> /usr/share/nginx/html/haha.log;sleep 30"
[root@master231 pods]# 	

3.测试验证

[root@worker233 ~]# tail -100f  /dingzhiyan-shaonao/haha.log 
initContainer at 2025-04-20_07:34:44
command at 2025-04-20_07:34:45
postStart at 2025-04-20_07:34:55
startupProbe at 2025-04-20_07:34:56
readinessProbe at 2025-04-20_07:34:56
livenessProbe at 2025-04-20_07:34:59
readinessProbe at 2025-04-20_07:34:59
readinessProbe at 2025-04-20_07:35:02
livenessProbe at 2025-04-20_07:35:02
livenessProbe at 2025-04-20_07:35:05
...
preStop at 2025-04-20_07:36:29

三、实现端口转发

1.准备Pod

[root@master231 scheduler]# kubectl get pods -o wide
NAME                                      READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
scheduler-nodeselector-774bf9875f-8hs2f   1/1     Running   0          28s   10.100.203.133   worker232   <none>           <none>
scheduler-nodeselector-774bf9875f-mr8zf   1/1     Running   0          28s   10.100.203.147   worker232   <none>           <none>
scheduler-nodeselector-774bf9875f-tn5rp   1/1     Running   0          28s   10.100.203.140   worker232   <none>           <none>
scheduler-nodeselector-774bf9875f-vbzs2   1/1     Running   0          28s   10.100.203.141   worker232   <none>           <none>
scheduler-nodeselector-774bf9875f-wstzv   1/1     Running   0          28s   10.100.203.144   worker232   <none>           <none>
[root@master231 scheduler]# 

2.端口转发

[root@master231 scheduler]# kubectl port-forward po/scheduler-nodeselector-774bf9875f-8hs2f 9999:80 --address=0.0.0.0
Forwarding from 0.0.0.0:9999 -> 80
Handling connection for 9999
Handling connection for 9999

3.测试验证

http://10.0.0.231:9999/
posted @ 2025-05-21 13:19  丁志岩  阅读(31)  评论(0)    收藏  举报