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

 

Kubernetes 支持 postStart 和 preStop 事件。當一個主容器啓動後,Kubernetes 將當即發送 postStart 事件;在主容器被終結以前,Kubernetes 將發送一個 preStop 事件。node

 

主機配置規劃

服務器名稱(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就能互相通訊。nginx

kubernetes中的pause容器主要爲每一個業務容器提供如下功能:

PID命名空間:Pod中的不一樣應用程序能夠看到其餘應用程序的進程ID。docker

網絡命名空間:Pod中的多個容器可以訪問同一個IP和端口範圍。api

IPC命名空間:Pod中的多個容器可以使用SystemV IPC或POSIX消息隊列進行通訊。服務器

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

Pod中的各個容器能夠訪問在Pod級別定義的Volumes。app

 

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

Kubernetes 支持 postStart 和 preStop 事件。當一個主容器啓動後,Kubernetes 將當即發送 postStart 事件;在主容器被終結以前,Kubernetes 將發送一個 preStop 事件。分佈式

 

postStart 和 preStop 處理函數示例

pod yaml文件ide

 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^-) !

相關文章
相關標籤/搜索