K8S中的Pod生命周期之重启策略

devtools/2025/1/16 16:03:38/

三种策略

Kubernetes 中的 Pod 支持以下三种重启策略:

  • Always

    • 描述:无论容器退出的原因是什么,都会自动重启容器

    • 默认值:如果未指定重启策略,Kubernetes 默认使用 Always。

  • OnFailure

    • 描述:仅当容器以非零退出码终止时,才会重启容器

    • 条件:需要指定退出码来触发重启。

  • Never

    • 描述:不论容器退出的原因是什么,都不会重启容器

重启延迟

  • 首次重启:首次需要重启的容器将立即进行重启。

  • 后续重启:随后如果再次需要重启,kubelet 将会引入延迟,延迟时长从 10 秒开始,并呈指数增长。

  • 延迟时长序列:10s、20s、40s、80s、160s,之后达到最大延迟时长。

  • 最大延迟时长:300s,这是后续重启操作的最大延迟时长。

Never

可以发现pod不会重启

[root@k8s-master ~]# vim pod-restartpolicy.yaml
---
apiVersion: v1
kind: Pod
metadata:name: pod-restartpolicynamespace: test
spec:restartPolicy: Never   #论容器退出的原因是什么,都不会重启容器containers:- name: nginximage: nginx:1.17.1ports:- containerPort: 80name: nginx-portlivenessProbe:httpGet:scheme: HTTPport: 80path: /hello
[root@k8s-master ~]# kubectl apply -f pod-restartpolicy.yaml 
Error from server (NotFound): error when creating "pod-restartpolicy.yaml": namespaces "test" not found
[root@k8s-master ~]# kubectl create ns test
namespace/test created
[root@k8s-master ~]# kubectl apply -f pod-restartpolicy.yaml 
pod/pod-restartpolicy created
[root@k8s-master ~]#  kubectl describe pod pod-restartpolicy -n test
Name:         pod-restartpolicy
Namespace:    test
Priority:     0
Node:         k8s-node2/192.168.58.233
Start Time:   Tue, 14 Jan 2025 20:45:51 -0500
Labels:       <none>
Annotations:  cni.projectcalico.org/containerID: 77f405fd3543f24391d29b1f878fff24dda621f6583dd7df8e7020da258b9f4dcni.projectcalico.org/podIP: 10.244.169.129/32cni.projectcalico.org/podIPs: 10.244.169.129/32
Status:       Pending
IP:           
IPs:          <none>
Containers:nginx:Container ID:   Image:          nginx:1.17.1Image ID:       Port:           80/TCPHost Port:      0/TCPState:          WaitingReason:       ContainerCreatingReady:          FalseRestart Count:  0Liveness:       http-get http://:80/hello delay=0s timeout=1s period=10s #success=1 #failure=3Environment:    <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-sf6xn (ro)
Conditions:Type              StatusInitialized       True Ready             False ContainersReady   False PodScheduled      True 
Volumes:kube-api-access-sf6xn:Type:                    Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds:  3607ConfigMapName:           kube-root-ca.crtConfigMapOptional:       <nil>DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type    Reason     Age   From               Message----    ------     ----  ----               -------Normal  Scheduled  22s   default-scheduler  Successfully assigned test/pod-restartpolicy to k8s-node2Normal  Pulling    20s   kubelet            Pulling image "nginx:1.17.1"
[root@k8s-master ~]#  kubectl describe pod pod-restartpolicy -n test
Name:         pod-restartpolicy
Namespace:    test
Priority:     0
Node:         k8s-node2/192.168.58.233
Start Time:   Tue, 14 Jan 2025 20:45:51 -0500
Labels:       <none>
Annotations:  cni.projectcalico.org/containerID: 77f405fd3543f24391d29b1f878fff24dda621f6583dd7df8e7020da258b9f4dcni.projectcalico.org/podIP: cni.projectcalico.org/podIPs: 
Status:       Running
IP:           10.244.169.129
IPs:IP:  10.244.169.129
Containers:nginx:Container ID:   docker://19f7e2ca6a7f4a9487b75fc5dee7d85cf2baef4547ae8b6f1d68f8dfd5d7bb1aImage:          nginx:1.17.1Image ID:       docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbbPort:           80/TCPHost Port:      0/TCPState:          RunningStarted:      Tue, 14 Jan 2025 20:46:18 -0500Ready:          TrueRestart Count:  0Liveness:       http-get http://:80/hello delay=0s timeout=1s period=10s #success=1 #failure=3Environment:    <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-sf6xn (ro)
Conditions:Type              StatusInitialized       True Ready             True ContainersReady   True PodScheduled      True 
Volumes:kube-api-access-sf6xn:Type:                    Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds:  3607ConfigMapName:           kube-root-ca.crtConfigMapOptional:       <nil>DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type     Reason     Age               From               Message----     ------     ----              ----               -------Normal   Scheduled  50s               default-scheduler  Successfully assigned test/pod-restartpolicy to k8s-node2Normal   Pulling    48s               kubelet            Pulling image "nginx:1.17.1"Normal   Pulled     24s               kubelet            Successfully pulled image "nginx:1.17.1" in 24.531177953sNormal   Created    23s               kubelet            Created container nginxNormal   Started    23s               kubelet            Started container nginxWarning  Unhealthy  0s (x3 over 20s)  kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404Normal   Killing    0s                kubelet            Stopping container nginx
[root@k8s-master ~]# kubectl get pod pod-restartpolicy -n test
NAME                READY   STATUS      RESTARTS   AGE
pod-restartpolicy   0/1     Completed   0          53s

Always

可以发现pod会一直重启

[root@k8s-master ~]# vim pod-restartpolicy.yaml 
^C[root@k8s-master ~]#  cat pod-restartpolicy.yaml 
---
apiVersion: v1
kind: Pod
metadata:name: pod-restartpolicynamespace: test
spec:restartPolicy: Always   #论容器退出的原因是什么,都不会重启容器containers:- name: nginximage: nginx:1.17.1ports:- containerPort: 80name: nginx-portlivenessProbe:httpGet:scheme: HTTPport: 80path: /hello[root@k8s-master ~]# kubectl delete pod-restartpolicy.yaml 
error: the server doesn't have a resource type "pod-restartpolicy"
[root@k8s-master ~]# kubectl delete -f pod-restartpolicy.yaml 
pod "pod-restartpolicy" deleted
[root@k8s-master ~]# kubectl apply -f pod-restartpolicy.yaml 
pod/pod-restartpolicy created
[root@k8s-master ~]# kubectl get pod pod-restartpolicy -n test -w
NAME                READY   STATUS    RESTARTS   AGE
pod-restartpolicy   1/1     Running   0          5s
pod-restartpolicy   1/1     Running   1          31s

 

Pod常见状态转换场景

Pod中的容器Pod状态发生事件AlwaysOnFailureNever
包含一个容器Running容器成功退出RunningSucceededSucceeded
包含一个容器Running容器失败退出RunningRunningFailed
包含两个容器Running1个容器失败退出RunningRunningRunning
包含两个容器Running容器内存溢出挂掉RunningRunningFailed

注释:

  • 对于 Always 重启策略,容器将立即重启。

  • 对于 OnFailureNever 重启策略,如果容器成功退出且退出码为0,Pod状态将变为Succeeded。

  • 对于 Always 重启策略,容器将立即重启。

  • 对于 OnFailure 重启策略,容器将以非零退出码退出,因此会重启。

  • 对于 Never 重启策略,容器将不会重启,Pod状态将变为Failed。

  • 对于 Always 重启策略,由于内存溢出导致的容器终止将重启容器

  • 对于 OnFailure 重启策略,内存溢出导致的容器终止会触发重启,因为退出码是非零的。

  • 对于 Never 重启策略,容器将不会重启,Pod中其他容器继续运行,但失败的容器状态将为Terminated。


http://www.ppmy.cn/devtools/150990.html

相关文章

STL——map

目录 map类 map的构造 map的增删查 map的数据修改 map类 Key就是map底层关键字的类型&#xff0c;T是map底层value的类型&#xff0c;set默认要求Key⽀持 ⼩于⽐较&#xff0c;如果不⽀持或者需要的话可以⾃⾏实现仿函数传给第⼆个模版参数&#xff0c;map底层存储数据的内存…

Ubuntu 系统支持高刷显示:探索与实践

Ubuntu 系统支持高刷显示&#xff1a;探索与实践 引言 随着技术的发展&#xff0c;显示器的刷新率从传统的60Hz逐渐提升到了120Hz、144Hz甚至更高。高刷新率屏幕能够提供更加流畅的视觉体验&#xff0c;尤其在游戏和动画播放中显得尤为重要。对于使用Ubuntu操作系统的用户来说…

pytorch张量分块投影示例代码

张量的投影操作 背景 张量投影 是深度学习中常见的操作,将输入张量通过线性变换映射到另一个空间。例如: Y=W⋅X+b 其中: X: 输入张量(形状可能为 (B,M,K),即批量维度、序列维度、特征维度)。W: 权重矩阵((K,N),将 K 维投影到 N 维)。b: 偏置向量(可选,(N,))。Y:…

硬件实用技巧:TPS54331DR横杠标识识别1引脚

若该文为原创文章&#xff0c;转载请注明原文出处 本文章博客地址&#xff1a;https://hpzwl.blog.csdn.net/article/details/145116969 长沙红胖子Qt&#xff08;长沙创微智科&#xff09;博文大全&#xff1a;开发技术集合&#xff08;包含Qt实用技术、树莓派、三维、OpenCV…

Zookeeper(1)什么是Zookeeper?

Zookeeper 是一个分布式协调服务&#xff0c;主要用于分布式应用中的协调工作。它提供了分布式应用所需的一些基础服务&#xff0c;如配置管理、命名服务、分布式同步、组服务等。Zookeeper 通过其简单且高效的原语&#xff08;如节点、会话、观察者等&#xff09;实现了这些功…

G1原理—G1的GC日志分析解读

1.TLAB的GC日志解读 (1)一套基本的参数设置 首先参数上要有一套基本的设置&#xff1a; -XX:InitialHeapSize128M -XX:MaxHeapSize128M -XX:UseG1GC -XX:PrintGCDetails -XX:PrintGCTimeStamps -XX:PrintTLAB -XX:UnlockExperimentalVMOptions -XX:G1LogLevelfinest -XX:MaxGCP…

GaussDB分布式数据倾斜处理

常规数据倾斜巡检 在库中表个数少于1W的场景&#xff0c;直接使用倾斜视图查询当前库内所有表的数据倾斜情况 SELECT * FROM pgxc_get_table_skewness ORDER BY totalsize DESC;在库中表个数非常多&#xff08;至少大于1W&#xff09;的场景&#xff0c;因PGXC_GET_TABLE_SKEWN…

[Qt] 窗口 | 菜单栏MenuBar

目录 QMainWindow 概述 一、菜单栏 1、创建菜单栏 2、在菜单栏中添加菜单 3、创建菜单项 4、在菜单项之间添加分割线 5、添加快捷键 6、添加子菜单 7、添加图标 综合示例 QMainWindow 概述 Qt 窗口是通过 QMainWindow 类来实现的。 QMainWindow 是一个为用户 提供主…