skip to Main Content

Is there a command that can be executed in between this two instructions (which fails if the pod does not exist yet), and will wait for the pod to be created in the api-server within a specified timeout?

kubectl run mypod --image=nginx 
# Add a command which qait for 'kubectl get pod' to return mypod,
# so that 'kubectl wait' command will not fail
<wanted command>
kubectl wait pod/mypod --for=condition=Ready -l olm.catalogSource=argocd-catalog --timeout=120s

2

Answers


  1. You can create combined commands using && (and) and || (or) between commands to implement how to run according to the different conditions.

    The following combined command tries to run/create mypod. If it is created in a specific timeout, mypod will be created. After ||, the delete command doesn’t run, because the previous command works properly.

    kubectl run mypod --image=nginx && kubectl wait --for=condition=Ready pod/mypod --timeout=20s || kubectl delete pod mypod
    

    Output:

    pod/mypod created
    pod/mypod condition met
    

    Worst case scenario: The following command tries to run nginx2 (it won’t work, due to the ImagePullBackOff). It first created the pod, but the pod is not ready, so it’ll run the delete command after ||.

    kubectl run mypod --image=nginx2 && kubectl wait --for=condition=Ready pod/mypod --timeout=20s || kubectl delete pod mypod
    

    Output:

    pod/mypod created
    error: timed out waiting for the condition on pods/mypod
    pod "mypod" deleted
    

    You can create a similar combination with get pods instead of creating a pod.

    Login or Signup to reply.
  2. As per the Github link, you can use the below command as a workaround.

    kubectl wait timeout argument is poorly documented and ill-suited to waiting on multiple resources.You can use timeout before the kubectl wait or oc wait. For example with a timeout of max. 305s (the timeout of the timeout command should be a little larger than the timeout of the kubectl command)

    timeout $((300+5)) kubectl wait --for=condition=Ready --all pod --timeout=300s
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search