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.
Introduction to Kubernetes and Its Importance for Developers
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
Focus on writing code instead of managing infrastructure
Automated deployment and scaling
Built-in best practices for container management
Reliability
Self-healing capabilities
Automated rollbacks
High availability configurations
Flexibility
Cloud provider agnostic
Support for multiple programming languages
Various deployment strategies
Cost Efficiency
Better resource utilization
Automated scaling based on demand
Reduced operational overhead
Basic Kubernetes Commands
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
- kubectl get pods # List all pods
- kubectl get services # List all services
- kubectl get deployments # List all deployments
- kubectl get nodes # List all nodes
# Describe resources in detail
- kubectl describe pod <pod-name>
- kubectl describe service <service-name>
# Delete resources
- kubectl delete pod <pod-name>
- kubectl delete service <service-name>
These basic commands allow developers to inspect and manage resources, facilitating day-to-day interactions with the cluster.
Pod Management Commands
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
- kubectl create -f pod.yaml
- kubectl apply -f pod.yaml
# Execute commands in pods
- kubectl exec -it <pod-name> -- /bin/bash
# View pod logs
- kubectl logs <pod-name>
- kubectl logs -f <pod-name> # Follow log output
# Copy files to/from pods
kubectl cp <pod-name>:/path /local/path
These commands are indispensable for managing pods, investigating issues, and debugging applications in real-time.
Service and Networking Commands
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.
Scaling and Updating Resources
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.
Troubleshooting Kubernetes Clusters
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.
Conclusion
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:
Always use --dry-run flag when trying new commands
Set up command aliases for frequently used commands
Use kubectl explain to learn more about resource types
Practice these commands in a test environment first