Skip to content
DBDeependra Bhatta~/notes
Kubernetes#kubectl · #cheat-sheet · #kubernetes

kubectl Cheat Sheet by Category

The most-used kubectl commands, grouped by task: cluster info, namespaces, pods, Deployments, Services, ConfigMaps, Secrets, storage, contexts and debugging.

· updated · 8 min read
ON THIS PAGE

Most day-to-day cluster work relies on a small set of kubectl commands. This reference groups them by task, from cluster info to debugging, and every command works the same on minikube, a kubeadm cluster or Amazon EKS. The earlier parts of this series explain the objects behind them, and the official kubectl reference lists every command.

Cluster and node info

CommandUse
kubectl versionShow client and server versions
kubectl cluster-infoShow the control plane and core service addresses
kubectl get nodesList nodes
kubectl describe node <node-name>Detailed node information
kubectl top nodesNode CPU and memory use (needs metrics-server)

Namespaces

CommandUse
kubectl get namespacesList namespaces
kubectl create namespace devCreate a namespace
kubectl delete namespace devDelete a namespace and everything in it
kubectl config set-context --current --namespace=devMake dev the default namespace

Pods

CommandUse
kubectl run mynginx --image=nginx --port=80Start a single pod
kubectl get podsList pods
kubectl get pods -o wideList pods with node and IP
kubectl describe pod <pod>Detailed pod info and events
kubectl logs <pod>Show logs
kubectl logs <pod> -c <container>Logs of one container in the pod
kubectl exec -it <pod> -- /bin/bashOpen a shell in the container
kubectl port-forward <pod> 8080:80Forward local port 8080 to port 80 in the pod
kubectl delete pod <pod>Delete a pod

Deployments

CommandUse
kubectl get deploymentsList Deployments
kubectl create deployment nginx --image=nginxCreate a Deployment
kubectl describe deployment <name>Deployment details
kubectl scale deployment nginx --replicas=5Scale a Deployment
kubectl set image deployment/nginx nginx=nginx:1.27Update the container image
kubectl rollout status deployment/nginxWatch a rollout
kubectl rollout history deployment/nginxShow rollout history
kubectl rollout undo deployment/nginxRoll back to the previous revision
kubectl rollout restart deployment/nginxRestart all pods with a rolling update

Services

CommandUse
kubectl get svcList Services
kubectl expose deployment nginx --type=NodePort --port=80Create a Service for a Deployment
kubectl describe svc <name>Service details, including endpoints
kubectl delete svc <name>Delete a Service

ConfigMaps

CommandUse
kubectl create configmap mycfg --from-literal=ENV=prodCreate from a key-value pair
kubectl create configmap mycfg --from-file=app.confCreate from a file
kubectl get configmapsList ConfigMaps
kubectl describe configmap mycfgShow keys and values
kubectl delete configmap mycfgDelete a ConfigMap

Secrets

CommandUse
kubectl create secret generic mysecret --from-literal=password=<value>Create a Secret
kubectl get secretsList Secrets
kubectl describe secret mysecretShow keys and sizes, not values
kubectl get secret mysecret -o yamlShow the base64-encoded values
kubectl delete secret mysecretDelete a Secret

Secret values are only base64-encoded, not encrypted. To read one value:

terminal
$ kubectl get secret mysecret -o jsonpath='{.data.password}' | base64 --decode

Ingress

CommandUse
kubectl get ingressList Ingress objects
kubectl describe ingress <name>Ingress rules and backends
kubectl delete ingress <name>Delete an Ingress

Jobs and CronJobs

CommandUse
kubectl get jobsList Jobs
kubectl create job testjob --image=busybox -- echo "Hi"Create a one-off Job
kubectl get cronjobsList CronJobs
kubectl delete job <name>Delete a Job
kubectl delete cronjob <name>Delete a CronJob

Storage

CommandUse
kubectl get pvList PersistentVolumes
kubectl get pvcList PersistentVolumeClaims
kubectl describe pv <name>PersistentVolume details
kubectl describe pvc <name>Claim details and binding status
kubectl delete pvc <name>Delete a claim

Editing resources

CommandUse
kubectl edit deployment nginxEdit a live resource in your editor
kubectl edit svc myserviceEdit a live Service

Live edits are not saved in your YAML files. Use them only for quick tests.

Applying YAML manifests

CommandUse
kubectl apply -f app.yamlCreate or update the resources in a file
kubectl apply -f k8s/Apply every manifest in a folder
kubectl diff -f app.yamlShow what would change before applying
kubectl delete -f app.yamlDelete the resources in a file

Output formatting

CommandUse
kubectl get pods -o wideExtra columns
kubectl get pods -o yamlFull YAML output
kubectl get pods -o jsonFull JSON output
kubectl get pods --sort-by=.metadata.nameSort by a field
kubectl get pods -AAll namespaces

Debugging

CommandUse
kubectl describe <resource> <name>Status and events for any object
kubectl get events --sort-by=.metadata.creationTimestampEvent timeline
kubectl logs <pod> --previousLogs of the last crashed container
kubectl exec -it <pod> -- shShell into a pod that has no bash
kubectl debug node/<node> -it --image=busyboxStart a debug pod on a node

Contexts and kubeconfig

CommandUse
kubectl config get-contextsList contexts
kubectl config use-context <context>Switch cluster
kubectl config current-contextShow the current context
kubectl config viewShow the kubeconfig
kubectl config set-context <context> --namespace=<ns>Change a context's settings

Labels and annotations

CommandUse
kubectl label pod <pod> env=prodAdd a label
kubectl label pod <pod> env-Remove a label
kubectl get pods -l env=prodFilter by label
kubectl annotate pod <pod> description="My pod"Add an annotation

Deleting resources

CommandUse
kubectl delete pod <pod>Delete a pod
kubectl delete deployment <name>Delete a Deployment and its pods
kubectl delete namespace <name>Delete a namespace and everything in it
kubectl delete pods --allDelete all pods in the current namespace

Generate YAML

Generate the first draft of a manifest with kubectl instead of writing it from memory:

terminal
$ kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deployment.yaml
$ kubectl create configmap test --from-literal=a=1 --dry-run=client -o yaml

--dry-run=client builds the object without sending it to the cluster.

Short names

Short nameResource
popods
svcservices
deploydeployments
rsreplicasets
cmconfigmaps
nsnamespaces
pv / pvcpersistentvolumes / persistentvolumeclaims

For example, kubectl get po is the same as kubectl get pods. kubectl api-resources lists every short name.

Key takeaways

  • get, describe, logs and exec solve most day-to-day questions.
  • Use apply -f with files in Git. Keep edit, scale and set image for quick tests.
  • kubectl get events and describe show why a resource is failing, not only that it is failing.
  • --dry-run=client -o yaml is the fastest way to start a new manifest.
  • Contexts let one kubectl switch between minikube, kubeadm and EKS clusters.