1. 程式人生 > 實用技巧 >Kubernetes K8S之Pod 生命週期與postStart、preStop事件

Kubernetes K8S之Pod 生命週期與postStart、preStop事件

Kubernetes 支援 postStart 和 preStop 事件。當一個主容器啟動後,Kubernetes 將立即傳送 postStart 事件;在主容器被終結之前,Kubernetes 將傳送一個 preStop 事件。

主機配置規劃

伺服器名稱(hostname)系統版本配置內網IP外網IP(模擬)
k8s-master CentOS7.7 2C/4G/20G 172.16.1.110 10.0.0.110
k8s-node01 CentOS7.7 2C/4G/20G 172.16.1.111 10.0.0.111
k8s-node02 CentOS7.7 2C/4G/20G 172.16.1.112 10.0.0.112

Pod容器生命週期

Pause容器說明

每個Pod裡執行著一個特殊的被稱之為Pause的容器,其他容器則為業務容器,這些業務容器共享Pause容器的網路棧和Volume掛載卷,因此他們之間通訊和資料交換更為高效。在設計時可以充分利用這一特性,將一組密切相關的服務程序放入同一個Pod中;同一個Pod裡的容器之間僅需通過localhost就能互相通訊。

kubernetes中的pause容器主要為每個業務容器提供以下功能:

PID名稱空間:Pod中的不同應用程式可以看到其他應用程式的程序ID。

網路名稱空間:Pod中的多個容器能夠訪問同一個IP和埠範圍。

IPC名稱空間:Pod中的多個容器能夠使用SystemV IPC或POSIX訊息佇列進行通訊。

UTS名稱空間:Pod中的多個容器共享一個主機名;Volumes(共享儲存卷)。

Pod中的各個容器可以訪問在Pod級別定義的Volumes。

主容器生命週期事件的處理函式

Kubernetes 支援 postStart 和 preStop 事件。當一個主容器啟動後,Kubernetes 將立即傳送 postStart 事件;在主容器被終結之前,Kubernetes 將傳送一個 preStop 事件。

postStart 和 preStop 處理函式示例

pod yaml檔案

 1 [root@k8s-master lifecycle]# pwd
 2 /root/k8s_practice/lifecycle
3 [root@k8s-master lifecycle]# cat lifecycle-events.yaml 4 apiVersion: v1 5 kind: Pod 6 metadata: 7 name: lifecycle-demo-pod 8 namespace: default 9 labels: 10 test: lifecycle 11 spec: 12 containers: 13 - name: lifecycle-demo 14 image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 15 imagePullPolicy: IfNotPresent 16 lifecycle: 17 postStart: 18 exec: 19 command: ["/bin/sh", "-c", "echo 'Hello from the postStart handler' >> /var/log/nginx/message"] 20 preStop: 21 exec: 22 command: ["/bin/sh", "-c", "echo 'Hello from the preStop handler' >> /var/log/nginx/message"] 23 volumeMounts: #定義容器掛載內容 24 - name: message-log #使用的儲存卷名稱,如果跟下面volume欄位name值相同,則表示使用volume的nginx-site這個儲存卷 25 mountPath: /var/log/nginx/ #掛載至容器中哪個目錄 26 readOnly: false #讀寫掛載方式,預設為讀寫模式false 27 initContainers: 28 - name: init-myservice 29 image: registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24 30 command: ["/bin/sh", "-c", "echo 'Hello initContainers' >> /var/log/nginx/message"] 31 volumeMounts: #定義容器掛載內容 32 - name: message-log #使用的儲存卷名稱,如果跟下面volume欄位name值相同,則表示使用volume的nginx-site這個儲存卷 33 mountPath: /var/log/nginx/ #掛載至容器中哪個目錄 34 readOnly: false #讀寫掛載方式,預設為讀寫模式false 35 volumes: #volumes欄位定義了paues容器關聯的宿主機或分散式檔案系統儲存卷 36 - name: message-log #儲存卷名稱 37 hostPath: #路徑,為宿主機儲存路徑 38 path: /data/volumes/nginx/log/ #在宿主機上目錄的路徑 39 type: DirectoryOrCreate #定義型別,這表示如果宿主機沒有此目錄則會自動建立

啟動pod,檢視pod狀態

1 [root@k8s-master lifecycle]# kubectl apply -f lifecycle-events.yaml 
2 pod/lifecycle-demo-pod created
3 [root@k8s-master lifecycle]# kubectl get pod -o wide
4 NAME                 READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
5 lifecycle-demo-pod   1/1     Running   0          5s    10.244.2.30   k8s-node02   <none>           <none>

檢視pod詳情

 1 [root@k8s-master lifecycle]# kubectl describe pod lifecycle-demo-pod
 2 Name:         lifecycle-demo-pod
 3 Namespace:    default
 4 Priority:     0
 5 Node:         k8s-node02/172.16.1.112
 6 Start Time:   Sat, 23 May 2020 22:08:04 +0800
 7 Labels:       test=lifecycle
 8 ………………
 9 Init Containers:
10   init-myservice:
11     Container ID:  docker://1cfabcb60b817efd5c7283ad9552dafada95dbe932f92822b814aaa9c38f8ba5
12     Image:         registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24
13     Image ID:      docker-pullable://registry.cn-beijing.aliyuncs.com/ducafe/busybox@sha256:f73ae051fae52945d92ee20d62c315306c593c59a429ccbbdcba4a488ee12269
14     Port:          <none>
15     Host Port:     <none>
16     Command:
17       /bin/sh
18       -c
19       echo 'Hello initContainers'   >> /var/log/nginx/message
20     State:          Terminated
21       Reason:       Completed
22       Exit Code:    0
23       Started:      Sat, 23 May 2020 22:08:06 +0800
24       Finished:     Sat, 23 May 2020 22:08:06 +0800
25     Ready:          True
26     Restart Count:  0
27     Environment:    <none>
28     Mounts:
29       /var/log/nginx/ from message-log (rw)
30       /var/run/secrets/kubernetes.io/serviceaccount from default-token-v48g4 (ro)
31 Containers:
32   lifecycle-demo:
33     Container ID:   docker://c07f7f3d838206878ad0bfeaec9b4222ac7d6b13fb758cc1b340ac43e7212a3a
34     Image:          registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17
35     Image ID:       docker-pullable://registry.cn-beijing.aliyuncs.com/google_registry/nginx@sha256:7ac7819e1523911399b798309025935a9968b277d86d50e5255465d6592c0266
36     Port:           <none>
37     Host Port:      <none>
38     State:          Running
39       Started:      Sat, 23 May 2020 22:08:07 +0800
40     Ready:          True
41     Restart Count:  0
42     Environment:    <none>
43     Mounts:
44       /var/log/nginx/ from message-log (rw)
45       /var/run/secrets/kubernetes.io/serviceaccount from default-token-v48g4 (ro)
46 Conditions:
47   Type              Status
48   Initialized       True 
49   Ready             True 
50   ContainersReady   True 
51   PodScheduled      True 
52 Volumes:
53   message-log:
54     Type:          HostPath (bare host directory volume)
55     Path:          /data/volumes/nginx/log/
56     HostPathType:  DirectoryOrCreate
57   default-token-v48g4:
58     Type:        Secret (a volume populated by a Secret)
59     SecretName:  default-token-v48g4
60     Optional:    false
61 QoS Class:       BestEffort
62 Node-Selectors:  <none>
63 Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
64                  node.kubernetes.io/unreachable:NoExecute for 300s
65 Events:
66   Type    Reason     Age        From                 Message
67   ----    ------     ----       ----                 -------
68   Normal  Scheduled  <unknown>  default-scheduler    Successfully assigned default/lifecycle-demo-pod to k8s-node02
69   Normal  Pulled     87s        kubelet, k8s-node02  Container image "registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24" already present on machine
70   Normal  Created    87s        kubelet, k8s-node02  Created container init-myservice
71   Normal  Started    87s        kubelet, k8s-node02  Started container init-myservice
72   Normal  Pulled     86s        kubelet, k8s-node02  Container image "registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17" already present on machine
73   Normal  Created    86s        kubelet, k8s-node02  Created container lifecycle-demo
74   Normal  Started    86s        kubelet, k8s-node02  Started container lifecycle-demo

此時在k8s-node02檢視輸出資訊如下:

1 [root@k8s-node02 log]# pwd
2 /data/volumes/nginx/log
3 [root@k8s-node02 log]# cat message 
4 Hello initContainers
5 Hello from the postStart handler

由上可知,init Container先執行,然後當一個主容器啟動後,Kubernetes 將立即傳送 postStart 事件。

停止該pod

1 [root@k8s-master lifecycle]# kubectl delete pod lifecycle-demo-pod
2 pod "lifecycle-demo-pod" deleted

此時在k8s-node02檢視輸出資訊如下:

1 [root@k8s-node02 log]# pwd
2 /data/volumes/nginx/log
3 [root@k8s-node02 log]# cat message 
4 Hello initContainers
5 Hello from the postStart handler
6 Hello from the preStop handler

由上可知,當在容器被終結之前, Kubernetes 將傳送一個 preStop 事件。

完畢!


———END———
如果覺得不錯就關注下唄 (-^O^-) !