Catch all the action from KubeCon India 2024—click here for the recap!
Kubernetes has quickly become the backbone of modern cloud-native applications, and mastering its command-line tool, kubectl, is essential for every developer working in this ecosystem.
Kubernetes has quickly become the backbone of modern cloud-native applications, and mastering its command-line tool, kubectl, is essential for every developer working in this ecosystem. In this cheat sheet, we've gathered some of the most commonly used commands to help you manage clusters, deploy applications, and troubleshoot issues efficiently.
Kubernetes has completely changed how we deploy, scale, and manage containerized applications. As the de facto standard for container orchestration, it handles the complex tasks of container scheduling, load balancing, and service discovery. For developers, understanding Kubernetes commands is beneficial in the following ways:
Productivity
Reliability
Flexibility
Cost Efficiency
These commands help you view, inspect, and manage your Kubernetes resources. Think of them as your basic toolkit for interacting with your cluster. The 'get' command shows you what's running, 'describe' gives you detailed information, and 'delete' helps you clean up resources. To get started, here are some foundational kubectl commands:
# Get information about resources
# Describe resources in detail
# Delete resources
These basic commands allow developers to inspect and manage resources, facilitating day-to-day interactions with the cluster.
Pods are where your applications live. These commands help you manage your application containers - from creating new pods to viewing their logs and even executing commands inside them. They're essential for debugging and maintaining your applications.
Here are key commands for managing them:
# Create and run pods
# Execute commands in pods
# View pod logs
These commands are indispensable for managing pods, investigating issues, and debugging applications in real-time.
Services enable network access to your applications. These commands help you set up networking between pods and expose your applications to the outside world. They're crucial for making your applications accessible and managing traffic flow. These commands help manage networking aspects:
# Expose deployments as services
kubectl expose deployment <name> --port=8080
# Forward ports for local access
kubectl port-forward <pod-name> 8080:80
# Get service endpoints
kubectl get endpoints
# View service details
kubectl describe service <service-name>
Networking commands are vital for enabling communication between services and allowing external access when needed.
These commands help you manage application scale and handle updates smoothly. Whether you need to scale up during high traffic or roll out a new version of your application, these commands make it possible while maintaining availability.
# Scale deployments
kubectl scale deployment <name> --replicas=3
# Update deployments
kubectl rollout status deployment/<name>
kubectl rollout history deployment/<name>
kubectl rollout undo deployment/<name>
# Autoscale deployments
kubectl autoscale deployment <name> --min=2 --max=5
These commands help maintain high availability, ensuring applications remain responsive during scaling and updates.
When issues arise, these commands become your best friends. They help you understand what's happening in your cluster, identify problems, and gather information needed for solving them. From checking logs to monitoring resource usage, these commands are essential for maintaining healthy clusters.
# Debug pods
kubectl describe pod <pod-name>
kubectl logs <pod-name> --previous
# Check cluster health
kubectl get events
kubectl top pods
kubectl top nodes
# View resource usage
kubectl cluster-info
kubectl get componentstatuses
Effective troubleshooting is essential for maintaining cluster health and resolving issues promptly.
This cheat sheet covers the essential Kubernetes commands that developers need in their daily work. By mastering these commands, you'll be better equipped to manage Kubernetes clusters efficiently. Remember that while these commands are powerful, they should be used with proper understanding and caution in production environments.
Keep this reference handy, and you'll find yourself more confident in managing Kubernetes resources. As you grow more comfortable with these basic commands, you can explore more advanced Kubernetes features and operations.
Pro Tips: