apply
kubectl apply
Declaratively creates or updates resources from a manifest file, directory, URL, or kustomization. Tracks the last-applied configuration so subsequent applies compute a three-way merge patch rather than replacing the object.
kubectl apply -f FILENAME [-k DIRECTORY] [flags] Common flags
| Flag | Purpose |
|---|---|
| -f, --filename | Manifest file, directory, or URL (repeatable). |
| -k, --kustomize | Apply a kustomization directory instead of raw YAML. |
| -R, --recursive | Recurse into directories referenced by -f. |
| --dry-run | none, client, or server; server does full admission without persisting. |
| --server-side | Use server-side apply with field ownership tracking. |
| --force-conflicts | Force take ownership of fields during server-side apply. |
| --prune | Delete previously applied resources no longer in the input set. |
| -l, --selector | Restrict prune/apply to resources matching a label selector. |
Examples
kubectl apply -f deploy.yaml Create or update the resources defined in a manifest.
kubectl apply -k ./overlays/prod Apply a Kustomize overlay.
kubectl apply -f . -R --dry-run=server Validate a whole tree of manifests through the API server without persisting.
kubectl apply --server-side --force-conflicts -f deploy.yaml Take field ownership when migrating to SSA.
Gotcha
apply on an object created with create/kubectl edit has no last-applied annotation and may print a warning; server-side apply avoids that footgun. --prune is powerful and can delete more than you expect - always test with --dry-run=server first.
Related commands
kubectl create
Imperatively creates a resource from a file or from built-in generators (deployment, configmap, secret, job, etc.). Unlike apply, it fails if the resource already exists and does not record a last-applied annotation.
kubectl patch
Applies a partial update to a resource using strategic-merge, JSON-merge, or JSON-patch semantics. It is the surgical tool for changing a single field without rewriting the whole manifest.
kubectl replace
Replaces a resource with the complete definition from a file, overwriting the entire spec. With --force it deletes and recreates the resource, which is useful for immutable fields.
kubectl edit
Fetches a live resource, opens it in your $EDITOR, and applies the diff on save. Convenient for one-off tweaks; not recommended for production changes that should live in Git.