Essential kubectl Commands for Daily Kubernetes Operations
This guide lists the most useful kubectl commands for inspecting cluster health, creating and deleting resources, accessing logs, exposing services, managing labels, scaling workloads, performing rolling updates, rolling back revisions, and copying files between pods and the local machine.
Kubernetes administrators rely on kubectl for routine cluster inspection and troubleshooting. The following commands cover the most common tasks.
Basic cluster information
kubectl version // version information
kubectl get node // node details
kubectl get ns // namespaces
kubectl get cs/cluster-info // cluster health
kubectl get pod -A // all pods in the cluster
kubectl explain pod // pod template YAML
kubectl explain pod.spec // pod.spec template YAML (and others)Creating and deleting resources (usually via YAML)
# Create a simple pod
kubectl run nginx --image=nginx
# Generate a YAML manifest without running it
kubectl run nginx --image=nginx --dry-run -o yaml > nginx.yaml
# Create a deployment (v1.19+ syntax)
kubectl run nginx-deploy --image=nginx --replicas=1 --port=80 // creates a deployment named nginx-deploy
# Explicit deployment creation
kubectl create deployment nginx-dep1 --image=nginx
kubectl create deployment nginx-dep1 --image=nginx -o yaml --dry-run > nginx-deploy.yaml
kubectl create deployment nginx-dep1 --image=nginx --dry-run
# Delete resources
kubectl delete pods myapp // delete a specific pod
kubectl delete -f myapp.yaml // delete via manifest file
kubectl delete deployment --all // delete all deployments (not for older versions)
kubectl delete pods --all // delete all pods
kubectl delete svc svc-name // delete a specific service
kubectl replace -f nginx.yaml // replace configurationViewing details, logs, and entering containers
# Real‑time pod start monitoring
kubectl get pods -w
# Detailed pod information
kubectl get pods -o wide
# Service inspection
kubectl get services // short form: svc
# Log inspection
kubectl logs app-daemon busybox
kubectl logs myapp-pod -c init-myserver
# Exec into a container
kubectl exec -it pods myapp -c nginx -- /bin/bash // -c selects the containerExposing services
# Create an internal ClusterIP service
kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP
# View the actual endpoints
kubectl get ep my-serviceExternal access (NodePort)
# Change a service from ClusterIP to NodePort
kubectl edit svc svc-name // set type: NodePort, then access via nodeIP:port
kubectl get svc // view the mapped port
# Edit a replica set directly
kubectl edit rs myappLabel management and selection
# Show labels on pods
kubectl get pods --show-labels
kubectl get pods -l app --show-labels // filter by label "app"
# Add or overwrite labels
kubectl label pods myapp release=aaa
kubectl label pods myapp release=bbb --overwrite
# Complex label selectors
kubectl get pods -l "release in (aaa,bbb,ccc)"
kubectl get pods -l "release notin (aaa,bbb,ccc)"
kubectl get pods -l 'environment in (production),tier in (frontend)'Dynamic scaling
# Manual scaling
kubectl scale --replicas=5 deployment nginx-deploy
# Automatic scaling based on CPU
kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80Rolling updates
# Update container image in a controller
kubectl set image deployment nginx-dep1 nginx=ikubernetes/nginx:v2 // triggers a rolling updateRollback and rollout control
# Roll back to the previous revision
kubectl rollout undo deployment nginx-deployment
# Roll back to a specific revision
kubectl rollout undo deployment nginx-deployment --to-revision=1
# Pause, check status, view history, restart
kubectl rollout pause deployment nginx-deploy
kubectl rollout status deployment nginx-deploy
kubectl rollout history deployment nginx-deploy
kubectl rollout restart deployment nginx-deploy // restart all pods in the deployment
# Describe a pod to see the applied version
kubectl describe pods <pod-name>Copying files
# Copy from pod to local machine
kubectl cp <pod-name>:/tmp/test.txt ./test.txt -n default
# Copy from local machine to pod
kubectl cp ./test.txt <pod-name>:/tmp/ -n defaultMiscellaneous
# Simple loop to continuously request the app (for testing)
while true; do wget -O - -q nginx; sleep 1; doneSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Linux Cloud-Native Ops Stack
Focused on practical internet operations, sharing server monitoring, troubleshooting, automated deployment, and cloud-native tech insights. From Linux basics to advanced K8s, from ops tools to architecture optimization, helping engineers avoid pitfalls, grow quickly, and become your tech companion.
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.
