Hello and welcome! We have gathered the most commonly used commands in Kubernetes for you, all in one ready-to-use kubectl cheat sheet. In this "kubectl cheat sheet," we will walk you through step-by-step instructions on how to manage your Kubernetes cluster using the kubectl command-line tool.
What is kubectl?
Kubectl is the CLI tool for Kubernetes that allows you to interact with your clusters, create, manage, edit, and delete resources with ease. It communicates with the Kubernetes API server so you can monitor and control your applications.
How does kubectl work?
kubectl working
Kubectl operates by sending commands to the Kubernetes API server, which then carries out the requested operations within the cluster. When you execute a kubectl command, such as deploying an application or retrieving the status of resources, kubectl communicates with the API server over HTTP, transmitting your instructions. The API server processes these requests, updates the cluster's state in etcd (a distributed key-value store), and directs other components, like the scheduler or controller manager, to perform necessary actions to achieve the desired state. This interaction allows you to efficiently manage and monitor your Kubernetes resources from the command line.
In earlier versions of Kubernetes, kubectl utilized generators to create resource configurations based on specific inputs. These generators allowed users to specify the type of resource to create, such as a Pod, Deployment, or Job, by using the --generator flag. For example, kubectl run nginx --image=nginx --generator=run-pod/v1 would create a Pod resource. This approach enabled users to script and automate resource creation with predictable outcomes. However, it's important to note that the --generator flag was deprecated in Kubernetes v1.12 and removed in v1.18. As of v1.18, kubectl run is simplified to create only Pods, and the use of generators has been replaced by more explicit and declarative commands like kubectl create and kubectl apply
Now, let’s start:
Optional Flags
Kubectl provides optional flags that enhance command output and resource management. For example:
Output Formats:
JSON Output:
You can use the -o=json flag to format command output as JSON.
kubectl get pods -o=json
YAML Output:
You can use the -o=yaml flag to format command output as YAML.
kubectl get pods -o=yaml
Wide Output:
The -o=wide option provides detailed plain-text output (e.g., it displays the node name for pods).
kubectl get pods -o=wide
For more tailored outputs, kubectl offers the --custom-columns flag, allowing you to define specific columns to display.
For example, to list pods with their names and the nodes they're running on:
kubectl get pods -o=custom-columns="POD NAME:.metadata.name,NODE:.spec.nodeName"
This command will output:
POD NAME NODE
nginx-deployment-1 node-1
nginx-deployment-2 node-2
Namespace Specification:
By default, kubectl commands operate within the namespace defined by the current context. To target a specific namespace, you can use the -n or --namespace flag:
kubectl get pods -n
To view resources across all namespaces, the -A or --all-namespaces flag is used:
kubectl get pods -A
And to set a default namespace for the current context:
The -f flag is used to specify a filename, directory, or URL when creating a resource.
kubectl create -f ./file_name
Label Filtering:
You can use the -l flag to filter resources based on specific labels.
kubectl get pods -l name=label_name
Help Option:
To display help for kubectl commands, use the -h flag.
kubectl -h
Watch Mode:
You continuously monitor resource changes with the --watch (or -w) flag:
kubectl get pods -w
Sorting Output:
You can sort the output based on a specific field using the --sort-by flag:
kubectl get pods --sort-by=.metadata.name
kubectl Usage with Aliases
When you working with kubectl, it involves typing lengthy commands, which can be time-consuming and error-prone. To improve efficiency, many developers create aliases for frequently used commands.
Setting Up a Basic Alias
alias k=kubectl
echo 'alias k=kubectl' >> ~/.bashrc
This allows you to use k in place of kubectl.
You can use the kubectl-aliases repository, which provides hundreds of pre-defined aliases, enabling quicker command execution.
For Cluster Management, Context and Configuration
Cluster Management means setting up, viewing, and modifying your cluster.
Command
Description
kubectl cluster-info
Displays the endpoints and basic information about your Kubernetes cluster.
kubectl config get-contexts
Lists all contexts defined in your kubeconfig file.
kubectl config current-context
Shows the current active context.
kubectl config use-context <context>
Switches the active context to the one you specify.
kubectl config delete-context <context>
Removes a context from your kubeconfig file.
kubectl api-versions
Lists all the API versions available in the cluster.
kubectl api-resources
Lists all resource types available in your cluster, with details on API groups and namespaces.
kubectl config view
Displays the configuration details from your kubeconfig file (clusters, contexts, user settings).
kubectl version
Prints version info for both the kubectl client and the Kubernetes server.
Pod Management
Shortcode = po
A Pod is the smallest unit in Kubernetes, representing a single instance of a running process in your cluster. It can contain one or more containers that share storage and network resources.
Command
Description
kubectl get pods
Lists all pods in the current namespace.
kubectl describe pod <pod-name>
Provides detailed information about the specified pod, including status, events, and container details.
kubectl exec -it <pod-name> <command>
Executes the given command inside the specified pod interactively (the -it flags allow interactive terminal access).
Executes a command inside a specific container within a pod.
kubectl top pod
Displays resource usage (such as CPU and memory) for pods.
kubectl annotate pod <pod-name> <annotation>
Adds or updates an annotation on the specified pod.
kubectl get pods --field-selector=status.phase=Running
Lists pods that are currently in the Running phase by filtering on the pod status.
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
Sorts the list of pods by the restart count of the first container in each pod.
For ReplicaSets
Shortcode = rs
A ReplicaSet ensures a specified number of pod replicas are running. The main difference is that ReplicaSets support set-based label selectors.
Command
Description
kubectl get rs
Lists all ReplicaSets in the current namespace.
kubectl get rs <replicaset_name> -o yaml
Displays the configuration of the specified ReplicaSet in YAML format.
kubectl get rs <replicaset_name> -o json
Displays the configuration of the specified ReplicaSet in JSON format.
kubectl describe rs <rs_name>
Provides detailed information about the specified ReplicaSet.
kubectl get rs --namespace=<ns_name>
Lists all ReplicaSets in the specified namespace.
kubectl delete rs <rs_name> --cascade=orphan
Deletes the specified ReplicaSet but leaves its managed pods running (orphans the pods).
kubectl delete rs <rs_name>
Deletes the specified ReplicaSet along with all the pods it manages.
kubectl scale rs <rs_name> --replicas=<count>
Adjusts (scales) the number of pods managed by the specified ReplicaSet to the given count.
kubectl edit rs <rs_name>
Opens the ReplicaSet configuration in your default editor, allowing you to make live changes.
kubectl replace -f <rs_file.yaml>
Replaces the existing ReplicaSet configuration with the one provided in the YAML file.
For Daemonsets
Shortcode = ds
A DaemonSet ensures that a specific pod runs on all (or some) nodes in the cluster. This is useful for tasks like running a monitoring agent on every node.
Command
Description
kubectl get daemonset
Lists all DaemonSets in the current namespace.
kubectl describe daemonset <ds_name>
Provides detailed information about a specific DaemonSet.
kubectl get daemonset <daemonset_name> -o yaml
Displays the configuration of the specified DaemonSet in YAML format.
kubectl get daemonset <ds_name> -o json
Displays the configuration of the specified DaemonSet in JSON format.
kubectl delete daemonset <ds_name>
Deletes the specified DaemonSet from the cluster.
kubectl label daemonset <ds_name> <key>=<value>
Adds or updates a label on the specified DaemonSet.
Adds or updates an annotation on the specified DaemonSet.
kubectl rollout status daemonset <ds_name>
Checks the status of a DaemonSet rollout.
kubectl rollout history daemonset <ds_name>
Displays the rollout history for a DaemonSet.
For StatefulSets
Shortcode = sts
StatefulSets manage the deployment and scaling of pods, ensuring each has a unique, stable identity and persistent storage. This is important for stateful applications.
Command
Description
kubectl get statefulset
Lists all StatefulSets in the current namespace.
kubectl describe statefulset <statefulset_name>
Provides detailed information about the specified StatefulSet.
kubectl get statefulset <sts_name> -o yaml
Displays the configuration of the specified StatefulSet in YAML format.
kubectl get statefulset <sts_name> -o json
Displays the configuration of the specified StatefulSet in JSON format.
kubectl edit statefulset <sts_name>
Opens the configuration of the specified StatefulSet in your default text editor, allowing you to make live changes.
Adjusts the number of pod replicas managed by the specified StatefulSet to the provided number.
kubectl rollout status statefulset <statefulset_name>
Checks and displays the rollout status of the specified StatefulSet.
For Namespace
Shortcode = ns
Namespaces provide a way to divide cluster resources between multiple users or applications.
Command
Description
kubectl get namespace
Lists all namespaces in your cluster (the current context’s namespaces).
kubectl create namespace <namespace_name>
Creates a new namespace with the specified name.
kubectl describe namespace <namespace_name>
Provides detailed information about the specified namespace.
kubectl edit namespace <namespace_name>
Opens the configuration for the specified namespace in your default text editor so you can make live edits.
kubectl delete namespace <namespace_name>
Deletes the specified namespace from your cluster along with all resources contained within it.
For Services
Shortcode = svc
A Service defines a logical set of pods and a policy to access them. It enables communication between different parts of your application and can expose your application to external traffic.
Command
Description
kubectl get services
Lists all services in the current namespace
kubectl create service <svc-type> <svc-name> --tcp=<port>
Creates a new service of the specified type (for example, clusterip, nodeport, or loadbalancer) with the given name, mapping TCP on a given port
kubectl edit service <svc-name>
Opens an editor for editing the configuration of specified service
kubectl describe service <svc-name>
Provides detailed information about the specified service
kubectl delete service <svc-name>
Deletes the specified service from the cluster
kubectl get endpoints <svc-name>
Lists the endpoints (i.e., the IP addresses of the pods) that back the specified service
kubectl expose deployment [deploy_name]
Creates a service that exposes the specified deployment and possible resources for expose include pod (po), service (svc), replicaset (rs)
For Service Accounts
Shortcode = sa
A ServiceAccount provides an identity for processes running in a pod, allowing them to interact with the Kubernetes API securely.
Command
Description
kubectl get serviceaccounts
Lists all service accounts in the current namespace
kubectl describe serviceaccount <sa-name>
Displays detailed information for the specified service account
kubectl replace -f <sa.yaml>
Replace (update) a service account with a new configuration
kubectl delete serviceaccount <sa_name>
Deletes the specified service account from the cluster.
For Deployments
Shortcode = deploy
A Deployment provides declarative updates for pods and ReplicaSets. You can define the desired state of your application, and the Deployment controller changes the actual state to the desired state at a controlled rate.
Command
Description
kubectl get deployment
Lists all deployments in the current namespace.
kubectl describe deployment <deployment_name>
Displays detailed information about the specified deployment
Rolls back the deployment to a specific revision number
kubectl delete deployment <deployment-name>
Deletes the specified deployment along with its managed pods
For Events
Shortcode = ev
Events are records of what happened in your cluster, such as pod creations, deletions, or errors.
Command
Description
kubectl get events
Lists all events in the current namespace
kubectl get events --field-selector type!=Normal
Displays events that are not of type Normal
kubectl get events --field-selector type=Warning
Filters and displays only events of type Warning
kubectl get events -w
Watches for new events in real-time
kubectl get events --field-selector involvedObject.name=<object_name>
Shows events related to a specific object such as a pod or node
Logs
Logs are the output of your running applications and system components.
Command
Description
kubectl logs <pod_name>
Retrieves and displays the logs from the main container of the specified pod
kubectl logs -f <pod_name>
Streams the live logs from the specified pod
kubectl logs --since=6h <pod_name>
Displays logs from the specified pod that were generated within the last 6 hours
kubectl logs --tail=50 <pod_name>
Shows the last 50 lines of logs from the specified pod
kubectl logs -f <pod_name> -c <container_name>
Streams live logs from a specific container within a pod
kubectl logs <pod_name> > podfile.log
Saves the logs from a pod to a file named podfile.log
kubectl logs --previous <pod_name>
Displays logs from the previous instance of a container in the specified pod
For Secrets and Config Maps
Shortcode for ConfigMaps = cm
Secrets store sensitive information like passwords or keys, while ConfigMaps hold non-sensitive configuration data. Both allow you to decouple configuration artifacts from image content to keep your applications portable.
Command
Description
kubectl get secrets
Lists all secrets in the current namespace
kubectl get configmaps
Lists all ConfigMaps in the current namespace
kubectl describe secret <secret-name>
Provides detailed information about the specified secret
kubectl delete secret <secret-name>
Deletes the specified secret from the cluster
kubectl describe configmap <configmap-name>
Provides detailed information about the specified ConfigMap
Copies a file or directory from your local machine to the destination pod and namespace.
For Output Verbosity and Debugging
Verbosity refers to the level of detail included in the logs generated by components like kubectl and kubelet. You can adjust the verbosity of kubectl commands.
Command
Description
kubectl get <resource-type> --v=<verbosity-level>
This is the general form for setting the verbosity level. It tells kubectl how much debugging output to include when executing the command on the specified resource type.
kubectl get <resource-type> --v=0
Runs the command with minimal debug output (the default level).
kubectl get <resource-type> --v=7
Provides a higher verbosity level where additional debug information is printed, including HTTPS request headers sent by the client.
kubectl get <resource-type> --v=8
Increases the verbosity further. At level 8, HTTP request and response contents (body data) are also displayed.
Reduce up to 50% of your Cloud Costs with PerfectScale
PerfectScale is a powerful tool designed to work seamlessly with Kubernetes. By integrating PerfectScale into your Kubernetes environment, you can simplify and optimize the orchestration of your containers. This integration enhances the efficiency of managing your clusters, allowing you to allocate resources more effectively, reduce operational costs, and improve the overall stability and resilience of your applications. PerfectScale's AI-driven insights provide comprehensive visibility into your system's performance, enabling proactive adjustments to meet dynamic demands. This means you can ensure peak performance while cutting costs by up to 50% through data-driven, autonomous actions that continuously optimize each layer of your Kubernetes stack. To experience the benefits of PerfectScale firsthand, consider Booking a Demo today and Start a Free Trial today!
kubectl Cheat Sheet
Reduce your cloud bill and improve application performance today
Install in minutes and instantly receive actionable intelligence.
Essential kubectl cheat sheet with top commands & flags to manage Kubernetes clusters—quick reference for pods, deployments, services, namespaces, and more.
By clicking “Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.