kubectl Basics
kubectl essentials: contexts, namespaces, get / describe / logs / exec, apply, rollout, port-forward. The daily verbs.
Kubernetes — kubectl essentials
EXAMPLE
# ===== Contexts + namespaces =====
kubectl config get-contexts
kubectl config use-context my-cluster
kubectl config current-context
kubectl config set-context --current --namespace=dev
# Or use kubectx + kubens for fast switching.
# ===== Get / describe =====
kubectl get pods
kubectl get pods -A # all namespaces
kubectl get pods -n kube-system
kubectl get pods -l app=shop # label selector
kubectl get pods -o wide # IPs, nodes
kubectl get pods -o yaml # full YAML
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl describe pod my-pod
kubectl describe svc shop
# ===== Logs =====
kubectl logs my-pod
kubectl logs my-pod -c api # specific container in pod
kubectl logs -f my-pod # follow
kubectl logs -l app=shop --tail=200 # by label
kubectl logs my-pod --previous # previous container's logs
# Stern for multi-pod tail:
stern -n default '.*'
# ===== Exec =====
kubectl exec -it my-pod -- sh
kubectl exec my-pod -- env
kubectl exec my-pod -- cat /etc/hostname
# ===== Apply / delete =====
kubectl apply -f deploy.yaml
kubectl apply -f ./k8s/ -R # entire folder
kubectl delete -f deploy.yaml
kubectl delete pod my-pod # restarts under Deployment
kubectl delete pods --all # all in current namespace
# Server-side apply (idempotent + diffable):
kubectl apply --server-side -f deploy.yaml
# ===== Rollout =====
kubectl rollout status deploy/shop
kubectl rollout history deploy/shop
kubectl rollout undo deploy/shop
kubectl rollout restart deploy/shop # rolling restart with no spec change
# ===== Scale =====
kubectl scale deploy/shop --replicas=5
kubectl autoscale deploy/shop --min=2 --max=10 --cpu-percent=75
# ===== Port-forward =====
kubectl port-forward svc/shop 8080:80
kubectl port-forward pod/my-pod 5432:5432
# ===== Run + create =====
kubectl run mycurl --rm -it --image=curlimages/curl -- sh
kubectl create job hello --image=busybox -- echo hi
# ===== Diff + dry run =====
kubectl diff -f deploy.yaml # show changes before apply
kubectl apply --dry-run=client -f deploy.yaml
kubectl apply --dry-run=server -f deploy.yaml
# ===== Imperative -> declarative export =====
kubectl create deploy nginx --image=nginx -o yaml --dry-run=client > deploy.yaml
# Edit + version + apply.
# ===== Events =====
kubectl get events --sort-by=.metadata.creationTimestamp -A | tail -20
# ===== Aliases worth setting =====
alias k=kubectl
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kd='kubectl describe'
alias kex='kubectl exec -it'
# ===== Patterns to internalise =====
# - Set the namespace via context, not -n on every command
# - Use labels + selectors for batch ops (logs / delete / scale)
# - kubectl diff before apply on prod
# - Rollout restart for config / secret changes that need fresh pods
# ===== Pitfalls =====
# - Editing live resources with 'kubectl edit' -> drifts from git
# - Deleting StatefulSet pods to 'restart' (data risk)
# - Running 'kubectl apply' on prod from a wrong context
# - 'kubectl exec' for ops without a record (use audit logs)
Why it matters
kubectl is your daily driver: contexts + namespaces, get / describe / logs / exec, apply / diff, rollout / scale, port-forward. Pair it with kubectx / kubens / stern and the workflow gets snappy. Lean on labels and selectors for batch ops; avoid live edits that drift from git.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
kubectl get pods kubectl get all -A kubectl apply -f deployment.yaml kubectl logs -f pod/api-1Try it Yourself »
Exercise
List every pod in every namespace.
kubectl get pods
Two characters.
Discussion
Loading…