K8s新手系列之Pod的重启策略
概述
K8s中Pod的重启策略具有确保服务连续性、保证任务完整性、提升资源利用效率、便于故障排查的作用
Pod的重启策略可以根据restartPolicy字段定义.
重启策略适用于pod对象中的所有容器,首次需要重启的容器,将在其需要时立即进行重启,随后再次需要重启的操作将由kubelet延迟一段时间后进行,且反复的重启操作的延迟时长以此为10s、20s、40s、80s、160s和300s,300s是最大延迟时长。
可以通过官方文档阅读:https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy
也可以通过以下命令查看详细资源信息:
[root@node02 ~]# kubectl explain pod.spec.restartPolicy
KIND: Pod
VERSION: v1
FIELD: restartPolicy <string>
DESCRIPTION:
Restart policy for all containers within the pod. One of Always, OnFailure,
Never. Default to Always. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy
Possible enum values:
- `"Always"`
- `"Never"`
- `"OnFailure"`
通过上述可以发现Pod的重启策略有三种,分别是Always、Never、OnFailure
Pod的重启策略详解
Always
Always代表只要Pod终止就自动重启Pod。也是默认的重启策略
使用场景:适用于长时间运行的服务型容器,如 Web 服务器、数据库服务器等。这些容器需要保持持续运行,以提供稳定的服务。即使容器因意外故障(如内存泄漏、进程崩溃等)而终止,Always 策略也会立即重启容器,尽可能减少服务中断时间,保证业务的连续性。
示例:
# 创建一个pod
[root@master01 ~/pod]# cat always-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: always-restart-pod
spec:
containers:
- name: my-container
image: nginx:latest
# 定义重启策略为Always
restartPolicy: Always
OnFailure
OnFailure代表只有在Pod错误退出(退出状态非零)时才重新启动Pod。
使用场景:常用于批处理任务或一次性作业。例如,数据处理任务、备份任务等。这些任务在执行过程中可能会因为各种原因(如资源不足、依赖的服务不可用等)而失败,但只要任务没有成功完成,就需要重新执行,直到任务成功。OnFailure 策略可以保证任务在出现故障时自动重启,确保任务最终能够执行完成,而不会因为一次失败就终止。
示例:
# 定义资源文件
[root@master01 ~/pod]# cat onfailure-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: onfailure-pod
spec:
containers:
- name: my-container
image: busybox
command: ["sh", "-c", "exit 1"]
# 设置重启策略
restartPolicy: OnFailure
# 创建pod
[root@master01 ~/pod]# kubectl apply -f onfailure-pod.yaml
pod/onfailure-pod created
# 查看pod,状态为CrashLoopBackOff,RESTARTS字段表示重启次数,这里重启了两次
[root@master01 ~/pod]# kubectl get po onfailure-pod
NAME READY STATUS RESTARTS AGE
onfailure-pod 0/1 CrashLoopBackOff 2 (16s ago) 39s
# 查看详细信息,发现Pod一直在重启
[root@master01 ~/pod]# kubectl describe po onfailure-pod
Name: onfailure-pod
Namespace: default
#...省略万字内容
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m11s default-scheduler Successfully assigned default/onfailure-pod to node02
Normal Pulled 2m9s kubelet Successfully pulled image "busybox" in 1.511600493s (1.511617507s including waiting)
Normal Pulled 2m7s kubelet Successfully pulled image "busybox" in 1.177646325s (1.177650807s including waiting)
Normal Pulled 108s kubelet Successfully pulled image "busybox" in 1.425673685s (1.425683335s including waiting)
Normal Created 80s (x4 over 2m9s) kubelet Created container my-container
Normal Pulled 80s kubelet Successfully pulled image "busybox" in 1.310080949s (1.310086452s including waiting)
Normal Started 79s (x4 over 2m9s) kubelet Started container my-container
Warning BackOff 42s (x8 over 2m6s) kubelet Back-off restarting failed container my-container in pod onfailure-pod_default(f531482f-c254-4aa8-afd5-4c0b99723e81)
Normal Pulling 28s (x5 over 2m11s) kubelet Pulling image "busybox"
Never
Never表示不论状态如何,都不重启该Pod
使用场景:适用于执行完即结束的一次性任务,或者是希望人工干预容器重启过程的情况。例如,某些初始化脚本、一次性的配置任务等,执行完成后容器就没有继续运行的必要了,也不希望其自动重启。另外,当需要对容器的故障进行详细排查时,使用 Never 策略可以让容器保持在故障状态,以便运维人员查看容器的最终状态、日志等信息,有助于快速定位和解决问题。
示例:
# 定义资源清单
[root@master01 ~/pod]# cat never-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: never-pod
spec:
containers:
- name: my-container
image: busybox
command: ["sh", "-c", "exit 1"]
# 设置重启策略
restartPolicy: Never
[root@master01 ~/pod]# kubectl apply -f never-pod.yaml
pod/never-pod created
[root@master01 ~/pod]# kubectl get pod never-pod
NAME READY STATUS RESTARTS AGE
never-pod 0/1 Error 0 15s
本文来自博客园,作者:huangSir-devops,转载请注明原文链接:https://www.cnblogs.com/huangSir-devops/p/18856544,微信Vac6666666,欢迎交流

浙公网安备 33010602011771号