Cloud Native 13 min read

Essential kubectl Commands for DevOps Engineers

This guide introduces the most important and frequently used kubectl commands, explaining how to retrieve version information, manage clusters, list resources, apply configurations, scale deployments, expose services, and debug pods, enabling DevOps engineers to efficiently control Kubernetes environments.

DevOps
DevOps
DevOps
Essential kubectl Commands for DevOps Engineers

This article helps you understand the most important and commonly used kubectl commands required by DevOps engineers, allowing you to navigate and control Kubernetes clusters with ease.

1. Get kubectl version

Check the client and server versions of the kubectl tool.

kubectl version

2. Get cluster information

Collect detailed information about the Kubernetes cluster.

kubectl cluster-info

3. List available API resources

List all top‑level API resources available on the cluster API server.

kubectl api-resources

4. Retrieve Kubernetes contexts

List all contexts (clusters, users, namespaces) defined in the kubeconfig file.

kubectl config get-contexts

5. Switch clusters

Switch between different contexts/clusters, useful for managing multiple environments.

kubectl config use-context <context_name>

6. Set default namespace for a context

Change the default namespace used by a context.

kubectl config set-context --current --namespace <NAMESPACE_NAME>

7. Create or update resources

Apply a YAML manifest to create or update resources to match the desired state.

kubectl apply -f <file_path>

8. Create a namespace

kubectl create namespace <namespace_name>

9. Patch resources

Modify resource properties using merge, JSON merge, or JSON 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 pod image via strategic merge
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'

10. List resources

List all deployments in the current namespace.

kubectl get deploy -n kube-system

11. Check rollout status

Get detailed information about a specific pod rollout.

kubectl rollout status deployment/<deployment_name>

12. Describe a pod

Retrieve detailed information about a specific pod.

kubectl describe pod <pod_name> -n <NAMESPACE>

13. View container logs

Fetch logs from a running container inside a pod.

kubectl logs <pod_name> <container_name> -f

14. Execute commands in a pod

Run commands directly inside a container of a pod.

kubectl exec -it <pod_name> -c <container_name> -- /bin/sh

15. Scale replicas

Scale a Deployment, ReplicationController, or StatefulSet.

kubectl scale deployment <deployment_name> --replicas=3

16. Expose resources

Expose a Deployment as a NodePort service.

kubectl expose deployment <deployment_name> --type=NodePort --port=<port_number>

17. Delete resources

Delete resources defined in a YAML file or by name.

kubectl delete pod <pod_name>

18. Taint nodes

Add a taint to a node to restrict pod scheduling unless the pod tolerates the taint.

kubectl taint nodes <node_name> key=value:taint_effect

19. Cordon/Uncordon nodes

Mark a node as unschedulable (cordon) or schedulable (uncordon).

kubectl cordon NODE
kubectl uncordon NODE

20. Drain nodes

Evict pods from a node, optionally forcing or setting a grace period.

# Force drain
kubectl drain foo --force
# Drain with 15‑minute grace period
kubectl drain foo --grace-period=900

21. Explain resources

Get documentation for a specific resource type.

kubectl explain pods

22. List events

kubectl get events --sort-by=.metadata.creationTimestamp

23. Compare resource configuration

Show differences between the live cluster state and a manifest.

kubectl diff -f ./my-manifest.yaml

24. Set image for a deployment

kubectl set image deployment/frontend www=image:v2

25. Replace resources

Force replace a resource, which may cause service interruption.

kubectl replace --force -f ./pod.json

26. Manage labels

Add, remove, or overwrite labels on pods.

kubectl label pods my-pod new-label=awesome
kubectl label pods my-pod new-label-
kubectl label pods my-pod new-label=new-value --overwrite

27. Edit resources

Open a resource in your preferred editor for manual changes.

kubectl edit svc/docker-registry
KUBE_EDITOR="nano" kubectl edit svc/docker-registry

28. Debug resources

Create an interactive debugging session for a pod or node.

kubectl debug my-pod -it --image=busybox:1.28
kubectl debug node/my-node -it --image=busybox:1.28

29. Run a pod

Start a single‑instance container or a set of containers.

kubectl run -i --tty busybox --image=busybox:1.28

30. Copy files to/from containers

kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dir

31. Port‑forward to a pod

Access a pod locally without exposing it via a service.

kubectl port-forward <pod-name> <local-port>:<pod-port>

32. View resource metrics

Show CPU and memory usage for nodes, pods, containers, or services.

kubectl top [node|pod|container|service] [NAME|-l label]

33. Format output

Use the -o/--output flag to customize the output format (json, yaml, custom columns, go‑template, etc.).

kubectl get pods -o=custom-columns='NAME:.metadata.name,IMAGE:.spec.containers[*].image'

Mastering these key kubectl commands enables efficient management of Kubernetes clusters, facilitating seamless application deployment, scaling, and performance optimization.

CLIKubernetesDevOpsContainer Orchestrationkubectl
DevOps
Written by

DevOps

Share premium content and events on trends, applications, and practices in development efficiency, AI and related technologies. The IDCF International DevOps Coach Federation trains end‑to‑end development‑efficiency talent, linking high‑performance organizations and individuals to achieve excellence.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.