Become Kubernetes Pro min
Image Source 

What Is kubectl?

kubectl is a command-line utility that is used to manage and interact with a Kubernetes cluster. It provides a convenient way for administrators to control and monitor the state of the cluster and its resources. 

With kubectl, you can create, inspect, update, and delete resources in the cluster, such as pods, services, and deployments. It also allows you to view logs, troubleshoot issues, and perform other management tasks. The tool provides a simple, human-readable interface for interacting with the Kubernetes API, making it easy to automate common tasks and perform complex operations.

In this article I provide a quick guide explaining some of the most common commands. Refer to this expanded kubectl cheat sheet to see an extended list of commands and use cases.

Namespace Commands

Kubernetes namespaces allow for the separation of resources within a cluster into isolated environments for better organization and management.

To list all namespaces in the cluster, the command kubectl get namespaces can be used.

JeeW401h7kvn YyuV mmZ7jePtzSahiO3HJgixlzgotWWNk9ConRtyqEK0GABO61mUFh0q0upyaJKuDBReP2NY7x7ESz0xOXjA2zhEjvD UbqDd5B9GUQfdFLDpd 16yt45so9tPBL7 2t3RntA0Ifk

 To create a new namespace, the command kubectl create namespace is used. 

MZ eHEbV g9ouZ 8Teo D

To set the current namespace for the kubectl context, use the command kubectl config set-context –current –namespace=testns. 

iP6GwRENoEqHdbF4Y XIocP3bFZxNR IUrIYI6SimihtgvSrpRtViceyLxXEyNvRczz7YIucLVuCzfGlbS86o RsWk4uQb3Zh8IiCwOEs8RJwZJKLBrOmH5Zp SJz3 s5VE1ZvPq0cmCyK h08XmzD4

And to delete a namespace, kubectl delete namespace can be executed.

XbPNYRS3oA389PO8Bb 0NsUmE420jbys9pRzXnMm56 e1FkJb2IeqFn9TINdfP0iPZbAlfi99InWordrfnbqCTnjmkPhNpvtirPebVrOgZObqPyBsv2RRNVLwz5ADLp0c8R2m 0HAo1JxcRy 3qUplM

Node Commands

Nodes in a Kubernetes cluster are worker machines that host containers, running the applications and services in the cluster and managed by the master node.

To retrieve a list of all nodes, the command kubectl get nodes can be used. 

a7hE1pLbYzWCuv6vQt2frguqvtjcA0B3uXQmOTf2kjF5pca xYS4LC2VLlbkzMqlkl348KxMGTQpJFmzAg9skuIHc wEGjg m8qQdzY

The command kubectl top node displays resource usage information for a specific node.

N 925APyL15n GlTYSsBAjITwLvtxJRRnIwC9a t8QdszpouzeODeDzX9k 6hZNDDYtXp ine nvQeKRgp7N3mi9AKDDe9TsEHrzcOzYedsqJA85YEk7Pldi0oVxk09JZ4f4 inVyBHLIn2L7mZpsA

The kubectl cordon/uncordon node command either marks a node as schedulable or unschedulable. The cordon command makes it so that no new pods can be scheduled on the node, while uncordon allows new pods to be scheduled.

PnOXWFaJsUTpJ569iIxBIK9 EF1sOtU7xXhJ7
0rHS PqJBr3zB1puWANfZAp6j4ofi8Fu6spP3bVzeKUFNe9DliFIaxZKLwAtlBeUJY7 t2t9I2 QlBoRri53CCkv btMLdVsCCmJpE7NefxWp3 XvSjj0TDMOgIT8L7ugVPtbanePH7clMC hBu6QqY

kubectl drain node is used to evacuate all pods from a node in preparation for maintenance. This command removes the pods and reschedules them on other available nodes in the cluster, allowing for maintenance to be performed on the node without disrupting the running applications and services.

aAPGE0pl4hswm7oi4V11lUEED7AeoEBxdNriLmg xd5jS2U3UKM2VIUzhXEDuXLnF1KKh7NO2UeOnBbgsrDxr4cwrnF5mOlEXKuPVzewQAsvvYgtp16ChXO9fgxUeWpChNsx N7pFXp ra7OHw140EY

Scaling Commands

Kubernetes lets you automate many management tasks, including provisioning and scaling. Instead of manually allocating resources, you can create automated processes that save time, let you respond quickly to peaks in demand, and conserve costs by scaling down when resources are not needed. It can be used alongside the cluster autoscaler by allocating only the resources that are needed.

The Kubernetes autoscaling mechanism uses two layers:

  • Pod-based scaling—supported by the Horizontal Pod Autoscaler (HPA) and the newer Vertical Pod Autoscaler (VPA).
  • Node-based scaling—supported by the Cluster Autoscaler.

The kubectl command-line tool provides several commands for scaling resources in a Kubernetes cluster. Here are some of the most commonly used commands:

  • kubectl scale deployment: This command is used to scale the number of replicas for a deployment in a Kubernetes cluster. It takes the deployment name and the desired number of replicas as arguments.
weMmgWMDHrak6 jp9UHDqAQB4oqqrtyvaOWtIc0IwjX4ZxOvsxmV6VuejtHtELjryuVpplHKnNcrigY1TKhnf QC9CTTPLF0hIaMuk057ZB7PefkHmIlRx0eOo3 NZ
  • kubectl scale statefulset: This command is used to scale the number of replicas for a statefulset in a Kubernetes cluster. It takes the statefulset name and the desired number of replicas as arguments.
YHAiJ2hyw QFKnyPHbLIUTdJyf3pIokb
  • kubectl autoscale: This command is used to automatically scale the number of replicas for a deployment based on resource usage. It takes the deployment name, the minimum and maximum number of replicas, and the CPU utilization target as arguments.
nE obs8Q1XzbEdmQ9 LOWGX6mdgPkaDKkFlhWzZeP f107lcJ9HLEojIbcJLkuGqJx6 xrjNU5r ad7BzA nU RLmPAzJ1x4Ygqxgak2DvckH4Q6mDR 8tzCDck96lulOFhCjlTHzdLNyDZKMplTnMs

Commands for Managing Deployments, Pods, and Secrets

Kubernetes provides a set of commands for managing resources such as deployments, pods, and secrets. The following are some common kubectl commands for managing these resources:

  • kubectl get: This command lists the specified resources in a cluster. For example, kubectl get pods will list all of the pods in the cluster, and kubectl get deployments will list the deployments.
LRRcljL Fpd1MJpxkkDWxy0u75ePGK77jGBxkvk ZNQXFNyhPHCAovfd7IvFMAQLY haO6Kmlg17L7R7KPwayC1AWUIImgWTDKhz2pwmeGq0Fp6R bTZbynkPvd7Y1AIDy3kJ7Ev41 lT21rez7gnvk
  • kubectl create: This command creates new resources (of the specified type) in a cluster. For example, kubectl create -f <file.yaml> creates a new pod, deployment, or secret based on the configuration defined in the specified YAML file.
7EcWoNXj5RdYi3bmjTs gQD6iJxwmN4mgZBFAo2Zxrdx8RLnjQQ NrSXN8vkOe5vU5fdnyc6hSxPxPt0YuMUFxXJhHlf TKoFp SSKzK6yxXSn8336ZQRgAAIsnsr66smCB
  • kubectl delete: This command deletes the specified resources from the cluster. For example, kubectl delete pod <pod-name> will delete the named pod from the cluster.
IVFHBYh1DqG2JOz012tYG22KT4y0wTgRTTtyG4NH9H7gUx3iu0Ou5pgOYDQdoo iqJw633OGrfu7PCpENDXJob0H85Wnge8cDNb wSHfVKX NKLnbMNsBSAsMMtCFHL jP0SMJIr4FQ9PewPE8Lz y4
  • kubectl describe: This command is used to display detailed information about a given resource. For example, kubectl describe pod <pod-name> will return details about a specific pod, including its status, events, and logs.
Bc51W0mYZYlL54ixvQl2VO0RiDvoPzTr189QnCChMY86yo4h422pk9f DisOYnv51EuNn6
  • kubectl edit: This command enables developers to edit the configuration of a given resource in the cluster. For example, kubectl edit deployment <deployment-name> will open an editor to modify the configuration of a specific deployment.
3uF oah IYGwKeJ nUpkVUesBr fxJu9or9FMGkyDPVvifKeQQI8j43busskKgoJV5bdh3IfmLHYjYc0HiNpRs5wHwPSfkK5H1ExPbM9

Volume Commands

Kubernetes volumes are a way to persist data generated by containers in a pod. They allow data to persist even if the pod or the node it is running on fails. Commands for managing volumes include:

  • kubectl get pv: Used to list Persistent Volumes (PVs) in a cluster. PVs are the underlying storage resources that are used to provide storage for pods. 
JLR1bDXNER0FDA BBsj R4H3SOYT76jeqGdvtq0L4 xDW
  • kubectl describe pv: Used to display detailed information about a specific Persistent Volume. This includes information such as the capacity, access modes, and the current status of the PV.
JoCAeI
  • kubectl delete pv: Used to delete a Persistent Volume. This can be useful when you need to reclaim storage resources in the cluster.
vVUUW0c JLnrS49vStIOgkeItNBAX4o9LCT1sXLpPn3dvyuqvZs2u0YaNrFkgnV2aqZ jwwF PC4DLB2Xo aKZPoIzUEOcRxPGRo7v
  • kubectl cp: Used to copy files and directories between containers and the local file system. This command can be used to copy data into or out of a volume, allowing you to persist or retrieve data as needed.
39QSREvhPLzBNol06D0m7cn5aRpz0ZFuEaq3YCjR58 Fy HbsU3 f oRbVi4FOUQOMDFTc 8zR4Bg5R5tC4s08hOlKjU2CVtjdDRtfO06LBicjHqtO2G85fRkmuCMNl27M4zw25eyM SSwSytag3 AY

DaemonSet Commands

DaemonSets are a type of resource in Kubernetes that make sure that a specific pod runs on all nodes in the cluster or on a specific set of nodes. This is useful for running system-level services like monitoring or logging agents. 

Here are several kubectl commands that can be used for managing DaemonSets:

  • kubectl create daemonset: This command kubectl create daemonset creates a new DaemonSet in the cluster and schedules the specified pod to run on every node. The DaemonSet monitors the health of the pod and automatically reschedules it in the event of a failure or a change in the cluster, such as the addition or removal of nodes.
isJ8bep U2VNFNJIN7 uJFCugkRRMmwQTiYJs5FcuV6J8zzKNy5zF OT0uuGjgGUu rJk2VMYVjn6oIfsiBvuGZ4 k4cLoGYht0rU fFZ4siFkvAJoVvV2n0NHAvS F6TvVnOY3MGy483E829scrh8
  • kubectl describe daemonset <daemonset-name>: This command retrieves detailed information about a specific DaemonSet, including its status, events, and the pods it is managing.
  • kubectl edit daemonset <daemonset-name>: This command opens an editor to edit the configuration of a specific DaemonSet.
0XkMNYQz0Ij2wlIl9J9G51gC68e0pbmaUXCoCbeRjt8yIE e5CGSRC8K18XgPUtKlFXCv9CnR0XGKkT2Nh dBa9heyQ2Kxj0hqBriMWJjoHFQ2WM 2VPDduY7UFIWrMjRLE1g7srIwgUJtTtvrA9tmE

Books For Learning Kubernetes Administration:

Conclusion

In conclusion, kubectl is an essential tool for anyone working with Kubernetes. With its simple and straightforward command-line interface, kubectl provides a convenient way to manage and interact with a Kubernetes cluster and its resources, including pods, services, deployments, and DaemonSets. 

Whether you are a seasoned Kubernetes administrator or just starting out, this kubectl guide provides a comprehensive overview of the basic concepts and commands you need to know to become a Kubernetes pro. Whether you’re looking to manage resources, monitor the state of your cluster, or perform complex operations, kubectl provides the means to do so, making it an indispensable tool for anyone working with Kubernetes.

Author Bio: Gilad David Maayan

4YnkFj0Dh3bhN6Z S94XbqVOJp90qxE9 clxS4wBvxvIOJ9MeAr6AFCrWcbcUxF4Uws8X5b6NfH0 A7wPNk1jTApGS3mw9RSlosclqXyyyPY1boVE8McP4NwaW5IveMk9e1kOYmsgEIf lTEBBhi51E

Image Source

Gilad David Maayan is a technology writer who has worked with over 150 technology companies including SAP, Imperva, Samsung NEXT, NetApp and Check Point, producing technical and thought leadership content that elucidates technical solutions for developers and IT leadership. Today he heads Agile SEO, the leading marketing agency in the technology industry.

LinkedIn: https://www.linkedin.com/in/giladdavidmaayan/

LEAVE A REPLY

Please enter your comment!
Please enter your name here