weima
weima

Reputation: 4902

kubectl wait for a pod to complete

I have a pod spec which runs a command like rm -rf /some/path

i create the pod using kubectl apply -f ...

now i want to wait till the pod completes. i can see that the pod is done, kubectl get pod/<mypod> shows STATUS Completed

How do i wait for this condition?

i have looked at kubectl wait ... but that doesnt seem to help me

kubectl wait --for=condition=complete pod/<my-pod> seems to just block. I havent deleted the pod, it is still there in the Completed status

Upvotes: 5

Views: 9035

Answers (3)

Wytrzymały Wiktor
Wytrzymały Wiktor

Reputation: 13858

The command that you use: kubectl wait --for=condition=complete pod/<my-pod> will not work because a pod doesn't have such condition. Pod Conditions are as follows:

  • PodScheduled: the Pod has been scheduled to a node.

  • ContainersReady: all containers in the Pod are ready.

  • Initialized: all init containers have completed successfully.

  • Ready: the Pod is able to serve requests and should be added to the load balancing pools of all matching Services.

The phase for a successfully completed pod is called succeeded:

All containers in the Pod have terminated in success, and will not be restarted.

It would be better however if you use kubectl wait for Jobs instead of bare Pods and than execute kubectl wait --for=condition=complete job/myjob.

Upvotes: 10

S&#246;ren Metje
S&#246;ren Metje

Reputation: 41

As proposed in this post, you can use the condition Ready=False. The following script waits until the pod stops running. It timeouts after 1 hour.

# wait until pod starts
kubectl wait --for condition=Ready pod/pod-name
# then wait until pod stops
kubectl wait --for condition=Ready=False --timeout=1h pod/pod-name

Upvotes: 1

kenske
kenske

Reputation: 2354

The other answers are better solutions, but if you must check the container status, you can use this:

kubectl wait --for=jsonpath="status.containerStatuses[0].state.terminated.reason"=Completed --timeout=10s pod/my-pod

This will only check the status of the first container in the pod.

Upvotes: 0

Related Questions