Kubernetes Real-Time Examples and Solutions
Scenario 1: Zero-Downtime Deployment (Rolling Update)
Problem: Updating an application causes downtime for users while the new version starts up.
Solution: Use a Kubernetes Deployment with a Rolling Update strategy.
* Configuration: Set maxUnavailable to 0 (or a low number) and maxSurge to 1 (or more).
* Process: Kubernetes will start a new pod with the new version. Only when the Readiness Probe passes for the new pod will it terminate an old pod. This ensures capacity never drops below 100%.
Scenario 2: Autoscaling based on CPU Load
Problem: The application gets slow during peak hours because the pods are CPU-bound.
Solution: Implement Horizontal Pod Autoscaler (HPA).
* Requirement: Define resources.requests.cpu in the Deployment manifest.
* HPA Object:
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
* Result: When average CPU usage exceeds 70%, Kubernetes adds more pods. When it drops, it removes them.
Scenario 3: Managing Secrets Securely
Problem: Database passwords are hardcoded in the application config, which is a security risk.
Solution: Use Kubernetes Secrets.
* Creation: kubectl create secret generic db-pass --from-literal=password='supersecret'
* Usage: Mount the secret as an environment variable in the Pod.
yaml
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-pass
key: password