Kubernetes Tutorial · Kubernetes Basics · Lesson 3 of 26
kubectl Essentials
The kubectl commands you'll use daily: get, describe, apply, delete, logs, exec, and output formats.
Everyday commands
kubectl get pods # list (add -A for all namespaces, -o wide for more columns)
kubectl describe pod web-7d9f8 # details and recent events
kubectl apply -f deploy.yaml # create or update from a file (declarative)
kubectl delete -f deploy.yaml
kubectl logs deploy/web --tail 50 # logs (-f to follow, --previous after a crash)
kubectl exec -it web-7d9f8 -- sh # a shell in a container
kubectl explain deployment.spec.strategy # built-in API documentation
Output formats
-o yaml, -o json, -o jsonpath='{.status.phase}' and -o custom-columns=... turn kubectl into a scripting
tool. Use kubectl apply with files in Git rather than imperative create/edit for anything that lasts.
kubectl create namespace demo-kubectl > /dev/null
trap 'kubectl delete namespace demo-kubectl --wait=false > /dev/null' EXIT
kubectl -n demo-kubectl create deployment web --image=nginx:1.29-alpine > /dev/null
kubectl -n demo-kubectl rollout status deployment/web --timeout=120s > /dev/null
kubectl -n demo-kubectl get deployment web -o custom-columns=NAME:.metadata.name,READY:.status.readyReplicas,IMAGE:.spec.template.spec.containers[0].image
echo "pod phase: $(kubectl -n demo-kubectl get pods -l app=web -o jsonpath='{.items[0].status.phase}')"
kubectl explain deployment.spec.replicas | sed -n '/DESCRIPTION/,$p' | head -3
Output
NAME READY IMAGE
web 1 nginx:1.29-alpine
pod phase: Running
DESCRIPTION:
Number of desired pods. This is a pointer to distinguish between explicit
zero and not specified. Defaults to 1.Run this on your computer — see “Get Started” for setup.
See the kubectl reference.
Test yourself
Why prefer kubectl apply -f over kubectl edit for production changes?
apply is fasterFiles in Git are reviewable, repeatable and recoverable; edits are notedit is deprecatedapply works offline
Show answer
Answer: Files in Git are reviewable, repeatable and recoverable; edits are not
Declarative files are the source of truth that GitOps and reviews rely on.