☸ Kubernetes Cheat Sheet update
kubectl • Pods • Deployments • Services • ConfigMaps • Secrets • Storage • Networking • Troubleshooting
📚 Contents
``` 1. Kubernetes Basics2. Cluster Commands3. Pods4. Deployments5. Services6. Namespaces7. ConfigMaps8. Secrets9. Labels & Selectors10. Logs & Debugging11. Execute Commands12. Scaling13. Rolling Updates14. Resource Management15. Storage16. Networking17. Ingress18. RBAC19. Jobs & CronJobs20. YAML Templates21. Helm22. Troubleshooting23. Advanced Commands ```
1. Kubernetes Basics
```
Term Meaning Cluster A group of machines running Kubernetes. Node A machine inside the Kubernetes cluster. Pod The smallest deployable unit in Kubernetes. Deployment Manages replicated Pods and application updates. Service Provides stable network access to Pods. ConfigMap Stores non-sensitive configuration. Secret Stores sensitive configuration such as passwords and tokens. Namespace Provides logical isolation inside a cluster.
```
2. Cluster Commands
```
```
kubectl cluster-info
kubectl version
kubectl get nodes
kubectl get nodes -o wide
kubectl describe node NODE_NAME
kubectl get componentstatuses
kubectl config get-contexts
kubectl config current-context
kubectl config use-context CONTEXT_NAME 3. Pods
```
List Pods
```
kubectl get pods
kubectl get pods -A
kubectl get pods -o wide
kubectl get pods -n NAMESPACE ```
Create Pod
```
kubectl run nginx --image=nginx
kubectl run nginx
--image=nginx
--port=80 ```
Pod Details
```
kubectl describe pod POD_NAME
kubectl get pod POD_NAME -o yaml ```
Delete Pod
```
kubectl delete pod POD_NAME
kubectl delete pod POD_NAME -n NAMESPACE ```
Pods managed by a Deployment will usually be recreated automatically after deletion.
```
4. Deployments
```
```
kubectl create deployment nginx --image=nginx
kubectl get deployments
kubectl describe deployment nginx
kubectl get deployment nginx -o yaml
kubectl delete deployment nginx ```
Generate Deployment YAML
```
kubectl create deployment nginx
--image=nginx
--dry-run=client
-o yaml > deployment.yaml ```
Apply YAML
```
kubectl apply -f deployment.yaml
kubectl apply -f . 5. Services
```
Service Type Purpose ClusterIP Internal cluster access. NodePort Exposes service through a node port. LoadBalancer Uses an external load balancer when supported. ExternalName Maps a service to an external DNS name.
Create Service
```
kubectl expose deployment nginx
--port=80
--target-port=80
kubectl expose deployment nginx
--type=NodePort
--port=80 ```
View Services
```
kubectl get services
kubectl get svc
kubectl describe svc nginx 6. Namespaces
```
```
kubectl get namespaces
kubectl create namespace dev
kubectl get pods -n dev
kubectl apply -f app.yaml -n dev
kubectl delete namespace dev ```
Set Default Namespace
```
kubectl config set-context --current
--namespace=dev 7. ConfigMaps
```
```
kubectl create configmap app-config
--from-literal=APP_ENV=production
kubectl get configmaps
kubectl describe configmap app-config
kubectl get configmap app-config -o yaml ```
From File
```
kubectl create configmap app-config
--from-file=config.properties 8. Secrets
```
```
kubectl create secret generic app-secret
--from-literal=DB_USER=admin
--from-literal=DB_PASSWORD=password
kubectl get secrets
kubectl describe secret app-secret
kubectl get secret app-secret -o yaml ```
Kubernetes Secrets are base64-encoded by default, not automatically encrypted for every storage/use scenario. Use appropriate cluster encryption and access controls for production.
Decode Secret
```
echo 'BASE64_VALUE' | base64 -d 9. Labels & Selectors
```
```
kubectl get pods --show-labels
kubectl get pods -l app=nginx
kubectl get pods -l environment=production
kubectl label pod nginx app=web
kubectl label pod nginx environment=prod --overwrite ```
Common Labels
```
app=frontend
app=backend
environment=production
environment=staging
version=v1
team=devops 10. Logs & Debugging
```
```
kubectl logs POD_NAME
kubectl logs POD_NAME -f
kubectl logs POD_NAME --tail=100
kubectl logs POD_NAME --since=1h
kubectl logs POD_NAME -c CONTAINER_NAME
kubectl logs -l app=nginx ```
Previous Container Logs
```
kubectl logs POD_NAME --previous 11. Execute Commands Inside Pod
```
```
kubectl exec -it POD_NAME -- /bin/bash
kubectl exec -it POD_NAME -- /bin/sh
kubectl exec POD_NAME -- ls
kubectl exec POD_NAME -- env
kubectl exec POD_NAME -- ps aux ```
Multi-container Pod
```
kubectl exec -it POD_NAME
-c CONTAINER_NAME
-- /bin/sh 12. Scaling
```
```
kubectl scale deployment nginx --replicas=3
kubectl get deployment
kubectl get pods
kubectl autoscale deployment nginx
--min=2
--max=10
--cpu-percent=70 ```
Check HPA
```
kubectl get hpa
kubectl describe hpa 13. Rolling Updates
```
Update Image
```
kubectl set image deployment/nginx
nginx=nginx:1.27 ```
Check Rollout
```
kubectl rollout status deployment/nginx
kubectl rollout history deployment/nginx ```
Rollback
```
kubectl rollout undo deployment/nginx
kubectl rollout undo deployment/nginx
--to-revision=2 ```
Restart Deployment
```
kubectl rollout restart deployment/nginx 14. Resource Management
```
CPU and Memory
```
resources:
requests:
cpu: "250m"
memory: "256Mi"
limits:
cpu: "500m"
memory: "512Mi" ```
View Usage
```
kubectl top nodes
kubectl top pods
kubectl top pod POD_NAME ```
Requests influence scheduling. Limits constrain resource usage.
```
15. Storage
```
Persistent Volumes
```
kubectl get pv
kubectl describe pv PV_NAME
kubectl get pvc
kubectl describe pvc PVC_NAME
kubectl get storageclass
kubectl describe storageclass STORAGE_CLASS ```
Common Storage Objects
Object Purpose PV Cluster storage resource. PVC Request for storage. StorageClass Defines dynamic storage provisioning.
```
16. Networking
```
```
kubectl get svc
kubectl get endpoints
kubectl get endpointslices
kubectl get pods -o wide
kubectl describe svc SERVICE_NAME ```
DNS Test
```
kubectl run dns-test
--image=busybox:1.36
-it --rm --restart=Never
-- nslookup kubernetes.default ```
Port Forward
```
kubectl port-forward pod/POD_NAME 8080:80
kubectl port-forward svc/SERVICE_NAME 8080:80 17. Ingress
```
```
kubectl get ingress
kubectl get ingress -A
kubectl describe ingress INGRESS_NAME
kubectl get ingressclass
kubectl describe ingressclass INGRESS_CLASS ```
Example Ingress
```
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
spec:
ingressClassName: nginx
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx
port:
number: 80 18. RBAC
```
```
kubectl get roles
kubectl get rolebindings
kubectl get clusterroles
kubectl get clusterrolebindings
kubectl describe role ROLE_NAME
kubectl auth can-i get pods
kubectl auth can-i create deployments
kubectl auth can-i delete pods ```
Check Specific User
```
kubectl auth can-i get pods
--as=[user@example.com](mailto:user@example.com) 19. Jobs & CronJobs
```
Jobs
```
kubectl get jobs
kubectl describe job JOB_NAME
kubectl delete job JOB_NAME ```
CronJobs
```
kubectl get cronjobs
kubectl describe cronjob CRONJOB_NAME
kubectl create job manual-job
--from=cronjob/CRONJOB_NAME ```
Cron Format
```
---
│ │ │ │ │
│ │ │ │ └── Day of week
│ │ │ └──── Month
│ │ └────── Day of month
│ └──────── Hour
└────────── Minute ```
```
*/5 * * * * Every 5 minutes
0 * * * * Every hour
0 0 * * * Every day at midnight
0 0 * * 0 Every Sunday 20. YAML Templates
```
Deployment
```
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
```
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Service
```
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: ClusterIP ```
Apply / Delete
```
kubectl apply -f app.yaml
kubectl delete -f app.yaml 21. Helm
```
Repository
```
helm repo add NAME URL
helm repo update
helm repo list
helm search repo nginx ```
Install
```
helm install my-app CHART_NAME
helm install my-app CHART_NAME
-n production
--create-namespace ```
List Releases
```
helm list
helm list -A ```
Upgrade
```
helm upgrade my-app CHART_NAME
helm upgrade my-app CHART_NAME
-f values.yaml ```
Rollback
```
helm history my-app
helm rollback my-app 1 ```
Uninstall
```
helm uninstall my-app 22. Troubleshooting
```
Pod Not Running
```
kubectl get pods
kubectl describe pod POD_NAME
kubectl logs POD_NAME
kubectl logs POD_NAME --previous ```
Pod Pending
```
kubectl describe pod POD_NAME
kubectl get nodes
kubectl describe node NODE_NAME
kubectl get pvc ```
ImagePullBackOff
```
kubectl describe pod POD_NAME
kubectl get events --sort-by=.lastTimestamp ```
CrashLoopBackOff
```
kubectl logs POD_NAME
kubectl logs POD_NAME --previous
kubectl describe pod POD_NAME ```
Service Not Working
```
kubectl get svc
kubectl describe svc SERVICE_NAME
kubectl get endpoints SERVICE_NAME
kubectl get pods --show-labels ```
View Events
```
kubectl get events
kubectl get events -A
kubectl get events
--sort-by=.lastTimestamp 23. Advanced Commands
```
Get Everything
```
kubectl get all
kubectl get all -A ```
Output Formats
```
kubectl get pods -o wide
kubectl get pods -o yaml
kubectl get pods -o json
kubectl get pods -o name
kubectl get pods -o custom-columns=NAME:.metadata.name ```
Delete Resources
```
kubectl delete pod POD_NAME
kubectl delete deployment DEPLOYMENT_NAME
kubectl delete service SERVICE_NAME
kubectl delete -f app.yaml
kubectl delete all --all ```
⚠️ Be careful:kubectl delete all --all can remove many workload resources in the current namespace. Always verify the current context and namespace before destructive commands.
Current Context
```
kubectl config current-context
kubectl config get-contexts
kubectl config use-context CONTEXT_NAME ```
API Resources
```
kubectl api-resources
kubectl explain pod
kubectl explain deployment.spec
kubectl explain service.spec ```
Dry Run
```
kubectl create deployment nginx
--image=nginx
--dry-run=client
-o yaml
kubectl run nginx
--image=nginx
--dry-run=client
-o yaml ⚡ Quick Reference
```
Task Command List Pods kubectl get pods All Namespaces kubectl get pods -A Pod Details kubectl describe pod POD Pod Logs kubectl logs POD Enter Pod kubectl exec -it POD -- /bin/sh Deploy App kubectl apply -f app.yaml Delete App kubectl delete -f app.yaml Scale kubectl scale deployment APP --replicas=3 Restart kubectl rollout restart deployment APP Rollback kubectl rollout undo deployment APP Services kubectl get svc Ingress kubectl get ingress Storage kubectl get pv,pvc Node Resources kubectl top nodes Pod Resources kubectl top pods Events kubectl get events --sort-by=.lastTimestamp
```
🚀 Typical Kubernetes Deployment Workflow
```
```
# 1. Check cluster
kubectl cluster-info
# 2. Check nodes
kubectl get nodes
# 3. Create namespace
kubectl create namespace production
# 4. Deploy application
kubectl apply -f deployment.yaml -n production
# 5. Create service
kubectl apply -f service.yaml -n production
# 6. Check pods
kubectl get pods -n production
# 7. Check service
kubectl get svc -n production
# 8. Check logs
kubectl logs POD_NAME -n production
# 9. Scale application
kubectl scale deployment APP
--replicas=3
-n production
# 10. Update application
kubectl set image deployment/APP
APP=IMAGE:TAG
-n production
# 11. Check rollout
kubectl rollout status deployment/APP
-n production
# 12. Rollback if required
kubectl rollout undo deployment/APP
-n production 🔐 Production Safety Checklist
```
Verify
kubectl config current-contextbefore destructive operations.Verify the namespace before applying or deleting resources.
Use RBAC with least privilege.
Set CPU and memory requests/limits for workloads.
Use Secrets for sensitive configuration.
Configure appropriate network policies where required.
Use readiness and liveness probes.
Use rolling deployments for application updates.
Monitor nodes, Pods and application logs.
Keep backups for persistent application data.
```
Kubernetes Cheat Sheet • kubectl Reference • DevOps