Important Kubernetes kubectl Commands Explained with Examples (2026)

Kubernetes is the standard platform in 2026 for orchestrating, scaling, and managing containerised applications in production environments. Learning kubectl commands allows you to interact with Kubernetes clusters, deploy applications, manage resources, and troubleshoot issues effectively.

In our previous tutorial, we explained the most important Docker commands and how containers are created and run. Now, Kubernetes becomes the next logical step for managing and scaling those containers in production.

This guide serves as both a learning resource and a professional reference for developers and system administrators.

Kubernetes kubectl Commands for Cluster Information

kubectl version

This command shows the version of the kubectl tool running on your client and the version of the Kubernetes cluster server. We use it to check compatibility between the client and the server before performing operations.

Example:

kubectl version

kubectl cluster-info

We use this command to display the endpoints of the master services and cluster services. It helps us verify that we are connected to the correct cluster and that the control plane is reachable.

Example:

kubectl cluster-info

kubectl config view

This command prints the merged configuration settings from all configuration files. We use it to see details like clusters, users, and contexts currently configured in our kubeconfig file.

Example:

kubectl config view

kubectl config current-context

This command displays the current context name. We use it to quickly confirm which cluster and user namespace we are currently targeting with our other commands.

Example:

kubectl config current-context

Kubernetes kubectl Commands for Managing Resources

kubectl get

This command lists one or many resources in the cluster. We use it to see pods, services, or nodes and their status, ensuring our applications are running correctly.

Example:

kubectl get pods

kubectl get all

This command lists all resources in the current namespace, including pods, services, and deployments. We use it to get a quick overview of everything running in our project environment.

Example:

kubectl get all

kubectl describe

This command shows detailed state of one or more resources, including events. We use it to troubleshoot issues because it reveals why a resource might not be running or starting correctly.

Example:

kubectl describe pod my-pod

kubectl apply

This command applies a configuration change to a resource by filename or stdin. We use it to create or update resources based on a declarative configuration file, which is the standard way to manage Kubernetes.

Example:

kubectl apply -f deployment.yaml

kubectl create

This command creates a resource from a file or from stdin. We use it when we want to instantiate a new object immediately without using a declarative configuration file approach.

Example:

kubectl create deployment nginx --image=nginx

kubectl delete

This command deletes resources by file names, stdin, resources, or names. We use it to remove pods, services, or other objects that we no longer need in the cluster.

Example:

kubectl delete pod my-pod

kubectl edit

This command edits a resource on the server using the default editor. We use it to make quick changes to a running configuration without manually updating and reapplying YAML files.

Example:

kubectl edit deployment my-deployment

Kubernetes kubectl Commands for Deployments and Apps

kubectl scale

This command scales the number of pods in a deployment or replica set. We use it to increase or decrease the capacity of our application to handle traffic load changes.

Example:

kubectl scale deployment my-app --replicas=5

kubectl set image

This command updates the image of a pod template. We use it to deploy a new version of our application container without taking the service down.

Example:

kubectl set image deployment/my-app my-container=my-image:2.0

kubectl rollout status

This command shows the status of the rollout. We use it to watch the progress of a deployment update to ensure it completes successfully.

Example:

kubectl rollout status deployment/my-app

kubectl rollout history

This command displays previous revisions of a deployment. We use it to see the history of changes we have made to our application over time.

Example:

kubectl rollout history deployment/my-app

kubectl rollout undo

This command reverts a rollout to a previous revision. We use it when a new update causes problems and we need to go back to the last stable version quickly.

Example:

kubectl rollout undo deployment/my-app

kubectl autoscale

This command automatically scales a deployment based on CPU utilization. We use it to ensure our application has enough resources during high traffic periods without manual intervention.

Example:

kubectl autoscale deployment my-app --min=2--max=10--cpu-percent=80

Kubernetes kubectl Commands for Troubleshooting and Logs

kubectl logs

This command prints the logs for a container in a pod. We use it to read the application output and identify errors or debug application behavior inside the pod.

Example:

kubectl logs my-pod

kubectl logs -f

This command streams the logs for a container in a pod. We use it to watch the logs in real time as the application runs, which is useful for debugging live issues.

Example:

kubectl logs -f my-pod

kubectl logs –previous

This command prints the logs for a previous instance of a container if it crashed. We use it to find out why a container terminated unexpectedly.

Example:

kubectl logs --previous my-pod

kubectl exec

This command executes a command inside a container in a pod. We use it to run diagnostic tools or open an interactive shell inside a running container.

Example:

kubectl exec -it my-pod -- /bin/bash

kubectl cp

This command copies files and directories to and from containers. We use it to move configuration files or retrieve log files from our local machine to the pod.

Example:

kubectl cp ./local-file.txt my-pod:/path/in/container/

kubectl attach

This command attaches to a process that is already running inside a container. We use it to interact with the process directly if we need to debug the main application process.

Example:

kubectl attach my-pod -c main-container

kubectl port-forward

This command forwards one or more local ports to a pod. We use it to access a service running on a pod securely without exposing it to the public network.

Example:

kubectl port-forward pod/my-pod 8080:80

kubectl top nodes

This command displays CPU and memory usage of nodes. We use it to monitor the health and resource consumption of our cluster infrastructure.

Example:

kubectl top nodes

kubectl top pods

This command displays CPU and memory usage of pods. We use it to identify which applications are consuming the most resources so we can optimize them.

Example:

kubectl top pods

Kubernetes kubectl Commands for Namespaces and Contexts

kubectl create namespace

This command creates a new namespace to organize resources. We use it to separate different environments or projects within the same Kubernetes cluster.

Example:

kubectl create namespace dev-environment

kubectl get namespaces

This command lists all namespaces in the cluster. We use it to see what logical groups or environments currently exist.

Example:

kubectl get namespaces

kubectl config set-context

This command sets a context entry in kubeconfig. We use it to define which cluster and namespace combination we want to use as a specific context.

Example:

kubectl config set-context --current--namespace=dev-environment

kubectl config use-context

This command sets the current-context in a kubeconfig file. We use it to switch between different clusters or projects quickly.

Example:

kubectl config use-context my-cluster-context

Kubernetes kubectl Commands for Configuration and Secrets

kubectl create configmap

This command creates a config map from a file, directory, or literal value. We use it to store non-sensitive configuration data that our application needs to run.

Example:

kubectl create configmap my-config --from-file=path/to/file

kubectl create secret generic

This command creates a secret from a file or literal value. We use it to store sensitive information like passwords or API keys securely.

Example:

kubectl create secret generic my-secret --from-literal=password=mypass

kubectl get secrets

This command lists secrets in the namespace. We use it to verify that our sensitive credentials have been stored correctly in the cluster.

Example:

kubectl get secrets

kubectl annotate

This command updates the annotations on a resource. We use it to add metadata to objects for documentation or integration purposes without changing the object configuration itself.

Example:

kubectl annotate pod my-pod description='My test pod'

kubectl label

This command updates the labels on a resource. We use it to tag objects so that other commands or selectors can easily identify and group them.

Example:

kubectl label pod my-pod app=frontend

Kubernetes kubectl Commands for Node Maintenance

kubectl cordon

This command marks a node as unschedulable. We use it when performing maintenance tasks to ensure no new pods are scheduled to that specific node.

Example:

kubectl cordon node-1

kubectl uncordon

This command marks a node as schedulable. We use it to bring a node back online after maintenance so that it can accept new pods again.

Example:

kubectl uncordon node-1

kubectl drain

This command evicts all pods from a node in preparation for maintenance. We use it to safely move all workloads to other nodes before rebooting or shutting down a node.

Example:

kubectl drain node-1 --ignore-daemonsets

kubectl taint

This command updates the taints on a node. We use it to control which pods can be scheduled on a node, often for dedicated nodes or master nodes.

Example:

kubectl taint nodes node-1 key=value:NoSchedule

Kubernetes kubectl Commands for Output and Formatting

kubectl get -o yaml

This command outputs the resource details in YAML format. We use it to see the exact configuration of a live object for inspection or backup.

Example:

kubectl get pod my-pod -o yaml

kubectl get -o json

This command outputs the resource details in JSON format. We use it when we need to process the output with other tools that require JSON data.

Example:

kubectl get pod my-pod -o json

kubectl get -o wide

This command outputs the resource with more details, such as the IP address and the node where the pod is running. We use it to get a broader view of our resources.

Example:

kubectl get pods -o wide

kubectl explain

This command documents the fields of a specific resource. We use it to understand the schema and available options for writing our own manifest files.

Example:

kubectl explain pod.spec

Additional Useful Kubernetes kubectl Commands

We cover all essential Kubernetes kubectl commands, but if you are looking for some specific not too much used commands, here is the list for remaining commands.

  • kubectl api-resources – We use this to print the supported API resources on the server, helping us see what objects we can create.
  • kubectl api-versions – This command prints the supported API versions on the server so we can check compatibility with our files.
  • kubectl diff – We use this to show the differences between the current live configuration and a proposed configuration file.
  • kubectl auth can-i – This command checks whether we have permission to perform a specific action on a resource.
  • kubectl auth reconcile – We use this to reconcile rules for RBAC roles, role bindings, and cluster role bindings.
  • kubectl wait – This command waits for a specific condition on one or many resources to be met before proceeding.
  • kubectl proxy – We use this to run a proxy to the Kubernetes API server on our local machine.
  • kubectl replace – This command updates a resource by replacing it entirely using a file or stdin.
  • kubectl patch – We use this to update specific fields of a resource using a strategic merge patch.
  • kubectl certificate approve – This command approves a certificate signing request, allowing a node or user to join the cluster.
  • kubectl certificate deny – This command denies a certificate signing request to reject a node or user.
  • kubectl completion – We use this to output shell completion code for the specified shell like bash or zsh.
  • kubectl plugin list – This command lists all visible plugin executables available on our user’s PATH.
  • kubectl cluster-info dump – We use this to dump relevant information for debugging and diagnosis into a file.
  • kubectl debug – This command creates debugging sessions for troubleshooting workloads and nodes easily.
  • kubectl rollout restart – We use this to restart a deployment without changing the pod template specification.
  • kubectl api-groups – This command prints the available API groups on the server to help us understand the API structure.
  • kubectl convert – We use this to convert config files between different API versions, though it is now deprecated.
  • kubectl create quota – This command creates a resource quota to limit the total resources a namespace can consume.
  • kubectl create serviceaccount – We use this to create a service account for pods to use when interacting with the API server.
  • kubectl create priorityclass – This command creates a priority class to assign importance to pods relative to others.
  • kubectl create rolebinding – We use this to bind a role to specific users, groups, or service accounts at the namespace level.
  • kubectl create clusterrolebinding – This command binds a cluster role to users, groups, or service accounts across the whole cluster.
  • kubectl get events – We use this to see a list of events that show what has happened recently in the cluster.
  • kubectl kustomize – This command builds a kustomization target from a directory or a remote URL.
  • kubectl subresource – We use this to interact with subresources of an object, like status or scale.
  • kubectl alpha – We use this to access commands related to alpha features, which are experimental and may change.
  • kubectl top – We use this to display resource usage (CPU/Memory) for nodes or pods.
  • kubectl config get-contexts – This command displays one or many contexts from the kubeconfig file.
  • kubectl config delete-context – We use this to delete a specified context from our kubeconfig file.
  • kubectl config set-credentials – This command sets a user entry in kubeconfig.
  • kubectl config set-cluster – We use this to set a cluster entry in kubeconfig.
  • kubectl delete all --all – We use this command carefully to delete all resources in the current namespace.
  • kubectl logs --limit-bytes – This command limits the number of bytes returned from the logs to prevent our terminal from crashing.
  • kubectl logs --since – We use this to show logs only since a specific time, like 1 hour ago.
  • kubectl logs --tail – This command shows only the last N lines of logs from the pod.
  • kubectl exec -c – This command specifies a container name to use when a pod has multiple containers.
  • kubectl cp -c – We use this to specify the container to copy files to or from.
  • kubectl label --overwrite – We use this to overwrite an existing label on a resource.
  • kubectl annotate --overwrite – This command overwrites an existing annotation on a resource.
  • kubectl port-forward --address – We use this to specify a specific address to listen on when forwarding ports.
  • kubectl get --sort-by – This command sorts the list of objects returned by a specific field.
  • kubectl get --watch – We use this to watch for changes to specific objects and list them as they occur.
  • kubectl apply --server-side – This command applies a configuration using server-side apply, which is managed by the cluster.
  • kubectl apply --prune – We use this to automatically delete resources that are no longer defined in the applied file.
  • kubectl apply --force-conflicts – This command forces the apply operation even if there are conflicts in field ownership.
  • kubectl get --raw – We use this to print the raw URL response from the server for debugging.
  • kubectl create deployment --replicas – We use this to specify the number of replicas when creating a new deployment.
  • kubectl expose – This command creates a service for a pod, deployment, or replica set to make it network accessible.
  • kubectl run – We use this to run a particular image in the cluster, which is useful for quick testing.
  • kubectl debug node – This command creates a debugging session on a specific node to troubleshoot node-level issues.
  • kubectl create pdb – This command creates a Pod Disruption Budget to limit the number of pods that go down during voluntary disruptions.
  • kubectl get csrs – We use this to list all pending certificate signing requests.
  • kubectl get pv – This command lists all persistent volumes in the cluster.
  • kubectl get pvc – This command lists all persistent volume claims used by pods to request storage.
  • kubectl get sc – This command lists storage classes which define different types of storage provisioned in the cluster.
  • kubectl get endpoints – This command lists the endpoints that implement a service, showing the IP addresses of the pods behind it.
  • kubectl get hpa – This command lists Horizontal Pod Autoscalers which automatically scale applications based on metrics.
  • kubectl get ing – This command lists Ingress resources which manage external access to the services in the cluster.
  • kubectl get ds – This command lists DaemonSets which ensure a copy of a pod runs on all or some nodes.
  • kubectl get sts – This command lists StatefulSets which manage stateful applications with stable network identities.
  • kubectl get jobs – This command lists Jobs which create one or more pods to perform a specific task and ensure completion.
  • kubectl get cronjobs – This command lists CronJobs which run jobs on a time-based schedule.
  • kubectl apply --record – This command records the current command in the resource annotation, which is useful for tracking rollout history.
  • kubectl version --client – We use this to show only the version of the kubectl client tool.
  • kubectl version --output=yaml – This command prints the version information in YAML format for easier parsing.
  • kubectl options – This command lists the global command-line options available for use with kubectl.
  • kubectl help – We use this to display help information about any specific command or the general usage of kubectl.

Conclusion

We hope this comprehensive list of Kubernetes kubectl commands with detailed explanations and simple examples helps you manage Kubernetes clusters with more confidence and clarity, and handle day-to-day cluster operations more effectively.

To go deeper into monitoring and real-world usage, we recommend reading our Application Performance Monitoring in Kubernetes guide. It explains how to track performance, identify issues, and keep Kubernetes workloads healthy.

Reference:
https://kubernetes.io/docs/reference/kubectl/

Aditya Gupta
Aditya Gupta
Articles: 488
Review Your Cart
0
Add Coupon Code
Subtotal