Kubernetesはコンテナ化されたアプリケーションを自動デプロイ・スケール・管理するオーケストレーションプラットフォームです。本記事ではKubernetesの基礎から実践的な本番運用まで解説します。
Kubernetesが必要な理由:Dockerだけでは不十分なケース
Dockerだけでも開発環境では問題ありませんが、本番環境では以下の課題が生じます。Kubernetesはこれらを解決します。
- 高可用性:サーバー障害時の自動フェイルオーバー
- 自動スケール:トラフィックに応じたコンテナ数の自動調整
- ローリングアップデート:ダウンタイムなしのデプロイ
- ヘルスチェックと自動復旧:障害コンテナの自動再起動
- サービスディスカバリ:コンテナ間の通信管理
- 設定管理:ConfigMapとSecretによる環境設定の一元管理
Kubernetesの主要コンポーネント
| コンポーネント | 説明 |
|---|---|
| Pod | K8sの最小単位。1つ以上のコンテナをまとめたもの |
| Deployment | Podの宣言的管理(レプリカ数、更新戦略など) |
| Service | Podへの安定したネットワークアクセスを提供 |
| Ingress | 外部からのHTTP/HTTPSトラフィックのルーティング |
| ConfigMap | アプリの設定情報を管理 |
| Secret | パスワード・APIキーなどの機密情報を管理 |
| PersistentVolume | 永続的なストレージの管理 |
| HorizontalPodAutoscaler | CPU/メモリ使用率に基づく自動スケール |
| Namespace | リソースの論理的な分離(dev/staging/prod) |
実践:Next.jsアプリのKubernetesデプロイメント設定
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nextjs-app
namespace: production
labels:
app: nextjs-app
spec:
replicas: 3
selector:
matchLabels:
app: nextjs-app
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: nextjs-app
spec:
containers:
- name: nextjs
image: myorg/nextjs-app:v1.2.3
ports:
- containerPort: 3000
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "512Mi"
cpu: "500m"
env:
- name: NODE_ENV
value: "production"
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: app-secrets
key: database-url
livenessProbe:
httpGet:
path: /api/health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /api/ready
port: 3000
initialDelaySeconds: 5
periodSeconds: 5
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: nextjs-service
namespace: production
spec:
selector:
app: nextjs-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: ClusterIP
---
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nextjs-ingress
namespace: production
annotations:
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
tls:
- hosts:
- myapp.example.com
secretName: myapp-tls
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nextjs-service
port:
number: 80
HorizontalPodAutoscaler(HPA)で自動スケール
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nextjs-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nextjs-app
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
managed Kubernetesサービス比較
| サービス | 提供元 | 特徴 | 月額目安 |
|---|---|---|---|
| EKS | AWS | AWSサービスとの連携が強力 | $73〜(Control Plane) |
| GKE | Google Cloud | K8sの開発元、Autopilotモードが便利 | $73〜 |
| AKS | Azure | Control Plane無料、Azure ADと統合 | $0(管理費)+ ノード代 |
| DigitalOcean K8s | DigitalOcean | シンプルで使いやすい、低コスト | $12〜 |
まとめ:K8sを学んでインフラエンジニアとしての市場価値を上げよう
KubernetesはDevOps・SREエンジニアに必須のスキルとなっています。習得の難易度は高いですが、一度マスターすればキャリアの大きな武器になります。まずはローカルでkind(Kubernetes in Docker)を使って実験環境を構築するのがおすすめです。