taikun.cloud

Managing Kubernetes With Kubectl

One of the recommended command-line methods to manage your Kubernetes setup is to use kubectl. With the kubectl command, you can interact with Kubernetes API servers to manage workloads in the Kubernetes infrastructure. 

In this blog, we will cover all aspects of the kubectl command that you would need to get started on managing Kubernetes with it. If you wish to get an overview of Kubernetes, you can read our series of blogs on it starting here.

Let’s start with understanding what kubectl is and how it works with Kubernetes. 

Kubectl basics

Kubectl is a standalone program that interacts with Kubernetes’ API server to get things done. It can help deploy applications, monitor and manage clusters, and view logs. 

Kubectl is installed separately as a standalone program. Let’s understand how kubectl works and interacts with a Kubernetes system.

Kubectl workflow

Kubernetes system as you would know contains master node and worker nodes. The only way to communicate with the Kubernetes infrastructure is the API server. The server is one of the main components of the control plane. It exposes an HTTP REST API layer that allows internal and external components to communicate with each other.  

All requests, including the ones from the kubectl program, are sent as REST requests to monitor and manage the state of all resources within the Kubernetes infrastructure, 

Kubectl interprets the YAML or JSON file which specifies the desired state of Kubernetes setup. The interpreted file is converted into operations that are passed onto the API server, which in turn communicates with backend Kubernetes services to achieve the desired state. 

You can learn more about Kubernetes architecture from our blog here. Let’s now see how to install the kubectl tool. 

Installing kubectl

Kubectl tool is available for all major OSes i.e., Linux, macOS and Windows. The installation is quite straightforward. 

You can install kubectl on Linux and Mac using package installers like curl or brew. You can use package installers like chocolatey and PowerShell for Windows.

Detailed instructions for installation are available in the official docs

Configuring kubectl with kubeconfig

To access the Kubernetes cluster resources, kubectl uses a configuration file called as kubeconfig. The file is usually kept in the user’s home directory under .kube directory. 

The kubeconfig file (default location: $HOME/.kube/config) contains the user ID and password information that helps kubectl in authentication for cluster access. It also contains information about clusters, users, and namespaces.

Here is a sample kubeconfig file:

Kubeconfig file also sets a context element that helps group all access parameters under a simple name. The file also sets a current context to communicate with the cluster. 

The file is loaded on the tool before the kubectl commands are executed. If you wish to view the current kubeconfig being used by the kubectl command, you can run the below command to view it:

$kubectl config view

If you wish to use a separate kubeconfig file for the kubectl tool, you can do that with –kubeconfig flag. The other way is to set an environment variable $KUBECONFIG which is picked up by the kubectl tool if nothing else is specified. 

Kubectl syntax

kubectl command syntax is as follows:

[command] part refers to the action you wish kubectl to perform. For eg. create, delete etc. 

[type] part refers to the type of resource the action needs to be performed on. This could be a pod, service or others. 

[name] part specifies the name of the resource being acted on.

[flags] are optional parameters used when you want to specify additional attributes to 

Command. For eg. as discussed earlier, if you wish to set a specific kubeconfig for the command, you can do that by using the flag –kubeconfig

With the fundamentals of kubectl out of the way, let us now see how to use this command to manage your Kubernetes setup and perform common operations. 

Essential kubectl commands

Kubectl apply

When you wish to apply a YAML or JSON file to change the state of Kubernetes resources, you can use kubectl apply command. The YAML/JSON file will contain the desired state of the objects in focus.

$ kubectl apply -f <YAML/JSON file>

Sample commands: 

You can get more details of kubectl apply command from here.

Kubectl create

Create command is similar to apply command, but it creates a resource from the YAML file passed to it. The format is as follows: 

$kubectl create -f <file name>

Here is how is varies from an apply command:

Here is a sample create command: 

The official documentation for create command is here.

Kubectl get

If you wish to list one or more resources from the Kubernetes setup, you can use a kubectl get command. The commands follow a simple format: 

$ kubectl get <resource type> <other options>

Here are some of the most common kubectl get commands:

The output for the “get pods” command will be something as follows:

Some of the other popular use cases of get commands are,

To list all services, 

$ kubectl get services

If you wish to list every resource in the Kubernetes system, you can use the following command:

$ kubectl get all –all-namespaces

To list all deployments,

$ kubectl get deployment

Whenever any resources have a change of state or encounter any error, the event gets recorded. You can view all recent events in the system with the below get command:

$ kubectl get events

If you wish to see events from all namespaces, 

$ kubectl get events –all-namespaces

You can get more details about kubectl get here.

Kubectl describe

If you wish to see the details for a Kubernetes resource, kubectl describe can get you that information. It can take a type of resource or a particular resource name as a parameter. 

Here are some examples of describe command:

Official reference documentation for kubectl describe is here.

Kubectl delete

If you wish to delete a certain Kubernetes resource from the infrastructure, kubectl delete command can be used. The command can take a file, resource type, or name as a parameter. 

Here are some examples of the command:

You can get more information about delete command here.

Kubectl exec

There may be some instances when you wish to execute a certain command inside a container. Exec command can help you in such instances. 

Here is some examples of the exec command:

Kubectl exec command’s reference documentation is here.

Kubectl logs

During debugging issues in a Kubernetes setup, you may need to view logs of containers in a pod. Logs command will help you with that.

Other options of logs command can be found here.

Getting help for kubectl command

There are many resources that can help you find the right command for any use case. We suggest you start with using -h flag in the command to get immediate help on the command you are trying. 

If you need more extensive help, the official reference documentation for kubectl command is the best option. It describes every option and commands that kubectl tool offers. 

Another great resource to learn more about kubectl and how to use it with other tools, there is a great resource by the Linux Foundation.

Minikube

Since kubectl tool is a powerful tool, you may want to first try this tool in a sandbox before using it in any of your cloud setups. You can do this using minikube. Minikube is a local Kubernetes setup that lives in your machine. So, you can easily practice all commands and get comfortable with them. 

Taikun – a better way

Kubectl is a very powerful command-line tool for managing Kubernetes. But not everyone, is comfortable with using command-line for managing huge Kubernetes infrastructure. This is where Taikun can help. 

Taikun – Best Kubernetes management UI

Taikun is an intuitive dashboard that works across cloud systems – public, private, or hybrid. It helps in managing and monitoring your Kubernetes clusters with a few clicks. It can help you do everything that a kubectl command can do but also take Kubernetes management to the next level. 

Taikun abstracts the low-level commands into simple workflows that help teams manage complex cloud infrastructures without hiring highly technical engineers. You can read about how Taikun can help in Kubernetes cluster management in our usecases here.

Taikun is the Kubernetes engineer that you always wanted to be a part of your team. It saves you time and cost that you would otherwise spend in hiring expensive resources but also helps you optimize your costs by maximizing returns from the cloud resources. 

Knowing kubectl command helps but don’t miss out on using Taikun UI to get ahead in your cloud game quickly.