Q: Explain the difference between `kubectl apply` with a file vs `kubectl apply -k` (kustomize).
kubectl apply -f vs apply -k: -f applies the manifest files exactly as written, while -k first builds a directory containing a kustomization.yaml (adding overlays, patches, name prefixes, image overrides and generated ConfigMaps).
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: <strong>kubectl apply -f vs apply -k:</strong> <code>-f</code> applies the manifest files exactly as written, while <code>-k</code> first builds a directory containing a <code>kustomization.yaml</code> (adding overlays, patches, name prefixes, image overrides and generated ConfigMaps) and then applies the rendered result. Both use the same declarative apply mechanism afterwards. Interviewers ask this to see whether you understand template-free configuration management and when plain files are enough. The right answer covers both sides: simple, single-environment manifests fit <code>-f</code>, multi-environment variation fits <code>-k</code>.
What each flag does
Same apply semantics, different input.
- apply -f: reads a file, directory (non-recursive unless
-R) or URL and sends each object as given. - apply -k: takes a directory with
kustomization.yaml, renders it through the Kustomize library built into kubectl, then applies the output. Files not listed in the kustomization are ignored. - Exclusive:
-fand-kcannot be combined in a single command. - No templating language: Kustomize patches and transforms valid YAML, unlike Helm which templates it.
A base and overlay layout
Keep shared manifests in a base and environment differences in overlays.
# overlays/prod/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: prod
resources:
- ../../base
images:
- name: registry.example.com/my-app
newTag: 1.4.2
replicas:
- name: my-app
count: 6
configMapGenerator:
- name: my-app-config
literals:
- LOG_LEVEL=info
- base: common Deployment and Service with no environment details.
- overlay: sets namespace, replica count, image tag and patches per environment.
- Image and name transforms:
images,namePrefixandcommonLabelsedit every matching object without touching the base. - Generators:
configMapGeneratorappends a content hash to the name and rewrites references, so a config change triggers a rollout.
Commands: render, diff and kubectl apply
Always preview what Kustomize produces before applying.
# Plain manifests
kubectl apply -f deploy/ --dry-run=server
# Kustomize
kubectl kustomize overlays/prod | less
kubectl diff -k overlays/prod
kubectl apply -k overlays/prod
kubectl get deploy my-app -n prod
- Render only:
kubectl kustomizeprints the final YAML without touching the cluster. - Diff:
kubectl diff -kcompares the rendered result to live state (exit code 1 means differences). - Apply:
kubectl apply -k; add--server-sideto use server-side apply and field ownership. - Plain files:
apply -fwith--dry-run=servervalidates against the API server without persisting.
When to use which
Match the tool to the amount of variation.
- Choose -f when: one environment, a handful of static manifests, vendor YAML you do not want to modify, or quick experiments.
- Choose -k when: the same app deploys to dev, staging and prod with different replicas, images, namespaces or config.
- Beyond both: Helm suits packaged, parameterised distribution; Kustomize suits patching YAML you own or a rendered chart.
- GitOps: Argo CD and Flux render Kustomize natively, so the same overlays work from the CLI and in-cluster.
Pitfalls and CI practice
Most surprises are about file selection and hidden transforms.
- Forgotten resources: a new YAML file does nothing under
-kuntil it is added toresources:. - Pruning:
--pruneneeds a label selector (-l) or--alland can delete objects, so test with--dry-runfirst. - Hash suffixes: generated names change on content updates, so reference them through Kustomize, never hard-code them.
- Pipeline gate: run
kubectl kustomizepiped to a validator such as kubeconform in CI before deploying.
- apply -f sends manifests as written, apply -k renders a kustomization with overlays, patches and generators first and then applies the output.
- I verify with kubectl kustomize and kubectl diff -k, plus a server-side dry run.
- I use -f for simple single-environment YAML and -k for multi-environment variation, and I automate the render and diff in CI or GitOps.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Adding a new file to an overlay directory without listing it in resources Kustomize ignores unlisted files, so the object never reaches the cluster.
- Trying to use -f and -k together kubectl rejects the combination; put plain files under resources: in the kustomization instead.
- Hard-coding generated ConfigMap names The content hash suffix changes with the data; only Kustomize-managed references are rewritten.
- Applying overlays without previewing Patches and image transforms can change many objects silently; render or diff first.