Master Kubernetes Cluster Management: Essential kubectl Commands Explained
This guide walks you through essential kubectl commands for viewing cluster status, inspecting resources, creating and modifying objects, labeling, annotating, and launching pods, providing practical examples and command syntax to help you manage Kubernetes clusters effectively.
1. Viewing
1.1 View cluster status
# View client and server version
<code>kubectl version --short=true</code># View cluster information
<code>kubectl cluster-info</code>1.2 View resource objects
# List namespaces
<code>kubectl get namespace</code># List all pods (add
-n <namespace>for a specific namespace,
-o widefor details)
<code>kubectl get pod
kubectl get pod -n kube
kubectl get pod -o wide</code># Label selector
<code>kubectl get pods -l app=example</code># Watch resource changes
<code>kubectl get pod -w</code># Output in YAML or JSON
<code>kubectl get pod <pod-name> -o yaml
kubectl get pod <pod-name> -o json</code># Common resource types
node (节点)
po (pod)
ns (namespace)
instance (实例)
svc (service):定义了 Pod 的逻辑分组和访问策略
cm (configMap):存储全局配置变量
ds (daemonSet):在每个节点上运行守护进程
deploy (deployment):管理 Pod 或 ReplicaSet 的部署
ingress:通过 HTTP/HTTPS 暴露 Service,提供外部 URL、负载均衡、SSL/TLS 等
endpoint、pv、pvc 等存储相关资源
1.3 View details
The
describecommand provides detailed information about a resource, especially its current state and events.
<code>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}</code>1.4 View logs
# Real‑time logs
<code>kubectl logs -f <pod_name></code># Specify container if pod has multiple containers
<code>kubectl logs -f <pod_name> -c <container_name></code># Attach to a container (similar to
docker attach)
<code>kubectl attach <pod_name> -c <container_name></code>1.5 View Kubernetes configuration
1.6 Explain resource fields
<code>kubectl explain pod
kubectl explain pod.apiVersion</code>1.7 View node labels
<code>kubectl get node --show-labels</code>1.8 File transfer
# Copy files between pod and local machine
<code># Create a file inside the pod
kubectl exec -it mysql-478535978-1dnm2 sh -c "echo 'this is a message from $(hostname)' > /tmp/message.log"
# Copy the file out
kubectl cp mysql-478535978-1dnm2:/tmp/message.log ./message.log
# Modify locally and copy back
echo "information added" >> message.log
kubectl cp ./message.log mysql-478535978-1dnm2:/tmp/message.log</code>2. Create/Modify Resources
2.1 kubectl create / apply
<code># 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 a manifest (idempotent)
kubectl apply -f my-nginx.yaml</code>2.2 kubectl replace / patch
Use
replaceto delete and recreate a resource, or
patchto modify fields in place.
<code># Replace entire resource
kubectl replace -f <yaml_file>
# Patch a pod's label
kubectl patch pod rc-nginx-2-kpiqt -p '{"metadata":{"labels":{"app":"nginx-3"}}}'</code>2.3 kubectl edit
Opens the resource in an editor for interactive changes.
<code># Edit a pod
kubectl edit po rc-nginx-btv4j</code>2.4 kubectl set
Set resource limits, requests, or update images.
<code># Set CPU and memory limits for a deployment
kubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi
# Update container image
kubectl set image deployment/nginx nginx=nginx:1.9.1</code>2.5 kubectl label and annotate
Manage labels and annotations on resources.
<code># Add a label
kubectl label pods foo unhealthy=true
# Overwrite a label
kubectl label --overwrite pods foo status=unhealthy
# Delete a label
kubectl label pods foo bar-
# Add an annotation
kubectl annotate pods foo description='my frontend'</code>3. Launch Pods
# Create and run a pod from an image
<code>kubectl run nginx --image=nginx:1.16 --port=80 --replicas=1</code>The command creates a deployment named "nginx" and you can list the resulting pod with:
<code>kubectl get pods</code>Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.