top of page

Taking Kubernetes for a Spin: Beginner's Guide to kubectl

What Is kubectl?

Kubectl is a command-line interface (CLI) for running commands against Kubernetes clusters. It allows developers and administrators to interact with the Kubernetes API Server, which is the central management entity in a Kubernetes cluster. The name "kubectl" is derived from "kube CTL," where "kube" is short for Kubernetes and "CTL" is short for control.

Kubectl provides a way to manage your Kubernetes objects and resources. Whether you want to create, view, update, or delete Kubernetes pods, services, volumes, or deployments, kubectl has got you covered. It's a swiss army knife of sorts for interacting with Kubernetes, providing the ability to perform a wide range of tasks.

In the following sections we’ll provide a quick start guide to kubectl. See this blog post for a full kubectl cheat sheet for more advanced users.

Installing and Setting Up kubectl

Installing kubectl involves downloading the latest release with the command:

curl -LO$(curl -s

After downloading, we need to make the kubectl binary executable with the command chmod +x ./kubectl.

Next, we move the binary into our PATH with the command sudo mv ./kubectl /usr/local/bin/kubectl.

To test the installation, we run the command kubectl version --client. The output will show the version that has been installed.

Setting up kubectl to interact with your Kubernetes cluster involves creating a kubeconfig file, which provides the necessary details about your cluster, user, namespace, and authentication mechanisms. Usually, when setting up a Kubernetes cluster, the setup process will generate this kubeconfig file for you. For multiple clusters, you can manage different kubeconfig files and switch between them using the KUBECONFIG environment variable.

Basic Operations with kubectl

Creating and Managing Kubernetes Objects

Kubernetes objects like Pods, Services, Deployments, etc., are fundamental building blocks of the Kubernetes system. These objects represent the state of your cluster and kubectl allows you to create and manage these objects.

Creating Kubernetes objects using kubectl often involves writing a configuration file in YAML or JSON format that describes the desired state for the object. For instance, to create a Deployment, you would write a Deployment configuration file and then use the command kubectl apply -f ./my-deployment.yaml to create the Deployment.

Managing Kubernetes objects involves several operations like viewing, updating, and deleting objects. The kubectl commands for these operations are kubectl get, kubectl edit, kubectl delete, respectively.

Viewing and Inspecting Resources

kubectl provides a wide range of commands to view and inspect your Kubernetes resources. The kubectl get command is one of the most used commands. It lists one or more resources of a specific resource type. For example, kubectl get pods will list all the Pods in your current namespace.

For detailed information about a specific resource, you can use the kubectl describe command. This command shows the detailed state of an object, including its specs and status.

Furthermore, you can use the kubectl logs command to fetch the logs of a Pod, which can be crucial for debugging purposes.

Working with Pods and Deployments

Pods and Deployments are two of the most common Kubernetes objects you'll work with. A Pod represents a single instance of a running process in your cluster and can contain one or more containers. A Deployment is a higher-level concept that manages Pods and provides features like scaling and rolling updates.

Creating and managing Pods and Deployments is similar to other Kubernetes objects. You write a configuration file and use kubectl apply -f ./my-pod.yaml or kubectl apply -f ./my-deployment.yaml to create a Pod or Deployment, respectively.

Scaling a Deployment involves updating the number of replicas in the Deployment configuration or using the kubectl scale command. For instance, kubectl scale deployment my-deployment --replicas=3 will scale your Deployment to have three Pods.

Using kubectl for Logs and Debugging

Logs are an essential part of any application. They provide valuable insights into the application's behavior and are crucial for debugging. Kubernetes and kubectl provide several mechanisms for logging and debugging your applications.

The kubectl logs command allows you to fetch the logs of a Pod. For instance, kubectl logs my-pod will fetch the logs of the Pod named my-pod. If your Pod has multiple containers, you can specify the container with -c.

Debugging in Kubernetes often involves interacting with your Pods. The kubectl exec command allows you to run commands in a container. For instance, kubectl exec my-pod -- ls / will run the ls / command in your Pod.

Best Practices for Using kubectl

Like any tool, kubectl is most effective when used correctly. There are certain best practices that can help you get the most out of kubectl.

Use Aliases and Autocompletion

If you're using kubectl frequently, you might find yourself typing the same commands over and over again. This can be tiresome and inefficient. Luckily, kubectl supports aliases and autocompletion.

Aliases are shortcuts for long commands. For instance, you can create an alias "kd" for "kubectl describe". This way, instead of typing the whole command, you just need to type "kd". This can significantly speed up your workflow.

Autocompletion, on the other hand, saves you from having to remember and type out the entire names of your Kubernetes resources. With autocompletion enabled, you just need to type the first few characters of the resource name, and kubectl will automatically fill in the rest. This not only speeds up your work but also reduces the chance of errors.

Prefer Declarative Management for Complex Tasks

While kubectl provides both imperative and declarative ways to manage Kubernetes resources, it's generally recommended to prefer declarative management for complex tasks.

In imperative management, you give kubectl commands to perform actions on resources. For instance, you might use kubectl create to create a new pod, or kubectl delete to delete a pod.

In contrast, declarative management involves creating a file that describes the desired state of the resources, and then using kubectl apply to enforce that state. This approach is more robust and scalable, as it allows you to manage complex systems with many interdependent resources.

Use Labels and Selectors for Efficient Resource Management

Labels and selectors are a powerful feature of Kubernetes that can make your resource management more efficient. Labels are key-value pairs that can be attached to Kubernetes objects, while selectors are expressions that match objects with certain labels.

By labeling your resources, you can easily group and select them based on their labels. For instance, you might label all your frontend pods with "tier=frontend", and then use a selector to select all the pods with this label. This can be incredibly useful for managing large or complex systems.

In conclusion, kubectl is a potent tool for managing Kubernetes clusters, but like any tool, it requires a bit of learning and practice to use effectively. By following the best practices discussed above, you can make your work with kubectl more efficient and enjoyable.

Author Bio: Gilad David Maayan

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.




bottom of page