Mise à l’échelle et auto-rétablissement des applications
Mettez en œuvre des stratégies de mise à l’échelle de base pour vos déploiements et comprenez comment Kubernetes garantit la disponibilité et la résilience des applications.
Mise à l’échelle et auto-rétablissement des applications est une leçon Docker & Kubernetes for Developers gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Docker & Kubernetes for Developers, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Docker & Kubernetes for Developers comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
Scaling & Self-Healing Intro
Welcome! In this lesson, we'll dive into making your applications robust and responsive using Kubernetes.
We'll explore how to scale your apps to handle varying loads and how Kubernetes enables self-healing to recover from failures automatically. These are crucial for reliable, high-availability services.
Why Scaling Matters
Scaling an application means adjusting its capacity to meet demand. Imagine a sudden surge in users for your online store—without scaling, your app might slow down or crash.
- Handle Traffic: Distribute load across multiple instances.
- Improve Performance: Maintain responsiveness under heavy use.
- Boost Availability: If one instance fails, others can take over.
Scaling with Deployments
In Kubernetes, Deployments are key to scaling. They manage a set of identical pods, ensuring a desired number of replicas are always running.
You can manually scale a Deployment by changing its replicas field. For example, to scale an app named my-app to 3 pods:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
ports:
- containerPort: 80Introducing Horizontal Pod Autoscaler
While manual scaling works, it's not ideal for dynamic workloads. This is where the Horizontal Pod Autoscaler (HPA) comes in!
HPA automatically adjusts the number of pods in a Deployment (or StatefulSet) based on observed metrics like CPU utilization or custom metrics.
Configuring an HPA
An HPA resource defines the target for your application's scaling. It monitors metrics and increases or decreases pod replicas within defined minimum and maximum limits.
Here's a basic HPA manifest that targets 50% CPU utilization:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app-deployment
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50What is Self-Healing?
Beyond scaling, Kubernetes also excels at self-healing. This means it can automatically detect and recover from application failures without human intervention.
If a pod crashes, becomes unresponsive, or gets terminated, Kubernetes' controllers (like the Deployment controller) will work to replace it and restore the desired state.
Liveness Probes: Are You Alive?
Liveness probes tell Kubernetes if your application inside a container is still running and healthy. If a liveness probe fails, Kubernetes will restart the container.
This is crucial for apps that might deadlock or become unresponsive but aren't technically 'crashed'.
apiVersion: apps/v1
kind: Deployment
metadata:
name: liveness-demo
spec:
replicas: 1
selector:
matchLabels:
app: liveness-app
template:
metadata:
labels:
app: liveness-app
spec:
containers:
- name: liveness-container
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5Readiness Probes: Ready for Traffic?
Readiness probes tell Kubernetes if your application is ready to serve network traffic. If a readiness probe fails, Kubernetes stops sending traffic to that pod.
This prevents new requests from going to a pod that's still starting up, loading data, or temporarily unhealthy.
apiVersion: apps/v1
kind: Deployment
metadata:
name: readiness-demo
spec:
replicas: 1
selector:
matchLabels:
app: readiness-app
template:
metadata:
labels:
app: readiness-app
spec:
containers:
- name: readiness-container
image: nginx:latest
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 5Scaling & Healing Synergy
HPA, Liveness, and Readiness probes work together to make your applications highly available and resilient.
- HPA handles varying load by adjusting replicas.
- Liveness probes ensure containers are restarted if they become unresponsive.
- Readiness probes ensure traffic only goes to fully operational pods.
This combined approach significantly improves application reliability in dynamic environments.
Quick Check
Which of the following statements about Kubernetes scaling and self-healing mechanisms are TRUE?
Recap & Next Steps
Great job! You've learned how Kubernetes helps your applications stay available and performant:
- Scaling: Adjusting capacity with Deployments and HPA.
- Self-Healing: Automatic recovery using Liveness and Readiness probes.
These powerful features are fundamental for building robust, cloud-native applications. Keep practicing to master them!
Questions Fréquemment Posées
La leçon « Mise à l’échelle et auto-rétablissement des applications » est-elle gratuite ?
Oui — le texte complet de « Mise à l’échelle et auto-rétablissement des applications » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Docker & Kubernetes for Developers, passe à CoddyKit PRO. Le cours Docker & Kubernetes for Developers comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Mise à l’échelle et auto-rétablissement des applications » ?
Mettez en œuvre des stratégies de mise à l’échelle de base pour vos déploiements et comprenez comment Kubernetes garantit la disponibilité et la résilience des applications. Tu pratiques Docker & Kubernetes for Developers avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Docker & Kubernetes for Developers ?
Aucune expérience préalable n'est requise. Docker & Kubernetes for Developers sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.
Combien de temps prend la leçon « Mise à l’échelle et auto-rétablissement des applications » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Docker & Kubernetes for Developers ?
Oui. Chaque leçon Docker & Kubernetes for Developers inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Comprendre les déploiements Kubernetes
- Exposer des applications avec des services
- Mise à l’échelle et auto-rétablissement des applications
- Mises à jour progressives et retours en arrière