LinuxCommandLibrary

kubectl-wait

Wait for Kubernetes resource condition to be met

TLDR

Wait for a deployment to become available

$ kubectl wait --for=condition=available deployment/[deployment_name]
copy

Wait for all pods with a certain [l]abel to be ready
$ kubectl wait --for=condition=ready pod [[-l|--selector]] [label_key]=[label_value]
copy

Wait for a pod to be deleted
$ kubectl wait --for=delete pod [pod_name]
copy

Wait for a job to complete, within 120 seconds (if the condition isn't met on time, the exit status will be unsuccessful)
$ kubectl wait --for=condition=complete job/[job_name] --timeout 120s
copy

SYNOPSIS

kubectl wait [--for=condition] [--timeout=duration] [--selector=string] [--field-selector=string] [(-f filename | -k directory | resource.group/resource.name)]

PARAMETERS

--for=condition
    The condition to wait for. Valid conditions are 'delete', 'condition=ConditionName' or 'jsonpath={jsonpath}'

--timeout=duration
    Maximum time to wait (e.g., 30s, 5m). Defaults to no timeout.

--selector=string
    Selector (label query) to filter on, supports '=', '==', and '!='. (= or == is required). For example, 'key1=value1,key2=value2'.

--field-selector=string
    Selector (field query) to filter on, supports '=', '==', and '!='. (= or == is required). The server only supports a limited number of field queries per type.

-f filename
    Filename, directory, or URL to files containing the resource(s) to wait for.

-k directory
    Process the set of Kubernetes manifests in the directory specified.

resource.group/resource.name
    Resource and name (e.g., pod/mypod, deployment.apps/mydeployment).

--all
    Select all resources in the namespace matched by the selector

--allow-missing-template-keys
    If true, ignore any errors in templates when a field or map key is missing in the template. Only applies if -o=template or -o=go-template is provided. Defaults to false.

--dry-run=string[="none"]
    Must be "none", "client", or "server". If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource.

--server-dry-run
    DEPRECATED: Server-side dry run is enabled by --dry-run=server flag.

--namespace=string
    If present, the namespace scope for this CLI request.

-o=string
    Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=....

--output=string
    Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=....

--recursive
    Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.

DESCRIPTION

kubectl wait is a powerful command-line tool within Kubernetes (kubectl) that allows you to pause execution until specific conditions are met for one or more Kubernetes resources. This is invaluable for automating deployments, ensuring dependencies are ready before proceeding, and orchestrating complex workflows. Instead of repeatedly polling a resource's status, kubectl wait blocks until a condition is satisfied or a timeout is reached. It checks the status of resources based on criteria you specify using selectors, resource names, and condition types. Conditions checked include resource readiness, existence, deletion, or specific fields reaching a desired value. This eliminates the need for custom scripting and improves reliability and efficiency in managing Kubernetes resources.
Crucially, incorrect usage, especially concerning selectors, timeouts, or resource kinds, can lead to unexpected delays or stalled processes. Therefore, understand selectors, target resources, and desired condition for reliable automation workflows.

CAVEATS

Ensure the service account used by kubectl has sufficient permissions to retrieve the status of the specified resources. Using very broad selectors can lead to excessive waiting times or targeting unintended resources.

EXAMPLES

Wait for a deployment to become available:
kubectl wait --for=condition=Available --timeout=60s deployment/my-deployment
Wait for a pod to be deleted:
kubectl wait --for=delete pod/my-pod --timeout=30s
Wait for any pod with label `app=myapp` to be running:
kubectl wait --for=condition=Ready --timeout=30s --selector=app=myapp pods

CONDITION TYPES

The most common condition type is `Ready` which is used to check if a resource is ready to serve traffic. `Available` may be specific to a Deployment. Check the API documentation for each resource kind for accepted conditions.

SEE ALSO

kubectl get(1), kubectl apply(1), kubectl delete(1)

Copied to clipboard