Docker & Kubernetes for Developers · レッスン

アプリケーションのスケーリングと自己修復

Deploymentの基本的なスケーリング戦略を実装し、Kubernetesがアプリケーションの可用性と回復力を確保する仕組みを理解します。

レッスン 3/411 ステップ

「アプリケーションのスケーリングと自己修復」はCoddyKit上の無料Docker & Kubernetes for Developersレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「アプリケーションのスケーリングと自己修復」レッスンは無料ですか?

はい。「アプリケーションのスケーリングと自己修復」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Docker & Kubernetes for Developersコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Docker & Kubernetes for Developersコースには全4レッスンが含まれています。

「アプリケーションのスケーリングと自己修復」で何を学びますか?

Deploymentの基本的なスケーリング戦略を実装し、Kubernetesがアプリケーションの可用性と回復力を確保する仕組みを理解します。 ブラウザで直接実行するハンズオンコードでDocker & Kubernetes for Developersを演習し、24時間対応の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 Deploymentを理解する
  2. Serviceによるアプリケーションの公開
  3. アプリケーションのスケーリングと自己修復
  4. ローリングアップデートとロールバック
← Docker & Kubernetes for Developersに戻る