Here’s a comprehensive Kubernetes cheat sheet, covering essential commands, objects, and concepts:
Basic Kubernetes Concepts
Cluster: A set of machines (nodes) that run containerized applications.
Node: A single machine in the cluster (physical/virtual).
Pod: The smallest deployable unit in Kubernetes (one or more containers).
Service: Exposes a set of Pods as a network service.
Deployment: Manages replica sets and ensures desired state.
Namespace: Provides scope for resources in a cluster.
Kubernetes Command Basics
Command | Description |
kubectl version | Show kubectl and cluster version |
kubectl cluster-info | Display information about the cluster |
kubectl get nodes | List nodes in the cluster |
kubectl get pods | List pods in the default namespace |
kubectl get services | List all services |
kubectl get deployments | List all deployments |
kubectl get namespaces | List all namespaces |
kubectl get events | List events in the cluster |
kubectl config view | Show kubectl config |
kubectl config current-context | Show the current context |
Viewing Resources
Command | Description |
kubectl get [resource] | List specific resource type (e.g., pods, services, deployments) |
kubectl describe [resource] [name] | Detailed description of a resource |
kubectl logs [pod-name] | Get logs for a specific pod |
kubectl logs [pod-name] -c [container-name] | Get logs for a specific container in a pod |
kubectl exec [pod-name] -- [command] | Execute a command in a pod (like docker exec ) |
Creating and Managing Pods
Command | Description |
kubectl run [pod-name] --image=[image] | Create a pod from an image |
kubectl delete pod [pod-name] | Delete a pod |
kubectl scale deployment [name] --replicas=[n] | Scale a deployment to n replicas |
kubectl expose pod [pod-name] --port=[port] | Expose a pod as a service |
Working with Deployments
Command | Description |
kubectl create deployment [name] --image=[image] | Create a deployment |
kubectl rollout status deployment [name] | Check the status of a deployment rollout |
kubectl rollout undo deployment [name] | Undo a deployment rollout |
kubectl delete deployment [name] | Delete a deployment |
Services and Networking
Command | Description |
kubectl expose deployment [name] --type=[type] --port=[port] | Expose a deployment as a service (types: ClusterIP, NodePort, LoadBalancer) |
kubectl get svc | List all services |
kubectl describe svc [service-name] | Get details of a service |
kubectl delete svc [service-name] | Delete a service |
Namespaces
Command | Description |
kubectl create namespace [name] | Create a new namespace |
kubectl get namespaces | List all namespaces |
kubectl config set-context --current --namespace=[name] | Set current namespace |
kubectl delete namespace [name] | Delete a namespace |
ConfigMaps and Secrets
Command | Description |
kubectl create configmap [name] --from-literal=[key=value] | Create a ConfigMap from literal values |
kubectl create configmap [name] --from-file=[file-path] | Create a ConfigMap from a file |
kubectl get configmaps | List ConfigMaps |
kubectl describe configmap [name] | Show ConfigMap details |
kubectl delete configmap [name] | Delete a ConfigMap |
Command | Description |
kubectl create secret generic [name] --from-literal=[key=value] | Create a Secret from literal values |
kubectl get secrets | List Secrets |
kubectl describe secret [name] | Show Secret details |
kubectl delete secret [name] | Delete a Secret |
Volumes and Storage
Command | Description |
kubectl create -f [persistent-volume-file].yaml | Create a PersistentVolume from a YAML file |
kubectl get pv | List all PersistentVolumes |
kubectl describe pv [pv-name] | Show details of a PersistentVolume |
kubectl create -f [persistent-volume-claim].yaml | Create a PersistentVolumeClaim |
kubectl get pvc | List all PersistentVolumeClaims |
kubectl describe pvc [pvc-name] | Show details of a PersistentVolumeClaim |
Rolling Updates and Rollbacks
Command | Description |
kubectl rollout status deployment/[deployment-name] | View status of a rolling update |
kubectl set image deployment/[name] [container-name]=[new-image] | Update container image |
kubectl rollout history deployment/[deployment-name] | View rollout history |
kubectl rollout undo deployment/[deployment-name] | Rollback to the previous deployment |
Scaling Applications
Command | Description |
kubectl scale deployment [name] --replicas=[n] | Scale the number of replicas |
kubectl autoscale deployment [name] --min=[n] --max=[m] --cpu-percent=[percent] | Autoscale a deployment based on CPU usage |
Resource Quotas and Limits
Command | Description |
kubectl create -f [resource-quota].yaml | Create a ResourceQuota |
kubectl get resourcequotas | List ResourceQuotas |
kubectl describe resourcequota [name] | Show details of a ResourceQuota |
kubectl delete resourcequota [name] | Delete a ResourceQuota |
Port Forwarding
Command | Description |
kubectl port-forward [pod-name] [local-port]:[remote-port] | Forward local port to a pod’s port |
kubectl port-forward service/[service-name] [local-port]:[remote-port] | Forward local port to a service port |
Debugging
Command | Description |
kubectl describe pod [pod-name] | Get details of a specific pod (useful for troubleshooting) |
kubectl logs [pod-name] | View pod logs |
kubectl exec -it [pod-name] -- /bin/sh | Execute a shell inside the pod |
kubectl get events | Get a list of events in the cluster |
YAML Essentials
Pod Definition Example
apiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: default
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
Deployment Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
Kubernetes Components
Component | Description |
Kubelet | Agent that runs on nodes and ensures containers are running |
Kube-Proxy | Manages network communication inside the cluster |
etcd | Key-value store for cluster data |
API Server | Frontend for the Kubernetes control plane |
Controller Manager | Manages cluster controllers like deployments and replicas |
Scheduler | Schedules pods to run on available nodes |
Other Useful Commands
Command | Description |
kubectl apply -f [file].yaml | Create/update resources using a YAML file |
kubectl delete -f [file].yaml | Delete resources defined in a YAML file |
kubectl edit [resource] [name] | Edit a resource in place |
kubectl get all | List all resources in the current namespace |
kubectl top nodes | Show resource usage by nodes |
kubectl top pods | Show resource usage by pods |
This cheat sheet should give you a solid starting point for working with Kubernetes. Let me know if you'd like further details on any specific area!