Mastering Kubernetes: A Comprehensive Cheat Sheet with Examples
Kubernetes, often referred to as K8s, is an open-source container orchestration platform that automates application deployment, scaling, and management. Whether you're a seasoned Kubernetes user or just starting your journey, this cheat sheet will serve as a handy reference for common tasks and commands.
Kubernetes Basics and Setup
Before diving into the cheat sheet, ensure you have a running Kubernetes cluster. You can set up a local cluster using Minikube or use a managed Kubernetes service like Amazon EKS, Azure Kubernetes Service, or Google Kubernetes Engine.
Checking Kubernetes Version and Cluster Info
Check the client version:

kubectl version
Check the server version and cluster info:
kubectl cluster-info
Kubernetes Objects and Commands
Kubernetes uses various objects to manage resources. Here are some essential commands to work with these objects.
Pods
List all pods in all namespaces:

kubectl get pods
Describe a specific pod:
kubectl describe pod <pod-name>
Delete a pod:
kubectl delete pod <pod-name>
Deployments
List all deployments:

kubectl get deployments
Scale a deployment:
kubectl scale deployment <deployment-name> --replicas=<number-of-replicas>
Rollout a new version of a deployment:
kubectl set image deployment <deployment-name> <container-name>= <new-image:tag>
Services
List all services:
kubectl get services
Expose a deployment as a service:
kubectl expose deployment <deployment-name> --type=<service-type> --port=<port>
Namespaces
List all namespaces:
kubectl get namespaces
Switch to a specific namespace:
kubectl config set-context $(kubectl config current-context) --namespace=<namespace-name>
Kubernetes Configuration and Contexts
Kubernetes uses config files to store cluster information and credentials. Here's how to manage these configurations.
Viewing and Managing Contexts
List all contexts:
kubectl config view --minify --output 'jsonpath={..contexts[].name}'
Use a specific context:
kubectl config use-context <context-name>
Managing Kubeconfig File
View the kubeconfig file:
kubectl config view --minify
Merge kubeconfig files:
kubectl config view --minify --output 'jsonpath={..clusters}'
Troubleshooting Kubernetes
When encountering issues, use the following commands to diagnose problems.
Checking Node and Pod Status
| Command | Description |
|---|---|
kubectl get nodes |
List all nodes and their status |
kubectl get pods --all-namespaces |
List all pods in all namespaces |
Viewing Events and Logs
View events for a specific pod:
kubectl get events --sort-by=.metadata.creationTimestamp -w <pod-name>
View logs for a specific pod:
kubectl logs <pod-name>
This cheat sheet provides a solid foundation for working with Kubernetes. As you gain more experience, you'll find that many tasks can be automated using scripts, config maps, and secrets. Happy orchestrating!






















