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

 

posted on 2020-08-18 21:27  踏歌行666  阅读(3466)  评论(0编辑  收藏  举报

导航