0Pricing
Docker & Kubernetes for Developers · 课时

扩展与自愈应用

为部署实施基本的扩展策略,并了解 Kubernetes 如何确保应用的可用性和韧性。

扩展与自愈应用 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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: 80

Introducing 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: 50

What 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: 5

Readiness 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: 5

Scaling & 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!

常见问题解答

「扩展与自愈应用」课时是免费的吗?

是的 — 「扩展与自愈应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。

「扩展与自愈应用」这节课中我会学到什么?

为部署实施基本的扩展策略,并了解 Kubernetes 如何确保应用的可用性和韧性。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Docker & Kubernetes for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「扩展与自愈应用」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?

能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 了解 Kubernetes 部署
  2. 使用服务暴露应用
  3. 扩展与自愈应用
  4. 滚动更新与回滚
← 返回 Docker & Kubernetes for Developers