Operations 14 min read

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.

Raymond Ops
Raymond Ops
Raymond Ops
Master Kubernetes Cluster Management: Essential kubectl Commands Explained

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 &lt;namespace&gt;

for a specific namespace,

-o wide

for 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 &lt;pod-name&gt; -o yaml
kubectl get pod &lt;pod-name&gt; -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

describe

command provides detailed information about a resource, especially its current state and events.

<code>kubectl describe pod &lt;Pod Name&gt;
kubectl describe deployment &lt;Deployment Name&gt;
kubectl describe service &lt;Service Name&gt;
kubectl describe node [node ip]
kubectl describe pods &lt;Deployment Name&gt;
kubectl describe pod/{pod_name} -n {namespace}</code>
图片
图片

1.4 View logs

# Real‑time logs

<code>kubectl logs -f &lt;pod_name&gt;</code>

# Specify container if pod has multiple containers

<code>kubectl logs -f &lt;pod_name&gt; -c &lt;container_name&gt;</code>
图片
图片

# Attach to a container (similar to

docker attach

)

<code>kubectl attach &lt;pod_name&gt; -c &lt;container_name&gt;</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

replace

to delete and recreate a resource, or

patch

to modify fields in place.

<code># Replace entire resource
kubectl replace -f &lt;yaml_file&gt;
# 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>
KubernetesDevOpscluster managementpodkubectlresource commands
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

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.