⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Kubernetes Interview Questions Scenario 108 of 207 in Kubernetes
Senior DevOps / SRE [L2] Kubernetes Advanced Scenarios Production Scenario [L2]
🎯 Target Role / Context: Kubernetes Scenario #98 (L2) · Advanced Scenarios

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).

#Kubernetes #Advanced Scenarios #L2 #Container Orchestration #K8s #Comparisons
🎙️ Candidate Opening & Architectural Context
"<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>."
Advertisement
⚡ Recommended Practice Lab

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>.

1️⃣

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: -f and -k cannot be combined in a single command.
  • No templating language: Kustomize patches and transforms valid YAML, unlike Helm which templates it.
2️⃣

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, namePrefix and commonLabels edit every matching object without touching the base.
  • Generators: configMapGenerator appends a content hash to the name and rewrites references, so a config change triggers a rollout.
Advertisement
3️⃣

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 kustomize prints the final YAML without touching the cluster.
  • Diff: kubectl diff -k compares the rendered result to live state (exit code 1 means differences).
  • Apply: kubectl apply -k; add --server-side to use server-side apply and field ownership.
  • Plain files: apply -f with --dry-run=server validates against the API server without persisting.
Pro Tip: The Kustomize bundled in kubectl lags the standalone <code>kustomize</code> binary; check <code>kubectl version</code> output for the embedded Kustomize version if a newer field is rejected.
4️⃣

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.
5️⃣

Pitfalls and CI practice

Most surprises are about file selection and hidden transforms.

  • Forgotten resources: a new YAML file does nothing under -k until it is added to resources:.
  • Pruning: --prune needs a label selector (-l) or --all and can delete objects, so test with --dry-run first.
  • Hash suffixes: generated names change on content updates, so reference them through Kustomize, never hard-code them.
  • Pipeline gate: run kubectl kustomize piped to a validator such as kubeconform in CI before deploying.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Run kubectl diff -k overlays/<env> in CI before every apply -k, so you review the rendered result that actually reaches the cluster rather than the base files."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Adding a new file to an overlay directory without listing it in resources Kustomize ignores unlisted files, so the object never reaches the cluster.
  2. Trying to use -f and -k together kubectl rejects the combination; put plain files under resources: in the kustomization instead.
  3. Hard-coding generated ConfigMap names The content hash suffix changes with the data; only Kustomize-managed references are rewritten.
  4. Applying overlays without previewing Patches and image transforms can change many objects silently; render or diff first.
Advertisement
📥 FREE DOWNLOAD · 101-PAGE COMPANION HANDBOOK
Studying for Kubernetes & SRE Technical Rounds?
Download the complete 100-question PDF field guide covering all 11 core modules with offline diagnostic runbooks.
📥 Download PDF (Free) Read Online Guide →
Want more Kubernetes scenarios?
Explore our complete collection of scenario-based Kubernetes interview runbooks.
Browse All Kubernetes Questions →