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

78wU6DhPnAl MQWVn5cuWa8hWjqPiTpBCS5CIbN OmgHzTL8o22G9RFbF ql5VxiCdNGVg3vqXZTinC6xqoHYJP5M ClqwqsnjN jYZ7 q5QuOpLxEL4jG phDnH7B2tv PSfEn5zfSka1eMGRN2

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.  

21qcNdUINDmTToJs UJEAnGpl xS4yrDCYJNjyVHxfdwkL7ruqtwAsXDWGe9sY9inIARveefbCSIrPekm AP6So4QisSrpt3boqZ9b Z89Lnn8Nj9u3vWiLJWy0sr34wm2DZMCAOf6WqluKwtYg2VrPfVsNcIFYF1jobz5B jE2 8BB630 jBEg3dwM1yw

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.

E0QDzbTtG7dhsVAH S3dHCvpf8eE686u3UWvfX9 5w3eZcafH5jWZmWrL982R8KXOB8Z1D fFoYyZcGurNxWGVp7cLC5NXItZdGC

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:

a4esq9muP4yhS0FlCtVzTKxdwCUpRaKk2z 3dLZvh8 LQfsKaibcXnvYRSgIPbrClHymgBZxQRLY2 C6AP1hLZS0 0yBnvOwD8h HF7MhphU MfKMCtahVwS9VUrLuFhMxjg4UJ4kWx UqfvxZiV Bo7w7F6DFwnRzCAsxl0pSEYRr n3B8n6j7zXrGQ

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:

hnYnb2XTTvA0kpdmxqujKktQtTfXe6CRxe6At92OzC30wO2QaBMwhNj7JmhkQrbslml47kzWoVy7DE19oq3QtVRkMVqPJFIhk0WPxDrEDlrQDVTn7Mohwb8wMUv1A 3y1FsWSfk jA

Here is a sample create command: 

FWNLjjiCi11IcLv qnXyeC ar4ktKovDhXUVkpVYAH9JdFz3cPmFbp5m t0i7XR1nwTVz7QRUMU epiE6Ah0zRyXm9rTzU34sWhtXStpvOnH zMt1EYPs4O0emQcRPFav2qJxoOQ7HsUOPziM6gMbn0KqA lg 8LpNSNvvMVq5aO2JhwKlfd5q8SVYUiw

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:

xP2HYnNjCtAsdG7YZkSkgydCb fvlVPBid47aOI7kmyVyvsARGMNdPZS93yrTbOr9dUtq60gn1FSVRZF

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

90FMye3c0uaHk OOnDo3ggqXqekXp1HRz6pK2R9AQmq59SiQp1j7eAv pQHXyvAmkzCx0iNbRiq01Xt7JGixsc48mXq9RuUz8OZCG qJ5nS mNFzukaXnzi885K4eINXml7rtqzX5fcz4 4igpK52il8nyznHWy CEVJC UlUvdaP4y4PXjMiuQvGRm99g

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:

IKAD7uOwpfd3n32w LkPZDk33D BnYB3eGu8XTKDSga6TWEyN0ap4reWf8nDXb2fF

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:

Xlo hjXmmFhs6 BifF8nWpVTOqojZJXrl9z7XQzjaEaRwaTRVG5 B wNAZYiClK3tMPRkKuQLJ982YREvL6sCn fSWPG19q2375nH8psL49aJWoeHwvQPWrCFbKzoER9xW9kdg8TWUA8K0tO lDS2jamLKyGeb Y

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:

8613TlLUc3dPKu IQpJNYSF5dmcAgq5XcHyEiQIrUkXzsho s8rwq1JPdBubeAbjsy3F ORV4RULzRtphSfsscz3 X7npAY4EYi6WL4fSJ8YnyjmAgxDUNMIwzEmEAy6WJsYcnKUkJKgTUIwjbYAS7Bavidj7YqPeEzqS7V22fOnU 6U7aKh2i7qvZ gYQ

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.

I1d3YDJUGpm0IJ ad1Qow5hMLlIwe5qNxzIpuNVx2QFV0eJWbVEeETn ir7pU3e2I2qC8eAzxfINWcrxcgUxRUnC zgCv qNTBIz VuEQ0fdBfg9RiWG tWMRf0hXkw9c8 77B8xvEnFjMi XWy0g DftgjEOjnpcEEdFXizBH JeGtNXg

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.


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. 

KlBKZCvvjDKwgYyIZPpAhfxHXoo5Q BCFC60zEY46igqEY4SYKRx98N1Uhy0OpBfsAsVmuwqlDU4Xo35olG2W8WG3jggnd2MhyYj7wqDSpSm OdGOnBAIN2lNlGCOxq Wn737Hoe0iSyrcXyOF G94bJTH sD7SNS39DhUdQHfyYv whW hBkXzm8LUwaQ

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.

Try it now for FREE         Schedule a call with us