kubernetes-operations について
Kubernetes 運用者向けの実践ガイドです。デプロイマニフェスト、Helm 設定、自動スケーリング、トラブルシューティングに加え、アンチパターンと本番環境の確認項目を扱います。
主な機能
- 01デプロイマニフェストとリソース設定
- 02環境別の Helm 設定
- 03HorizontalPodAutoscaler の設定
- 04Pod とノードの診断コマンド
- 05本番デプロイのチェックリスト
活用シーン
- 01高可用性 Kubernetes デプロイの計画
- 02環境別の Helm 設定の整理
- 03Pod の障害とリソース使用量の調査
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ rohitg00/ awesome-claude-code-toolkit --skill kubernetes-operationsインストール後、Claude Code で/kubernetes-operationsを入力して読み込まれたか確認します。
詳細
Kubernetes の運用
デプロイメントマニフェスト
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-server
labels:
app: api-server
version: v1
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: api-server
template:
metadata:
labels:
app: api-server
version: v1
spec:
containers:
- name: api
image: registry.example.com/api:1.2.0
ports:
- containerPort: 8080
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 10
periodSeconds: 15
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-credentials
key: url
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: api-server
リソースのリクエストと制限は必ず設定してください。高可用性を実現するには、トポロジー分散制約を使用してください。
Helm チャートの構成
chart/
Chart.yaml
values.yaml
values-staging.yaml
values-production.yaml
templates/
deployment.yaml
service.yaml
ingress.yaml
hpa.yaml
_helpers.tpl
# values.yaml
replicaCount: 2
image:
repository: registry.example.com/api
tag: "1.2.0"
pullPolicy: IfNotPresent
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
autoscaling:
enabled: true
minReplicas: 2
maxReplicas: 10
targetCPUUtilization: 70
HorizontalPodAutoscaler
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-server
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api-server
minReplicas: 2
maxReplicas: 10
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
トラブルシューティングのコマンド
# Pod diagnostics
kubectl describe pod <pod-name> -n <namespace>
kubectl logs <pod-name> -c <container> --previous
kubectl exec -it <pod-name> -- /bin/sh
# Resource usage
kubectl top pods -n <namespace> --sort-by=memory
kubectl top nodes
# Network debugging
kubectl run debug --image=nicolaka/netshoot --rm -it -- bash
nslookup <service-name>.<namespace>.svc.cluster.local
# Events sorted by time
kubectl get events -n <namespace> --sort-by='.lastTimestamp'
# Find pods not running
kubectl get pods -A --field-selector=status.phase!=Running
アンチパターン
securityContext.runAsNonRoot: trueを設定せずに、コンテナを root として実行する- リソースのリクエスト/制限を設定しない(スケジューリングの問題や、他のワークロードへの悪影響が発生します)
- 固定したイメージバージョンではなく、
latestタグを使用する - 重要なワークロードに
PodDisruptionBudgetを設定しない - ConfigMaps にシークレットを保存し、Secrets(または外部のシークレットマネージャー)を使用しない
- レプリケートされたデプロイメントで、Pod のアンチアフィニティを無視する
チェックリスト
- すべてのコンテナにリソースのリクエストと制限が設定されている
- Liveness プローブと Readiness プローブが設定されている
- イメージに
latestではなく、特定のバージョンタグが使用されている - シークレットが Kubernetes Secrets または外部のボールトに保存されている
- 本番ワークロードに PodDisruptionBudget が設定されている
- NetworkPolicies によって、名前空間間のトラフィックが制限されている
- HA のために、トポロジー分散制約またはアンチアフィニティが設定されている
- Helm の値が環境(ステージング、本番)ごとに分割されている
コメント
Markdown 対応