10 Must-Know Kubectl Commands for Kubernetes Administrators

Kubernetes is a powerful container orchestration platform that enables administrators to easily manage, deploy, and scale their applications. As such, Kubectl, the command-line tool used to interact with Kubernetes clusters, is a powerful tool for Kubernetes administrators to master.

In this article, we will discuss 10 must-know Kubectl commands that every Kubernetes administrator should be familiar with.

 

Must-Know Kubectl Commands

 

10 Must-Know Kubectl Commands for Kubernetes Administrators

 

1. kubectl get

The “kubectl get” command is the most basic Kubectl command that is used to retrieve information about Kubernetes objects. It can be used to print out the details of a single object, such as a Pod or Service, or a list of objects, such as ReplicaSets or ConfigMaps.

The command is structured as follows:

kubectl get <object-type> <object-name>

For example, to get the details of a Pod with the name “nginx”, you would run the following command:

kubectl get pod nginx

Example Output of the command

controlplane ~  kubectl get pod nginx
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          18s

 

2. kubectl describe

The “kubectl describe” command is used to get detailed information about a Kubernetes object. It can be used to get more information than the “get” command, such as the status of the object, labels, and events.

The command is structured as follows:

kubectl describe <object-type> <object-name>

For example, to get detailed information about a Pod with the name “nginx”, you would run the following command:

kubectl describe pod nginx

Example Output of the command

controlplane ~  kubectl describe pod nginx
Name:             nginx
Namespace:        default
Priority:         0
Service Account:  default
Node:             node01/10.53.211.9
Start Time:       Fri, 03 Feb 2023 15:56:39 -0500
Labels:           
Annotations:      
Status:           Running
IP:               10.244.1.2
IPs:
  IP:  10.244.1.2
Containers:
  nginx:
    Container ID:   containerd://8e8cc2e3d27231a9394a36044f99c7660e52d42af89c662dc0f2057428aa9f1e
    Image:          nginx
.................
................
Events:
  Type    Reason   Age    From     Message
  ----    ------   ----   ----     -------
  Normal  Pulling  5m28s  kubelet  Pulling image "nginx"
  Normal  Pulled   5m22s  kubelet  Successfully pulled image "nginx" in 5.066662663s (5.066679715s including waiting)
  Normal  Created  5m22s  kubelet  Created container nginx
  Normal  Started  5m22s  kubelet  Started container nginx

 

3. kubectl create

The “kubectl create” command is used to create new Kubernetes objects. It can be used to create Pods, Services, Deployments, namespace, and more. You can use this command to directly create objects directly or from YAML files.

The command is structured as follows:

kubectl create <object-type> <object-name>
kubectl create -f <filename>

For example, to create a namespace called “myns” directly and create a pod niginx from a configuration file named “nginx.yaml ”, you would run the following commands:

kubectl create namespace myns
kubectl create -f nginx.yaml

Example Output of the command

controlplane ~  kubectl create namespace myns
namespace/myns created

controlplane ~  kubectl create -f nginx.yaml 
pod/nginx created

controlplane ~ kubectl create deployment nginx --image=nginx
deployment.apps/nginx created

 

4. kubectl edit

The “kubectl edit” command is used to edit existing Kubernetes objects. It can be used to change the configuration of an existing object, such as the labels or environment variables of a Pod or Service. You can also use this command to directly change some parameters of deployments.

The command is structured as follows:

kubectl edit <object-type> <object-name>

For example, to edit a deployment with the name “frontend”, you would run the following command:

kubectl edit deployments frontend

controlplane ~  kubectl edit deployments frontend
deployment.apps/frontend edited

 

Kubernetes Interview Questions for Beginners [Answers Included]

 

5. kubectl delete

The “kubectl delete” command is used to delete existing Kubernetes objects. It can be used to delete Pods, Services, Deployments, and more. The command is structured as follows:

kubectl delete <object-type> <object-name>

For example, to delete a deployment with the name “frontend” or to delete a pod named “nginx”, you would run the following command:

kubectl delete deployments frontend
kubectl delete pod nginx

controlplane ~  kubectl delete deployments frontend-deployment 
deployment.apps "frontend-deployment" deleted

controlplane ~  kubectl delete pod nginx
pod "nginx" deleted

 

6. kubectl logs

The “kubectl logs” command is used to view the logs of a container in a Pod. It can be used to view the output of a specific container or to follow the logs of a container in real-time. The command is structured as follows:

kubectl logs <pod-name> -c <container-name>

For example, to view the logs of a container named “nginx” in a Pod named “nginx”, you would run the following command:

kubectl logs my-pod -c my-container

controlplane ~  kubectl logs nginx -c nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/02/03 21:13:31 [notice] 1#1: using the "epoll" event method
2023/02/03 21:13:31 [notice] 1#1: nginx/1.23.3
2023/02/03 21:13:31 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/02/03 21:13:31 [notice] 1#1: OS: Linux 5.4.0-1093-gcp
2023/02/03 21:13:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/02/03 21:13:31 [notice] 1#1: start worker processes
...........
...........

If you only have one container in the pod, then you can skip the “-c” option.

 

7. kubectl exec

The “kubectl exec” command is used to execute a command in a container in a Pod. It can be used to run any commands such as “uname”, “ls” or “cat” to view system information, and the contents of files, or view the environment variables of a container.

The command is structured as follows:

kubectl exec <pod-name> -c <container-name> -- <command>

For example, to view the system information of a container named “nginx” in a Pod named “nginx”, you would run the following command:

kubectl exec my-pod -c my-container — env

controlplane ~  kubectl exec nginx -c nginx -- uname -a
Linux nginx 5.4.0-1093-gcp #102~18.04.1-Ubuntu SMP Sat Oct 29 06:35:49 UTC 2022 x86_64 GNU/Linux

controlplane ~  kubectl exec nginx -- uname -a
Linux nginx 5.4.0-1093-gcp #102~18.04.1-Ubuntu SMP Sat Oct 29 06:35:49 UTC 2022 x86_64 GNU/Linux

 

8. kubectl label

The “kubectl label” command is used to add or remove labels to Kubernetes objects. Labels are used to organize and identify objects and can be used to filter objects based on their labels.

The command is structured as follows:

kubectl label <object-type> <object-name> <label-name>=<label-value>

For example, to add a label with the name “env” and the value “prod” to a Pod with the name “nginx”, you would run the following command:

kubectl label pod my-pod env=prod

controlplane ~ kubectl label pod nginx env=prod
pod/nginx labeled

To remove the label from the pod using kubectl command, you can run kubectl label pod nginx env-.

controlplane ~  kubectl label pod nginx env-
pod/nginx unlabeled

 

9. kubectl scale

The “kubectl scale” command is used to scale the number of replicas in a Deployment or ReplicaSet. It can be used to increase or decrease the number of replicas in a Deployment or ReplicaSet.

The command is structured as follows:

kubectl scale <deployment-name> –replicas=<number-of-replicas>

For example, to scale a Deployment named “my-deployment” to 5 replicas, you would run the following command:

kubectl scale my-deployment–replicas=5

controlplane ~ kubectl scale deployment my-deployment --replicas=5
deployment.apps/my-deployment scaled

 

10. kubectl apply

The “kubectl apply” command is used to apply changes to existing Kubernetes objects or create new ones. It can be used to change the configuration of existing objects, such as the labels or environment variables of a Pod or Service.

The command is structured as follows:

kubectl apply -f <filename>

For example, to apply changes from a configuration file named “my-config.yaml” to a Pod with the name “my-pod”, you would run the following command:

kubectl apply -f my-config.yaml

controlplane ~ kubectl apply -f nginx.yaml 
pod/nginx created

 

Kubernetes administrators must be familiar with Kubectl commands in order to effectively manage their clusters. In this article, we discussed 10 must-know Kubectl commands for Kubernetes administrators. From the “get” command to the “apply” command, these commands are essential for any Kubernetes administrator to know.

If you would like to see more such Kubernetes tutorials, please subscribe to our free newsletter.

Buy me a coffeeBuy me a coffee

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.