Sachin Chaurasiya

Toolbox · Kubernetes

Kubernetes Commands

Everyday kubectl: context, workloads, rollout control, RBAC checks and resource inspection.

Last reviewed
2026-09-18
On this page

Context and namespaces

CommandPurpose
kubectl config get-contextsList clusters/contexts
kubectl config use-context prodSwitch context
kubectl config set-context --current --namespace=paymentsDefault namespace
kubectl get ns --show-labelsNamespaces with Pod Security labels

Workloads

CommandPurpose
kubectl get pods -o widePods with node and IP
kubectl get all -n paymentsCommon resources in a namespace
kubectl describe pod <pod>Events, conditions, mounts
kubectl logs <pod> -c <container> --previousLogs from the last crashed container
kubectl logs -l app=api --all-containers --tail=200Logs by label
kubectl exec -it <pod> -- shShell (only if the image has one)
kubectl debug -it <pod> --image=busybox:1.36 --target=<container>Ephemeral debug container
kubectl port-forward svc/api 8080:80Local access to a service
kubectl top pods --containersCPU/memory (metrics-server)

Rollouts

CommandPurpose
kubectl rollout status deploy/apiWait for rollout
kubectl rollout history deploy/apiRevisions
kubectl rollout undo deploy/api --to-revision=3Roll back
kubectl rollout restart deploy/apiRecreate pods (e.g. after a ConfigMap change)
kubectl scale deploy/api --replicas=0Caution stop serving without deleting

Apply and diff

CommandPurpose
kubectl apply -k overlays/prod --dry-run=serverServer-side validation without applying
kubectl diff -f manifest.yamlShow what would change
kubectl apply --server-side --field-manager=ci -f manifest.yamlServer-side apply
kubectl get deploy api -o yaml | kubectl neatClean YAML (krew plugin)

RBAC and security checks

CommandPurpose
kubectl auth can-i create pods -n paymentsPermission check
kubectl auth can-i --list --as=system:serviceaccount:payments:apiEverything a service account can do
kubectl get rolebindings,clusterrolebindings -A -o wideAll bindings
kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}/{.metadata.name}{"\t"}{.spec.securityContext.runAsNonRoot}{"\n"}{end}'runAsNonRoot per pod
kubectl get events -A --sort-by=.lastTimestamp | tail -30Recent cluster events