Essential kubectl Commands for Viewing, Managing, and Debugging Kubernetes
This guide walks you through essential kubectl commands for checking cluster status, inspecting resources, retrieving detailed object information, monitoring logs, managing configurations, labeling, and performing create, update, and delete operations, empowering you to efficiently view, troubleshoot, and control Kubernetes workloads.
1. Viewing
1.1 Check cluster status
# View client and server version kubectl version --short=true # View cluster information
kubectl cluster-info1.2 List resources
# List namespaces kubectl get namespace # List pods (all namespaces, wide output, label selector, watch)
kubectl get pod
kubectl get pod -n kube
kubectl get pod -o wide
kubectl get pods -l app=example
kubectl get pod -w# Show pod details in YAML or JSON
kubectl get pod <pod-name> -o yaml
kubectl get pod <pod-name> -o json# Common resource types
node (node)
po (pod)
ns (namespace)
instance (instance)
svc (service)
cm (configMap)
ds (daemonSet)
deploy (deployment)
ingress
endpoint
pv (persistent volume)
pvc (persistent volume claim)
1.3 Describe resources
# Detailed information (no -o option)
kubectl describe pod <Pod-Name>
kubectl describe deployment <Deployment-Name>
kubectl describe service <Service-Name>
kubectl describe node [node-ip]
kubectl describe pods <Deployment-Name>
kubectl describe pod/<pod_name> -n {namespace}1.4 View logs
# Stream logs (single container) kubectl logs -f <pod_name> # Stream logs for a specific container kubectl logs -f <pod_name> -c <container_name> # Attach to a container (similar to docker attach)
kubectl attach <pod_name> -c <container_name>1.5 Inspect configuration and fields
# Show configuration
# Explain resource schema
kubectl explain pod
kubectl explain pod.apiVersion1.6 Node labels
# List node labels
kubectl get node --show-labels1.7 File transfer
# Copy files between pod and local machine
# Inside pod
kubectl exec -it mysql-478535978-1dnm2 sh
cd /tmp
echo "this is a message from `hostname`" >message.log
cat message.log
# Copy out
kubectl cp mysql-478535978-1dnm2:/tmp/message.log ./message.log
# Modify locally and copy back
echo "information added in `hostname`" >>message.log
kubectl cp ./message.log mysql-478535978-1dnm2:/tmp/message.log2. Creating / Modifying Resources
2.1 Create or apply
# Create a deployment from command line
kubectl create deployment nginx --image=nginx:1.14# Create from a manifest file (once) kubectl create -f my-nginx.yaml # Apply from a manifest file (idempotent)
kubectl apply -f my-nginx.yaml2.2 Replace / patch
# Replace an existing resource kubectl replace -f <yaml_file> # Patch a pod label without deleting the pod
kubectl patch pod rc-nginx-2-kpiqt -p '{"metadata":{"labels":{"app":"nginx-3"}}}'2.3 Edit
# Open resource in editor and replace
kubectl edit po rc-nginx-btv4j2.4 Set resources
# Set CPU and memory limits/requests for a deployment
# Set limits only
kubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi
# Set both limits and requests
kubectl set resources deployment nginx --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi
# Remove resource limits
kubectl set resources deployment nginx --limits=cpu=0,memory=0 --requests=cpu=0,memory=0# Update container image
kubectl set image deployment/nginx nginx=nginx:1.9.1
kubectl set image deployments,rc nginx=nginx:1.9.1 --all
kubectl set image daemonset abc *=nginx:1.9.12.5 Label and annotate
# Add or modify a label
# Add label
kubectl label pods foo unhealthy=true
# Overwrite label
kubectl label --overwrite pods foo status=unhealthy
# Add label to all pods in a namespace
kubectl label pods --all status=unhealthy
# Delete a label
kubectl label pods foo bar-# Add or modify an annotation
# Set annotation
kubectl annotate pods foo description='my frontend'
# Overwrite annotation
kubectl annotate --overwrite pods foo description='my frontend running nginx'
# Delete annotation
kubectl annotate pods foo description-3. Starting Pods
3.1 Run a deployment
# Create and run an nginx deployment
kubectl run nginx --image=nginx:1.16 --port=80 --replicas=1# Verify the pod is running
kubectl get podsSigned-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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
