Kubectl Command Cheat Sheet
Last Updated :
27 Feb, 2025
If you are inspired to become a DevOps (Devlopment+Operations)’s Engineer and start your journey as a beginner, or if you’re a professional looking to refresh your DevOps knowledge or transition into DevOps, or even if you’re a learning geek seeking to expand your knowledge base, then you landed to the right place. Nowadays, Kubernetes (sometimes shortened to K8s with the 8 standing for the number of letters between the âKâ and the âsâ ) is a trending technology in the field of DevOps, and having a good understanding of it is crucial.
The Kubernetes Cheat Sheet is a comprehensive guide that serves as a quick reference for learning both the basics and advanced commands of Kubernetes. Whether you are a beginner just starting your journey with Kubernetes or an experienced professional with over 5 years of experience, this guide provides all the necessary commands for managing clusters, nodes, namespaces, and more.
.webp)
Pre-requisites: Before moving to the Cheat sheet you should have a basic understanding of What Kubernetes exactly is, what are their uses, and how it helps. and knowledge of EKS, and AKS are additional advantages.
What is Kubernetes?
Kubernetes is an open-source Container Management tool that automates container deployment, container scaling, and container load balancing (also called a container orchestration tool). It is written in Golang and has a vast community because it was first developed by Google and later donated to CNCF (Cloud Native Computing Foundation). Kubernetes can group ânâ number of containers into one logical unit for managing and deploying them easily. It works brilliantly with all cloud vendors i.e. public, hybrid, and on-premises.
Kubectl (CLI): Kubectl is a command line configuration tool (CLI) for kubernetes used to interact with a master node of Kubernetes. Kubectl has a config file called kubeconfig, this file has the information about the server and authentication information to access the API Server.
Kubernetes (Kubectl) CheatSheet
Kubernetes CheatSheet servers as a quick reference guide for some commands and operations which are widely used in the kubernetes cluster. In this cheat sheet, Cluster management, node management, namespace management, resource creation, resource viewing and finding, resource deletion, file and directory copying, resource patching, resource scaling, pod management, deployment management, ReplicaSets management, service management, config maps and secrets, networking, storage, stateful sets, monitoring, troubleshooting, and other operations will be covered.
Kubernetes Master Node Components Important Terminologies
- API Server
- Scheduler
- Controller-Manager
- etcd
API Server
Kube API Server interacts with API, its a frontend of the kubernetes control plane. Communication center for developers and other kubernetes components.Receiving queries from multiple clients, including the kubectl command-line tool, the API server serves as the front-end interface for the Kubernetes control plane, coordinating cluster-wide operations.
Scheduler
The scheduler watches the pods and assigns the pods to run on specific hosts. A new pod does not have a specific node assigned when it is formed, whether by a user, a deployment controller, or a replication controller. The scheduler chooses a suitable node for the pod to run on after assessing the resource needs of the pod, such as CPU and memory usage, as well as any restrictions or affinity/anti-affinity rules supplied.
Controller-Manager
The controller manager runs the controllers in the background which run different tasks in the kubernetes cluster. Performs cluster-level functions(Replication, Tracking worker nodes, Handling failures).
etcd
etcd is a simple distributed key-value store. kubernetes uses etcd as its database to store all cluster data. Some of the data stored in etcd is job scheduling information, pods, state information and etc.
Kubernetes Worker Node Components Important Terminologies
Worker nodes are the node where the application actually runs in a kubernetes cluster, it is also known as a minion. These worker nodes are controlled by the master node using Kublet processes.
- kubelet
- kube-Proxy
- Container runtime
Kubelet
The main node agent, known as Kubelet, operates on each node and reads the container manifests to make sure that the containers are active and in good condition. It ensures that pods of containers are operating. Containers that weren’t made by Kubernetes are not managed by the kubelet.
Kube-Proxy
By managing network rules on the host and managing connections, kube-proxy supports the kubernetes service abstraction. On nodes, Kube-proxy keeps track of network rules. Network connectivity to your pod is permitted by these network rules from both inside and outside of your cluster. Having a network proxy and load balancer for the service on a single worker node is beneficial to us.
Container Runtime
To process commands from the master server to run containers, each node needs a container runtime, such as Docker, containerd, or another container runtime.
Kubernetes (Kubectl) Commands
For Cluster Management
Command
|
Description
|
kubectl cluster-infokubectl |
Get cluster information. |
kubectl get nodes |
Views all the nodes present in the cluster. |
For Node Management
Command
|
Description
|
kubectl get nodes |
List all nodes in the cluster. |
kubectl describe node <node-name> |
Describe a specific node. |
kubectl drain <node-name> |
Drain a node for maintenance. |
kubectl uncordon <node-name> |
Uncordon a node after maintenance. |
kubectl label node <node_name> <key>=<value> |
You can label the node by using key-value pair. |
kubectl label node <node_name> <label_key>- |
You can remove the label which is already attached to the node. |
For Namespace Management
Command
|
Description
|
kubectl describe namespace <namespace-name> |
Describe a namespace. |
kubectl create namespace <namespace-name> |
Create a namespace. |
kubectl get namespaces |
List all namespaces. |
kubectl config set-context –current –namespace=<namespace-name> |
Switch to a different namespace. |
kubectl delete namespace <namespace-name> |
Delete a namespace. |
kubectl edit namespace <namespace_name> |
Edit and update the namespace definition. |
For Creating Resources
Command
|
Definition
|
kubectl apply -f <resource-definition.yaml> |
Create or Update a resource from a YAML file. |
kubectl create |
Create an object imperatively. |
kubectl apply -f https://url-to-resource-definition.yaml |
Create a resource by using the URL. |
For Viewing and Finding Resources
Command
|
Description
|
kubectl get <resource-type> |
List all resources of a specific type. |
kubectl get <resource-type> -o wide |
List all resources with additional details. |
kubectl describe <resource-type> <resource-name> |
Describe a specific resource. |
kubectl get <resource-type> -l <label-key>=<label-value> |
List all resources with a specific label. |
kubectl get <resource-type> –all-namespaces |
List all resources in all namespaces. |
kubectl get <resource-type> –sort-by=<field> |
List all resources sorted by a specific field. |
kubectl get <resource-type> -l <label-selector> |
List resources with a specific label selector. |
kubectl get <resource-type> –field-selector=<field-selector> |
List resources with a specific field selector. |
kubectl get <resource-type> -n <namespace> |
List all resources in a specific namespace. |
For Deleting Resources
Command
|
Description
|
kubectl delete <resource-type> <resource-name> |
Delete a resource. |
kubectl delete <resource-type1> <resource-name1> <resource-type2> <resource-name2> |
Delete multiple resources. |
kubectl delete <resource-type> –all |
Delete all resources of a specific type. |
kubectl delete -f <resource-definition.yaml>kubectl delete -f https://url-to-resource-definition.yaml |
Delete the resource by using url. |
kubectl delete <resource-type> –all -n <namespace> |
Delete all resources in a specific namespace. |
For Copying Files and Directories
Command
|
Description
|
kubectl cp <local-path> <namespace>/<pod-name>:<container-path> |
Copy files and directories to a container. |
kubectl cp <namespace>/<pod-name>:<container-path> <local-path> |
Copy files and directories from a container. |
kubectl cp <namespace>/<pod-name>:<source-container-path> <destination-namespace>/<destination-pod-name>:<destination-container-path> |
Copying files from one container to another within the same pod. |
kubectl cp <namespace>/<source-pod-name>:<source-container-path> <destination-namespace>/<destination-pod-name>:<destination-container-path> |
Copying files from one container to another in a different pod. |
For Patching Resources
Command
|
Description
|
kubectl patch <resource-type> <resource-name> -p ‘<patch-document> |
Patch a resource using a JSON or YAML document. |
kubectl patch <resource-type> <resource-name> –patch-file=<patch-file> |
Patch a resource using a JSON or YAML file. |
For Scaling Resources
Command
|
Description
|
kubectl scale deployment <deployment-name> –replicas=<replica-count> |
Scale a deployment. |
kubectl scale statefulset <statefulset-name> –replicas=<replica-count> |
Scale a statefulset. |
kubectl scale replicaset <replicaset-name> –replicas=<replica-count> |
Scale a replica set. |
For Pod Management
Command
|
Description
|
kubectl create -f <pod-definition.yaml> |
Create a pod from a YAML file. |
kubectl get pods |
List all pods in the cluster. |
kubectl describe pod <pod-name> |
Describe a specific pod. |
kubectl logs <pod-name> |
Get logs from a pod. |
kubectl logs -f <pod-name> |
Stream logs from a pod. |
kubectl logs -l <label-key>=<label-value> |
Get logs with a specific label. |
kubectl exec -it <pod-name> — <command> |
Exec into a pod. |
kubectl delete pod <pod-name> |
Delete a pod. |
kubectl create pod <pod-name> |
Create a pod with the name. |
kubectl get pod -n <namespace_name> |
List all pods in a namespace. |
For Deployment Management
Command
|
Description
|
kubectl create deployment <deployment-name> –image=<image-name> |
Create a deployment. |
kubectl get deployments |
List all deployments. |
kubectl describe deployment <deployment-name> |
Describe a specific deployment. |
kubectl scale deployment <deployment-name> –replicas=<replica-count> |
Scale a deployment. |
kubectl set image deployment/<deployment-name> <container-name>=<new-image-name> |
Update a deployment’s image. |
kubectl rollout status deployment/<deployment-name> |
Rollout status of a deployment. |
kubectl rollout pause deployment/<deployment-name> |
Pause a deployment rollout. |
kubectl rollout resume deployment/<deployment-name> |
Resume a deployment rollout. |
kubectl rollout undo deployment/<deployment-name> |
Rollback a deployment to the previous revision. |
kubectl rollout undo deployment/<deployment-name> –to-revision=<revision-number> |
Rollback a deployment to a specific revision. |
kubectl delete deployment <deployment-name> |
Delete deployment name. |
For ReplicaSets Management
Command
|
Description
|
kubectl create -f <replicaset-definition.yaml> |
Create a ReplicaSet. |
kubectl get replicasets |
List all ReplicaSets. |
kubectl describe replicaset <replicaset-name> |
Describe a specific ReplicaSet. |
kubectl scale replicaset <replicaset-name> –replicas=<replica-count> |
Scale a ReplicaSet. |
For Service Management
Command
|
Description
|
kubectl create service <service-type> <service-name> –tcp=<port> |
Create a service. |
kubectl get services |
List all services. |
kubectl expose deployment <deployment-name> –port=<port> |
Expose a deployment as a service. |
kubectl describe service <service-name> |
Describe a specific service. |
kubectl delete service <service-name> |
Delete a service. |
kubectl get endpoints <service-name> |
Get information about a service. |
For Config Maps and Secrets
Command
|
Description
|
kubectl create configmap <config-map-name> –from-file=<path-to-file> |
Create a config map from a file. |
kubectl create secret <secret-type> <secret-name> –from-literal=<key>=<value> |
Create a secret. |
kubectl get configmaps |
List all config maps. |
kubectl get secrets |
List all secrets. |
kubectl describe configmap <config-map-name> |
Describe a specific config map. |
kubectl describe secret <secret-name> |
Describe a specific secret. |
kubectl delete secret <secret_name> |
Delete a specific secret. |
kubectl delete configmap <config-map-name> |
Delete a specific config map. |
For Networking
Command
|
Description
|
kubectl port-forward <pod-name> <local-port>:<pod-port> |
Port forward to a pod. |
kubectl expose deployment <deployment-name> –type=NodePort –port=<port> |
Expose a deployment as a NodePort service. |
kubectl create ingress <ingress-name> –rule=<host>/<path>=<service-name> –<service-port> |
Create an Ingress resource. |
kubectl describe ingress <ingress-name> |
Get information about an Ingress. |
kubectl get ingress <ingress-name> -o jsonpath='{.spec.rules[0].host}’ |
Retrieves the most value from the first rule of the specified Ingress resource. |
For Storage
Command
|
Description
|
kubectl create -f <persistent-volume-definition.yaml> |
Create a PersistentVolume. |
kubectl get pv |
List all PersistentVolumes. |
kubectl describe pv <pv-name> |
Describe a specific PersistentVolume. |
kubectl create -f <persistent-volume-claim-definition.yaml> |
Create a PersistentVolumeClaim. |
kubectl get pvc |
List all PersistentVolumeClaims. |
kubectl describe pvc <pvc-name> |
Describe a specific PersistentVolumeClaim. |
For StatefulSets
Command
|
Description
|
kubectl create -f <statefulset-definition.yaml> |
Create a StatefulSet. |
kubectl get statefulsets |
List all StatefulSets. |
kubectl describe statefulset <statefulset-name> |
Describe a specific StatefulSet. |
kubectl scale statefulset <statefulset-name> –replicas=<replica-count> |
Scale a StatefulSet. |
For Monitoring and Troubleshooting
Command
|
Description
|
kubectl get events |
Check cluster events. |
kubectl get component statuses |
Get cluster component statuses. |
kubectl top nodes |
Get resource utilization of nodes. |
kubectl top pods |
Get resource utilization of pods. |
kubectl debug <pod-name> -it –image=<debugging-image> |
Enable container shell access debugging. |
Miscellaneous
Command
|
Description
|
kubectl delete <resource-type> <resource-name> |
Delete a resource. |
kubectl describe <resource-type> <resource-name> |
Get detailed information about a resource. |
kubectl proxy |
Access the Kubernetes dashboard. |
kubectl completion <shell-type> |
Install kubectl completion. |
Kubectl Output Verbosity and Debugging
The verbosity of kubernetes can be controlled by using a command which is kubectl verbosity. We can add no flags according to our requirements.
Command
|
Command
|
kubectl get <resource-type> –v=<verbosity-level |
By using this command you set the level of verbosity output.
|
kubectl get <resource-type> –v=0 |
Used to be visible to a cluster operator. |
kubectl get <resource-type> –v=3 |
You can more information like extended information about changes.
|
kubectl get <resource-type> –v=7 |
Displays the HTTPS request headers. |
kubectl get <resource-type> –v=8 |
Display HTTP request contents. |
Conclusion
The Kubernetes Cheatsheet will help to have a quick reference of the most commonly used in Kubernetes (kuberctl commands). Kubernetes is one of the powerful container orchestration platforms by which you can manage applications in the form of containers. The commands which are mentioned in the Cheat Sheet provide a quick reference guide for both beginners and experienced before attending any interviews also.
Similar Reads
Kubernetes - Kubectl Commands
Pre-requisites: Kubernetes The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. Some basic Kubectl commands in a Kubernetes cluster are as follows: kubectl C
5 min read
Terraform Cheat Sheet | Commands and Examples
Terraform is an extremely effective solution for cloud resource deployment, administration, and provisioning in the quickly expanding field of infrastructure as code (IaC). Clients may describe their infrastructure in code thanks to Terraform's declarative configuration language and rich ecosystem,
11 min read
Git Cheat Sheet
Git Cheat Sheet is a comprehensive quick guide for learning Git concepts, from very basic to advanced levels. By this Git Cheat Sheet, our aim is to provide a handy reference tool for both beginners and experienced developers/DevOps engineers. This Git Cheat Sheet not only makes it easier for newcom
10 min read
Docker Cheat Sheet : Complete Guide (2024)
Docker is a very popular tool introduced to make it easier for developers to create, deploy, and run applications using containers. A container is a utility provided by Docker to package and run an application in a loosely isolated environment. Containers are lightweight and contain everything neede
11 min read
Kubernetes - Kubectl Delete
Kubernetes is an open-source Container Management tool that automates container deployment, container scaling, descaling, and container load balancing (also called a container orchestration tool). It is written in Golang and has a huge community because it was first developed by Google and later don
8 min read
Kubernetes - Dashboard Setup
Kubernetes - Dashboard Setup is a web-based user interface that offers a summary of your Kubernetes cluster. You may manage your resources using a graphical interface and view information about your pods, deployments, services, and more with the dashboard. How do you maintain track of all the contai
15 min read
How To kubectl Exec Into Pod?
To run a command within a container that is already executing inside a pod, use the command "kubectl exec into pod." This feature helps with debugging, troubleshooting, and administrative chores in the containerized environment. What Is Kubectl?A command-line interface for working with Kubernetes cl
5 min read
Kubernetes - Kubectl Create and Kubectl Apply
Kubernetes is an open-source Container Orchestrating software platform that is widely used in modern application deployment and management. It comes up with many internal resources to manage the other working nodes Organizing as a Cluster facilitates seamless automatic scaling, and deployment update
8 min read
How to Change Namespace in Kubernetes ?
Kubernetes namespaces offer an indispensable instrument for resource organization and isolation in the ever-changing realm of container orchestration. Like distinct districts in a busy metropolis, each namespace in your Kubernetes cluster serves as a dedicated place where you may organize related st
4 min read
Kubernetes - Kubectl
Kubectl is a command-line software tool that is used to run commands in command-line mode against the Kubernetes Cluster. Kubectl runs the commands in the command line, Behind the scenes it authenticates with the master node of Kubernetes Cluster with API calls and performs the operations on Kuberne
12 min read