Easy Updates and Rollbacks
One of Kubernetes’ standout features is its ability to facilitate seamless updates and rollbacks of containerized applications. This is achieved through Deployments, which allow you to declaratively manage the desired state of your application. Let’s consider a scenario where you have a web application, and you want to update it to a new version:
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-web-app
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: my-web-app
image: your-registry/my-web-app:v2
Apply the update:
kubectl apply -f deployment.yaml
Kubernetes will gracefully update the pods, ensuring zero downtime. If any issues arise, rolling back is as simple as:
kubectl rollout undo deployment my-web-app
Storage Distribution
Kubernetes provides robust support for storage distribution through Persistent Volumes (PVs) and Persistent Volume Claims (PVCs). Let’s say you need to deploy a database that requires persistent storage:
# database-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: database-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
Now, associate this claim with your database deployment:
# database-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: database
spec:
replicas: 1
template:
metadata:
labels:
app: database
spec:
containers:
- name: database
image: your-registry/database:latest
volumeMounts:
- name: database-storage
mountPath: /var/lib/data
volumes:
- name: database-storage
persistentVolumeClaim:
claimName: database-pvc
This ensures your database data persists across pod restarts.
Secret Handling
Managing sensitive information such as API keys or database passwords is a critical concern. Kubernetes handles this through Secrets:
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: my-secrets
type: Opaque
data:
api-key: <base64-encoded-api-key>
db-password: <base64-encoded-db-password>
Referencing secrets in a pod:
# pod-with-secrets.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: your-registry/my-container:latest
env:
- name: API_KEY
valueFrom:
secretKeyRef:
name: my-secrets
key: api-key
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: my-secrets
key: db-password
Self-Healing
Kubernetes ensures the health of your applications through Probes. Consider a scenario where a web server is continuously monitored:
# web-server-probe.yaml
apiVersion: v1
kind: Pod
metadata:
name: web-server
spec:
containers:
- name: web-server
image: your-registry/web-server:latest
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
If the /healthz
endpoint is not responsive, Kubernetes automatically restarts the container.
Load Balancing
Kubernetes simplifies load balancing with Services. For example, exposing a web application internally:
# web-service.yaml
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
Any pod labeled with app: web
is now accessible via the web-service
within the cluster.
Easy Scaling
Scaling your application is a breeze with Kubernetes. For example, scaling a deployment to five replicas:
kubectl scale deployment my-web-app --replicas=5
This instantly adjusts the number of running instances to meet demand.