5 kubectl Commands Everyone Should Know

4 Min. Read

Kubectl commands are fundamental to managing Kubernetes clusters, allowing interaction and control over various cluster components. In this article, we will dive into these powerful tools to enhance your Kubernetes knowledge and expertise.

By the end of this article, you’ll have valuable insights into managing Kubernetes clusters using imperative object configuration and YAML files, leveraging essential kubectl commands such as apply, exec, and delete service, among others.

What Is kubectl?

kubectl is a command-line tool that allows you to interact with Kubernetes, an open-source container orchestration platform. Kubernetes enables developers and IT operations teams to deploy, scale, and manage containerized applications across clusters of machines.

Kubectl is essential for managing Kubernetes clusters, offering commands to create, update, and delete resources within the cluster, and providing insights into its components. It also assists in troubleshooting issues by providing information about the state of your cluster’s components.

kubectl Cheat Sheet: Must-Know Commands

The “kubectl Cheat Sheet” is a helpful resource for quickly accessing essential commands and their options when working with Kubernetes clusters using the ‘kubectl‘ command-line tool.

kubectl Cheat Sheet: Must-Know Commands
kubectl Cheat Sheet: Must-Know Commands

#1 Namespaces

Namespaces are crucial in clusters. They provide a way to organize and isolate different resources your application may need. Typically, you are in the default namespace, and the resources Kubernetes needs for its functioning are in the kube-system namespace.

Here are some commonly used commands related to namespaces:

* kubectl get namespaces: Lists all namespaces you have created.

* kubectl create namespace <namespace name>: Creates a new namespace. It does not set the new namespace as the default, so when creating resources, you would still need to use the –namespace flag.

* kubectl config set-context –current –namespace=<namespace name>: Changes the default namespace for all subsequent commands while maintaining the same context. Remember that the default’s namespace is called ‘default’ if you want to switch back.

* kubectl delete namespace <namespace name>: Deletes the specified namespace and all resources within it! Resource deletion is asynchronous, so the namespace may be in a “Terminating” state for a while before it is entirely removed.

#2 Nodes

Nodes make up your cluster, so it’s common to interact with them. Here are some helpful commands related to nodes:

* kubectl get nodes: Displays a list of all nodes, their roles (control plane, worker, etc.), and the Kubernetes version they’re running.

* kubectl top node <node name>: Shows the CPU and memory usage of a specified node.

* kubectl cordon/uncordon node <node name>: Cordoning nodes mark them as unschedulable, meaning no pods will be placed on that node. This is useful for isolating a node during updates or debugging.

* kubectl drain node <node name>: Drains a node by marking it unschedulable and deleting all running pods. If those pods are managed by a Deployment, DaemonSet, etc., they will be recreated on the remaining nodes.

#3 Pods, Deployments, and Secrets

A set of commands are commonly used when interacting with various Kubernetes resources. Replace ‘X’ with the resource name, and you can use these:

* kubectl get X: Lists all resources of the specified type in the current namespace.

* kubectl create X <name> <other properties>: Creates the specified resource in the current namespace with the specified name and properties.

* kubectl delete X <name>: Deletes the specified resource with the given name in the current namespace.

* kubectl describe X <name>: Provides details and events related to the specified resource, often used for debugging.

* kubectl edit X <name>: Enables editing a specified resource directly without dealing with YAML files. Remember to update your YAML files after editing, as changes don’t persist if the resource is deleted.

#4 Storage Volumes

Here are some of the most commonly used kubectl commands for managing volumes in a Kubernetes cluster:

* kubectl get pv: Retrieving information about Persistent Volumes (PVs).

* kubectl describe pv X: These commands will retrieve detailed information about the specified PV, including its configuration, status, and associated events.

* kubectl delete pv X: These commands will delete the specified PV from your cluster.

* kubectl cp: This command is used to copy files and directories between a pod and the local file system. For example, you can use this command to copy data from a volume mounted in a pod to your local file system for backup or analysis.

#5 DaemonSets

Kubernetes DaemonSets make sure that each specific node in your cluster runs at least one copy of a particular pod. This feature enables you to manage distributed processes across your system more effectively. Additionally, you can run sidecar services (such as storage, log collection, and monitoring) within these nodes to enhance your observability.

To create a new DaemonSet with a custom name, use the following command:

kubectl create daemonset <daemonset_name>

You can define DaemonSets using a configuration file and use the apply command to apply any configurations.

If you want to dive deeper, see a more extensive kubectl cheat sheet with all the commands you need to know to operate Kubernetes.

Best Practices for Using kubectl

In this section, we cover some best practices for utilizing kubectl – a command-line interface (CLI) tool that helps you manage Kubernetes clusters effectively.

Best Practices for Using kubectl
Best Practices for Using kubectl

By adhering to these guidelines, you can optimize your workflow and efficiency when working with Kubernetes.

// See Also: 5 threats facing your Kubernetes deployments today.

Leveraging Context and Namespaces

Namespaces allow you to organize resources within a Kubernetes cluster, providing separation between different environments or applications running on the same cluster. You can combine namespaces with context – an entry in your kubeconfig file containing specific namespace connection information for a cluster.

To set the current context’s namespace, run this command:

kubectl config set-context --current --namespace=<insert-namespace-name-here>

This enables you to interact with resources in that namespace without specifying it every time you execute a command.

Employing Label and Field Selectors

By using labels and field selectors, you can filter resources based on their metadata or attributes. This helps simplify the process of filtering resources only to those that are relevant in a given situation.

Handling Resources with YAML or JSON

It’s recommended to use YAML or JSON files when creating, updating, or defining resources. This approach allows you to apply version control to your resource definitions, making it easier for others to understand your configuration.

Activating the Autocomplete Feature

Enable the autocomplete feature to increase efficiency when using kubectl commands. This feature allows quick completion of command names, options, and arguments by pressing the Tab key.

Updating Your kubectl CLI Regularly

Keep your kubectl CLI up-to-date to benefit from new features, and improvements, and ensure compatibility between your client tools and cluster components. Check the current version of kubectl by running:

kubectl version --client

Conclusion

In conclusion, kubectl is an indispensable tool for managing Kubernetes clusters effectively. It enables users to handle applications and resources, as well as identify and solve potential issues. Kubectl facilitates communication with the API server, allowing operations like application deployment, resource scaling, and problem troubleshooting.

By adhering to best practices such as utilizing namespaces and creating resource manifests, Kubernetes practitioners can ensure efficient and secure deployments.

__
Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

Previous

Employee Security Index Benchmark Report

Getting Started With Microsoft Defender for DevOps – Comprehensive Guide

Next

Let us know what you think, or ask a question...