Essential kubectl Commands for DevOps Engineers
This guide presents a comprehensive collection of the most important and frequently used kubectl commands, explaining how to retrieve version information, manage clusters, list resources, manipulate contexts, create, update, patch, scale, expose, delete, and debug Kubernetes objects, as well as format output and control verbosity, enabling DevOps engineers to efficiently operate Kubernetes clusters.
Get kubectl version
Check the client and server versions of the kubectl tool.
kubectl versionGet cluster information
Retrieve detailed information about the Kubernetes cluster.
kubectl cluster-infoList available Kubernetes API resources
List all top‑level API resources that the API server makes available.
kubectl api-resourcesRetrieve Kubernetes contexts
List all contexts (clusters, users, namespaces) defined in the kubeconfig file.
kubectl config get-contextsSwitch clusters
Switch between different contexts/clusters, useful for managing multiple environments.
kubectl config use-context <context_name>Set default namespace for a context
Set or change the default namespace used by kubectl for a given context.
kubectl config set-context --current --namespace <NAMESPACE_NAME>Create or update resources with kubectl
Create or update Kubernetes resources to match the desired state defined in a YAML file.
kubectl apply -f <file_path>Create resources with kubectl
Create a new resource, e.g., a namespace.
kubectl create namespace <namespace_name>Patch Kubernetes resources
Modify resource attributes using merge, JSON merge, or JSON patch formats (JSON and YAML are accepted). Custom resources do not support merge patches.
kubectl patch (-f FILENAME | TYPE NAME) [-p PATCH|--patch-file FILE]Examples:
# Update node JSON
kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
# Update node YAML
kubectl patch node k8s-node-1 -p $'spec:\n unschedulable: true'
# Strategic merge patch on a node
kubectl patch -f node.json -p '{"spec":{"unschedulable":true}}'
# Patch container image (merge key required)
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
# JSON patch to update container image
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
# Merge patch to scale a deployment
kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'List any resources
List all deployments in the current namespace.
kubectl get deploy -n kube-systemManage deployments
Manage rollout and updates of deployments.
kubectl rollout status deployment/<deployment_name>Describe pod information
Get detailed information about a specific pod.
kubectl describe pod <pod_name> -n <NAMESPACE>View container logs
Retrieve logs from a running container inside a pod.
kubectl logs <pod_name> <container_name> -fExecute commands in a pod
Run a command directly inside a container of a pod.
kubectl exec -it <pod_name> -c <container_name> -- /bin/shScale replicas
Scale the number of replicas for a Deployment, ReplicationController, or StatefulSet.
kubectl scale deployment <deployment_name> --replicas=3Expose Kubernetes resources
Expose a Deployment, ReplicaSet, or Pod as a Service; example uses a NodePort service.
kubectl expose deployment <deployment_name> --type=NodePort --port=<port_number>Delete Kubernetes resources
Delete resources defined in a YAML file or delete them directly by name.
kubectl delete pod <pod_name>Taint nodes in Kubernetes
Add a taint to a node to restrict pod scheduling unless the pod tolerates the taint.
kubectl taint nodes <node_name> key=value:taint_effectMark nodes as unschedulable
Mark a node as unschedulable (cordon) or make it schedulable again (uncordon).
kubectl cordon NODE
kubectl uncordon NODEDrain Kubernetes nodes
Evacuate a node, optionally forcing the operation or specifying a grace period.
# Drain node "foo" even if pods are not managed by a controller
kubectl drain foo --force
# Drain with a 15‑minute grace period
kubectl drain foo --grace-period=900Explain resources
Get documentation for a resource’s schema.
kubectl explain podsList events
List cluster events sorted by creation timestamp.
kubectl get events --sort-by=.metadata.creationTimestampDiff resource configuration
Compare the live cluster state with the state defined in a manifest.
kubectl diff -f ./my-manifest.yamlSet configuration resources
Perform a rolling update of a deployment’s container image.
kubectl set image deployment/frontend www=image:v2Replace resources in Kubernetes
Force replace a resource (delete then recreate), which may cause service interruption.
kubectl replace --force -f ./pod.jsonManage labels
Add, delete, or overwrite labels on resources.
kubectl label pods my-pod new-label=awesome # add
kubectl label pods my-pod new-label- # delete
kubectl label pods my-pod new-label=new-value --overwrite # overwriteEdit resources
Edit any API resource using the default editor or a specified one.
kubectl edit svc/docker-registry
KUBE_EDITOR="nano" kubectl edit svc/docker-registryDebug resources
Create an interactive debugging pod attached to an existing pod or node.
kubectl debug my-pod -it --image=busybox:1.28
kubectl debug node/my-node -it --image=busybox:1.28Run a pod
Launch a single‑instance container or a set of containers.
kubectl run -i --tty busybox --image=busybox:1.28 # interactive shellCopy files/directories to/from containers
Copy files between the local machine and a pod.
kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dirPort-forward to a Kubernetes pod
Forward a local port to a pod port for local access without exposing a service.
kubectl port-forward <pod-name> <local-port>:<pod-port>View resource metrics in Kubernetes
Show resource consumption for nodes, pods, containers, or services.
kubectl top [node | pod | container | service] [NAME | -l label]Format output
Use the -o (or --output ) flag to control the output format of kubectl commands, supporting custom columns, Go templates, JSON, YAML, name only, wide, etc.
# List all images running in the cluster
kubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image'
# Images in the default namespace, grouped by pod
kubectl get pods --namespace default --output=custom-columns="NAME:.metadata.name,IMAGE:.spec.containers[*].image"
# Exclude a specific image
kubectl get pods -A -o=custom-columns='DATA:spec.containers[?(@.image!="registry.k8s.io/coredns:1.6.2")].image'
# Show all metadata fields
kubectl get pods -A -o=custom-columns='DATA:metadata.*'kubectl output verbosity and debugging
Control the verbosity of kubectl logs with --v followed by an integer (0‑9), where higher numbers provide more detailed debugging information.
--v=0 # minimal output
--v=3 # extended information
--v=6 # show requested resource
--v=9 # full HTTP request content without truncationConclusion
Mastering these essential kubectl commands enables efficient management of Kubernetes clusters, facilitating seamless application deployment, scaling, and performance optimization; further exploration of the official Kubernetes documentation and hands‑on practice in a test environment will build confidence for production operations.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.