50+ Commonly Used Kubectl Commands for Managing Kubernetes Clusters

Kubectl is a command-line utility in Kubernetes used to run commands against Kubernetes clusters. You can use it to perform a wide range of tasks, including deploying applications/microservices, viewing and managing resources, inspecting log files, and so much more.

In this tutorial, we feature commonly used kubectl commands for managing a Kubernetes (K8S) cluster.

We are using Minikube on Ubuntu 22.04 as our local Kubernetes cluster.

Creating a Kubernetes Objects / Resources

In Kubernetes, objects are persistent entities that ensure your cluster achieves the desired state. They are the building blocks of your workloads in a cluster. Objects include replicasets, deployments, pods, daemonsets, services, namespaces, configmaps and secrets, and persistent volumes to mention a few.

The most common way of creating objects is using a YAML manifest file usually with a .yaml or .yml file extension.

To create an object from a YAML manifest file, run:

$ kubectl apply -f ./file1.yaml

To create resources from multiple YAML files, run:

$ kubectl apply -f ./file1.yaml  -f ./file2.yaml 

To create resources from all manifest files in a directory run:

$ kubectl apply -f ./dir   

To create resources from a URL, execute:

$ kubectl apply -f https://sample-url.io

Nodes in Kubernetes Cluster

A node is a physical or virtual machine on which pods and other resources that underpin your workload run. Each node is managed by a master node known as a control pane which contains multiple services required to run pods. A cluster typically has multiple nodes.

To get the number of nodes on your cluster, run:

$ kubectl get nodes
List Nodes in Kubernetes Cluster
List Nodes in Kubernetes Cluster

To get the pods running on a node, execute:

$ kubectl get pods -o wide | grep <node_name>
List Pods Running on Kubernetes Cluster
List Pods Running on Kubernetes Cluster

To mark your node as unschedulable, run.

$ kubectl cordon minikube

node/minikube cordoned

To mark your node as schedulable, run.

$ kubectl uncordon minikube

node/minikube uncordoned

To display resource usage metrics such as RAM and CPU run:

$ kubectl top node <node_name>

To delete a node or multiple nodes, run the command:

$ kubectl delete node <node_name>

Cluster Management and Context

A Kubernetes Cluster is a group of nodes or servers that run containerized applications. A node can run one or multiple pods which contain one or more running containers.

To list information about the master.

$ kubectl cluster-info
List Master Node Info
List Master Node Info

To check the kubectl version.

$ kubectl version --short
Check kubectl Version
Check kubectl Version

To display Kubernetes cluster configuration.

$ kubectl config view
View Kubernetes Cluster Configuration
View Kubernetes Cluster Configuration

To display contexts.

$ kubectl config get-contexts
List Kubernetes Contexts
List Kubernetes Contexts

To list available API resources.

$ kubectl api-resources
View API Resources
View API Resources

To list available API versions.

$ kubectl api-versions
View API Versions
View API Versions

Kubernetes Pods

In Kubernetes, a pod is the smallest deployable unit you can create and manage in a cluster. A pod contains a group of containers with shared volumes and network resources alongside instructions on how to run the application.

Pods are ephemeral and disposable, which means that they are short-lived and hence do not last for long periods of time. As such, the best approach to deploy pods is using the declarative configuration using a YAML file.

This is more reproducible and convenient since it can be reused. In addition, it ensures that resources defined in the YAML file are constantly monitored by Kubernetes and running as specified.

Listing / Deleting Pods

To get the list of running pods in your cluster, run the command:

$ kubectl get pods
List Running Pods in Kubernetes Cluster
List Running Pods in Kubernetes Cluster

To display pods alongside their labels, run:

$ kubectl get pods --show-labels
List Pods with Labels
List Pods with Labels

To list all pods in all namespaces, run:

$ kubectl get pods --all-namespaces
List Pods Namespaces
List Pods Namespaces

To list pods in the current namespace and with more details append the -o wide parameter.

$ kubectl get pods -o wide
List Pods Namespaces with Info
List Pods Namespaces with Info

To display the detailed state of a specific pod, run:

$ kubectl describe pod <pod-name>
View State of Pod
View State of Pod

To add a label to a pod, run the command:

$ kubectl label pods my-pod new-label=awesome

To remove the label, execute:

$ kubectl label pods my-pod new-label-
Remove Pod Label
Remove Pod Label

To gain an interactive shell with a container pod run the command:

$ kubectl exec -it <pod_name> /bin/sh

To delete a specific pod, run the following:

$ kubectl delete pod <pod name>

ReplicaSets in Cluster

A ReplicaSet (RS) ensures that there is always a stable number of running pods at any given time. It guarantees the availability of a specific number of pods as defined in the YAML manifest file.

To get the number of ReplicaSets in a cluster, run the command:

$ kubectl get replicasets
View ReplicaSets on Cluster
View ReplicaSets on Cluster

To get more information about a specific replica set, run the command:

$ kubectl describe replicasets <replicasetname>
List ReplicaSets Info
List ReplicaSets Info

Services in Pods

A service is a network abstraction over a pod. Services provide discovery and routing between pods. For instance, a service can connect an application’s front end to its back end, each running in separate deployments.

To list the services run:

$ kubectl get services
List Services in Pods
List Services in Pods

To get more details about service, run the command:

$ kubectl describe services
List Details of Service in Pods
List Details of Service in Pods

Service Accounts in Pods

A service account is an object used by a pod in a cluster to access the Kubernetes API server.

To list the service accounts, run:

$ kubectl get serviceaccounts
List Service Accounts in Pod
List Service Accounts in Pod

To get more detailed information about the service account, execute:

$ kubectl describe serviceaccounts
List Service Accounts Info
List Service Accounts Info

To delete a service account, use the following syntax.

$ kubectl delete serviceaccounts <serviceaccount>

Namespaces

Namespaces are units that provide a means by which a single cluster can be further subdivided into multiple sub-clusters which can then be managed individually.

To list namespaces run:

$ kubectl get namespaces
List Namespaces
List Namespaces

To display detailed information about a namespace use the syntax:

$ kubectl describe namespace <namespace_name>
Get Detailed Info About Namespace
Get Detailed Info About Namespace

To create a namespace run the command:

$ kubectl create namespace <namespace_name>

To edit or modify a namespace run the command:

$ kubectl edit namespace <namespace_name>

To display the resource usage of a namespace run:

$ kubectl top namespace <namespace_name>

To delete a namespace run the command:

$ kubectl delete namespace <namespace_name>

Kubernetes Secrets

In Kubernetes, secrets is an object that holds sensitive & confidential information such as usernames and passwords.

To list existing secrets run the command:

$ kubectl get secret

To list detailed information about existing secrets execute:

$ kubectl describe secrets

To create a secret, run

$ kubectl create secret [flags] [options]

To delete a secret, run:

$ kubectl delete secret <secret_name>

Kubernetes Deployments

A deployment is an object that enables you to declare the desired state of your application. You can define the number of replicas, the image for the pods, the number of pods, and other resources.

To get the number of deployments, run the command:

$ kubectl get deployment
List Kubernetes Deployments
List Kubernetes Deployments

List the detailed state of a specific deployment.

$ kubectl describe deployment <deployment-name>
Check State of Kubernetes Deployments
Check the State of Kubernetes Deployments

To delete a deployment, run the command:

$ kubectl delete deployment <deployment-name>
Delete Kubernetes-Deployment
Delete Kubernetes-Deployment

To create a new deployment, execute the command:

$ kubectl create deployment <deployment-name>

Kubernetes Events

In Kubernetes, an event is an object that is generated in response to a change in the state of a resource such as a pod, container, or node. A good example is the change of a pod’s state from pending to running, or successful to failed.

To list all events, run:

$ kubectl get events
List Kubernets Events
List Kubernets Events

To list events but exclude pod events run the command:

$ kubectl get events --field-selector involvedObject.kind!=Pod
List Events by Excluding Pod Events
List Events by Excluding Pod Events

To list events alongside their timestamps and sorted according to time of creation, run:

$ kubectl get events --sort-by=.metadata.creationTimestamp
List Kubernets Events by Timestamps
List Kubernetes Events by Timestamps

Kubernetes Logs

Logs are critical in recording cluster events and helping in debugging and troubleshooting faults. You can view or configure log verbosity using the following commands:

To print logs for a specific pod run:

$ kubectl logs <pod_name>

To print and also follow the real-time logs for a specific pod execute:

$ kubectl logs -f <pod_name>

To print logs for a pod for the last X hours, say 3 hours, run:

$ kubectl logs --since=3h <pod_name>

To print logs for a specific container inside a pod, run

$ kubectl logs -c <container_name> <pod_name>

Generate the most recent 20 lines of logs

$ kubectl logs --tail=20 <pod_name>

Generate logs of a pod and save the logs in a logfile called pod.log

$ kubectl logs <pod_name> pod.log
Conclusion

That was a general overview of some of the most commonly used kubectl commands for administering a Kubernetes cluster.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.