CKA [Logging] – Container Restart Policy的讲解

白话的讲就是Always 不管是否是程序走完,都是遇到failure都是直接重启,默认没有set restart policy的话就是Always的。

而OnFailure就是只是有failure才重启了,程序走完了的是不会重启的。

Never就是任何情况都不会重启。

✅ Always Restart Policy

1. 在busybox container中运行nonexistentcommand, nonexistentcommand是不存在的命令行来的,会报错。默认没有指定Restart Policy的话就是Always的

kubectl run test-pod --image=busybox -- nonexistentcommand

2. 查看Pod , 我们可以看到RunContainerError了,并且重启了2次

kubectl get pods -w

✅ Never Restart Policy

1. 在busybox container中运行nonexistentcommand, nonexistentcommand是不存在的命令行来的,会报错。在这里我们指定使用Never的RestartPolicy

    kubectl run test-pod --restart=Never --image=busybox -- nonexistentcommand

    2. 查看Pod, 即便是ContainerCannotRun 也不会再重启,Restart数维持为0

    ✅ OnFailure Restart Policy

    1. 创建2种Pod, 一种是特地写错command造成Failure。而另一种就是程序执行完毕的Pod。

    【特地写错command造成Failure】
    kubectl run test-pod-1 --restart=OnFailure --image=busybox -- nonexistentcommand
    
    【程序执行完毕】
    kubectl run test-pod-2 --restart=OnFailure --image=busybox -- echo "Hello Kubernetes"

    2. 查看2种Pod的情况

    kubectl get pods -w

    对于【特地写错command造成Failure】的Pod就会重启,restart数我们看到是4次

    对于【程序执行完毕】的Pod就不会再重启,restart数我们看到是0次

    Loading

    Facebook评论