Skip to main content

Kubectl Cheatsheet

Searchable reference of 72 essential kubectl commands. Find what you need fast.

72 commands found

Cluster Info

Show cluster info

kubectl cluster-info

List all nodes

kubectl get nodes

Show node details

kubectl describe node <node-name>

Get cluster component statuses

kubectl get componentstatuses

View cluster API resources

kubectl api-resources

Pods

List pods in current namespace

kubectl get pods

List pods in all namespaces

kubectl get pods --all-namespaces

List pods with wide output

kubectl get pods -o wide

Describe a pod

kubectl describe pod <pod-name>

Delete a pod

kubectl delete pod <pod-name>

Get pod YAML definition

kubectl get pod <pod-name> -o yaml

Run a temporary debug pod

kubectl run debug --image=busybox --rm -it -- sh

Execute command in a pod

kubectl exec -it <pod-name> -- /bin/sh

Watch pod status changes

kubectl get pods -w

Deployments

List deployments

kubectl get deployments

Create deployment from image

kubectl create deployment <name> --image=<image>

Delete a deployment

kubectl delete deployment <name>

Edit a deployment

kubectl edit deployment <name>

Set deployment image

kubectl set image deployment/<name> <container>=<image>

Describe a deployment

kubectl describe deployment <name>

Services

List services

kubectl get services

Expose deployment as service

kubectl expose deployment <name> --port=80 --type=LoadBalancer

Delete a service

kubectl delete service <name>

Get service endpoints

kubectl get endpoints <service-name>

Describe a service

kubectl describe service <name>

ConfigMaps & Secrets

List configmaps

kubectl get configmaps

Create configmap from file

kubectl create configmap <name> --from-file=<path>

Create configmap from literal

kubectl create configmap <name> --from-literal=key=value

List secrets

kubectl get secrets

Create secret from literal

kubectl create secret generic <name> --from-literal=key=value

Decode a secret value

kubectl get secret <name> -o jsonpath='{.data.key}' | base64 -d

Namespaces

List namespaces

kubectl get namespaces

Create a namespace

kubectl create namespace <name>

Delete a namespace

kubectl delete namespace <name>

Set default namespace

kubectl config set-context --current --namespace=<name>

Get resources in a namespace

kubectl get all -n <namespace>

Logs & Debugging

View pod logs

kubectl logs <pod-name>

Stream pod logs

kubectl logs -f <pod-name>

View previous container logs

kubectl logs <pod-name> --previous

View logs for specific container

kubectl logs <pod-name> -c <container-name>

Get events in namespace

kubectl get events --sort-by=.metadata.creationTimestamp

Top pods by resource usage

kubectl top pods

Top nodes by resource usage

kubectl top nodes

Port forward to a pod

kubectl port-forward <pod-name> 8080:80

Scaling

Scale deployment replicas

kubectl scale deployment <name> --replicas=3

Autoscale deployment

kubectl autoscale deployment <name> --min=2 --max=10 --cpu-percent=80

Get horizontal pod autoscalers

kubectl get hpa

Delete horizontal pod autoscaler

kubectl delete hpa <name>

Rollouts

Check rollout status

kubectl rollout status deployment/<name>

View rollout history

kubectl rollout history deployment/<name>

Undo last rollout

kubectl rollout undo deployment/<name>

Rollback to specific revision

kubectl rollout undo deployment/<name> --to-revision=2

Pause a rollout

kubectl rollout pause deployment/<name>

Resume a rollout

kubectl rollout resume deployment/<name>

Restart deployment (rolling)

kubectl rollout restart deployment/<name>

Networking

List ingresses

kubectl get ingress

Describe an ingress

kubectl describe ingress <name>

List network policies

kubectl get networkpolicies

Test DNS resolution from pod

kubectl exec -it <pod> -- nslookup <service>

Get all endpoints

kubectl get endpoints

RBAC

List cluster roles

kubectl get clusterroles

List role bindings

kubectl get rolebindings

List cluster role bindings

kubectl get clusterrolebindings

Check API access

kubectl auth can-i create pods

Check access as another user

kubectl auth can-i get pods --as=system:serviceaccount:ns:sa

Create service account

kubectl create serviceaccount <name>

Nodes

Cordon a node (prevent scheduling)

kubectl cordon <node-name>

Uncordon a node

kubectl uncordon <node-name>

Drain a node for maintenance

kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data

Taint a node

kubectl taint nodes <node> key=value:NoSchedule

Remove taint from a node

kubectl taint nodes <node> key=value:NoSchedule-

Label a node

kubectl label nodes <node-name> disktype=ssd

What is kubectl?

kubectl is the official command-line interface (CLI) for interacting with Kubernetes clusters. It communicates with the Kubernetes API server to create, inspect, update, and delete resources like pods, deployments, services, and config maps. Whether you are managing a local minikube cluster or a production EKS/GKE/AKS environment, kubectl is the primary tool for day-to-day Kubernetes operations and is essential knowledge for any DevOps engineer or platform team member.

This kubectl cheatsheet organizes the most frequently used commands by workflow: viewing cluster state, deploying applications, debugging issues, managing configurations, and handling networking. DevOps engineers use kubectl dozens of times daily for checking pod health, tailing logs, rolling out deployments, scaling services, executing into containers for debugging, and applying infrastructure changes. Having these commands readily accessible saves significant time during incident response and routine operations.

Frequently Asked Questions

How to delete a pod in Kubernetes?

Use kubectl delete pod pod-name -n namespace to delete a specific pod. If the pod is managed by a deployment (most common case), Kubernetes will automatically create a replacement pod. To force-delete a stuck pod, add --grace-period=0 --force. To delete all pods in a namespace, use kubectl delete pods --all -n namespace, but be careful—this affects all running workloads.

How to get logs from a pod?

Use kubectl logs pod-name to view logs from the main container. Add -f for real-time streaming (like tail -f), --previous to see logs from the last crashed container, -c container-name for multi-container pods, and --since=1h to filter by time. For aggregating logs across multiple pods, use kubectl logs -l app=myapp to query by label selector.

How to scale a deployment with kubectl?

Use kubectl scale deployment deployment-name --replicas=5 to change the number of pod replicas. You can also use kubectl autoscale deployment deployment-name --min=2 --max=10 --cpu-percent=80 to create a Horizontal Pod Autoscaler. To check current scale, use kubectl get deployment deployment-name and look at the READY column showing current/desired replicas.