k8s生命周期---pod重启策略

一旦容器存活探测出现了问题,kubernetes就会对容器所在的pod进行重启,其实这是由pod的重启策略决定的,pod的重启策略有3种,分别如下:

  • Always:容器失效时,自动重启该容器,这也是默认值。
  • OnFailure :容器终止运行且退出码不为0(异常终止)时重启
  • Never :不论状态为何,都不重启该容器

重启策略适用于pod对象中的所有容器,首次需要重启的容器,将在其需要时立即进行重启,随后再次需要重启的操作将由kubelet延迟一段时间后进行,且反复的重启操作的延迟时长以此为10s、20s、40s、80s、160s和300s,300s是最大延迟时长。

[root@k8s-master ~]# vim pod-restartpolicy.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-restartpolicy
  namespace: test
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    ports:
    - name: nginx-port
      containerPort: 80
    livenessProbe:
      httpGet:
        scheme: HTTP
        port: 80
        path: /hello
  restartPolicy: Always

[root@k8s-master ~]# kubectl create -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          3s
pod-restartpolicy   1/1     Running   1          27s
pod-restartpolicy   1/1     Running   2          56s
pod-restartpolicy   1/1     Running   3          86s
pod-restartpolicy   1/1     Running   4          116s
pod-restartpolicy   0/1     CrashLoopBackOff   4          2m26s
pod-restartpolicy   1/1     Running            5          3m15s
pod-restartpolicy   0/1     CrashLoopBackOff   5          3m36s
pod-restartpolicy   1/1     Running            6          4m58s
pod-restartpolicy   0/1     CrashLoopBackOff   6          5m26s
# 因为就绪检测不满足条件,并且根据pod的restartPolicy重启策略为Always,那么该pod会一直重启并根据重启次数到达一定值时,不再重启,启动失败。

# 删除,将restartPolicy重启策略修改为Never
[root@k8s-master ~]# kubectl delete -f pod-restartpolicy.yaml 
pod "pod-restartpolicy" deleted
[root@k8s-master ~]# cat pod-restartpolicy.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-restartpolicy
  namespace: test
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    ports:
    - name: nginx-port
      containerPort: 80
    livenessProbe:
      httpGet:
        scheme: HTTP
        port: 80
        path: /hello
  restartPolicy: Never

[root@k8s-master ~]# kubectl get pod pod-restartpolicy -n test -w
NAME                READY   STATUS    RESTARTS   AGE
pod-restartpolicy   1/1     Running   0          6s
pod-restartpolicy   0/1     Completed   0          30s    # 不会重启,STATUS变为Completed
[root@k8s-master ~]# kubectl describe pod pod-restartpolicy -n test 
……省略……
  Warning  Unhealthy  46s (x3 over 66s)  kubelet, k8s-node01  Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Killing    46s                kubelet, k8s-node01  Stopping container nginx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值