Kubectl Commands All Beginners Must Know | by Harsha Vardhan | Mar, 2022

Control Kubernetes Cluster like a Pro

Photo by Domenico Loia on Unsplash

Kubernetes is a container orchestration system that is used to automate the deployment, administration, and scaling of containerized applications such as “Docker.” There is an excessive amount of reliance on cloud infrastructure, with Kubernetes being used to deploy and manage software on the cloud among other things.

Numerous processes and programs can be combined into a single container for improved processing and security, and managing a single container is much simpler when working with multiple containers.

When a new container is launched in Kubernetes, it is automatically added as a Node to the corresponding cluster; this is why it operates in cluster mode. As a result, there is only one machine in your cluster, which is referred to as Node.

Nodes serve as a layer of abstraction. They can store permanent or temporary data in the form of a cache as well as perform other functions. As a result, Kubernetes automates their containerization and administration.

When compared to virtual machines, monitoring and scaling are far more straightforward in Kubernetes. It gives greater flexibility for running applications. Using containers, developers may deploy applications on a variety of platforms, including virtualized private clouds, on-premises servers, and cloud-based infrastructures.

Since Kubernetes provides such a high level of flexibility to consumers and organizations, many organizations want to employ it.

Image source

It is necessary to use a command-line tool called Kubectl to execute commands on Kubernetes. Kubectl runs on the workstation or any other machine where it has been set up.

With the use of commands, developers can execute a wide range of tasks, such as examining or controlling a cluster, deploying apps, or reading logs across a distributed system. Take a look at the following commands that are useful for Kubernetes.

A user can use the kubectl describe command to get more information about a certain resource belonging to a particular group.

kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME)

Using this field, you can describe a file’s name, type, and label. It is possible to receive an error indicating kubelet stopped posting node status, which could be caused by the kubelet process on the node not functioning properly.

To create a resource from a YAML file, a user must use the command listed below. The -f option specifies the name of the file in this command.

kubectl create -f Test.yaml

This command can be used by a user who wants to create an image of a specific type in Kubernetes. A deployment or job is created for the purpose of managing a subset of the newly generated container instances.

kubectl run Name — image=nginx

As we are supplying the name “nginx new” for the instance, this command will start a single instance of nginx with ‘nginx_new’ as the instance name and will look somewhat like this:

kubectl run ngnix_new — image=nginx

In this command, a user can specify additional details, such as a port number, if desired. These arguments can be passed easily, and the resulting command will look something like this:

kubectl run hazelcast — image=hazelcast — port=1111

When we change the name of the image to “hazelcast,” it creates a single instance of hazelcast, which allows the container to be exposed on port 1111.

According, if the end user wishes to duplicate instances of any image, the “replicas” feature can be utilized to do this. In the following command, three instances of “nginx” will be replicated or:

kubectl run nginx –image=nginx –replicas=3

As a result, “kubectl run” can be used to execute a variety of tasks, such as modifying an environment variable and outputting the corresponding API objects without having to create them, which is feasible when using the “ — dry-run” arguments when running this command. In addition, users can pass custom arguments by using the syntax “—arg1> arg2>…argn>”.

Image source

If a user wanted to apply a configuration to a resource based on the name of a file, the user can do so by using the following command:

Kubectl apply -f test.yaml

The -f argument specifies that the file is to be utilized, and the file name “test.yaml” indicates that it is to be used.

The system will create a resource if one does not already exist, and then it will apply the policies to the newly created resource. It accepts files in both JSON and YAML formats.

A relative URL of the file where a user wants configuration changes to be applied can be supplied in place of an absolute URL if the user does not have an absolute URL.

If the user wants to attach to a process that is already executing within an existing container, this command can be utilized:

Kubectl attach <Pod> -c <Container>

The “-c” option is used to indicate the container name, and if the user does not specify the -c option, the first container name will be used by default in this command.

The base command is shown above, and the user can customize it according to his or her requirements. For example, if the user wants to attach “python-container” from pod “100–1000”, the following command will be

Kubectl attach 100–10000 -c python-container

When the user wants to view the information about a Kubernetes cluster It is possible to use the following command.

Kubectl cluster-info

This command will only display the addresses of services and master information for clusters for which the label “kubernetes.io/cluster-service” has been set to “true” by the user.

If the user wants to dump cluster information for the purposes of troubleshooting and diagnosing the cluster, he or she can make a small modification to the base command. The updated command looks like this:

Kubectl cluster-info dump

Using the above command, everything is dumped into “stdout”. If the user wanted to have it dumped into a specific directory, he can specify the “output-directory” parameter with the path, which can be either absolute or relative. The following is an example of a command:

Kubectl cluster-info dump –output-directory=/path/to/cluster-state

“Kubectl cluster-info” is also capable of working with namespaces. The value of the “ — all-namespace” argument is false by default. It will dump all of the name spaces if the “–all-namespaces” argument is set to true; otherwise, it will dump only the name spaces listed below.

Kubectl cluster-info dump –all-namespaces

If a user wants to make changes to the kubeconfig file It is possible to use the command shown below.

Kubectl config <SUBCOMMAND> –<Kubeconfig>

It modifies Kubeconfig files using the “subcommands” parameter. Such as

Kubectl config set current contect MY

This command makes use of the kubeconfig flag, which can only be set once per command. If the flag is not set, the file cannot be loaded. Following that, it determines whether the environment variable is set or not.

If it is, the path is used; otherwise, it is not. It is added to the beginning of the existing file as the first line. If the file does not already exist, the list will be expanded to include the most recent file added.

If the user wishes to use the “kubeconfig” option for a certain kubeconfig file, the parameter is specified as such.

As Kubernetes has gained popularity in the cloud computing sphere, the Kubectl command-line tool has gained popularity as well as it allows users to conduct a wide range of operations.

It is simple to use and users have no difficulty understanding it because there is a large amount of material available on the subject. A large number of users prefer to utilize commands since they are more efficient and allow them to receive output more frequently.

Leave a Comment