Common Kubectl Commands in Kubernetes

In the Kubernetes ecosystem, kubectl is the sole standard tool for users to interact with clusters. Fundamentally, it acts as a command-line interface (CLI) that converts user instructions into API requests and processes the resulting responses. Whether deploying applications, troubleshooting issues, or monitoring resources, developers rely on kubectl to execute these operations.

Before using kubectl, it is essential to ensure the local environment is correctly configured. Typically, running kubectl config view allows you to inspect the name of the current cluster context. In most production environments, the default context points to a cluster named kubernetes or minikube. To switch clusters, use the kubectl config use-context <cluster-name> command to complete the transition quickly, ensuring subsequent operations target the desired environment.
The core concept in Kubernetes is "resource objects," such as Deployments, Pods, and Services. kubectl offers a rich set of commands to manage these objects. The most fundamental operation is viewing resource details; the kubectl get command can quickly list resources within a cluster along with their status.

# List all Pods across all namespaces
kubectl get pods

# View Deployments in a specific namespace
kubectl get deployments -n production

# View detailed information for a specified resource (includes YAML format)
kubectl describe pod <pod-name>

The kubectl describe command is particularly useful as it displays an object's complete metadata, event logs, and connection status. This is crucial for analyzing reasons behind Pod startup failures, such as checking for OOMKilled errors or warnings indicating containers are not ready. Additionally, kubectl logs <pod-name> can be used to view standard output and error logs from containers in real-time, aiding in locating runtime issues at the application level.

Deployment and Update Strategies for Applications

Deploying applications to a Kubernetes cluster is one of the core daily tasks. While resources can be manually created by writing YAML configuration files, the kubectl apply command provides more flexible dynamic management capabilities. This command supports various submission methods and automatically adjusts resource configurations based on the current state of the cluster.

# Apply local YAML file to the cluster
kubectl apply -f deployment.yaml

# Preview changes using --dry-run=client without actually modifying the cluster
kubectl apply -f deployment.yaml --dry-run=client

# Overwrite existing resources (force mode)
kubectl apply -f update.yaml --overwrite

In practice, it is recommended to combine kubectl set commands to modify attributes of running objects without redeploying the entire resource. For example, to update an image version or replica count for a Deployment, simply execute:

# Update image tag
kubectl set image myapp/my-service my-app=tag:v2.0 --record

# Increase Pod replica count
kubectl scale deployment my-app --replicas=5

These commands not only improve operational efficiency but also reduce the risk of business interruptions caused by frequent service restarts. Additionally, pairing these with the kubectl patch command enables finer-grained field updates, suitable for scenarios requiring dynamic configuration adjustments.

Troubleshooting and Cluster Diagnostics

When applications exhibit anomalies or cluster performance degrades, kubectl provides powerful diagnostic tools. By combining multiple commands, you can quickly identify the root cause of problems and formulate solutions.

# View node resource utilization
kubectl top nodes

# View detailed resource consumption for Pods
kubectl top pods -n production

# Check specific event logs
kubectl get events --sort-by='.lastTimestamp' | grep <pod-name>

The kubectl top series of commands visually presents key metrics such as CPU and memory, helping to identify resource bottlenecks. If a node is found to have excessive load, you may need to consider scaling up or adjusting load balancing configurations. Furthermore, the kubectl debug command allows users to mount a debug container to a target Pod, providing direct access to its file system to execute debugging scripts, which is an effective method for deeply analyzing code-level issues.

Automation and Continuous Integration

With the widespread adoption of microservice architectures, CI/CD processes have become an integral part of DevOps practices. kubectl can be integrated with tools like GitLab CI and Jenkins to achieve automated deployments. By writing Shell scripts or Helm Charts, complex sequences of kubectl commands can be encapsulated into reusable release pipelines.

# Example: Create a backup and perform recovery operations
kubectl get pods -o yaml > backup.yaml
kubectl apply --dry-run=client -f backup.yaml

In actual work environments, it is advisable to combine common commands into script files and manage them through version control. This not only enhances team collaboration efficiency but also lays a solid foundation for future automation operations. By reasonably utilizing the various features of kubectl, developers can build more stable and efficient cloud-native infrastructure.