The Wayback Machine - https://web.archive.org/web/20240828080605/https://www.geeksforgeeks.org/kubectl-cheatsheet/
Open In App

Kubectl Command Cheat Sheet

Last Updated : 28 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Kubernetes Cheatsheet

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

  1. API Server
  2. Scheduler
  3. Controller-Manager
  4. 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.

  1. kubelet
  2. kube-Proxy
  3. 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.

FAQs on Kubernetes (Kubectl) Cheat Sheet

Q1: How do people use Kubernetes?

Answer:

Kubernetes are used for automates container deployment, container scaling, and container load balancing (also called a container orchestration tool). According to a 2020 CNCF survey, 91% of people are using Kubernetes, with 83% using it in production.

Q2: What are the benefits of using a Kubernetes cheat sheet?

Answer:

Kubernetes cheat sheet is a valuable resource for anyone working with Kubernetes, as it helps streamline tasks, improve productivity, and enhance collaboration. Here are some Benefits of using Kubernetes Cheat sheets –

  1. Quick reference
  2. Time-saving
  3. Improved productivity
  4. Learning aid
  5. Troubleshooting assistance
  6. Portability

Q3: What is a real-life example of Kubernetes?

Answer:

Airbnb utilizes Kubernetes to manage its diverse set of microservices and applications, allowing for seamless deployment, auto-scaling, and resilience of their infrastructure. Kubernetes helps Airbnb handle the dynamic demands of their platform, ensuring reliable service availability and efficient resource utilization.

Q4: Can I Use Docker Without Kubernetes?

Answer:

yes, Docker can function without Kubernetes. we see, Docker is a standalone software designed to run containerized applications. Since container creation is part of Docker, you don’t need any separate software for Docker to execute.



Next Article

Similar Reads

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 updates making applications highly available and fault t
8 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 needed to run an application, such as libraries and oth
11 min read
Ansible Cheat Sheet
Ansible is a powerful open-source automation tool that is meant for configuration management and application deployment. It works with agentless architecture and a push-based mechanism. It uses YAML syntax for orchestrating the infrastructure efficiently across different environments. It will be effective and preferable in managing both small-scale
10 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, which promotes automation, consistency, and scalab
11 min read
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 Commands The most popular kubectl commands and flag
5 min read
Deploying NGINX Image To AKS Using Azure CLI and Advanced Kubectl Commands
Pre-requisite: AKS Features Azure Kubernetes Service (AKS) is a managed Kubernetes service that enables you to quickly deploy and manage clusters. AKS makes it easy to deploy and manage containerized applications without having to worry about the underlying infrastructure. It is a fully managed service, meaning that Microsoft handles the maintenanc
3 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 Kubernetes Resources such as Creation, Deletion, Modifica
12 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 clusters is called Kubectl. It lets users to do a lo
5 min read
Kubectl Rollout Restart for Statefulset - Kubernetes
Stateful applications are managed via Kubernetes StatefulSets, a particular workload API object. They offer guarantees concerning the ordering and uniqueness of pods, in contrast to deployments. Applications like databases and distributed systems, which need trustworthy, unique network IDs and long-term storage, rely upon stateful sets. They guaran
9 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 donated to CNCF (Cloud Native Computing Foundation).
8 min read
Terraform Plan Command
When we talk about an open-source infrastructure as a code (IaC) tool Terraform automatically comes into the picture. Terraform allows declarative definition and provisioning of infrastructure resources. The Terraform command-line interface (CLI) allows you to establish and maintain your infrastructure across several cloud providers after defining
4 min read
Terraform Validate Command
When we talk about an open-source infrastructure as a code (IaC) tool Terraform automatically comes into the picture. Terraform allows declarative definition and provisioning of infrastructure resources. The Terraform command-line interface (CLI) allows you to establish and maintain your infrastructure across several cloud providers after defining
3 min read
How To Reset Jenkins Security Settings From The Command Line ?
This article guides you through in resetting Jenkins security settings from the command line. It is crucial to understand the associated risks and prioritize alternative solutions whenever possible. We'll dive into the completely disabling security (a highly discouraged option. Remember, security is essential so perform the configurations carefully
5 min read
What Is Docker rm Command ?
"docker rm" is used to remove one or more Docker containers that are running on the Docker host. A Docker container uses up system resources like CPU, memory, and disk space when it is running. To free up such resources, you may choose to uninstall a container after using it for a while. docker rm [OPTIONS] CONTAINER [CONTAINER...] OPTIONS: Various
3 min read
How to Install ""ifconfig"" Command in My Ubuntu Docker Image?
Docker images are templates that include all the code, libraries, dependencies, and runtime required to run an application. They guarantee consistency and portability across several systems, which makes them indispensable in containerized contexts. Docker images make it simple to scale, deploy, and manage applications, which promotes quicker develo
6 min read
How to Run curl Command From Within a Kubernetes Pod?
Ever needed to fetch data or test connectivity from within a Kubernetes pod? The curl command becomes your trusty companion in these situations. This guide explores different approaches to execute curl commands directly from within your Kubernetes pod, empowering you to diagnose issues and interact with external services effectively. What are Kuber
3 min read
What Is AWS CLI (Command Line Interface) ? Complete Guide
AWS CLI is a command line tool that is used for managing the AWS Services from the command line. On downloading the AWS CLI software and configuring it with your AWS credentials you can control the AWS services from the command line and can automate the work through scripts. Whether you are launching EC2 instances, attaching the EBS Volumes, or man
13 min read
How to Fix the "Docker Compose Command Not Found" Error
To determine and operate multi-container Docker applications, you'll need Docker Compose. The "Docker Compose command not found" error is a frequent problem that many engineers encounter, nevertheless. This problem usually arises from an incorrect setup or non-existence of the Docker Compose binary in the PATH of the system. We'll look at how to re
5 min read
Copy Multiple Directories with One Command
System administration and development must have their files and directories well administered. One such way of doing this that is really effective is the task of copying multiple directories together, which actually saves one from consuming a huge amount of time and decreases the possibility of errors that could be associated with doing them one by
7 min read
How to See RUN Command Output in Docker BuildKit
Docker build is one of the most crucial parts of the operation of containerizing applications. Docker's traditional build system has served developers well, but with projects growing in complexity, the need for better builds that are more efficient and flexible was well apparent. Enter BuildKit, the next-generation build architecture for Docker, ai
6 min read
System Design Cheat Sheet
Welcome to the System Design Cheat Sheet. Join us on a journey to master the principles and techniques of designing complex systems and get ready to dive into the world of designing scalable, efficient, and robust systems
3 min read
Complete DevOps Roadmap - Beginner to Advanced
DevOps is considered a set of practices that combines the abilities of Software Development i.e Dev and IT Operations i.e Ops together, which results in delivering top-notch quality software fastly and more efficiently. Its focus is to encourage communication, collaboration, and integration between the software development team and IT operations to
8 min read
Kubernetes Interview Questions and Answers
Kubernetes is a powerful open-source platform for automating deployment, scaling, and operations of application containers across clusters of hosts. It provides a container-centric infrastructure that is efficient, making it a leading choice for managing complex applications. Derived from the Greek word meaning "captain," "helmsman," or "governor,"
15+ min read
DevOps Tutorial
DevOps is a collection of two words, " Development " and " Operations ," representing a cultural approach that emphasizes collaboration between development and operations teams to streamline the entire software delivery lifecycle. This DevOps tutorial is designed to help learn DevOps basics and advanced concepts, including Git, Ansible, Docker, Pup
8 min read
Docker Tutorial
In this Docker Tutorial, you’ll learn all the basic to advanced concepts like Docker installation, Docker container, Docker commands, Docker run, Docker images, Docker compose, Docker engine, Docker networking, etc. Docker is a powerful tool used for developing, packaging, and deploying applications efficiently. Docker is a container management ser
8 min read
Kubernetes Tutorial
In this Kubernetes Tutorial you’ll learn all the basics to advanced concepts like Kubernetes service , Kubernetes cluster construction , Kubernetes deployment , Kubernetes Architecture , etc. This free tutorial on Kubernetes will provide all the essential information needed to understand and work with Kubernetes, including the use of APIs, installa
7 min read
Amazon Web Services (AWS) Tutorial
This AWS tutorial, or Amazon Web Service tutorial, is designed for beginners and professionals to learn AWS's basic and advanced concepts . Learn about the various topics of AWS such as introduction, history of AWS, global infrastructure, features of AWS, IAM, storage services, database services, application Services, etc., and other AWS products s
8 min read
Microsoft Azure Tutorial
Designed by Microsoft in 2010, Microsoft Azure is one of the widely used cloud computing platforms. Azure provides a wide variety of services, such as cloud storage, compute services, network services, cognitive services, databases, analytics, and IoT. It makes building, deploying, and managing applications very easy. All the Microsoft Azure fundam
13 min read
Docker Commands
Docker is an open-source project that automates the deployment of applications as movable, independent containers that can run locally or in the cloud. You can divide your applications from your infrastructure with the help of Docker, allowing for quick software delivery and it also allows you to manage your infrastructure in the same ways that you
7 min read
What is Docker?
Docker is a set of Platforms as a service (PaaS) products that use Operating system-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are
15+ min read