kubectl 1.37 reference · Basics

kubectl apply Command (kubectl)

Create or update resources from files, directories or Kustomize folders declaratively; the standard way to deploy manifests.

kubectl 1.37

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

kubectl apply -f FILE|DIR|URL | -k DIR [--dry-run=server] [--server-side]

Parameters

ParameterDescription
-f FILE Manifest file, directory or - for stdin.
-k DIR Apply a Kustomize directory.
--dry-run=server Validate against the API server without saving.
--server-side Server-side apply with field ownership.

Examples

Create, change, no-op
kubectl create namespace rc-ref-apply > /dev/null
trap 'kubectl delete namespace rc-ref-apply --wait=false > /dev/null' EXIT
cat > cm.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata: {name: app}
data: {LOG_LEVEL: info}
EOF
kubectl -n rc-ref-apply apply -f cm.yaml
sed -i 's/info/debug/' cm.yaml
kubectl -n rc-ref-apply apply -f cm.yaml
kubectl -n rc-ref-apply apply -f cm.yaml

Output

configmap/app created
configmap/app configured
configmap/app unchanged

Run this on your computer — see “Get Started” for setup.

Server-side validation without changes
kubectl create namespace rc-ref-apply2 > /dev/null
trap 'kubectl delete namespace rc-ref-apply2 --wait=false > /dev/null' EXIT
cat > bad.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: web}
spec:
  replicas: three
EOF
kubectl -n rc-ref-apply2 apply --dry-run=server -f bad.yaml 2>&1 | grep -oE 'replicas[^,]*' | head -1

Output

replicas of type int32

Run this on your computer — see “Get Started” for setup.

Was this page helpful?