Kubectl and basic commands

Tram Ho


Kubectl is a Kubernetes administration tool through a command line interface, allowing you to execute commands in the Kubernetes cluster. Kubectl uses the Kubernetes API to interact with the Kubernetes cluster. To be able to use kubectl in the cluster, you will need to do some installation and configuration steps for it, see more here .

Through the kubectl commands, you can deploy applications, monitor, and manage cluster resources. In this article, I will learn with you the most basic commands of kubectl, which are essential for the administration of Kubernetes systems.

Create and manage resources in the cluster

Kubectl create

Assuming you already have a working Kubernetes cluster, you can now deploy your application on top of the cluster. To deploy an application to your cluster, you need to create different types of resources. And with kubectl, you can create almost any type of resource in a cluster. Resources you create can include: namespace (ns), deployment, service, cronjob (cj), job. You can observe some examples below:

Create a new namespace

Create a deployment redis, using image redis

Create a job that prints “Hi, thanhthu”

Kubectl edit

After you have created the necessary resources for your application, most likely during the test, you will then need to update and modify them. Then, we can think of using the kubectl edit command. With this command, you can edit whatever resources are running on your cluster.

For example, when you want to edit the job you just created above, you can use the Kubectl edit deployment/edit command. This job’s config file will then open with your default text editor like so:

You can then edit the necessary configs and save them:

Kubectl delete

When you no longer need a resource, use kubectl to delete them to avoid wasting cluster resources. For example:

Note: One little note is that after a resource is deleted, it cannot be recovered, you must recreate it. So before you delete a resource, be sure to understand that resource and know what you are doing

Kubectl apply

In addition to the above ways to create and edit kubernetes cluster resources, we also have the command kubectl apply . Compared to using kubectl create or kubectl edit , kubectl apply offers more flexibility. kubectl apply manages applications through the Kubernetes resource definition files. The resource definition files in Kubernetes are called manifest files and can be written in yaml or json . It creates and updates the resources in a cluster through running kubectl apply . This is the resource creation and usage method that is often applied in practice.

For example, I have a yaml manifest file named deployment-nginx.yaml that defines a simple deployment like this:

Then, to create the above deployment, I simply executed the command with the kubectl apply (-f FILENAME | -k DIRECTORY) structure kubectl apply (-f FILENAME | -k DIRECTORY) .

To update the resources, we just need to edit the manifest files and reapply them.

Track, monitor and fix errors

When applications deployed on your cluster fail or when the problem arises from your own cluster, how can you find the error and fix them? In such situations, the cluster administrator will often immediately think about using the kubectl commands to find the cause as well as find a solution. Kubectl provides many very useful commands to help us do just that.

Kubectl get

The kubectl get statement is used to get a list of a resource object in the cluster, along with basic information. For example:

In addition, to get information of an object with more complete parameters, we can use the -o wide option, or use the -o yaml option to get information in the form of yaml as follows:

When an error occurs, using kubectl get will help us determine which object the error is located on in the cluster.

Kubectl describe

With the kubectl describe , we can get a detailed description of an object type. The most common use case for this statement is to look at a description of a pod or node in the cluster to see if there is an error described in the Events section or if the resource is limited.

Kubectl logs

While kubectl describe mainly gives you information about what’s happening with the applications running in your pods, kubectl logs provide details about all that is going on in your kubernetes cluster. to a specific pod. You also need to understand this difference to determine whether the error is occurring within your application or on the Kubernetes side.

Kubectl exec

To fix the error, or when the aforementioned commands do not give you the information needed to find the error, you will most likely have to visit a specific container in the pod to fix the problem directly from the container. The structure of the kubectl exec command is as follows:

kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...]

For example:

Note : With a pod containing only 1 container, you do not need to specify the container name in the pod. However, with pods containing many containers, you need to use the -c option to specify the name of a particular container. Also, the kubectl exec command should always accompany the shell you are using in the pod.


Above are the most basic commands I usually use to administer a Kubernetes cluster. In addition, there are many commands and many different options. Kubectl is a powerful and very useful tool for cluster administration, you can read more commands from this Kubectl Cheat sheet . Thank you for watching my post!

Reference source

Share the news now

Source : Viblo