LinuxCommandLibrary

kubectl-apply

Apply or update Kubernetes configurations

TLDR

Apply a configuration to a resource by file name

$ kubectl apply [[-f|--filename]] [path/to/file]
copy

Apply a configuration to a resource from kustomization.yaml in a directory
$ kubectl apply [[-k|--kustomize]] [path/to/directory]
copy

Apply a configuration to a resource by stdin
$ [cat pod.json] | kubectl apply [[-f|--filename]] -
copy

Edit the latest last-applied-configuration annotations of resources from the default editor
$ kubectl apply edit-last-applied [[-f|--filename]] [path/to/file]
copy

Set the latest last-applied-configuration annotations by setting it to match the contents of a file
$ kubectl apply set-last-applied [[-f|--filename]] [path/to/file]
copy

View the latest last-applied-configuration annotations by type/name or file
$ kubectl apply view-last-applied [[-f|--filename]] [path/to/file]
copy

SYNOPSIS

kubectl apply [OPTIONS] [--] [FILE | DIRECTORY | URL]+

PARAMETERS

-f, --filename=[]
    Identify Kubernetes resources by file names, directories or URLs. Multiple resources can be specified.

--dry-run='none'
    none|client|server: Dry run to preview changes without applying.

--field-manager='kubectl-apply'
    String identifying the field manager for ownership tracking.

--force-conflicts
    Force resolution of conflicts in server-side apply.

-k, --kustomize=''
    Process directory as Kustomize package before applying.

--prune=false
    Remove resources listed in prune annotations not in applied set.

--recursive=false
    Process directory recursively for files.

--server-side
    Use server-side apply instead of client-side.

--template=''
    Template string or path to apply.

--validate=true
    Validate resources before applying.

--wait=false
    Wait for resources to be ready before returning.

DESCRIPTION

kubectl apply is a core command in the Kubernetes CLI tool kubectl, used to declaratively manage resources by applying configurations from YAML or JSON files, directories, or URLs.

It creates resources if they don't exist, updates them if they do, using a strategic merge patch to reconcile changes without overwriting unrelated fields. This makes it ideal for continuous deployment and GitOps workflows.

Unlike kubectl create, which fails on existing resources, apply handles updates seamlessly. It supports server-side apply (default in newer versions) for better concurrency and field ownership tracking via managers.

Key features include dry-run previews, validation, pruning of removed resources, and integration with Kustomize for overlays. Use --dry-run=client to simulate or --server-side for atomic applies.

Common workflow: kubectl apply -f config.yaml. It outputs annotations like age and status, confirming changes.

CAVEATS

Strategic merge may not handle all list/map changes intuitively; use strategic-merge-patch or server-side apply for complex updates.
Client-side apply deprecated in Kubernetes 1.29+; prefer server-side.
Namespace mismatches cause failures unless overridden.

EXAMPLES

kubectl apply -f deployment.yaml
kubectl apply -k ./overlays/prod
kubectl apply -f - < config.yaml

APPLY VS CREATE

apply merges changes declaratively; create fails if resource exists and does full create.

HISTORY

Introduced in Kubernetes v1.0 (2014) for declarative management. Server-side apply added in v1.15 (2018) for improved concurrency. Client-side apply deprecated in v1.22, fully removed in v1.29 (2023). Evolved with Kustomize integration in v1.14.

SEE ALSO

kubectl create(1), kubectl patch(1), kubectl replace(1), kustomize(1), kubectl diff(1)

Copied to clipboard