Open In App
Related Articles

Kubectl Command Cheat Sheet

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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-infokubectlGet cluster information.
kubectl get nodesViews all the nodes present in the cluster.

For Node Management

Command

Description

kubectl get nodesList 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 namespacesList 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 createCreate an object imperatively.
kubectl apply -f https://url-to-resource-definition.yamlCreate 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 wideList 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-namespacesList 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> –allDelete all resources of a specific type.
kubectl delete -f <resource-definition.yaml>kubectl delete -f https://url-to-resource-definition.yamlDelete 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 podsList 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 deploymentsList 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 replicasetsList 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 servicesList 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 configmapsList all config maps.
kubectl get secretsList 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 pvList all PersistentVolumes.
kubectl describe pv <pv-name>Describe a specific PersistentVolume.
kubectl create -f <persistent-volume-claim-definition.yaml>Create a PersistentVolumeClaim.
kubectl get pvcList 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 statefulsetsList 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 eventsCheck cluster events.
kubectl get component statusesGet cluster component statuses.
kubectl top nodesGet resource utilization of nodes.
kubectl top podsGet 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 proxyAccess 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=0Used 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=7Displays the HTTPS request headers.
kubectl get <resource-type> –v=8Display 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.



Last Updated : 28 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads