実務Tips
AWS CLI / kubectl / eksctl コマンド
Section titled “AWS CLI / kubectl / eksctl コマンド”クラスター管理
Section titled “クラスター管理”# クラスター作成(シンプル)eksctl create cluster \ --name my-cluster \ --region ap-northeast-1 \ --nodegroup-name standard-workers \ --node-type t3.medium \ --nodes 3 \ --nodes-min 1 \ --nodes-max 10 \ --managed
# クラスター作成(詳細設定)eksctl create cluster -f cluster.yaml
# クラスター一覧eksctl get cluster
# クラスター詳細aws eks describe-cluster \ --name my-cluster \ --region ap-northeast-1
# クラスター削除eksctl delete cluster \ --name my-cluster \ --region ap-northeast-1
# kubeconfig更新aws eks update-kubeconfig \ --name my-cluster \ --region ap-northeast-1
# クラスターバージョン確認kubectl version --shortノードグループ管理
Section titled “ノードグループ管理”# マネージドノードグループ作成eksctl create nodegroup \ --cluster my-cluster \ --name ng-spot \ --node-type t3.medium \ --nodes 3 \ --nodes-min 1 \ --nodes-max 10 \ --spot
# ノードグループ一覧eksctl get nodegroup \ --cluster my-cluster
# ノードグループスケールeksctl scale nodegroup \ --cluster my-cluster \ --name ng-general \ --nodes 5 \ --nodes-min 2 \ --nodes-max 10
# ノードグループ削除eksctl delete nodegroup \ --cluster my-cluster \ --name ng-spot \ --drain=true
# ノード一覧kubectl get nodes
# ノード詳細kubectl describe node <node-name>アドオン管理
Section titled “アドオン管理”# アドオン一覧確認eksctl get addons --cluster my-cluster
aws eks list-addons \ --cluster-name my-cluster
# VPC CNI アドオン追加eksctl create addon \ --cluster my-cluster \ --name vpc-cni \ --version latest \ --service-account-role-arn arn:aws:iam::123456789012:role/AmazonEKSVPCCNIRole
# CoreDNS アドオン更新eksctl update addon \ --cluster my-cluster \ --name coredns \ --version latest
# EBS CSI Driver追加eksctl create addon \ --cluster my-cluster \ --name aws-ebs-csi-driver \ --service-account-role-arn arn:aws:iam::123456789012:role/AmazonEKS_EBS_CSI_DriverRole
# アドオン削除eksctl delete addon \ --cluster my-cluster \ --name vpc-cniIRSA(IAM Roles for Service Accounts)
Section titled “IRSA(IAM Roles for Service Accounts)”# OIDC プロバイダー作成eksctl utils associate-iam-oidc-provider \ --cluster my-cluster \ --approve
# OIDC プロバイダーURL取得aws eks describe-cluster \ --name my-cluster \ --query "cluster.identity.oidc.issuer" \ --output text
# ServiceAccount with IAM Role作成eksctl create iamserviceaccount \ --cluster my-cluster \ --namespace default \ --name my-service-account \ --attach-policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess \ --approve
# ServiceAccount一覧eksctl get iamserviceaccount --cluster my-cluster
# ServiceAccount削除eksctl delete iamserviceaccount \ --cluster my-cluster \ --namespace default \ --name my-service-accountFargate Profile
Section titled “Fargate Profile”# Fargate Profile作成eksctl create fargateprofile \ --cluster my-cluster \ --name fp-default \ --namespace default
# ラベルセレクター付きeksctl create fargateprofile \ --cluster my-cluster \ --name fp-production \ --namespace production \ --labels env=production,tier=backend
# Fargate Profile一覧eksctl get fargateprofile \ --cluster my-cluster
# Fargate Profile削除eksctl delete fargateprofile \ --cluster my-cluster \ --name fp-defaultkubectl 基本操作
Section titled “kubectl 基本操作”# Pod一覧kubectl get podskubectl get pods -n kube-systemkubectl get pods --all-namespaceskubectl get pods -o wide
# Pod詳細kubectl describe pod <pod-name>
# Podログkubectl logs <pod-name>kubectl logs <pod-name> -c <container-name>kubectl logs -f <pod-name> # フォロー
# Pod内でコマンド実行kubectl exec -it <pod-name> -- /bin/bashkubectl exec <pod-name> -- env
# Deployment作成kubectl create deployment my-app --image=nginx:latest
# Deployment一覧kubectl get deployments
# Deployment スケールkubectl scale deployment my-app --replicas=5
# Deployment ローリングアップデートkubectl set image deployment/my-app nginx=nginx:1.21
# ロールバックkubectl rollout undo deployment/my-appkubectl rollout history deployment/my-appkubectl rollout status deployment/my-app
# Service作成kubectl expose deployment my-app --port=80 --target-port=8080 --type=LoadBalancer
# Service一覧kubectl get serviceskubectl get svc
# ConfigMap作成kubectl create configmap my-config --from-literal=key1=value1 --from-file=config.txt
# Secret作成kubectl create secret generic my-secret --from-literal=password=mypassword
# リソース削除kubectl delete pod <pod-name>kubectl delete deployment my-appkubectl delete service my-app高度なkubectl操作
Section titled “高度なkubectl操作”# リソース使用状況kubectl top nodeskubectl top pods
# Podのデバッグkubectl debug <pod-name> -it --image=busybox
# Port Forwardkubectl port-forward svc/my-app 8080:80
# Dry Runkubectl apply -f deployment.yaml --dry-run=clientkubectl apply -f deployment.yaml --dry-run=server
# リソース適用kubectl apply -f deployment.yamlkubectl apply -f ./ -R # ディレクトリ配下全て
# リソース差分確認kubectl diff -f deployment.yaml
# ラベル操作kubectl label pods <pod-name> env=productionkubectl get pods -l env=production
# アノテーション操作kubectl annotate pods <pod-name> description="my application"
# JSONPathkubectl get pods -o jsonpath='{.items[*].metadata.name}'kubectl get pods -o jsonpath='{.items[*].spec.containers[*].image}'ベストプラクティス
Section titled “ベストプラクティス”クラスター設計
Section titled “クラスター設計”本番環境: 推奨: - マルチAZ構成 - マネージドノードグループ - プライベートエンドポイント - VPCエンドポイント活用
ノード: - 複数インスタンスタイプ - Spot Instances併用 - オートスケーリング設定
開発環境: 推奨: - 小規模構成 - コスト最適化 - Fargate活用セキュリティ強化
Section titled “セキュリティ強化”ネットワーク: - プライベートサブネット配置 - セキュリティグループ厳格化 - NetworkPolicy実装 - Pod Security Standards
認証・認可: - IRSA活用 - RBAC詳細設定 - 最小権限の原則 - ServiceAccount分離
データ保護: - Secrets暗号化(KMS) - etcd暗号化 - イメージスキャン(ECR) - プライベートレジストリRBAC設定例
Section titled “RBAC設定例”# Role(namespace内権限)apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: namespace: production name: pod-readerrules:- apiGroups: [""] resources: ["pods"] verbs: ["get", "watch", "list"]---# RoleBindingapiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: read-pods namespace: productionsubjects:- kind: ServiceAccount name: my-service-account namespace: productionroleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.ioリソース管理
Section titled “リソース管理”推奨設定: requests: - 必須設定 - スケジューリング基準 - 最小値
limits: - 推奨設定 - 上限値 - OOM Killerトリガー
例: CPU: requests: 250m limits: 500m
Memory: requests: 256Mi limits: 512Miオートスケーリング設定
Section titled “オートスケーリング設定”# Horizontal Pod AutoscalerapiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: name: my-app-hpaspec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: my-app 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コスト最適化
Section titled “コスト最適化”戦略: Spot Instances: - 適切なワークロード選択 - 中断許容設計 - 70-90%コスト削減
Fargate: - 小規模Pod - バースト型ワークロード - ノード管理コスト削減
リソース適正化: - requests/limits最適化 - 未使用リソース削減 - HPAで効率的スケーリング
クラスター統合: - マルチテナント - namespace分離 - クラスター数削減モニタリング
Section titled “モニタリング”必須監視項目: クラスターレベル: - CPU / メモリ使用率 - ノード数 - Pod数 - API Server応答時間
ノードレベル: - CPU / メモリ - ディスクI/O - ネットワーク
Podレベル: - CPU / メモリ - 再起動回数 - レスポンスタイム - エラー率
ツール: - CloudWatch Container Insights - Prometheus + Grafana - X-Ray(分散トレーシング)トラブルシューティング
Section titled “トラブルシューティング”よくある問題:
1. Pod起動失敗: 確認: - kubectl describe pod <pod-name> - kubectl logs <pod-name> - イメージプル失敗 - リソース不足
対処: - イメージ名確認 - ECR認証確認 - ノード追加
2. Service接続不可: 確認: - kubectl get endpoints - セレクター確認 - NetworkPolicy
対処: - ラベル確認 - セキュリティグループ - ポート番号確認
3. ノード NotReady: 確認: - kubectl describe node - kubelet ログ - VPC CNI状態
対処: - ノード再起動 - ノード削除・追加 - CNI再インストール
4. IRSA動作しない: 確認: - OIDC プロバイダー - IAMロール信頼ポリシー - ServiceAccount annotation
対処: - アノテーション確認 - IAMポリシー確認 - Pod再作成バックアップ・DR
Section titled “バックアップ・DR”推奨: etcd バックアップ: - EKSマネージド(自動) - スナップショット保持
アプリケーション: - Velero活用 - 定期バックアップ - クロスリージョン
ディザスタリカバリ: - マルチリージョン構成 - IaC(Terraform/CloudFormation) - GitOps(ArgoCD/Flux)実務での重要ポイント:
- eksctl でクラスター管理効率化
- IRSA でセキュアなアクセス制御
- kubectl を使いこなす
- リソース管理でコスト最適化
- モニタリングで可観測性確保
- RBAC で細かい権限制御
- オートスケーリングで柔軟な対応