Pod对象从创建到删除的这段时间范围称为Pod的生命周期,主要包含下面的过程:
在整个生命周期中,Pod会出现5种状态:
初始化容器是在Pod的主容器启动之前要运行的容器,主要是做一些主容器的前置工作,它具有两大特征:
初始化容器有很多的应用场景,下面列出的是最常见的几个:
接下来做一个案例,模拟下面这个需求:
新建pod-lifecycle.yaml,内容如下。然后进行pod创建
user: bulut
spec:
containers:
- name: nginx-container
image: nginx:latest
initContainers:
- name: init-mysql
image: busybox
command: ['/bin/sh', '-c', 'until ping 192.168.23.188 -c 1; do echo waiting for mysql; sleep 3; done;']
- name: init-redis
image: busybox
command: ['/bin/sh', '-c', 'until ping 192.168.23.189 -c 1; do echo waiting for redis; sleep 3; done;']
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pod-lifecycle.yaml
pod/pod-lifecycle created
[root@k8s-master ~]#
查看pod启动状态。只有第一个init成功,卡在第二个init container了
[root@k8s-master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-lifecycle 0/1 Init:1/2 0 28s
[root@k8s-master ~]#
kubernetes在主容器启动之后和删除之前提供了两个钩子函数:
钩子处理器支持三种定义方式:
lifecycle:
postStart:
exec:
command:
- cat
- /tmp/test.txt
lifecycle:
postStart:
tcpSocket:
port: 8080
lifecycle:
postStart:
httpGet:
path: / # URI地址
port: 80 # 端口号
host: 192.168.23.161 # 主机地址
scheme: HTTP # 支持的协议,http或者https
新建pod-lifecycle.yaml,内容如下。然后进行pod创建
[root@k8s-master ~]# cat pod-lifecycle.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-lifecycle
namespace: dev
labels:
user: bulut
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
lifecycle:
postStart:
exec: # 修改Nginx的首页内容
command: ["/bin/sh", "-c", "echo postStart > /usr/share/nginx/html/index.html"]
preStop:
exec: # 停止Nginx的服务
command: ["/usr/sbin/nginx", "-s", "quit"]
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pod-lifecycle.yaml
pod/pod-lifecycle created
[root@k8s-master ~]#
访问nginx服务。可以看到postStat起作用了
[root@k8s-master ~]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-lifecycle 1/1 Running 0 64s 10.244.169.150 k8s-node2
[root@k8s-master ~]#
[root@k8s-master ~]# curl 10.244.169.150:80
postStart
[root@k8s-master ~]#
容器探测用于检测容器中的应用实例是否正常工作,kubernetes提供了三种探针来实现容器探测,本文主要讲解liveness probes和readiness probes
liveness probes和readiness probes均支持三种探测方式。语法如下,readinessProbe和livenessProbe的语法一样
livenessProbe:
exec:
command:
- cat
- /tmp/test.txt
livenessProbe:
tcpSocket:
port: 8080
livenessProbe:
httpGet:
path: / # URI地址
port: 80 # 端口号
host: 192.168.23.161 # 主机地址
scheme: HTTP # 支持的协议,http或者https
通过命令kubectl explain pod.spec.containers.livenessProbe
查看其它属性,整理如下:
exec
tcpSocket
httpGet
initialDelaySeconds # 容器启动后等待多少秒执行第一次探测
timeoutSeconds # 探测超时时间。默认1秒,最小1秒
periodSeconds # 执行探测的频率。默认是10秒,最小1秒
failureThreshold # 连续探测失败多少次才被认定为失败。默认是3。最小值是1
successThreshold # 连续探测成功多少次才被认定为成功。默认是1
新建pod-lifecycle.yaml,内容如下:
[root@k8s-master ~]# cat pod-lifecycle.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-lifecycle
namespace: dev
labels:
user: bulut
spec:
containers:
- name: nginx-container
image: nginx:latest
livenessProbe:
exec:
command: ["ls", "/tmp"]
[root@k8s-master ~]#
新建pod-lifecycle.yaml,内容如下:
[root@k8s-master ~]# cat pod-lifecycle.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-lifecycle
namespace: dev
labels:
user: bulut
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
livenessProbe:
tcpSocket:
port: 80
[root@k8s-master ~]#
新建pod-lifecycle.yaml,内容如下:
[root@k8s-master ~]# cat pod-lifecycle.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-lifecycle
namespace: dev
labels:
user: bulut
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
livenessProbe:
httpGet: # 其实就是访问http://127.0.0.1:80/
port: 80
scheme: HTTP
path: /
host: 127.0.0.0
[root@k8s-master ~]#
在容器探测中,一旦容器探测出现了问题,kubernetes就会对容器所在的Pod进行重启,其实这是由Pod的默认重启策略决定的。Pod的重启策略有3种:
重启策略是针对Pod设定的。首次需要重启的容器,将在其需要的时候立即进行重启,随后再次重启的操作将由kubelet延迟一段时间后进行,且反复的重启操作的延迟时长依次为10s、20s、40s、80s、160s和300s,300s是最大的延迟时长
新建pod-lifecycle.yaml,内容如下:
[root@k8s-master ~]# cat pod-lifecycle.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-lifecycle
namespace: dev
labels:
user: bulut
spec:
containers:
- name: nginx-container
image: nginx:latest
restartPolicy: Never
[root@k8s-master ~]#