Using the kubectl wait
command in Kubernetes allows you to wait for specific conditions on one or more resources before continuing execution. This can be crucial for synchronization in complex deployment scenarios or automation scripts.
**Example 1:** Waiting for a Pod to reach the ‘Running’ state.
kubectl wait --for=condition=Ready pod/my-pod
This command waits until the Pod named my-pod
is in the ‘Ready’ state. It polls the cluster until the specified condition is met.
**Example 2:** Waiting for all Pods in a Deployment to be ready.
kubectl wait --for=condition=Ready pods --all -n mynamespace
Here, it waits until all Pods in the namespace mynamespace
are ready. The --all
flag ensures it checks all Pods.
**Example 3:** Waiting for a specific timeout period.
kubectl wait --for=condition=Ready pod/my-pod --timeout=60s
This command waits for up to 60 seconds for the Pod my-pod
to be ready. If the condition isn’t met within the timeout, it exits with an error.
**Example 4:** Waiting for a custom resource to be established.
kubectl wait --for=condition=Established crd/my-crd
For custom resources, such as my-crd
, this command waits until it’s established, indicating it’s ready for use.
**Example 5:** Waiting for a Job to complete.
kubectl wait --for=condition=complete job/my-job
It waits until the Job my-job
is complete, useful for scenarios where job execution must finish before proceeding.
**Example 6:** Waiting for a Service to have an external IP assigned.
kubectl wait --for=condition=ServiceIPAssigned service/my-service
This command waits for my-service
to have an assigned external IP, ensuring it’s ready for external communication.
**Example 7:** Waiting for a specific label to be applied to a resource.
kubectl wait --for=condition=Ready pod -l app=myapp
Here, it waits for any Pod labeled with app=myapp
to be in the ‘Ready’ state.
**Example 8:** Waiting for a specific condition on all resources in a namespace.
kubectl wait --for=condition=Ready --all -n mynamespace
This command waits for all resources in the namespace mynamespace
to be in the ‘Ready’ state before proceeding.
**Example 9:** Waiting for a Deployment to have a specified number of replicas available.
kubectl wait --for=condition=available deployment/my-deployment --timeout=120s
It waits for the Deployment my-deployment
to have the desired number of replicas available within 120 seconds.
**Example 10:** Waiting for a Pod to be scheduled on a specific node.
kubectl wait --for=condition=Scheduled pod/my-pod --node-name=node-1
This command waits until my-pod
is scheduled on node-1
, useful for node-specific troubleshooting.
To verify if the kubectl wait
command has executed successfully, you can check the exit status. If the command completes without errors and the condition specified is met (such as a Pod being ‘Ready’ or a Job completing), it indicates successful execution. Additionally, inspecting the resource’s status using kubectl get
or kubectl describe
can provide detailed information confirming the expected state.
Also check similar articles.
Replace Kubernetes Resources with kubectl replace
Patching Kubernetes Resources with kubectl patch
Applying Kubernetes Configurations with kubectl apply
Diffing Kubernetes Configurations with kubectl diff
Listing Kubernetes Events with kubectl events
Discussion about this post