博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

K8S基础 - 04Pod生命周期

Posted on 2021-11-28 09:01  Kingdomer  阅读(836)  评论(0编辑  收藏  举报

K8S基础 - 04Pod生命周期

一、Pod生命周期

状态: Pending, Running, Failed, Succeeded, Unknown

官网文档: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase

 

Pending     The Pod has been accepted by the Kubernetes cluster, but one or more of the containers has not been set up and made ready to run. 
            This includes time a Pod spends waiting to be scheduled as well as the time spent downloading container images over the network.

Running     The Pod has been bound to a node, and all of the containers have been created. 
            At least one container is still running, or is in the process of starting or restarting.

Succeeded   All containers in the Pod have terminated in success, and will not be restarted.

Failed      All containers in the Pod have terminated, and at least one container has terminated in failure. 
            That is, the container either exited with non-zero status or was terminated by the system.

Unknown     For some reason the state of the Pod could not be obtained. 
            This phase typically occurs due to an error in communicating with the node where the Pod should be running.

 

Pod生命周期

 

二、初始化容器

 

三、 容器探测

livenessProbe (存活检查)   # 如果检查失败, 将杀死容器, 根据Pod的restartPolicy来操作

readinessProbe(就绪检查)   # 如果检查失败, Kubernetes会把Pod从Service Endpoints中剔除。

Probe三种检查方法:

  • httpGet:   发送HTTP请求, 返回200-400范围内状态码为成功
  • exec:      执行shell命令返回状态码为0表示成功。
  • tcpSocket: 发起TCP Socket建立成功。

3.1 livenessProbe

[root@k8s-master pod-k8s]# cat pod-live.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-live-exec
  namespace: default
spec:
  containers:
  - name: container-live-exec
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh", "-c", "touch /tmp/healthy-test; sleep 60; rm -rf /tmp/healthy-test; sleep 3600"]
    livenessProbe:
      exec:
        command: ["test", "-e", "/tmp/healthy-test"]
      initialDelaySeconds: 1   # 容器初始化后 多久开始执行 livenessProbe探测
      periodSeconds: 3         # 探测执行频率

 

[root@k8s-master ~]# kubectl create -f pod-live.yaml 
pod/pod-live-exec created
[root@k8s-master ~]# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
pod-live-exec                 1/1     Running   0          7s

[root@k8s-master ~]# kubectl describe pod pod-live-exec

  State:          Running
      Started:      Sat, 23 Oct 2021 16:37:40 +0800

  State:          Running
      Started:      Sat, 23 Oct 2021 16:39:19 +0800
    Last State:     Terminated
      Reason:       Error
      Exit Code:    137
      Started:      Sat, 23 Oct 2021 16:37:40 +0800
      Finished:     Sat, 23 Oct 2021 16:39:18 +0800
Events:
  Type     Reason     Age                  From               Message
  ----     ------     ----                 ----               -------
  Normal   Scheduled  2m42s                default-scheduler  Successfully assigned default/pod-live-exec to k8s-node32.bearpx.com
  Warning  Unhealthy  93s (x3 over 99s)    kubelet            Liveness probe failed:
  Normal   Killing    93s                  kubelet            Container container-live-exec failed liveness probe, will be restarted
  Normal   Pulled     63s (x2 over 2m42s)  kubelet            Container image "busybox" already present on machine
  Normal   Created    62s (x2 over 2m42s)  kubelet            Created container container-live-exec
  Normal   Started    62s (x2 over 2m41s)  kubelet            Started container container-live-exec

[root@k8s-master ~]# kubectl get pods
NAME                    READY   STATUS        RESTARTS    AGE
pod-live-exec           1/1     Running       2           4m2s

3.2 liveness HTTPGET方式

[root@k8s-master pod-k8s]# cat pod-live-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-live-httpget
  namespace: default
spec:
  containers:
  - name: container-live-httpget
    image: sun2010wg/my-nginx:v2
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 81
    livenessProbe:
      httpGet:
        port: http
      initialDelaySeconds: 1
      periodSeconds: 3

  

Events:
  Type     Reason     Age               From               Message
  ----     ------     ----              ----               -------
  Normal   Scheduled  16s               default-scheduler  Successfully assigned default/pod-live-httpget to k8s-node32.bearpx.com
  Normal   Killing    9s                kubelet            Container container-live-httpget failed liveness probe, will be restarted
  Normal   Pulled     8s (x2 over 17s)  kubelet            Container image "sun2010wg/my-nginx:v2" already present on machine
  Normal   Created    8s (x2 over 17s)  kubelet            Created container container-live-httpget
  Normal   Started    8s (x2 over 16s)  kubelet            Started container container-live-httpget
  Warning  Unhealthy  3s (x5 over 15s)  kubelet            Liveness probe failed: Get "http://10.244.3.14:81/": dial tcp 10.244.3.14:81: connect: connection refused

[root@k8s-master ~]# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
pod-live-httpget              1/1     Running   1          2m38s

3.3 readinessProbe

[root@k8s-master pod-k8s]# cat pod-read-httpget.yml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-live-httpget
  namespace: default
spec:
  containers:
  - name: container-live-httpget
    image: sun2010wg/my-nginx:v2
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    readinessProbe:
      httpGet:
        port: http
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3

  

### 1. Pod正常启动
Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 28s default-scheduler Successfully assigned default/pod-live-readiness to k8s-node33.bearpx.com Normal Pulled 27s kubelet Container image "sun2010wg/my-nginx:v2" already present on machine Normal Created 27s kubelet Created container container-live-httpget Normal Started 27s kubelet Started container container-live-httpget ### 2. 删除容器的index.html [root@k8s-master pod-k8s]# kubectl exec -it pod-live-readiness -- /bin/sh /usr/share/nginx/html # rm -rf index.html ### 3. Pod内的容器READY数量变化 [root@k8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-live-readiness 0/1 Running 0 103s ### 4. Pod检测到Unhealthy Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 3m25s default-scheduler Successfully assigned default/pod-live-readiness to k8s-node33.bearpx.com Normal Pulled 3m25s kubelet Container image "sun2010wg/my-nginx:v2" already present on machine Normal Created 3m25s kubelet Created container container-live-httpget Normal Started 3m25s kubelet Started container container-live-httpget Warning Unhealthy 58s (x22 over 2m1s) kubelet Readiness probe failed: HTTP probe failed with statuscode: 404 ### 5. 新增index.html /usr/share/nginx/html # echo "123" > index.html ### 6. Pod内的容器恢复正常 [root@k8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-live-readiness 1/1 Running 0 3m58s [root@k8s-master ~]# curl 10.244.2.32 123

  

四、 Post Start 

# 执行失败
apiVersion: v1 kind: Pod metadata: name: pod-poststart namespace: default spec: containers: - name: busybox-httpd image: busybox imagePullPolicy: IfNotPresent lifecycle: postStart: exec: command: ["/bin/sh","-c","mkdir -p /data/web/html; echo 'Home Page' >> /data/web/html/home.html"] command: ["/bin/httpd"] args: ["-f","-h /data/web/html"]

 

在容器启动时, 需要 /data/web/html, 容器启动失败 

Events:
  Type     Reason               Age                From               Message
  ----     ------               ----               ----               -------
  Normal   Scheduled            40s                default-scheduler  Successfully assigned default/pod-poststart to k8s-node32.bearpx.com
  Normal   Pulled               24s (x3 over 41s)  kubelet            Container image "busybox" already present on machine
  Normal   Created              24s (x3 over 40s)  kubelet            Created container busybox-httpd
  Normal   Started              24s (x3 over 40s)  kubelet            Started container busybox-httpd
  Warning  FailedPostStartHook  24s (x3 over 40s)  kubelet            Exec lifecycle hook ([/bin/sh -c mkdir -p /data/web/html; echo 'Home Page' >> /data/web/html/home.html]) 
                                                                      for Container "busybox-httpd" in Pod "pod-poststart_default(6b5e789f-8213-4a6f-9c38-bb34889005df)" failed - error: 
                                command '/bin/sh -c mkdir -p /data/web/html; echo 'Home Page' >> /data/web/html/home.html' exited with 126: , message: "cannot exec in a stopped state: unknown\r\n"
  Normal   Killing              24s (x3 over 40s)  kubelet            FailedPostStartHook
  Warning  BackOff              9s (x4 over 39s)   kubelet            Back-off restarting failed container

  

[root@k8s-master pod-k8s]# cat pod-lifecycle.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-poststart
  namespace: default
spec:
  containers:
  - name: busybox-httpd
    image: busybox
    imagePullPolicy: IfNotPresent
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","echo Home_Page >> /tmp/index.html"]
    command: ["/bin/httpd"]
    args: ["-f", "-h /tmp"]

 

[root@k8s-master ~]# kubectl logs pod-poststart
httpd: can't change directory to ' /tmp': No such file or directory


Events:
  Type     Reason               Age              From               Message
  ----     ------               ----             ----               -------
  Normal   Scheduled            4s               default-scheduler  Successfully assigned default/pod-poststart to k8s-node32.bearpx.com
  Normal   Pulled               3s (x2 over 3s)  kubelet            Container image "busybox" already present on machine
  Normal   Created              3s (x2 over 3s)  kubelet            Created container busybox-httpd
  Normal   Started              3s (x2 over 3s)  kubelet            Started container busybox-httpd
  Warning  FailedPostStartHook  3s (x2 over 3s)  kubelet            Exec lifecycle hook ([/bin/sh -c echo Home_Page >> /tmp/index.html]) for Container "busybox-httpd" 
                                in Pod "pod-poststart_default(e22da6dc-b4d4-47ac-879f-659f08dcce5e)" failed - error: command '/bin/sh -c echo Home_Page >> /tmp/index.html' exited with 126: ,
                                message: "cannot exec in a stopped state: unknown\r\n"
  Normal   Killing              3s (x2 over 3s)  kubelet            FailedPostStartHook
  Warning  BackOff              1s (x2 over 2s)  kubelet            Back-off restarting failed container

 

[root@k8s-master pod-k8s]# cat pod-poststart.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-poststart-ok
  namespace: default
spec:
  containers:
  - name: busybox-httpd
    image: busybox
    imagePullPolicy: IfNotPresent
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","echo Home_Page >> /tmp/index.html"]
    command: ["/bin/httpd"]
    args: ["-f"]

  

[root@k8s-master pod-k8s]# kubectl exec -it  pod-poststart-ok -- /bin/sh
/ # netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 :::80                   :::*                    LISTEN      1/httpd
/ # cd /tmp/
/tmp # ls
index.html
/tmp # cat index.html 
Home_Page

  

Pod回顾总结

apiVersion,  kind,  metadata,  spec,  status(只读)
spec:
  containers	
  initContainers	
  nodeName	
  nodeSelector	 <map[string]string>
  restartPolicy	 <string>  One of Always, OnFailure,Never. Default to Always. 
  tolerations	<[]Object>
  volumes	<[]Object>

  containers:
    name
    image
    imagePullPolicy: Always、 Never、 IfNotPresent
    ports:
      name
      containerPort
    lifecycle
    livenessProbe
    readinessProbe
      ExecAction: exec
      TcpSocketAction: tcpSocket
      HTTPGetAction: httpGet