kubernetes commands

9 important Kubernetes Commands – kubectl commands

Last Updated on March 4, 2023 by cscontents

Introduction

Kubernetes is one of the most popular container orchestration platforms. Kubernetes was originally developed by Google, but currently, it is maintained by CNCF (Cloud Native Computing Foundation).

If you would like some detailed high-level information about Kubernetes, please head over to the below article.

Introduction to Kubernetes | Background of Kubernetes

In this article, we will see some important commands using which we can interact with the Kubernetes cluster. Now, when we speak about commands, we need to have some CLI tool that provides us the interface to execute the commands. In the case of Kubernetes, the CLI tool is kubectl.

So, we will be discussing some important kubectl commands which can be used to interact with the Kubernetes cluster.

Kubectl Command Syntax

It is very important to understand and know the syntax of kubectl commands. If we understand the syntax it will be very simple for you to remember any command.

kubectl [command] [TYPE] [NAME] [flags]

In the above syntax –

  • command: It specifies the operation which we want to perform.
    • For example: create, get, delete, describe, logs,
  • TYPE: It specifies the Kubernetes resource type. The spelling of this TYPE is case-insensitive. And we can mention singular, and plural forms.
    • For example, pod, deployment, service,
  • NAME: It specifies the Kubernetes resource name. These names are case-sensitive.
    • For example, myapp-deployment, myapp-service, etc.
  • flags: It specifies any optional flags.

We have understood the syntax of the kubectl commands. Now we will see some of the commands.

kubectl commands

Below are the kubectl commands.

kubectl command to create any resource: kubectl create/apply

There are two commands for creating any resource.

  • kubectl create – kubectl create -f <manifest file (YAML)>
  • kubectl apply – kubectl apply -f <manifest file (YAML)>

Example using kubectl create:

kubectl create -f myapp-pod.yaml
kubectl create -f my-deployment.yaml
kubectl create -f my-service.yaml

Example using kubectl apply:

kubectl apply -f myapp-pod.yaml
kubectl apply -f my-deployment.yaml
kubectl apply -f my-service.yaml

kubectl command to update any resource: kubectl apply

To update any existing resource, we need to use the ‘kubectl apply’ command.

For example, you have updated the manifest file (YAML file) of a resource. Now, to apply the changes we can run this ‘kubectl apply’ command.

Example:

kubectl apply -f myapp-pod.yaml
kubectl apply -f my-deployment.yaml
kubectl apply -f my-service.yaml

kubectl command to get any resource: kubectl get

To get any resource from the Kubernetes cluster we need to use the ‘kubectl get’ command.

For example,

kubectl get pod
kubectl get po
kubectl get pods

We can use the short form for resources.

pod (s) po
deployment (s) deploy
replicaset (s) rs
service (s) svc
daemonset (s) ds
namespace (s) ns
persistentvolume (s) pv
persistentvolumeclaim (s) pvc

kubectl command to describe any resource: kubectl describe

To describe or to see the detail of any resource that is running in the Kubernetes cluster we need to use the ‘kubectl describe’ command.

Example:

kubectl describe pod my-sample-pod
kubectl describe deploy my-sample-deploy
kubectl describe svc my-sample-svc

kubectl command to edit/modify/update any running resource: kubectl edit

To edit//modify/update any running resource that is running in the Kubernetes cluster we need to edit its configuration/manifest file (YAML file). To do this we need to use the ‘kubectl edit’ command. This command will open up the YAML configuration file in the default test editor and you can do the changes & save the file to apply the changes.

This ‘kubectl edit’ command can be used to update/modify any Kubernetes object like deployments, pods, config maps, etc.

Example:

kubectl edit deployment my-sample-deployment

The above command will open the configuration of ‘my-sample-deployment’ in the default text editor. And once you update the configuration file, it will override the existing configuration.

It is recommended not to use the ‘kubectl edit’ command directly to modify/update resources in the production cluster. Instead, you can create/update a separate configuration file & use the ‘kubectl apply’ command and at first, apply it in a test environment.

kubectl command to check logs of any resource: kubectl logs

To check or print the logs of any Kubernetes resource we need to use the ‘kubectl logs’ command.

Example:

kubectl logs my-sample-pod -c my-container

This command will show the logs of the container with the name ‘my-container’ that is running inside my-sample-pod.

kubectl command to run commands inside a container: kubectl exec

To execute any command inside a container that is running in a pod we need to use the ‘kubectl exec’ command. This command can be used to troubleshoot issues with that pod.

kubectl exec my-sample-pod -c my-container -- ls /

The above command will execute the ‘ls /’ command in the container with the name ‘my-container’ that is running inside my-sample-pod.

kubectl command to delete any resource: kubectl delete

To delete any resource that is running in the Kubernetes cluster, we need to use the ‘kubectl delete’ command. It is used to delete any pods, deployments, services, etc.

Example:

kubectl delete pod my-sample-pod
kubectl delete deploy my-sample-deployment
kubectl delete svc my-sample-svc

kubectl command to scale any deployment: kubectl scale

To scale the number of replicas in a deployment we need to use the ‘kubectl scale’ command. This command can be used to increase/decrease the number of replicas in a deployment.

Example:

kubectl scale deployment my-sample-deployment --replicas=4

The above command will scale the number of replicas in the deployment with the name ‘my-sample-deployment’ to 4 replicas.

e.g., If the number of existing replicas is 2, then you can upscale it to 4. If the number of existing replicas is 4 then you can downscale it to 2. This is just an example, you can upscale/downscale according to your requirement.

 

Thank you.

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.