
20+ Kubernetes Best Practices
- Published on
- Authors
- Author
- Ram Simran G
- twitter @rgarimella0124
Kubernetes has revolutionized the way modern applications are deployed, scaled, and maintained. But like any powerful tool, its effectiveness depends on how well itβs configured and operated. In this post, weβll walk through essential Kubernetes best practices β starting from resource management to security, debugging, and monitoring.
π 1οΈβ£ Use Labels & Annotations Wisely π·οΈ
Labels are key-value pairs that are used to identify, group, and select Kubernetes resources. Annotations allow you to attach non-identifying metadata.
Example:
metadata:
labels:
env: production
app: payment-service
annotations:
owner: devops-team
description: 'Handles payment transactions' β
Pro Tip:
Design a label taxonomy early on. Use consistent naming conventions like:
team: devopsenv: stagingtier: backend
βοΈ 2οΈβ£ Set Resource Requests & Limits
Setting requests and limits prevents resource starvation and overcommitment.
Example:
resources:
requests:
cpu: '500m'
memory: '256Mi'
limits:
cpu: '1'
memory: '512Mi' β Pro Tip:
- Use Vertical Pod Autoscaler (VPA) for automatic resource adjustments.
- Monitor with Goldilocks to right-size resource allocations.
π΅οΈ 3οΈβ£ Debug Pods Like a Pro
When things break, use:
kubectl exec -it my-pod -- /bin/sh
kubectl logs my-pod β
Pro Tip:
Use kubectl cp to copy files into or out of a container for debugging:
kubectl cp my-pod:/var/log/app.log ./app.log π 4οΈβ£ Use kubectl get all --all-namespaces
A quick way to list all resources:
kubectl get all --all-namespaces β
Pro Tip:
Use kubectl top to monitor resource usage:
kubectl top pod --all-namespaces π 5οΈβ£ Rolling Updates, Not Outages
Avoid downtime with:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 2 β
Pro Tip:
Enable PodDisruptionBudgets (PDB) to manage voluntary disruptions.
spec:
minAvailable: 2 β€οΈβπ₯ 6οΈβ£ Use Liveness & Readiness Probes
Health check your applications:
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 5 β
Pro Tip:
Use startupProbe for slow-starting apps:
startupProbe:
httpGet:
path: /startup
port: 8080
failureThreshold: 30
periodSeconds: 10 π 7οΈβ£ Auto-Scale Like a Boss
Horizontal scaling:
kubectl autoscale deployment my-app --cpu-percent=50 --min=2 --max=10 β
Pro Tip:
Combine HPA with Cluster Autoscaler for dynamic infrastructure scaling.
π‘οΈ 8οΈβ£ Secure Your Cluster
- Use RBAC:
kubectl create rolebinding dev-binding --role=developer --user=john - Scan images with:
trivy image my-app:latest β
Pro Tip:
Enable Network Policies to restrict traffic between pods.
π 9οΈβ£ Use ConfigMaps & Secrets
Decouple your configuration:
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: db-secret β
Pro Tip:
Use sealed-secrets to safely store encrypted secrets in Git.
π π Monitor Everything
Use Prometheus and Grafana:
kubectl apply -f prometheus.yaml
kubectl apply -f grafana.yaml β
Pro Tip:
Set up Alertmanager for proactive alerting.
β¨ Bonus Tips You Shouldnβt Miss!
π¦ 11οΈβ£ Leverage initContainers
For pre-application bootstrapping or dependency checks:
initContainers:
- name: wait-for-db
image: busybox
command: ['sh', '-c', 'until nc -z db 5432; do sleep 2; done;'] π 12οΈβ£ Immutable Container Images
Tag images with a version or hash:
image: my-app:v1.0.3 Never use latest in production!
π 13οΈβ£ Clean Up Unused Resources
Regularly clean up:
kubectl get all --all-namespaces
kubectl delete pod <pod-name>
kubectl delete pvc <pvc-name> π 14οΈβ£ Use Helm Charts
Package and deploy your Kubernetes resources with ease:
helm install my-app ./my-app-chart ποΈ 15οΈβ£ Leverage Namespaces
Segment resources:
kubectl create namespace dev
kubectl create namespace prod π΅οΈ 16οΈβ£ Use kubectl describe
For in-depth resource details:
kubectl describe pod my-pod π 17οΈβ£ Manage External Traffic with Ingress
Use an Ingress Controller like NGINX:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 80 π 18οΈβ£ Leverage ServiceMonitors
When using Prometheus Operator:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor π¦ 19οΈβ£ Use Admission Controllers
Enforce policies at runtime, for example:
- Deny containers running as root
- Restrict certain labels
Tools like OPA Gatekeeper can help.
πΎ 20οΈβ£ Use PersistentVolumeClaims (PVC)
For stateful apps:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi π― Conclusion
Mastering Kubernetes isnβt just about knowing the commands β itβs about building reliable, observable, and secure systems. These tips, combined with proactive monitoring and sound DevOps practices, can save you hours of debugging, downtime, and frustration.
Cheers,
Sim