Kubectl Cheat Sheet
Kubectl is a command line tool for configuring Kubernetes. It connects to a Kubernetes API server and lets us manage Kubernetes objects by creating, inspecting, updating, and deleting them. This cheat sheet was prepared by our experts to help learners understand Kubernetes as a tool. This handy reference will help them implement their knowledge while resolving complex issues. It will also go through the basics of Kubernetes.
Watch this video on Kubernetes Tutorial for Beginners:
You can also download the printable PDF of this Kubernetes cheat sheet
What is Kubernetes?
Kubernetes is an open-source platform used to automate deployment and scale containers across clusters of hosts, providing container-centric infrastructure
- It is a container orchestrator.
- It can run a Linux container.
- Launches a container
- Maintains and monitors the container site
- Performs container-oriented networking
Become a master of DevOps by taking up this online instructor-led DevOps Training in London!
Kubernetes Architecture
The image below illustrates the architecture of Kubernetes. Let’s have a look at it.
The Kubernetes cluster comprises master and worker node(s), where the master node orchestrates pods onto worker nodes. It runs the Kubernetes control plane, including the API server, scheduler, controller manager and more, abstracting cluster management complexities. Worker nodes, also called minions, actively run pods and containers via services like container runtimes, kubelet, and kube-proxy.
Key Components of Kubernetes Architecture:
Here, we have explained the key components of Kubernetes architecture:
- Pod: Group of containers
- Node: It is a virtual machine which can run multiple pods.
- Label: Used to identify pods
- Kubelet: Container agents responsible for maintaining the set of pods
- Proxy: The load balancer for pods, helping in distributing tasks across them
- Etcd: A metadata service.
- CAdvisor: Used for monitoring resource usage and performance.
- Replication controller: Manages pod replication
- Scheduler: Used for pod scheduling in worker nodes
- API server: Kubernetes API server
Now, let’s understand the roles master and worker nodes play in the Kubernetes architecture.
Preparing for job interviews? Head to our most-asked Kubernetes Interview Questions and Answers.
Master Node in Kubernetes
- It is responsible for maintaining the desired state of the cluster we are working on.
- The term ‘master‘ indicates a set of processes that are used to manage a cluster.
- It contains Kubelet service info, an API, a scheduler, a replication controller, and a Kubernetes master.
Worker Nodes or Minions in Kubernetes
Worker Node is also called a ‘minion,’ a worker node contains the services necessary to run the pods that are managed by the master.
- The services it provides are Container Runtime, Kubelet, Kube-proxy, etc.
- It contains Kubelet, cAdvisor, Services, Pods, and Containers.
Learn more DevOps concepts from this DevOps Training in Sydney to get ahead in your career!
Kubernetes Commands
Let’s have a look at some of the Kubernetes (kubectl) Node, Pods, Namespaces, Deployment, Services, DaemonSets, Events, Logs commands. We are also going to cover Service Accounts, Replica Sets, Persistent Volume, and Storage class commands. We have covered how you can create, edit, delete, and check the logs of these services using these kubectl commands.
Commands for Nodes in Kubectl
A node is a fundamental unit of computing resources within a cluster. It can be a virtual or physical machine, depending on the cluster. To run on nodes, containers are placed into pods by Kubernetes to execute the workload.
Here are a few Kubernetes commands for nodes.
Command |
Function |
kubectl get nodes |
To list all current nodes in a cluster. |
kubectl get nodes -w |
Watches nodes continuously |
kubectl delete node <name> |
To delete a node |
kubectl label node <node-name> <label-key>=<label-value> |
To add a label to a node |
kubectl describe node |
To get information about the node |
kubectl get nodes –selector=<label-key>=<label-value> |
To get the information about nodes based on a specific label |
kubectl top node <name-of-node> |
To display CPU and memory usage for a specific node. |
Note: You can also use the shortcode for this node command: “no”, e.g: kubectl top no <name-of-node>
Commands for Cluster Introspection in Kubectl
Cluster is a collection of nodes that has some processing and storage capacity which runs containerized applications. As compared to virtual machines they are more flexible and lightweight.
Command |
Function |
kubectl version |
To get the information about the kubernetes version. |
kubectl cluster-info |
To get the information about the services in the cluster. |
kubectl config view |
To display the configuration of the cluster |
kubectl api-versions |
To get the available API versions. |
kubectl api-resources |
To get the available API resources. |
Wish to crack a DevOps interview? Prepare yourself with these Top DevOps Interview Questions and Answers!
Commands for Pods in Kubectl
A pod stands as the smallest unit of execution in Kubernetes, encapsulating one or more applications that run on nodes.
Let’s explore some Kubernetes commands related to pods:
Command |
Function |
kubectl get pods |
Lists all current pods |
kubectl get pods –output=wide |
To show additional information about pods in wider format. |
kubectl get pods -o=json |
To list all current pods in json format (-o: output) |
kubectl get pods -o=yaml |
To list all current pods in YAML format |
kubectl run my-pod –image=<image-name> |
To create a pod |
kubectl describe pods |
To provide detailed information about all or specific pods. |
Note: Shortcode for “pod” is “po”, in commands “pod” can be replaced with “po” it won’t be affecting the output. E.g.: kubectl describe po
Commands for Namespaces in Kubectl
Namespaces play an important role in partitioning resources and isolating workloads within a Kubernetes cluster. Users interacting with one namespace do not see the content within another namespace, ensuring effective isolation.
Here are the commands for Namespace:
Command |
Function |
Kubectl create namespace <name_of_namespace> |
To create a namespace |
kubectl get namespace <name_of_namespace> |
To get list of namespaces |
kubectl get namespace <namespace-name> -o yaml |
To get the YAML representation of a namespace. |
kubectl edit namespace <name_of_namespace> |
To edit and update the namespace definition |
kubectl delete namespace <name_of_namespace> |
To delete a namespace |
Note: In the command we can replace “namespace” with its shortcode “ns”. The output will remain same in both the cases.
E.g: kubectl get ns <name>
Commands for Deployments in Kubectl
Kubernetes deployments define pod creation, scaling, controlled updates, and version rollback, ensuring efficient management of containerized applications within clusters.
Here are some commands related to deployments:
Command |
Function |
kubectl get deployment |
To list all the current deployments. |
kubectl describe deployments |
To get the information about the deployments. |
kubectl apply -f <file-name> |
For creating a new deployment. |
kubectl delete deployment –all |
To delete all of the deployments. |
kubectl delete deployments <deployment-name> |
For deleting a deployment. |
kubectl rollout status deployment <deployment-name> |
To check the status of deployment rollout. |
kubectl rollout pause deployment <name-of-deployment> |
To pause a deployment rollout. |
kubectl rollout resume deployment <name-of-deployment> |
To resume a deployment |
kubectl rollout history deployment <name-of-deployment> |
To check rollout history of a deployment |
Commands for Services in Kubectl
A Kubernetes Service actively exposes an application running on Pods to be reachable within and outside the cluster. It provides a stable, dedicated IP address and DNS name for the application, load balancing traffic across the Pods backing the Service. Services facilitate loose coupling between microservices.
Command |
Function |
kubectl get svc |
To get information about services |
kubectl describe svc <name-of-service> |
To get detailed information about the specified service. |
kubectl get svc -n <name-of-namespace> |
To get the services in a specific namespace |
kubectl delete svc <name-of-service> |
To delete a specific service |
kubectl edit svc <name-of-service> |
To edit a service |
Note: “svc ” is a shortcode for “service” you can use either of them. It will not affect the output.
Commands for DaemonSets in Kubectl
Kubernetes DaemonSets actively ensure a copy of a Pod runs on each Node in a cluster, providing cluster services alongside user workloads. As nodes get added, Pods matching the DaemonSet specification get actively created on them; as nodes get removed, those Pods get garbage collected.
Let’s check out a few of the kubernetes commands for DaemonSets:
Command |
Function |
kubectl get daemonsets |
To get all the DaemonSets |
kubectl get daemonset -n <name-of-namespace> |
To get the DaemonSets in a specific namespace |
kubectl describe daemonset <name> |
Provides detailed information about a specific DaemonSet |
kubectl delete daemonset <name> |
Delete a DaemonSet |
kubectl apply -f daemonset.yaml |
To create a DaemonSet from a YAML file |
kubectl rollout restart daemonset <name> |
To roll update for a DaemonSet |
kubectl edit daemonset <name> |
To edit a daemon-set |
Note: Here in commands the “daemonset” can be replaced by its shortcode: “ds”. The output for both will remain the same.
In the IT sector, DevOps Engineers are among the highest paid professionals. Sign up for this DevOps Training in Hyderabad today and be one of them!
Commands for Events in Kubectl
Kubernetes events are auto-triggered upon specific actions on cluster objects. For example, creating a pod generates a corresponding event.
Let’s have a look at the Kubernetes events commands:
Command |
Function |
kubectl get events |
To display events from all namespaces. |
kubectl get events –watch |
To get real-time updates on the list of events. |
kubectl describe events <name> |
To get detailed information about the event |
Commands for Logs in Kubectl
Logs help us understand how our application is performing and what is happening inside of it.
Here are a few Kubernetes log commands:
Command |
Function |
kubectl logs -f<name>>[-c< $container>] |
To get logs from the service for a container |
kubectl logs -l name=<label name> |
To get logs based on a specified label |
kubectl logs –since=6h <pod_name> |
To get the last 6 hours of logs of a pod |
kubectl logs <name-of-pod> |
To get logs of a pod. |
kubectl logs —tail=20 <pod> |
To get the last 20 lines of logs from a specific pod |
kubectl logs -f -c <name-of-container> <pod-name> |
To stream the logs from a specific container within a pod in real-time |
Commands for Service Accounts in Kubectl
In Kubernetes, a service account is a special kind of non-human account that provides a unique identity in a Kubernetes cluster.
Here is how you can create, list, and delete Kubernetes service account using commands:
Command |
Function |
kubectl create serviceaccount <service-account-name> |
To create Service Account |
kubectl get serviceaccounts |
To list all Service Account |
kubectl delete serviceaccount <service-account-name> |
To delete a service Account |
Note: Here, in the command the “serviceaccount” can be replaced with its shortcode “sa”.
E.g: kubectl get sa
Commands for Secrets in Kubectl
A Secret is an object used to store confidential data like a key, passcode, or a token.
Here are few kubectl commands using which you can list, describe, create and delete Secrets in Kubernetes:
Command |
Function |
kubectl get secrets |
To list all the secrets. |
kubectl describe secrets |
To get the details about the secrets. |
kubectl create secrets |
To create a new secret. |
kubectl delete secret <name> |
To delete a specific secret. |
Commands for Replication Controllers in Kubectl
The Replication Controller is responsible for ensuring the smooth functioning of pod replicas. It makes sure that at a particular time, a specific number of pod replicas are running.
Command |
Function |
kubectl get rc |
To get the list of all the replicationControllers. |
Interested in getting a top-rated certification in DevOps? Join Intellipaat’s DevOps Course in Bangalore today!
Commands for Replica Sets in Kubectl
It is a resource designed to maintain a consistent set of active pods for a specific workload.
Listed below are a few of the Kubernetes Replica Set commands:
Command |
Function |
kubectl get rs |
To list the replica sets in the current namespace. |
kubectl describe rs <replica-set-name> |
To get detailed information about the replica set. |
kubectl create -f <rs.yaml> |
To create a new ReplicaSet using a YAML file. |
kubectl delete rs <replica-set-name> |
To delete a ReplicaSet. |
Note: Here, “rs” can be replaced by replicasets and vice-versa without altering the output.
E.g.: kubectl get replicasets
Commands for Persistent Volume in Kubectl
A persistent volume refers to a storage unit within a cluster that has been allocated by an administrator.
Here are some commands related to Kubernetes persistent volume:
Command |
Function |
kubectl get pv |
To get a list of all Persistent Volumes. |
kubectl describe pvc |
To get information about a specific Persistent Volume. |
kubectl create pv <pv-name> –<option>=<value> |
Create a Persistent Volume. |
kubectl delete pv <pv-name> |
To delete a Persistent Volume. |
kubectl get pv <pv-name> -o=jsonpath='{.spec.capacity.storage}’ |
To check Persistent Volume Capacity and Usage. |
Do you aspire to become a DevOps expert? Sign up today for the Best DevOps Course in Toronto!
Commands for Storage Class in Kubectl
Kubernetes storage classes define different storage types and provide a way to request a particular storage type for your workloads.
Let’s have a look at some of the Kubernetes storage class commands:
Command |
Function |
kubectl get sc |
To get a list of all storage class |
kubectl apply -f storageclass.yaml |
To create a storage class from a YAML file. |
kubectl delete sc <storageclass-name> |
To delete a storage class |
Note: The shortcode for storage class is “sc”.
Commands for StatefulSet in Kubectl
Statefulset is a type of workload API object which can be used for the management of stateful applications.
Command |
Function |
Kubectl get statefulset |
To list all of the statefulsets |
Kubectl delete statefulset/<statefulset-name> |
To delete a statefulset. |
Note: You can use the shortcode of the statefulset: “sts”. The output for both the commands will remain the same.
E.g: Kubectl get sts
Commands for Manifest Files in Kubectl
We define the desired state of a Kubernetes object by creating a Kubernetes Manifest file, which is a YAML or JSON file describing the object’s specifications like its metadata, properties, and desired state.
Command |
Function |
kubectl apply -f <manifest.yaml> |
To apply the configuration to the object using a yaml file. |
kubectl create -f <manifest.yaml> |
To create a service. |
kubectl create -f <URL> |
To create an object using a URL |
kubectl delete -f <manifest.yaml> |
To delete a service using a YAML file. |
Read on:- Git Commands to enhance your Knowledge!
Other Quick Commands in Kubectl
Additionally, we do have some quick commands that are often very useful in Kubectl:
Command |
Function |
Kubectl run<name> — image=<image-name> |
To launch a pod with a name and an image |
Kubectl create -f <manifest.yaml> |
To create a service described in <manifest.yaml> |
kubectl delete -f <config file> |
To delete a service |
Kubectl scale –replicas=<count>rc<name> |
To scale the replication counter to count the number of instances |
Expose rc<name> –port=<external>–target-port=<internal> |
To map the external port to the internal replication port |
Kubectl drain<n>– delete-local-data–force–ignore-daemonset |
To stop all pods in <n> |
Kubectltaintnodes –all-node-role.kuernetes.io/master- |
To allow the master node to run pods |
kubectl drain <node-name> |
To safely remove pods from a node before maintenance. |
kubectl uncordon <node-name> |
To mark a node as schedulable. |
Objects in Kubernetes
Kubernetes objects are persistent entities that represent the state of your cluster, describing what containerized applications are running, the resources available to them, and policies around application behavior. Almost every Kubernetes object has a specific field for the desired configuration and a status field for actual status. Some common objects are Pods, Deployments, replica sets, replication controllers, statefulsets, DaemonSets, PersistentVolume, Service, Namespaces, ConfigMaps & Secrets, and Job.
All |
Cluster Role Bindings |
Cluster Roles |
cm= conf gmaps |
controllerrevisions |
crd=custom resource definition |
Cronjobs |
cs=component statuses |
csr= certificate signing requests |
Deploy=deployments |
ds= daemon sets |
ep=end points |
ev= events |
hpa= horizontal pod autoscaling |
ing= ingress |
jobs |
limits=limit ranges |
Netpol- network policies |
No = nodes |
ns= namespaces |
pdb= pod distribution budgets |
po= pods |
Pod preset |
Pod templates |
Psp= pod security policies |
Pv= persistent volumes |
pvc= persistent volume claims |
quota= resource quotas |
rc= replication controllers |
Role bindings |
roles |
rs= replica sets |
sa= service accounts |
sc= storage classes |
secrets |
sts= stateful sets |
Some of the Features of Kubernetes
- Automated scheduling: It provides an advanced scheduler that helps launch containers on cluster nodes.
- Self-healing: Rescheduling, replacing, and restarting dead containers are possible.
- Automated rollouts and rollbacks: It supports rollback for systems in the case of failures and enables rollout and rollback for the desired state.
- Horizontal scaling: It can be scaled up and down as required and can be automated with respect to CPU usage.
- Service discovery and load balancing: It uses a unique IP and DNS name for containers that help in identifying them.
Conclusion
We hope this Kubernetes cheat sheet was helpful. These commands will definitely help you in mastering Kubernetes.
Enroll today in our comprehensive DevOps Course or join Intellipaat’s Cloud Computing and DevOps Course to start your career or enhance your skills in the field of DevOps and get certified today.