Kubernetes HPA/VPA完全ガイド:本番環境で失敗しないオートスケーリング設計と実装
はじめに Kubernetesを本番環境で運用する上で、適切なリソース管理とスケーリングは避けて通れない課題です。過剰なリソース割り当てはコストの無駄になり、不足していればパフォーマンス低下やサービス障害につながります。 本記事では、Kubernetesの自動スケーリング機能であるHorizontal Pod Autoscaler(HPA)とVertical Pod Autoscaler(VPA)について、基礎概念から本番環境での実践的な運用ノウハウまでを詳しく解説します。 HPAとVPAの違い Horizontal Pod Autoscaler(HPA) HPAは、Pod数を水平方向にスケールさせる機能です。負荷が増加するとPod数を増やし、負荷が減少するとPod数を減らします。 適用シーン: ステートレスなWebアプリケーション APIサーバー ワーカープロセス Vertical Pod Autoscaler(VPA) VPAは、個々のPodのリソース要求(CPU/メモリ)を垂直方向に調整する機能です。実際の使用状況に基づいて、より適切なリソース割り当てを推奨・適用します。 適用シーン: ステートフルなアプリケーション データベースやキャッシュサーバー バッチ処理ジョブ HPA実践ガイド 基本的なHPA設定 まずはシンプルなCPUベースのHPAから始めましょう。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 # deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: web-api namespace: production spec: replicas: 3 selector: matchLabels: app: web-api template: metadata: labels: app: web-api spec: containers: - name: web-api image: myregistry/web-api:v1.2.3 resources: requests: cpu: 200m memory: 256Mi limits: cpu: 1000m memory: 512Mi ports: - containerPort: 8080 readinessProbe: httpGet: path: /health port: 8080 initialDelaySeconds: 10 periodSeconds: 5 --- # hpa.yaml apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: web-api-hpa namespace: production spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: web-api minReplicas: 3 maxReplicas: 50 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 - type: Resource resource: name: memory target: type: Utilization averageUtilization: 80 behavior: scaleDown: stabilizationWindowSeconds: 300 policies: - type: Percent value: 10 periodSeconds: 60 scaleUp: stabilizationWindowSeconds: 0 policies: - type: Percent value: 100 periodSeconds: 15 - type: Pods value: 4 periodSeconds: 15 selectPolicy: Max カスタムメトリクスを使ったHPA CPUやメモリだけでなく、アプリケーション固有のメトリクスでスケーリングすることも可能です。 ...