Kubectl Cheat Sheet ๐Ÿ“Ž

Today, I want to share a comprehensive command reference for everyday Kubernetes use - cleaner, deeper, and more practical than the standard cheat sheets

Kubectl Cheat Sheet


๐Ÿ”ง Context & Configuration

CommandDescription
kubectl config get-contextsList all contexts
kubectl config use-context <context>Switch to a different context
kubectl config current-contextShow the active context
kubectl config view --minifyView config of current context
kubectl config set-contextCreate or modify a context
kubectl config set-clusterSet a cluster config
kubectl config set-credentialsConfigure user credentials
kubectl config unset users.<user>Remove a user from config

๐Ÿ“ฆ Pods

CommandDescription
kubectl get podsList all pods in current namespace
kubectl get pods -AList pods across all namespaces
kubectl get pod <name> -o wideShow pod details including IP and node
kubectl describe pod <name>Detailed pod information
kubectl logs <pod>Logs from main container
kubectl logs <pod> -c <container>Logs from a specific container
kubectl exec -it <pod> -- /bin/shOpen shell session in pod
kubectl delete pod <pod>Delete a pod (useful for restart)

๐Ÿš€ Deployments

CommandDescription
kubectl get deployList deployments
kubectl describe deploy <name>View deployment details
kubectl scale deploy <name> --replicas=3Scale deployment
kubectl rollout status deploy <name>Track rollout progress
kubectl rollout history deploy <name>Show rollout history
kubectl rollout undo deploy <name>Roll back to previous revision
kubectl edit deploy <name>Edit deployment in-place
kubectl delete deploy <name>Delete a deployment

๐Ÿ“‹ Services

CommandDescription
kubectl get svcList services
kubectl describe svc <name>Detailed service info
kubectl expose pod nginx --port=80 --type=ClusterIPExpose pod as service
kubectl port-forward svc/<svc> 8080:80Forward local port to service
kubectl get endpointsShow service endpoints

๐ŸŒ Ingress

CommandDescription
kubectl get ingressList ingress resources
kubectl describe ingress <name>Details of an ingress resource

๐Ÿ“ฆ ConfigMaps & Secrets

CommandDescription
kubectl get configmapList ConfigMaps
kubectl create configmap <name> --from-literal=key=valueCreate from literal
kubectl create configmap <name> --from-file=file.txtCreate from file
kubectl get secretList Secrets
kubectl create secret generic <name> --from-literal=password=secretCreate basic secret
kubectl get secret <name> -o yamlView base64-encoded secret
kubectl get secret <name> -o jsonpath="{.data.key}" \| base64 -dDecode a secret

๐Ÿ” RBAC & ServiceAccounts

CommandDescription
kubectl create sa <name>Create a ServiceAccount
kubectl get saList ServiceAccounts
kubectl get clusterrolebinding -AList all ClusterRoleBindings
kubectl describe clusterrolebinding <name>Show details of binding
kubectl auth can-i get pods --as=system:serviceaccount:ns:saCheck access for a ServiceAccount

๐Ÿ“‚ Namespaces

CommandDescription
kubectl get nsList namespaces
kubectl create ns <name>Create a namespace
kubectl delete ns <name>Delete a namespace
kubectl config set-context --current --namespace=<ns>Set default namespace for current context

๐Ÿ›  Jobs & CronJobs

CommandDescription
kubectl create job hello --image=busybox -- echo HelloRun one-time job
kubectl get jobsList jobs
kubectl delete job <name>Delete a job
kubectl create cronjob hello --image=busybox --schedule="*/5 * * * *" -- echo HiSchedule recurring job
kubectl get cronjobList CronJobs

๐Ÿ“ฆ Storage (PVs & PVCs)

CommandDescription
kubectl get pvList PersistentVolumes
kubectl get pvcList PersistentVolumeClaims
kubectl describe pvc <name>PVC details
kubectl delete pvc <name>Delete a PVC

๐Ÿ” Debugging & Troubleshooting

CommandDescription
kubectl get events --sort-by=.metadata.creationTimestampShow recent events
kubectl logs <pod> --previousLogs from a crashed pod
kubectl exec -it <pod> -- /bin/shOpen shell in pod
kubectl debug -it <pod> --image=busybox --target=<container>Ephemeral debug container
kubectl top podShow pod CPU/memory usage
kubectl get pods -o wideShow node assignments and IPs

๐Ÿ“ค YAML & Apply

CommandDescription
kubectl apply -f <file>.yamlApply a YAML manifest
kubectl delete -f <file>.yamlDelete resource defined in YAML
kubectl apply -f <file>.yaml --dry-run=client -o yamlPreview resource definition
kubectl explain <resource>Show schema for a resource

๐Ÿ“Š Output Formatting

CommandDescription
-o wideShow more details (e.g. IPs, nodes)
-o yamlOutput full YAML
-o jsonpath="{.items[*].metadata.name}"Query JSON paths
--field-selector status.phase=RunningFilter by field
-l app=nginxFilter by label
--sort-by=.metadata.nameSort output

๐Ÿ” Port Forwarding & Proxies

CommandDescription
kubectl port-forward pod/<pod> 8080:80Port forward to pod
kubectl port-forward svc/<svc> 9090:80Port forward to service
kubectl proxyRun API proxy at localhost:8001

๐Ÿงผ Cleanup & Deletion

CommandDescription
kubectl delete all --allDelete all resources in namespace
kubectl delete pod,svc -l app=nginxDelete resources by label
kubectl delete pvc --allRemove all PVCs

๐Ÿงช Common Shortcuts

TaskCommand
Restart a podkubectl delete pod <pod>ย (Deployment auto-recreates)
Watch pod statuswatch kubectl get pods
Quick deploy NGINXkubectl create deploy nginx --image=nginx
Expose NGINXkubectl expose deploy nginx --port=80 --type=LoadBalancer