Skip to content

kubectl Commands Explained

The kubectl commands used day to day, grouped by job: read the cluster, inspect workloads, apply and roll back changes, expose services, manage nodes, and create config objects. Each row pairs the command with a realistic invocation.

kubectl is your handle on a Kubernetes cluster. The model is declarative: you usually apply a manifest (apply -f) rather than imperatively creating things, then read back the result (get, describe, logs). The commands below cover daily operation; pair them with a namespace flag (-n name) when you are not in the default namespace.

Reference table · 59 entries
59 of 59 rows
Cluster & context
Show where the control plane and core services run.kubectl cluster-info
List the clusters and namespaces you can switch between.kubectl config get-contexts
Switch to a different cluster or namespace context.kubectl config use-context my-prod
Set the default namespace of the current context.kubectl config set-context --current --namespace=staging
Rename a saved context in your kubeconfig.kubectl config rename-context old-name prod
List every resource type the cluster knows.kubectl api-resources
Print the client and server Kubernetes versions.kubectl version
Show the fields a resource supports, read from the API schema.kubectl explain pod.spec.containers
Check whether the current user may run a given action.kubectl auth can-i delete pods
Inspecting workloads
List pods and their status; add -o wide for node and IP.kubectl get pods -A
List the common resources of a namespace in one call.kubectl get all
Add node and IP columns to any get command.kubectl get pods -o wide
Print a resource as the full YAML manifest behind it.kubectl get deploy/web -o yaml
List deployments and how many replicas are ready.kubectl get deployments
List stateful sets and their ready replicas.kubectl get statefulsets
List batch jobs and scheduled cron jobs.kubectl get jobs,cronjobs
Show events, labels, and status for one pod.kubectl describe pod web-abc
Show a node's capacity, conditions, and running pods.kubectl describe node node-1
Print a pod's stdout; -f to follow, --previous for the last crash.kubectl logs -f web-abc
Run a command inside a running pod.kubectl exec -it web-abc -- sh
Live CPU and memory for pods or nodes (needs metrics-server).kubectl top pods
List recent events such as failed pulls and backoffs.kubectl get events -A --sort-by=.lastTimestamp
List ingress rules and their load balancer addresses.kubectl get ingress
Copy files between your machine and a running pod.kubectl cp web-abc:/var/log/app.log ./app.log
Block until a resource meets a condition.kubectl wait --for=condition=ready pod -l app=web
Start a throwaway pod from an image, no manifest needed.kubectl run debug --rm -it --image=busybox -- sh
Apply, scale, roll out
Show what apply would change, without changing it.kubectl diff -f deploy.yaml
Create or update resources from a manifest file.kubectl apply -f deploy.yaml
Apply manifests and delete objects that left them.kubectl apply -f dir/ --prune -l app=web
Open a live resource in your editor and apply on save.kubectl edit deploy/web
Change a few fields on a live resource.kubectl patch deploy/web --patch-file fix.json
Point a deployment's containers at a new image.kubectl set image deploy/web web=nginx:1.27
Replace an object, deleting and recreating it when needed.kubectl replace --force -f deploy.yaml
Remove the resources declared in a manifest file.kubectl delete -f deploy.yaml
Change the replica count of a deployment.kubectl scale deploy/web --replicas=4
Watch a rollout finish or fail.kubectl rollout status deploy/web
List the revisions a rollout has been through.kubectl rollout history deploy/web
Restart every pod of a deployment, one at a time.kubectl rollout restart deploy/web
Roll a deployment back to its previous revision.kubectl rollout undo deploy/web
Services & networking
List services and their type and cluster IP.kubectl get svc
Show which pod IPs currently back each service.kubectl get endpoints web
Create a service that fronts a set of pods; --type=NodePort or LoadBalancer reaches outside the cluster.kubectl expose deploy/web --port=80
Show the load balancer address behind each ingress (alpha).kubectl ingress status -A
List the policies that restrict pod traffic.kubectl get networkpolicies
Tunnel a local port to a pod or service.kubectl port-forward svc/web 8080:80
Nodes & maintenance
List cluster nodes and their status.kubectl get nodes -o wide
Mark a node unschedulable (no new pods).kubectl cordon node-1
Mark a node schedulable again.kubectl uncordon node-1
Evict pods from a node and mark it unschedulable.kubectl drain node-1 --ignore-daemonsets
Add or remove a taint that repels pods from a node.kubectl taint node node-1 key=value:NoSchedule
Delete a stuck pod at once, skipping graceful shutdown.kubectl delete pod web-abc --grace-period=0 --force
Objects & config
Make a new namespace to scope resources to.kubectl create namespace staging
List namespaces and their status.kubectl get namespaces
Store config keys or files as a cluster object.kubectl create configmap app --from-file=config.yaml
List config maps in the namespace.kubectl get configmaps
Store a password or certificate as a secret.kubectl create secret generic db --from-literal=password=s3cret
List secrets in the namespace.kubectl get secrets
Attach or remove a label on a resource.kubectl label pod web-abc tier=backend
Attach metadata that selectors cannot match.kubectl annotate deploy/web release=july