一旦容器存活探测出现了问题,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