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
Syntax
kubectl apply -f FILE|DIR|URL | -k DIR [--dry-run=server] [--server-side]
Parameters
| Parameter | Description |
|---|---|
-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
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.
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.
Related
kubectl delete· Delete resources by name, file or label selector; deleting a namespace deletes everything inside it.kubectl diff· Show what kubectl apply would change in the cluster, as a diff against the live objects, before you apply it.- kubectl Essentials · tutorial