The Kubectl Cheat Sheet is your ultimate reference for mastering Kubernetes commands. Whether you’re deploying containers, managing pods, or troubleshooting clusters, kubectl is your primary interface with Kubernetes. Learning these commands helps you operate efficiently and automate Kubernetes workflows without depending heavily on graphical dashboards.
In this guide, you’ll find categorized kubectl commands with examples, explanations, and practical tips — everything you need to become proficient in Kubernetes administration.
Table of Contents
What Is Kubectl?
kubectl (pronounced cube-control) is the command-line tool that lets you communicate with your Kubernetes cluster’s API server. It allows you to create, inspect, update, and delete resources such as pods, deployments, services, namespaces, and more.
With kubectl, you can:
- Deploy and manage containerized applications.
- Inspect the current state of Kubernetes objects.
- Troubleshoot and debug issues in your cluster.
- Automate repetitive administrative tasks.
Kubectl Configuration Basics
Before diving into commands, ensure your kubectl is configured properly.
1. Set Your Cluster Context
kubectl config set-context my-cluster --cluster=cluster-name --user=user-name
Switch between contexts:
kubectl config use-context my-cluster
2. View Current Context
kubectl config current-context
3. List All Contexts
kubectl config get-contexts
Proper configuration ensures you execute commands against the right Kubernetes cluster — especially when managing multiple environments.
Kubectl Cheat Sheet: Cluster Information Commands
Cluster-level visibility is essential for DevOps and SRE professionals. Use these commands to get insights into your cluster’s state.
1. Check Cluster Info
kubectl cluster-info
2. Get All Nodes
kubectl get nodes
3. Describe a Node
kubectl describe node node-name
4. View Cluster Events
kubectl get events --sort-by=.metadata.creationTimestamp
These commands help ensure your cluster and nodes are healthy and running as expected.
Kubectl Namespace Commands
Namespaces let you organize Kubernetes resources logically.
1. List Namespaces
kubectl get namespaces
2. Create a Namespace
kubectl create namespace dev
3. Delete a Namespace
kubectl delete namespace dev
4. Set Default Namespace
kubectl config set-context --current --namespace=dev
Namespaces are crucial for environment isolation — like separating dev, staging, and prod workloads.
Kubectl Pod Management Commands
Pods are the smallest deployable units in Kubernetes. Managing them efficiently is core to mastering this Kubectl Cheat Sheet.
1. List All Pods
kubectl get pods
2. Describe a Pod
kubectl describe pod pod-name
3. Delete a Pod
kubectl delete pod pod-name
4. Execute a Command Inside a Pod
kubectl exec -it pod-name -- /bin/bash
5. View Pod Logs
kubectl logs pod-name
These commands allow you to inspect running pods, debug containers, and manage workloads interactively.
Kubectl Deployment Commands
Deployments manage ReplicaSets and rolling updates for your applications.
1. List Deployments
kubectl get deployments
2. Create a Deployment
kubectl create deployment nginx --image=nginx
3. Scale a Deployment
kubectl scale deployment nginx --replicas=5
4. Update a Deployment
kubectl set image deployment/nginx nginx=nginx:1.25
5. Rollback a Deployment
kubectl rollout undo deployment/nginx
6. Check Deployment Status
kubectl rollout status deployment/nginx
With these commands, you can manage rolling updates and maintain high application uptime.
Kubectl Service and Networking Commands
Kubernetes services expose pods to external traffic or internal communication.
1. List Services
kubectl get services
2. Create a Service
kubectl expose deployment nginx --port=80 --type=NodePort
3. Describe a Service
kubectl describe service nginx
4. Delete a Service
kubectl delete service nginx
Networking is essential to ensure your applications are reachable inside and outside the cluster.
Kubectl ReplicaSet and StatefulSet Commands
ReplicaSets and StatefulSets ensure high availability of your workloads.
1. Get ReplicaSets
kubectl get rs
2. Describe ReplicaSet
kubectl describe rs rs-name
3. Get StatefulSets
kubectl get statefulsets
4. Scale StatefulSet
kubectl scale statefulset mysql --replicas=3
These commands help maintain consistency and reliability in stateful applications.
Kubectl ConfigMap and Secret Commands
Use ConfigMaps and Secrets to manage configuration data securely.
1. Create a ConfigMap
kubectl create configmap app-config --from-literal=env=production
2. Get ConfigMaps
kubectl get configmaps
3. Create a Secret
kubectl create secret generic db-secret --from-literal=password=MyPassword123
4. Get Secrets
kubectl get secrets
Using Secrets helps avoid embedding sensitive data directly in deployment manifests.
Kubectl Logs and Debugging Commands
Troubleshooting is a vital skill. Use these debugging commands to find issues quickly.
1. View Pod Logs
kubectl logs pod-name
2. Stream Live Logs
kubectl logs -f pod-name
3. Get Logs from a Specific Container
kubectl logs pod-name -c container-name
4. Debug a Failing Pod
kubectl debug pod-name --image=busybox
These commands give you real-time insights into pod and container behavior.
Kubectl Resource Management Commands
Efficient resource management ensures optimal performance and cost control.
1. Get Resource Usage
kubectl top nodes
kubectl top pods
2. Describe Resource Limits
kubectl describe pod pod-name | grep -A5 "Limits"
3. Apply Configuration Files
kubectl apply -f deployment.yaml
4. Delete Configuration
kubectl delete -f deployment.yaml
These commands integrate well into CI/CD pipelines for automated resource adjustments.
Kubectl Label, Annotation, and Selector Commands
Labels and annotations are key to organizing Kubernetes resources.
1. Add a Label
kubectl label pods pod-name env=production
2. View Labels
kubectl get pods --show-labels
3. Filter by Label
kubectl get pods -l env=production
4. Add an Annotation
kubectl annotate pod pod-name owner=devops
Organizing resources with labels makes scaling, filtering, and automation seamless.
Kubectl Role-Based Access Control (RBAC) Commands
Control who can access what within your cluster using RBAC.
1. Get Roles
kubectl get roles
2. Describe Role
kubectl describe role role-name
3. Create Role Binding
kubectl create rolebinding dev-binding --role=developer --user=john --namespace=dev
4. Delete Role Binding
kubectl delete rolebinding dev-binding
Proper RBAC management ensures security and controlled access across environments.
Kubectl Backup and Restore Commands
Automate Kubernetes backups using kubectl and YAML exports.
1. Backup a Deployment
kubectl get deployment nginx -o yaml > nginx-backup.yaml
2. Restore Deployment
kubectl apply -f nginx-backup.yaml
3. Export All Resources
kubectl get all --all-namespaces -o yaml > cluster-backup.yaml
Regular backups protect your environment from accidental data loss or misconfiguration.
Kubectl Auto-Completion and Aliases
Simplify your workflow with command completion and aliases.
1. Enable Autocomplete
source <(kubectl completion bash)
2. Create Alias
alias k=kubectl
Now you can run:
k get pods
These small tweaks can save significant time during daily cluster management.
Troubleshooting Common Kubectl Errors
If kubectl isn’t working as expected, check these common issues:
- Connection Refused → Verify
kubeconfigand cluster endpoint. - Unauthorized Access → Ensure correct user and token configuration.
- Timeouts → Check API server availability.
- YAML Syntax Errors → Validate YAML files using
kubectl apply --dry-run=client -f file.yaml.
Proactive troubleshooting ensures smoother Kubernetes operations.
Best Practices for Using Kubectl
- Always verify the current context before running destructive commands.
- Use namespaces for better resource isolation.
- Apply YAML files through GitOps for traceability.
- Keep
kubectlupdated to match cluster versions. - Use descriptive labels and annotations for resource organization.
Following these best practices ensures secure and efficient cluster management.
Conclusion
This Kubectl Cheat Sheet serves as a complete guide for developers, DevOps engineers, and Kubernetes administrators. From managing pods to debugging nodes, these commands empower you to interact efficiently with your cluster.
Mastering kubectl commands not only simplifies Kubernetes management but also improves automation and operational stability. Keep this cheat sheet bookmarked and refer to it whenever you need quick command-line guidance.
FAQs About Kubectl Cheat Sheet
1. What is the use of kubectl in Kubernetes?
kubectl is the command-line tool used to manage and interact with Kubernetes clusters. It allows users to deploy applications, monitor cluster resources, and troubleshoot issues.
2. How can I get all pods using kubectl?
You can list all pods in the current namespace with:kubectl get podsTo include all namespaces:kubectl get pods --all-namespaces
3. How do I check cluster info using kubectl?
Run:kubectl cluster-infoIt displays the API server and other core service endpoints.
4. How do I debug a pod in Kubernetes?
Use:kubectl debug pod-name --image=busyboxThis creates an ephemeral debug container to inspect and troubleshoot the pod.
5. Can kubectl be used for backups?
Yes. You can export resources to YAML using:kubectl get all -o yaml > backup.yamland restore them later with:kubectl apply -f backup.yaml