KUBECTL COMMANDS: Everything You Need To Know

Kubectl Commands
Image by Freepik

If you are new to Kubernetes, it can be hard to keep track of everything. Knowing about all the different objects Kubernetes provides should be enough, right? No, it gets even more complicated for beginners when they try to interact with K8 clusters. As with Kubernetes, Kubectl is built and released as part of it, and it uses the same versioning system. Therefore, it is crucial to review the fundamentals by learning what Kubectl is, what it does, and how to run it on Windows before moving on to the cheat sheet. To help you, this article will walk you through the examples of Kubectl commands you need to know about.

Kubectl commands: Overview

Kubectl is a command-line tool for Kubernetes that lets you talk to and manage Kubernetes clusters. It talks to the Kubernetes API server to do management tasks. It verifies its identity with the master node of your cluster and uses API calls to do this. Kubectl also lets you create, inspect, update, and delete Kubernetes objects, as well as deploy apps, look at and manage cluster resources, and see logs. Kubectl provides three methods.

  • A technique called “imperative commands” tells Kubernetes exactly what to do with each object, like “kubectl create pod” or “kubectl delete service.”
  • The “Imperative Object Configuration” method is the same as the first one, but it uses manifest files instead of objects directly. For example, kubectl create -f manifest.yaml is this method’s command.
  • The “Declarative Object Configuration” technique also uses manifest files as input, but it adds objects if they don’t already exist or changes existing objects that don’t match the manifest files’ specifications. That command is kubectl apply -f manifest.yaml.

In essence, this indicates that the standard object notation is an object type, a slash, and then an object name.

Kubectl Commands Cheat Sheet

Kubectl is the command-line tool for Kubernetes configuration that talks to a Kubernetes API server. You can create, inspect, update, and delete Kubernetes objects with Kubectl. This cheat sheet will help you quickly run commands on a lot of common Kubernetes resources, Windows, and components.

Also, you can use the full command for an object on things like pod(s) or the shortcode variation that is said in the section headings. The result will be the same for all of them. When you run most of the commands, you should also make sure to add the <name of the resource you are managing.

Kubectl Commands Cheat Sheet: Terms Used in Kubernetes

Some terms you should know before you start using Kubernetes have been listed below:

  • The cluster, as I think of it, is a group of real or virtual servers that run Kubernetes.
  • Node: Nodes come in two different types:
  • unchecked A master node, which can be a physical or virtual server, is in charge of managing the Kubernetes cluster.
  • uncheckedThe worker node is the physical or virtual server where a given container technology is used to run the workload.
  • Pods are a set of containers that use the same namespaces on the network.
  • The labels: These are the key-value pairs that the user sets up and links to Pods.
  • Master: It manages the plane components that give administrators access to the cluster workloads.
  • command-lineService: An abstraction that stands in for a group of pods doing a “service” is one way to look at it.

Okay, now that we have a good idea of what Kubernetes is, let’s go to the cheat sheet.

Kubectl Commands Cheat Sheet With Examples

This part will list and explain each commonly used Kubernetes (K8S) category or component, along with the right Kubectl commands for quick reference!

#1. Common Options

In Kubectl, you can specify optional flags for use with various commands.

  • alias: Set an alias for Kubectl.

alias k=kubectl

echo ‘alias k=kubectl’ >> ~/.bashrc

  • -o=json: Output format in JSON.

kubectl get pods -o=json

  • -o=yaml: Output format in YAML.

kubectl get pods -o=yaml

  • -o=wide: Output in plain-text format with any additional information, and for pods, the node name is included.

#2. Files for Configuration (also known as YAML or Manifest Files)

Files for Configuration: Store multiple object configuration files in a directory, and use Kubectl to create and update Kubernetes objects as needed. This lets you create, update, and delete objects. In essence, there is no need to merge changes made to live objects back into the object configuration files with this method. Furthermore, Kubectl diff shows you a sneak peek of the changes that it will bring about.

  • kubectl create -f <configuration file>  Create objects.
  • kubectl create -f <configuration file directory> Create objects in all manifest files in a directory.
  • kubectl create -f <‘url’> – Create objects from a URL.
  • kubectl delete -f <configuration file>: Delete an object.

#3. Cluster Management and Context

Cluster management refers to querying information about the K8S cluster itself.

  • Kubectl cluster-info: Display endpoint information about the master and services in the cluster.
  • Kubectl version: Display the Kubernetes version running on the client and server.
  • Kubectl config view: Get the configuration of the cluster.

#4. Daemonsets

DaemonSets guarantees that all (or some) nodes run a copy of a pod. It is possible to add pods to nodes as they are added to the cluster. Any pods that are no longer needed are thrown away when a node is removed from the cluster. Also, cleaning up the pods that a DaemonSet made will happen when you delete them.

  • Kubectl gets a daemonset. List one or more daemonsets.
  • kubectl edit daemonset <daemonset_name>: Edit and update the definition of one or more daemonset.
  • kubectl delete daemonset <daemonset_name>: Delete a daemonset.
  • kubectl create daemonset <daemonset_name> Create a new daemonset.

#5. Deployments

A deployment gives Pods and ReplicaSets declarative updates. This is where you describe the state you want in a deployment, and at a controlled rate, the deployment controller changes the current state to the state you want. That is to say, one way to define deployments is to make new replica sets or to get rid of old deployments and use all of their resources in new ones. 

  • Kubectl get deployment: List one or more deployments.
  • Kubectl describes deployment <deployment_name> and displays the detailed state of one or more deployments.
  • Kubectl edit deployment <deployment_name>: Edit and update the definition of one or more deployments on the server.

#6. Logs

System component logs keep track of what’s happening in the cluster and can be very helpful for fixing bugs. Hence, the log verbosity can be changed so that you see more or less information. Also, logs can be as coarsely grained as displaying errors within a component as well as as finely grained as displaying step-by-step traces of events.

  • kubectl logs <pod_name> Print the logs for a pod.
  • kubectl logs –since=6h <pod_name> Print the logs for the last 6 hours for a pod.
  • kubectl logs –tail=50 <pod_name> Get the most recent 50 lines of logs.
  • kubectl logs -f <service_name> [-c <$container>] – Get logs from a service and optionally select which container.

#7. Namespaces 

A way to separate groups of resources within a Kubernetes cluster is through namespaces. It is okay for resource names to be different within the same namespace, but not between namespaces. This means you can only use namespace-based scoping on objects that are named (like deployments, services, etc.), not on objects that are used by the whole cluster (like StorageClass, nodes, PersistentVolumes, etc.).

  • kubectl create namespace <namespace_name>: Create a namespace.
  • kubectl get namespace <namespace_name> – List one or more namespaces.
  • kubectl describes namespace <namespace_name> – Display the detailed state of one or more namespaces.

#8. Nodes

Kubernetes handles your work by putting containers into pods that run on nodes. Depending on the cluster, a node could be a physical or virtual computer. Each node contains the services required to run pods and is controlled by the control plane. Although a cluster usually has several nodes, it might only have one node in a learning or resource-limited setting. One node has the kubelet, a container runtime, and the kube-proxy.

  • Kubectl taint node <node_name>: Update the taints on one or more nodes.
  • kubectl get node: List one or more nodes.
  • kubectl delete node <node_name>: Delete a node or multiple nodes.
  • kubectl top node <node_name> – Display Resource usage (CPU, memory, and storage) for nodes.

#9. Services

A simple way to make a network service out of an app running on a group of pods Because Kubernetes uses a familiar service discovery mechanism, you don’t have to change your app to use it. That is to say, Kubernetes pods have their own IP addresses and a single DNS name that is used for all of them. It can also spread the load across all of them.

  • kubectl get services: list one or more services.
  • kubectl describes services by displaying the detailed state of a service.
  • kubectl expose deployment [deployment_name]: expose a replication controller, service, deployment, or pod as a new Kubernetes service.
  • kubectl edit services edit  and update the definition of one or more services

#10. Pods

In Kubernetes, pods are the tiniest units of computing that can be deployed. If you think of a pod as a group of whales or peas, then a pod is a group of one or more containers that share storage and network resources and are told how to run.

  • kubectl gets pods. List one or more pods.
  • kubectl get pods –sort-by=’.status.containerStatuses[0].restart count: List pods sorted by restart count.
  • kubectl get pods –field-selector=status.phase=Running: Get all running pods in the namespace.

#11. ReplicaSets

The job of a ReplicaSet is to keep a stable group of replica pods running at all times. Because of this, it is often used to promise the availability of a certain number of pods that are exactly the same.

  • kubectl gets replicasets – listing ReplicaSets.
  • kubectl describes replicasets <replicaset_name> displays the detailed state of one or more ReplicaSets.
  • kubectl scale –replicas=[x] Scale a ReplicaSet

How to Run Kubectl Commands in Windows

Users must use a version of Kubectl that is only slightly different from the version used by their cluster. A v1.22 client, for example, can communicate with control panels from v1.23, v1.22, and v1.21. Therefore, it is preferable to keep Kubectl at its most recent version to avoid unexpected problems with the system. This tutorial will cover the installation and running of Kubectl on Windows systems.

How to Run Kubectl Commands in Windows: Step-by-Step Guide

Because Kubernetes is designed to work with Linux, the only way to use it on Windows is on a virtual machine. So, create a virtual environment for running Kubernetes by following the instructions below.

Step 1. 

Download the latest v1.22.0 release by using the following URL:

https://dl.k8s.io/release/v1.22.0/bin/windows/amd64/kubectl.exe

Note: In order to find out the latest stable Kubectl version, visit https://dl.k8s.io/release/stable.txt.

Step 2. 

(Optional) In order to validate the Kubectl binary, download the Kubectl checksum file by executing the following curl command:

curl -LO https://dl.k8s.io/v1.22.0/bin/windows/amd64/kubectl.exe.sha256

Step 3. 

To validate the Kubectl binary against the checksum file, use either one of the methods:

a) Manually compare CertUtil‘s output to the checksum file downloaded using the Command Prompt:

  • CertUtil -hashfile kubectl.exe SHA256
  • type kubectl.exe.sha256

b) Automate the verification using the -eq operator to get a true or false result using the PowerShell:

  •   $(CertUtil -hashfile .\kubectl.exe SHA256)[1] -replace ” “, “”) -eq $(type .\kubectl.exe.sha256)

Step 4. 

Next, add the kubectl binary to the user’s PATH.

Step 5. 

Finally, execute the following command to make sure that the available Kubectl version is the same as the downloaded version:

  • Kubectl version –client

What is the use of kubectl?

kubectl, allows you to run commands against Kubernetes clusters.

How do I use Kubernetes commands?

  • kubectl [command] [TYPE] [NAME] [flags] where command, TYPE, NAME, and flags are

kubectl config set-context –current –namespace=<namespace-name> Operations.

Which common command is used in Kubernetes?

It is kubectl 

What is Kubernetes vs Docker?

While Docker is a container runtime, Kubernetes is a platform for running and managing containers from many container runtimes.

Conclusion 

This tutorial presents the steps to install, set up, and run the Kubectl command-line tool on Windows systems. I hope this tutorial was helpful, and do reach out to us if you have any queries or suggestions.

References

  1. 20 Basic Linux Commands With Examples: A Beginner’s Guide
  2. How to Install PowerShell for Mac: Easy Step-By-Step
  3. HOW TO INSTALL NODE. js: The Complete Guide
  4. AWS FIREWALL: How Does It Work?
  5. Malvertising: What Is It & How Do You Prevent It?
0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like