Skip to content
beginner Phase 1 · Kubernetes Foundations

kubectl Fundamentals

Master the kubectl command-line tool for interacting with Kubernetes clusters.

1h
3 problems
Topic Progress 0%

Imperative Verbs

Imperative Verbs

kubectl is a REST client: each command maps to an HTTP verb on an API path. The core set:

kubectl get pods -n prod -o wide          # list with node and pod IP
kubectl describe pod web-7d9f-x2n9l       # object plus recent events
kubectl logs -f deploy/web --all-containers
kubectl exec -it web-7d9f-x2n9l -- sh     # shell into a running container
kubectl delete pod web-7d9f-x2n9l
kubectl top pods                          # needs metrics-server

Output formats change what you can see: -o yaml returns the full server object, -o jsonpath='{.status.phase}' extracts one field, and -o wide adds node and IP. describe is the debugging entry point because it appends scheduler and kubelet events beneath the spec.

Contexts, Namespaces, and Edits

Contexts, Namespaces, and Edits

A kubeconfig holds clusters, users, and contexts; the current context selects both endpoint and credentials:

kubectl config get-contexts
kubectl config use-context staging-admin
kubectl config set-context --current --namespace=payments

Guard destructive work with explicit scope and a rehearsal:

kubectl -n staging delete pod -l app=web --dry-run=server
kubectl diff -f web.yaml

Prefer kubectl apply -f on files tracked in Git. kubectl edit opens the object in your editor and PATCHes only the diff — fine for a hotfix, invisible to code review afterward. Use kubectl apply --dry-run=client to validate syntax before any cluster write.

Common kubectl Traps

Common kubectl Traps

Pitfall 1 — default namespace drift. Commands without -n act on default, and a mistyped resource type can delete something unintended. Pin your context namespace (see above) and use kubectl api-resources to confirm short names like deploy or rs.

Pitfall 2 — logs after a restart. kubectl logs shows the current container only; for the crashed one use --previous (-p), which requires the container to have terminated.

Interview-ready: state your debugging order — get for state, describe for events, logs for output, exec to inspect — plus the request path kubectl → apiserver (auth and RBAC) → object, and --dry-run=server as the final safety net.

Quiz

1. Which flag previews exactly what the API server would accept?

Question 1 options

2. How do you read logs from the container that just crashed?

Question 2 options

3. What does a kubeconfig "context" select?

Question 3 options

Flashcards

Question

get vs describe vs logs

Answer

get = current state, describe = state plus events, logs = container output.

Question

dry-run modes

Answer

client validates locally; server also runs API validation and admission without persisting.

Question

Switching targets safely

Answer

kubectl config use-context, or --context / -n flags for one-off overrides.

Question

Imperative vs declarative

Answer

Imperative commands mutate now; declarative apply reconciles files in Git as source of truth.

Revision Notes

Key Takeaways

  • 1. Every kubectl verb maps to an API operation you can reproduce with curl
  • 2. Contexts bundle cluster and identity — check current-context before destructive work
  • 3. Debug in order: get, describe, logs, exec

Interview Tips

  • • Name the request path and where RBAC rejects you (403 from apiserver)
  • • Mention dry-run and diff as your change-safety workflow

Cheat Sheet

get/describe/logs/exec for triage; -o yaml|jsonpath|wide for output; config use-context to switch; --dry-run=server and diff before apply.

Continue Learning