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

时间:2025-03-26 20:52:54
[root@k8s-master ~]# vim 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/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 "pod-restartpolicy" deleted [root@k8s-master ~]# cat 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