Docker&Kubernetes ❀ Kubernetes集群Pod生命周期资源配置清单
Posted 无糖可乐没有灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker&Kubernetes ❀ Kubernetes集群Pod生命周期资源配置清单相关的知识,希望对你有一定的参考价值。
文章目录
1、Pod生命周期概念简述
我们一般将Pod对象从创建至终的这段时间范围称为Pod的生命周期,主要包含下面过程:
- Pod创建过程;
- 运行初始化容器(Init Container)过程;
- 运行主容器(Main Container)过程;
- 容器启动后钩子(Post Start)、容器终止前钩子(Pre Stop);
- 容器的存活性探测(Liveness Probe)、容器的就绪性探测(Readiness Probe);
- Pod终止过程;
在整个生命周期中,Pod会出现五种状态,分别如下:
- 挂起(Pending):apiserver已经创建了pod资源对象,但是尚未被调度完成或者仍然处于下载镜像的过程中;
- 运行中(Running):pod已经被调度到某个node,并且所有容器已经被kubelet创建完成;
- 成功(Succeeded): pod中所有容器都已经成功运行并且不会重启;
- 失败(Failed):所有容器都已经运行,但是至少有一个容器运行是吧,返回了非0值的退出状态;
- 未知(Unknown):apiserver无法正常获取到pod对象的状态信息,通常由网络通信失败导致;
2、创建和终止
本章节主要介绍Pod从创建到终止的过程详情;
2.1 创建过程
- 用户通过kubelet或其他api客户端提交需要创建的pod信息给apiserver;
- apiserver开始生成pod对象的信息,并将信息存入etcd,然后返回确认信息至客户端;
- apiserver开始反应etcd中的pod对象的变化,其他组件使用watch机制来跟踪检查apiserver上的变动;
- scheduler发现有新的pod对象需要创建,开始为pod分配主机并将结果信息更新至apiserver;
- container-manager发现有新的pod对象需要创建时,从apiserver获取到将新pod创建在某个node上,调度该node上的kubelet;
- node节点上的kubelet发现有pod调度,尝试调用docker启动容器,并将结果返回至apiserver;
- apiserver将接收到的pod状态信息存入etcd中;
2.2 终止过程
- 用户向apiserver发送删除pod对象的命令
- apiserver中的pod对象信息会随着时间的推移而更新,在宽限期(默认为30s)内pod被识别dead;
- 将pod标记为terminating状态;
- kubelet在监控到pod对象转换为terminating状态的同时启动pod关闭进程;
- 端点控制器监控到pod对象的关闭行为时将其从所有匹配到此端口的service资源的端点列表中移除;
- 如果当前pod对象定义了prestop钩子处理器,则在其标识为terminating后即会以同步的方式启动执行;
- pod对象中的容器进程收到停止信号;
- 宽限期结束后,若pod还存在运行的进程,那么pod对象会立即终止该进程;
- kubelet请求apiserver将此pod资源的宽限期设置为0从而完成删除操作,此时pod对用户已不可见;
3、初始化容器
初始化容器是在pod的主容器启动之前要运行的容器,主要是做一些主容器的前置工作,具体有如下两大特征:
- 初始化容器必须运行完成直至结束,若是某初始化容器运行失败,那么kubernetes需要重启它知道成功完成;
- 初始化容器必须按照定义的顺序执行,当前一个成功之后,后面的一个才能继续运行;
初始化容器有很多应用场景,最常见的是:
- 提供主容器镜像中不具备的工具程序或者自定义代码;
- 初始化容器要先于应用容器串行启动并运行完成,因此可用于延后应用容器的启动直至其依赖条件得到满足;
下面需要完成一个案例,模拟此需求:
假设要以主容器来运行nginx,但是要求在运行过程之前先要能够连接上mysql和Redis所在服务器,为了简化测试,定义MySQL与Redis的服务器地址分别为10.81.20.170和10.81.20.171;
#创建YAML文件
[root@master ~]# vim pod-initcontainer.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-initcontainer
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- name: nginx-port
containerPort: 80
initContainers: #初始化容器
- name: test-mysql
image: busybox:1.30
command: ['sh','-c','until ping 10.81.20.170 -c 1;do echo waiting for mysql...;sleep 2;done'] #若无法ping通10.81.20.170,返回waiting for mysql...
- name: test-redis
image: busybox:1.30
command: ['sh','-c','until ping 192.168.88.141 -c 1;do echo waiting for redis...;sleep 2;done'] #若无法ping通10.81.20.171,返回waiting for redis...
#调用YAML文件
[root@master ~]# kubectl apply -f pod-initcontainer.yaml
pod/pod-initcontainer created
#使用动态方式查看pod创建过程
# -w 参数为动态查看参数
[root@master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pod-initcontainer 0/1 Init:0/2 0 29s
#查看发现pod创建过程为init:0/2(初始化状态),初始化容器2个均无法正常创建,其原因为无法与10.81.20.170、10.81.20.171通信
#开启第二个终端窗口,配置网卡多余IP地址,使得master可以通信10.81.20.170
[root@master ~]# nmcli connection modify ens33 +ipv4.addresses 10.81.20.170/24 ipv4.method manual
#重启网卡ens33(由于不同系统网卡名称不同,因此需要查看本地网卡实质名称使用命令 ifconfig 或 ip addr show )
[root@master ~]# nmcli connection down ens33 && nmcli connection up ens33
Connection 'ens33' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
#查看第一个窗口发现,状态更新为init:1/2
[root@master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pod-initcontainer 0/1 Init:0/2 0 29s
pod-initcontainer 0/1 Init:1/2 0 89s
#在第二个终端窗口,继续配置网卡多余IP地址,使得master可以通信10.81.20.171
[root@master ~]# nmcli connection modify ens33 +ipv4.addresses 10.81.20.171/24 ipv4.method manual
[root@master ~]# nmcli connection down ens33 && nmcli connection up ens33
Connection 'ens33' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
#查看第一个窗口发现,状态更新为PodInitializing,然后转变为Running
[root@master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pod-initcontainer 0/1 Init:0/2 0 29s
pod-initcontainer 0/1 Init:1/2 0 89s
pod-initcontainer 0/1 PodInitializing 0 102s
pod-initcontainer 1/1 Running 0 2m4s
#使用describe查看详细信息
[root@master ~]# kubectl describe pod pod-initcontainer -n dev
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m11s default-scheduler Successfully assigned dev/pod-initcontainer to node1.kubernetes
Normal Pulling 3m10s kubelet Pulling image "busybox:1.30"
Normal Pulled 3m7s kubelet Successfully pulled image "busybox:1.30" in 3.677222766s
Normal Created 3m7s kubelet Created container test-mysql
Normal Started 3m6s kubelet Started container test-mysql
Normal Pulled 102s kubelet Container image "busybox:1.30" already present on machine
Normal Created 102s kubelet Created container test-redis
Normal Started 102s kubelet Started container test-redis
Normal Pulling 89s kubelet Pulling image "nginx:1.17.1"
Normal Pulled 68s kubelet Successfully pulled image "nginx:1.17.1" in 21.059955763s
Normal Created 68s kubelet Created container nginx
Normal Started 68s kubelet Started container nginx
4、钩子函数
钩子函数能够感知自身生命周期中的事件,并在相应的时刻到来时运行用户指定的程序代码;
Kubernetes在主容器的启动之后和停止之前提供了两个钩子函数;
- post start:容器创建之后执行,如果失败了会重启容器;
- pre stop:容器终止之前执行,执行完成之后容器将成功停止,在其完成之前会阻塞删除容器的操作;
钩子处理器支持使用下面三种方式定义动作:
4.1 Exec命令
在容器内执行一次命令;
...
lifecycle:
postStart:
exec:
command: ['cat','/tmp/test.txt']
...
4.2 TCPSocket
在当前容器尝试访问指定的Socket;
...
lifecycle:
postStart:
tcpSocket:
port: 80
...
4.3 HTTPGet
在当前容器向某URL发起HTTP请求;
...
lifecycle:
postStart:
httpGet: #合并为scheme://host:port/
path: / #URI
port: 80 #端口号
host: 127.0.0.1 #主机地址,默认为127.0.0.1
scheme: HTTP #支持HTTP、HTTPS
...
下面就以exec为例,演示钩子函数的使用;
#创建YAML文件
[root@master ~]# vim pod-hook-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-hook-exec
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- name: nginx-port
containerPort: 80
lifecycle:
postStart:
exec: #容器启动之前执行命令,修改Nginx默认页面
command: ['/bin/sh','-c','echo this is test! > /usr/share/nginx/html/index.html']
preStop:
exec: #容器停止前停止Nginx服务
command: ['/usr/sbin/nginx','-s','quit']
#调用YAML文件
[root@master ~]# kubectl apply -f pod-hook-exec.yaml
pod/pod-hook-exec created
#查看Pod所在IP地址
[root@master ~]# kubectl get pod -n dev -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-hook-exec 1/1 Running 0 23s 10.244.80.80 node1.kubernetes <none> <none>
#访问页面,查看内容是否被修改
[root@master ~]# curl http://10.244.80.80
this is test!
#删除YAML文件所创建的Pod
[root@master ~]# kubectl delete -f pod-hook-exec.yaml
pod "pod-hook-exec" deleted
5、容器探测
容器探测用于检测容器中的应用实例是否正常工作,是保障业务可用性的一种传统机制,如果经过探测,实例的状态不符合预期设定,那么Kubernetes就会把该问题实例摘除,不承担业务流量,Kubernetes提供了两种探针来实现容器探测;
- Liveness probes:存活性探针,用于检测应用实例当前是否处于正常运行状态,如果不是则Kubernetes会重启容器;
- Readiness probes:就绪性探针,用于检测应用实例当前是否可以接收请求,如果不能则Kubernetes不会转发流量到该容器;
以上两种探针均支持下面三种探测方式;
5.1 Exec
在容器内执行一次命令,如果命令执行退出码为0,则认为程序正常,否则不正常;
细节补充:在Linux中,若命令执行成功,则使用echo $?返回值为0,若命令执行失败,则返回非0值;由此可以判断命令是否执行成功;
# 命令执行失败
[root@master ~]# cat /tmp/
cat: /tmp/: Is a directory
[root@master ~]# echo $?
1
#命令执行成功
[root@master ~]# cat pod-liveness-exec.yaml
[root@master ~]# echo $?
0
...
livenessProbe:
exec:
command: ['cat','/tmp/test.txt']
...
5.1.1 探测失败演示案例
[root@master ~]# vim pod-liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-liveness-exec
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- name: nginx-port
containerPort: 80
livenessProbe:
exec:
command: ['/bin/cat','/tmp/test.txt'] #执行一个查看文件的命令,目标文件不存在
[root@master ~]# kubectl apply -f pod-liveness-exec.yaml
pod/pod-liveness-exec created
#延时查看Pod会发生重启,即便状态为Running
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-liveness-exec 1/1 Running 1 (1s ago) 32s
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-liveness-exec 0/1 CrashLoopBackOff 7 (97s ago) 10m
#Nginx在启动之后进行容器探测,探测失败后,容器被kill,尝试重启(重启策略为第六章节内容),此时查看RESTARTS信息不再是0,而是目前已经重启的次数,随着时间持续增加;
#查看详细日志信息
[root@master ~]# kubectl describe pod pod-liveness-exec -n dev
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 7m30s default-scheduler Successfully assigned dev/pod-liveness-exec to node1.kubernetes
Normal Created 5m59s (x4 over 7m28s) kubelet Created container nginx
Normal Started 5m59s (x4 over 7m28s) kubelet Started container nginx
Normal Killing 5m59s (x3 over 6m59s) kubelet Container nginx failed liveness probe, will be restarted
Warning Unhealthy 5m49s (x10 over 7m19s) kubelet Liveness probe failed: /bin/cat: /tmp/test.txt: No such file or directory #告警信息,没有找到/tmp/test.txt文件或目录
Normal Pulled 2m20s (x7 over 7m29s) kubelet Container image "nginx:1.17.1" already present on machine
5.2 TCPSocket
将会尝试访问一个用户容器的端口,如果能够正常建立连接,则认为程序正常,否则不正常;
...
livenessProbe:
tcpSocket:
port: 80
...
5.2.1 探测失败演示案例
[root@master ~]# cat pod-liveness-tcpsocket.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-liveness-tcpsocket
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- name: nginx-port
containerPort: 80
livenessProbe:
tcpSocket:
port: 8080 #尝试连接8080端口,由于Nginx服务默认开启80端口,因此连接必然失败
[root@master ~]# kubectl apply -f pod-liveness-tcpsocket.yaml
pod/pod-liveness-exec created
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-liveness-tcpsocket 1/1 Running 2 (20s ago) 80s
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-liveness-tcpsocket 0/1 CrashLoopBackOff 4 (34s ago) 3m4s
[root@master ~]# kubectl describe pod pod-liveness-tcpsocket -n dev
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 117s default-scheduler Successfully assigned dev/pod-liveness-tcpsocket to node1.kubernetes
Normal Pulled 27s (x4 over 116s) kubelet Container image "nginx:1.17.1" already present on machine
Normal Created 27s (x4 over 116s) kubelet Created container nginx
Normal Started 27s (x4 over 116s) kubelet Started container nginx
Normal Killing 27s (x3 over 87s) kubelet Container nginx failed liveness probe, will be restarted
Warning Unhealthy 17s (x10 over 107s) kubelet Liveness probe failed: dial tcp 10.244.80.84:8080: connect: connection refused # 告警信息,10.244.80.84:8080端口连接失败
5.3 HTTPGet
调用容器内Web应用的URL,如果返回的状态码在200和399之间,则认为程序正常,否则不正常;
...
livenessProbe:
httpGet: #合并为scheme://host:port/
path: / #URI
port: 80 #端口号
host: 127.0.0.1 #主机地址,默认为127.0.0.1
scheme: HTTP #支持HTTP、HTTPS
...
5.3.1 探测失败演示案例
[root@master ~]# cat pod-liveness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-liveness-httpget
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- name: nginx-port
containerPort: 80
livenessProbe:
httpGet: #合并为scheme://host:port/
path: /hello #URI,此处文件不存在,因此访问失败
port: 80 #端口号
host: 127.0.0.1 #主机地址,默认为127.0.0.1
scheme: HTTP #支持HTTP、HTTPS
[root@master ~]# kubectl apply -f pod-liveness-httpget.yaml
pod/pod-liveness-httpget created
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-liveness-httpget 0/1 CrashLoopBackOff 4 (24s ago) 2m54s
[root@master ~]# kubectl describe pod pod-liveness-httpget -n dev
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m11s default-scheduler Successfully assigned dev/pod-liveness-httpget to node1.kubernetes
Normal Pulled 101s (x4 over 3m10s) kubelet Container image "nginx:1.17.1" already present on machine
Normal Created 101s (x4 over 3m10s) kubelet Created container nginx
Normal Started 101s (x4 over 3m10s) kubelet Started container nginx
Normal Killing 101s (x3 over 2m41s) kubelet Container nginx failed liveness probe, will be restarted
Warning Unhealthy 91s (x10 over 3m1s) kubelet Liveness probe failed: Get "http://127.0.0.1:80/hello": dial tcp 127.0.0.1:80: connect: connection refused #告警信息,连接拒绝
5.4 其他相关参数
其他参数主要为一些探测时间与探测频率,不配做均采用默认值;
[root@master ~]# kubectl explain pod.spec.containers.livenessProbe
KIND: Pod
VERSION: v1
RESOURCE: livenessProbe <Object>
FIELDS:
exec <Object>
tcpSocket <Object>
httpGet <Object>
exec <Object>
tcpSocket <Object>
httpGet <Object>
failureThreshold <integer>
grpc <Object>
initialDelaySeconds <integer> #容器启动后等待多少秒执行第一次探测
periodSeconds <integer> #执行探测的频率,默认10s,最小1s
successThreshold <integer> #连续探测成功多少次定为成功,默认1次
terminationGracePeriodSeconds <integer> #连续探测失败多少次定为失败,默认3次,最小1次
timeoutSeconds <integer> #探测超时时间,默认为1s,最小1s
6、重启策略
上述内容中,一旦容器探测出现问题,Kubernetes就会对容器所在的Pod进行重启,其实这是由Pod的重启策略决定的,Pod的重启策略有三种,分别如下:
- Always:容器失败时,自动重启该容器,默认选项;
- OnFailure:容器终止运行且退出码不为0时重启;
- Never:不论状态如何,都不重启该容器;
重启策略适用于Pod对象中的所有容器,首次需要重启的容器,将在其需要时立即进行重启,随后再次需要重启的操作将由kubelet延迟一段时间后进行,且反复重启操作的延迟时长以此为10s、20s、40s、80s、160s、300s,300s为最大延迟时长;
[root@master ~]# cat pod-restartpolicy.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-restartpolicy
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- name: nginx-port
containerPort: 80
livenessProbe:
httpGet:
path: /hello
port: 80
host: 127.0.0.1
scheme: HTTP
restartPolicy: Never #设置重启策略为Never
[root@master ~]# kubectl apply -f pod-restartpolicy.yaml
pod/pod-restartpolicy created
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-restartpolicy 1/1 Running 0 30s
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-restartpolicy 0/1 Completed 0 32s
[root@master ~]# kubectl describe pod pod-restartpolicy -n dev
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 57s default-scheduler Successfully assigned dev/pod-restartpolicy to node1.kubernetes
Normal Pulled 56s kubelet Container image "nginx:1.17.1" already present on machine
Normal Created 56s kubelet Created container nginx
Normal Started 56s kubelet Started container nginx
Warning Unhealthy 27s (x3 over 47s) kubelet Liveness probe failed: Get "http://127.0.0.1:80/hello": dial tcp 127.0.0.1:80: connect: connection refused
Normal Killing 27s kubelet Stopping container nginx #当容器探测失败立即停止Nginx服务
Warning FailedMount 10s (x6 over 26s) kubelet MountVolume.SetUp failed for volume "kube-api-access-zr72n" : object "dev"/"kube-root-ca.crt" not registered
以上是关于Docker&Kubernetes ❀ Kubernetes集群Pod生命周期资源配置清单的主要内容,如果未能解决你的问题,请参考以下文章
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法
Docker&Kubernetes ❀ Kubernetes集群实践与部署笔记知识点梳理
Docker&Kubernetes ❀ Docker 容器技术笔记链接梳理