Kubernetes
Updated: September 10, 2025Categories: Virtualization, Container
Printed from:
Kubernetes Cheatsheet: Comprehensive Guide to Container Orchestration
1. Installation and Cluster Setup
Minikube (Local Development)
Bash
123456789101112131415# Install Minikube
brew install minikube
# Start a local Kubernetes cluster
minikube start
# Start with specific Kubernetes version
minikube start --kubernetes-version=v1.24.0
# Stop the cluster
minikube stop
# Delete the cluster
minikube delete
Kubeadm (On-Premise Cluster)
Bash
1234567891011# Initialize master node
sudo kubeadm init
# Set up kubectl config for current user
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# Join worker nodes
kubeadm join [master-ip]:6443 --token [token] --discovery-token-ca-cert-hash [hash]
Managed Kubernetes Services
- Google Kubernetes Engine (GKE)
- Amazon EKS
- Azure AKS
- DigitalOcean Kubernetes
2. kubectl Command-Line Basics
Basic Commands
Bash
12345678910111213141516171819202122232425# View cluster information
kubectl cluster-info
# Get all resources
kubectl get all
# Get resources of a specific type
kubectl get pods
kubectl get deployments
kubectl get services
# Describe a resource
kubectl describe pod [pod-name]
# Create resources from YAML
kubectl apply -f resource.yaml
# Delete resources
kubectl delete -f resource.yaml
kubectl delete pod [pod-name]
# View logs
kubectl logs [pod-name]
kubectl logs -f [pod-name] # Follow logs
3. Core Kubernetes Concepts
Pods
YAML
123456789101112# Basic Pod manifest
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
Deployments
YAML
1234567891011121314151617181920apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Services
YAML
1234567891011121314151617181920212223242526# ClusterIP Service
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
# NodePort Service
apiVersion: v1
kind: Service
metadata:
name: my-nodeport-service
spec:
type: NodePort
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
nodePort: 30000
4. YAML Manifest Structure
Basic YAML Anatomy
YAML
1234567891011apiVersion: v1 # Kubernetes API version
kind: Pod # Resource type
metadata: # Metadata about the resource
name: example # Resource name
labels: # Key-value pairs for identification
app: myapp
spec: # Desired state of the resource
containers: # Container specifications
- name: container-name
image: image-name
5. Pod Management and Lifecycle
Pod Lifecycle Phases
- Pending
- Running
- Succeeded
- Failed
- Unknown
Pod Management Commands
Bash
123456789101112# List pods
kubectl get pods
# Get detailed pod information
kubectl describe pod [pod-name]
# Execute command in a pod
kubectl exec -it [pod-name] -- /bin/bash
# Port forwarding
kubectl port-forward [pod-name] 8080:80
6. Service Types and Networking
Service Types
- ClusterIP (default)
- NodePort
- LoadBalancer
- ExternalName
7. Deployment Strategies
Rolling Update
YAML
1234567891011apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
8. ConfigMaps and Secrets
ConfigMap
YAML
12345678apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
DATABASE_URL: mongodb://localhost:27017
LOG_LEVEL: debug
Secret
YAML
12345678apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
stringData:
DB_PASSWORD: mysecretpassword
9. Persistent Volumes
PersistentVolumeClaim
YAML
1234567891011apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
10. Resource Quotas
YAML
1234567891011apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-resources
spec:
hard:
requests.cpu: "1"
requests.memory: 1Gi
limits.cpu: "2"
limits.memory: 2Gi
11. Horizontal Pod Autoscaling
YAML
1234567891011121314151617apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: my-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-deployment
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 70
12. Ingress Controllers
YAML
1234567891011121314151617apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 80
13. StatefulSets and DaemonSets
StatefulSet
YAML
12345678910111213141516171819apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
serviceName: "my-service"
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image
14. Jobs and CronJobs
Job
YAML
123456789101112apiVersion: batch/v1
kind: Job
metadata:
name: my-job
spec:
template:
spec:
containers:
- name: job-container
image: my-job-image
restartPolicy: Never
CronJob
YAML
123456789101112131415apiVersion: batch/v1
kind: CronJob
metadata:
name: my-cronjob
spec:
schedule: "*/5 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: job-container
image: my-job-image
restartPolicy: OnFailure
15. RBAC (Role-Based Access Control)
Role and RoleBinding
YAML
1234567891011121314151617181920212223apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
16. Monitoring and Logging
Recommended Tools
- Prometheus
- Grafana
- ELK Stack (Elasticsearch, Logstash, Kibana)
- Kubernetes Dashboard
17. Troubleshooting and Debugging
Bash
123456789101112# View cluster events
kubectl get events
# Check pod status and issues
kubectl describe pod [pod-name]
# View container logs
kubectl logs [pod-name] -c [container-name]
# Interactive debugging
kubectl debug [pod-name] --image=busybox
18. Helm Package Manager
Bash
123456789101112131415161718# Install Helm
brew install helm
# Add a repository
helm repo add stable https://charts.helm.sh/stable
# Search for charts
helm search repo nginx
# Install a chart
helm install my-release stable/nginx
# List installed releases
helm list
# Uninstall a release
helm uninstall my-release
19. CI/CD Integration
Example GitLab CI Pipeline
YAML
123456789101112131415stages:
- build
- deploy
build:
stage: build
script:
- docker build -t myapp:$CI_COMMIT_SHA .
- docker push myregistry.com/myapp:$CI_COMMIT_SHA
deploy:
stage: deploy
script:
- kubectl set image deployment/myapp myapp=myregistry.com/myapp:$CI_COMMIT_SHA
20. Production Best Practices
- Use namespace separation
- Implement resource limits
- Use multi-stage Docker builds
- Enable RBAC
- Use network policies
- Implement proper logging and monitoring
- Regular security scans
- Use managed Kubernetes services
- Implement proper backup strategies
- Keep Kubernetes and container images updated
Additional Resources
- Official Kubernetes Documentation: https://kubernetes.io/docs/
- Kubernetes Slack Community
- CNCF (Cloud Native Computing Foundation)
Note: Always refer to the latest Kubernetes documentation for the most up-to-date information.
Continue Learning
Discover more cheatsheets to boost your productivity