1 分で読了 #devops
このページの目次

Kubernetes 運用マニュアル

Kubernetes はコンテナオーケストレーションプラットフォームです。主要な概念: pod (最小スケジューリング単位), deployment (宣言的な pod 管理), service (安定したネットワークエントリポイント), ingress (L7 ルーティング), configmap/secret (設定の注入), namespace (リソースの分離)。

確認

# ノード
kubectl get nodes -o wide                               # IP、OS、kubelet バージョンを含む
kubectl describe node <node>                            # 容量、条件、イベント

# Pod
kubectl get pods -A                                     # 全 namespace
kubectl get pods -n <ns> -o wide                        # IP、ノード、ステータスを含む
kubectl get pods -n <ns> --sort-by=.status.startTime    # 作成時刻順にソート
kubectl get pods -n <ns> --field-selector status.phase=Running

# 詳細表示 (最も一般的なデバッグコマンド: Events を確認してどこが詰まっているか把握)
kubectl describe pod <pod> -n <ns>
# Events の解釈:
#   FailedScheduling → リソース不足またはノードアフィニティの不一致
#   Failed to pull image → イメージ名/バージョンが間違っているか、レジストリに到達できない
#   CrashLoopBackOff → コンテナ起動直後に終了 (logs を確認)

# ログ
kubectl logs <pod> -n <ns> --tail=100 -f
kubectl logs <pod> -n <ns> --previous                    # 前回のクラッシュ時のログ
kubectl logs -l app=myapp -n <ns> --tail=50              # ラベルセレクター

# その他のリソース
kubectl get deploy,svc,ingress,cm,secret,pvc -n <ns>
kubectl get events -n <ns> --sort-by='.lastTimestamp'    # namespace 内の全イベント
kubectl top pods -A                                      # リソース使用状況 (metrics-server が必要)
kubectl config get-contexts                              # 現在の kubeconfig コンテキスト

操作

kubectl apply -f manifest.yaml                           # 作成/更新
kubectl delete -f manifest.yaml
kubectl delete pod <pod> -n <ns>                        # pod の削除 (deployment によって再作成される)
kubectl rollout restart deployment/<name> -n <ns>        # ローリング再起動
kubectl scale deployment/<name> --replicas=3 -n <ns>

kubectl exec -it <pod> -n <ns> -- sh
kubectl exec <pod> -n <ns> -- cat /etc/config
kubectl cp <pod>:/path/file ./file -n <ns>
kubectl port-forward <pod> 8080:80 -n <ns>               # ローカルデバッグ用
kubectl config use-context <context>

デバッグ

# pod が起動しない場合: Events を確認
kubectl describe pod <pod> -n <ns> | tail -20

# 完全な yaml の表示 (デフォルト値 + 実行時状態を含む)
kubectl get pod <pod> -n <ns> -o yaml

# 一時的なデバッグ用コンテナ
kubectl run debug --rm -it --image=alpine -- sh

# RBAC 権限の確認
kubectl auth can-i create pods --as=system:serviceaccount:<ns>:<sa>