運用

HPA、Probes、RBAC、リソース制限

リソース制限

requestsはスケジューリング時の保証値、limitsは使用上限。OOM Killerやスロットリングを防ぐために設定必須。

resources.yaml
yaml
1apiVersion: v1
2kind: Pod
3metadata:
4 name: app
5spec:
6 containers:
7 - name: app
8 image: myapp
9 resources:
10 requests:
11 memory: "128Mi" # 保証されるメモリ
12 cpu: "250m" # 0.25 CPU core
13 limits:
14 memory: "256Mi" # 上限(超過でOOM Kill)
15 cpu: "500m" # 上限(超過でスロットリング)
16
17---
18# LimitRangeでNamespace内のデフォルト値を設定
19apiVersion: v1
20kind: LimitRange
21metadata:
22 name: default-limits
23spec:
24 limits:
25 - default:
26 memory: "256Mi"
27 cpu: "500m"
28 defaultRequest:
29 memory: "128Mi"
30 cpu: "250m"
31 type: Container
32
33---
34# ResourceQuotaでNamespace全体のリソース制限
35apiVersion: v1
36kind: ResourceQuota
37metadata:
38 name: compute-quota
39spec:
40 hard:
41 requests.cpu: "10"
42 requests.memory: 20Gi
43 limits.cpu: "20"
44 limits.memory: 40Gi
45 pods: "50"

ヘルスチェック(Probes)

Liveness Probe

コンテナが生きているか確認。失敗するとコンテナ再起動。デッドロック検知に使用。

Readiness Probe

トラフィックを受け入れ可能か確認。失敗するとServiceのEndpointから除外。

Startup Probe

起動完了を確認。成功するまでLiveness/Readinessは無効。遅い起動のアプリ向け。

probes.yaml
yaml
1apiVersion: v1
2kind: Pod
3metadata:
4 name: app
5spec:
6 containers:
7 - name: app
8 image: myapp
9 ports:
10 - containerPort: 8080
11
12 # HTTP Probe
13 livenessProbe:
14 httpGet:
15 path: /healthz
16 port: 8080
17 initialDelaySeconds: 15
18 periodSeconds: 10
19 timeoutSeconds: 5
20 failureThreshold: 3
21
22 readinessProbe:
23 httpGet:
24 path: /ready
25 port: 8080
26 initialDelaySeconds: 5
27 periodSeconds: 5
28
29 # TCP Probe(DBなど)
30 # livenessProbe:
31 # tcpSocket:
32 # port: 5432
33
34 # Exec Probe(カスタムスクリプト)
35 # livenessProbe:
36 # exec:
37 # command:
38 # - /bin/sh
39 # - -c
40 # - pg_isready -U postgres
41
42 startupProbe:
43 httpGet:
44 path: /healthz
45 port: 8080
46 failureThreshold: 30
47 periodSeconds: 10 # 最大300秒待機

HorizontalPodAutoscaler (HPA)

HPAはCPU/メモリ使用率やカスタムメトリクスに基づいてPod数を自動調整。 Metrics Serverが必要。

hpa.yaml
yaml
1apiVersion: autoscaling/v2
2kind: HorizontalPodAutoscaler
3metadata:
4 name: web-hpa
5spec:
6 scaleTargetRef:
7 apiVersion: apps/v1
8 kind: Deployment
9 name: web
10 minReplicas: 2
11 maxReplicas: 10
12 metrics:
13 # CPU使用率
14 - type: Resource
15 resource:
16 name: cpu
17 target:
18 type: Utilization
19 averageUtilization: 70
20 # メモリ使用率
21 - type: Resource
22 resource:
23 name: memory
24 target:
25 type: Utilization
26 averageUtilization: 80
27 # カスタムメトリクス(Prometheusアダプタ等)
28 # - type: Pods
29 # pods:
30 # metric:
31 # name: http_requests_per_second
32 # target:
33 # type: AverageValue
34 # averageValue: 1000
35 behavior:
36 scaleDown:
37 stabilizationWindowSeconds: 300 # 5分間安定化
38 policies:
39 - type: Percent
40 value: 10
41 periodSeconds: 60
42 scaleUp:
43 stabilizationWindowSeconds: 0
44 policies:
45 - type: Pods
46 value: 4
47 periodSeconds: 60
1# HPA操作
2kubectl get hpa
3kubectl describe hpa web-hpa
4
5# 簡易作成
6kubectl autoscale deployment web --cpu-percent=70 --min=2 --max=10

RBAC(Role-Based Access Control)

RBACでAPIアクセス権限を制御。 Role/ClusterRole で権限を定義し、RoleBinding/ClusterRoleBinding でユーザー/ServiceAccountに付与。

Role / RoleBinding

Namespace内のリソースに対する権限

ClusterRole / ClusterRoleBinding

クラスタ全体のリソースに対する権限

rbac.yaml
yaml
1# ServiceAccount
2apiVersion: v1
3kind: ServiceAccount
4metadata:
5 name: app-sa
6 namespace: default
7
8---
9# Role(Namespace内)
10apiVersion: rbac.authorization.k8s.io/v1
11kind: Role
12metadata:
13 name: pod-reader
14 namespace: default
15rules:
16- apiGroups: [""]
17 resources: ["pods"]
18 verbs: ["get", "list", "watch"]
19- apiGroups: [""]
20 resources: ["pods/log"]
21 verbs: ["get"]
22
23---
24# RoleBinding
25apiVersion: rbac.authorization.k8s.io/v1
26kind: RoleBinding
27metadata:
28 name: read-pods
29 namespace: default
30subjects:
31- kind: ServiceAccount
32 name: app-sa
33 namespace: default
34roleRef:
35 kind: Role
36 name: pod-reader
37 apiGroup: rbac.authorization.k8s.io
38
39---
40# ClusterRole(クラスタ全体)
41apiVersion: rbac.authorization.k8s.io/v1
42kind: ClusterRole
43metadata:
44 name: secret-reader
45rules:
46- apiGroups: [""]
47 resources: ["secrets"]
48 verbs: ["get", "list"]
1# 権限確認
2kubectl auth can-i get pods --as=system:serviceaccount:default:app-sa
3kubectl auth can-i create deployments --as=user@example.com
4
5# 現在のユーザーの権限確認
6kubectl auth can-i --list

PodDisruptionBudget (PDB)

PDBはメンテナンス時の同時停止Pod数を制限。 ノードドレインやローリングアップデート時の可用性を保証。

pdb.yaml
yaml
1apiVersion: policy/v1
2kind: PodDisruptionBudget
3metadata:
4 name: web-pdb
5spec:
6 # 最低でも2つは維持
7 minAvailable: 2
8 # または: maxUnavailable: 1
9 selector:
10 matchLabels:
11 app: web

SRE/インフラ観点

リソース設定のポイント

  • • requests = 通常時の使用量、limits = ピーク時を考慮
  • • CPU limitsは設定しない方針もあり(スロットリング問題)
  • • LimitRange/ResourceQuotaでNamespaceを保護

Probesのベストプラクティス

  • • livenessとreadinessは異なるエンドポイント推奨
  • • 起動が遅いアプリにはstartupProbeを設定
  • • 依存サービスの障害でreadinessを落とす設計

HPAのチューニング

  • • スケールダウンは慎重に(stabilizationWindow設定)
  • • PDBと組み合わせて可用性を確保
  • • カスタムメトリクス(RPS等)でより精密な制御

セキュリティ

  • • 最小権限の原則でRBACを設計
  • • デフォルトServiceAccountは使わない
  • • Pod Security Standards(Restricted)を適用