Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Karpenter を用いた Kubernetes 環境でのコンテナ活用最適化/Optimizing Container Workloads using Karpenter on Kubernetes

Karpenter を用いた Kubernetes 環境でのコンテナ活用最適化/Optimizing Container Workloads using Karpenter on Kubernetes

Kyosuke Ochimizu

March 27, 2024
Tweet

More Decks by Kyosuke Ochimizu

Other Decks in Technology

Transcript

  1. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter を⽤いた Kubernetes 環境でのコンテナ活⽤最適化 落⽔ 恭介 コンテナスペシャリスト ソリューションアーキテクト アマゾン ウェブ サービス ジャパン合同会社 Wayne Toh Compute Specialist Solution Architect Amazon Web Services
  2. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. アジェンダ 2 • Karpenter のご紹介 Ø Kubernetes におけるオートスケーリング Ø Karpenter と Cluster Autoscaler Ø Karpenter の利点 • Karpenter のコンセプト • マルチアーキテクチャのコンテナイメージ • デモ (Demo) Ø マルチアーキテクチャ向けコンテナパイプライン
  3. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter のご紹介 3
  4. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter とは︖ 4 柔軟で⾼パフォーマンスのオープンソース Kubernetes クラスターオートスケーラー 可⽤性の向上 コスト最適化 効率的な運⽤ kubernetes-sigs/karpenter: https://github.com/kubernetes-sigs/karpenter aws/karpenter-provider-aws: https://github.com/aws/karpenter-provider-aws
  5. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Kubernetes のオートスケーリング 5 1. Horizontal Pod Autoscaling (HPA) 2. Vertical Pod Autoscaling (VPA) 3. Cluster Autoscaler Metrics Store HPA Pending pods X VPA Scale up ASG Cluster Autoscaler API call to ASG ※ ASG: Auto Scaling グループ
  6. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Kubernetes のオートスケーリング 6 1. Horizontal Pod Autoscaling (HPA) 2. Vertical Pod Autoscaling (VPA) 3. Cluster Autoscaler Metrics Store HPA Pending pods Cluster Autoscaler X VPA Scale up ASG API call to ASG Karpenter は Cluster Autoscaler に該当するもの ※ ASG: Auto Scaling グループ
  7. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. [補⾜] Cluster Autoscaler の動作 7 EKS node Kubernetes Cluster Autoscaler Auto Scaling 1. リソース (Node) の不⾜により Pod が Pending 状態に 2. Auto Scaling グループの 「希望するキャパシティ」を増加 EKS node Pod (Pending) 3. 新しい Node の追加 4. Pod のスケジューリング
  8. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Cluster Autoscaler (CAS) の課題 8 EKS Cluster AZ 1 m5.4xl AZ1 OD ASG m5.4xl AZ1 SPOT ASG m5.4xl AZ1 OD ASG c5.4xl AZ1 SPOT ASG p3.8xl AZ1 OD ASG p3.8xl AZ1 SPOT ASG AZ 2 m5.4xl AZ2 OD ASG m5.4xl AZ2 SPOT ASG m5.4xl AZ2 OD ASG c5.4xl AZ2 SPOT ASG p3.8xl AZ2 OD ASG p3.8xl AZ2 SPOT ASG 可⽤性、コストを考慮した最適な設定の難易度が⾼い • CAS は “node group” に対してスケーリングを実施 Ø AWS では「node group」=「Auto Scaling グループ」 • CAS が Node を追加する際のシミュレーションの関係から、 ASG の属性(例: vCPU, memory)は揃えることが推奨 • 多様なワークロード (Pod) に対応するためには、 複数の Auto Scaling グループを予め⽤意する必要がある Ø インスタンスタイプ Ø 購⼊オプション(オンデマンド、スポットインスタンス) Ø アベイラビリティーゾーン Ø ..., etc
  9. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter の動作 9 CA ASG EC2 API Cluster Auto- scaler Auto Scaling Group Pod Autoscaling Pending pods EC2 Fleet (instance) “node group” ではなく “instance” に対して スケーリングを実施
  10. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter による Node 管理 10 EKS cluster Karpenter NodePool AZ 1 AZ 2 AZ 3 g4 g5 P4
  11. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter の利点 11 可⽤性の向上 コスト最適化 効率的な運⽤ • ワークロードの負荷や Pod の配置要求に応じて、適切な Node を配置 • Auto Scaling グループを経由しないため、CAS よりも⾼速な Node 起動を実現 • 稼働率の低い Node の置き換えや Node の統合による不要なリソースの削除 • オンデマンドへの切り替えやスポットインスタンス中断通知へのネイティブ対応 • node group(Auto Scaling グループ)の事前準備が不要 • 少数の Node 起動設定(NodePool、EC2NodeClass)で多くの Pod 要求に対応
  12. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter のコンセプト 12
  13. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter のコンセプト 13 クラスター管理者 NodePool NodeClass (EC2NodeClass) 設定 参照 Node Node の追加、削除
  14. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. NodePool 14 NodePool • Karpenter が作成可能な Node の制約 • = 対応可能な Pod 要求 • 幅広い Pod 要求を単⼀の NodePool でカバー可能 apiVersion: karpenter.sh/v1beta1 kind: NodePool metadata: name: default spec: template: spec: requirements: - key: kubernetes.io/arch operator: In values: ["amd64”, "arm64”] - key: kubernetes.io/os operator: In values: ["linux"] - key: karpenter.sh/capacity-type operator: In values: [”spot”, "on-demand"] - key: karpenter.k8s.aws/instance-category operator: In values: ["c", "m", "r"] - key: karpenter.k8s.aws/instance-generation operator: Gt values: ["2"] nodeClassRef: name: default limits: cpu: “1000” Node の制約 NodeClass (EC2NodeClass) の参照 NodePool で管理可能なリソース上限
  15. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. NodePool 詳細 15 CPU アーキテクチャ supported values: amd64, arm64 オペレーティングシステム (OS) supported values: linux, windows EC2 購⼊オプション supported values: spot, on-demand インスタンスファミリー supported values: c, m, r, ... インスタンスの世代 supported values: 1, 2, 3, ... apiVersion: karpenter.sh/v1beta1 kind: NodePool metadata: name: default spec: template: spec: requirements: - key: kubernetes.io/arch operator: In values: ["amd64”, "arm64”] - key: kubernetes.io/os operator: In values: ["linux"] - key: karpenter.sh/capacity-type operator: In values: [”spot”, "on-demand"] - key: karpenter.k8s.aws/instance-category operator: In values: ["c", "m", "r"] - key: karpenter.k8s.aws/instance-generation operator: Gt values: ["2"] nodeClassRef: name: default limits: cpu: “1000”
  16. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. NodeClass (EC2NodeClass) 16 EC2NodeClass • NodePool が起動する EC2 インスタンス設定 • Cloud provider ごとに NodeClass に該当する カスタムリソースが存在 • AWS の場合は EC2NodeClass が該当 apiVersion: karpenter.k8s.aws/v1beta1 kind: EC2NodeClass metadata: name: default spec: amiFamily: AL2023 subnetSelectorTerms: - tags: karpenter.sh/discovery: eks-pod-identity securityGroupSelectorTerms: - tags: karpenter.sh/discovery: eks-pod-identity role: "KarpenterNodeRole-eks-pod-identity"
  17. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. EC2NodeClass 詳細 17 apiVersion: karpenter.k8s.aws/v1beta1 kind: EC2NodeClass metadata: name: default spec: amiFamily: AL2023 subnetSelectorTerms: - tags: karpenter.sh/discovery: eks-pod-identity securityGroupSelectorTerms: - tags: karpenter.sh/discovery: eks-pod-identity role: "KarpenterNodeRole-eks-pod-identity" AMI の種類 supported values: AL2, AL2023, Windows2019, ... Node サブネットの検出ロジック supported values: tags, id Node セキュリティグループの検出ロジック supported values: tags, id, name Node にアタッチする IAM ロール
  18. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter によるスケジューリング (1) 18 3 つのレイヤー (Layered constraints) で構成 例: nodeSelector, Node affinity
  19. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter によるスケジューリング (2) 19 Pod の要求が NodePool の制約に含まれていなければ Node は起動されない amd64 amd64 amd64 arm64 Pod Nodes Pod Nodes
  20. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Karpenter によるマルチアーキテクチャ環境の管理 20 単⼀あるいは少数のリソース・設定で効率的に運⽤可能 Cluster Autoscaler Karpenter m6i.large m6i.xlarge m6g.medium m6g.2xlarge c6i.large c6i.xlarge c6g.medium c6g.2xlarge ... amd64 amd64 arm64 arm64 (中略) spec: requirements: - key: kubernetes.io/arch operator: In values: ["amd64”, "arm64”] - key: karpenter.k8s.aws/instance-category operator: In values: ["c", "m"] - key: karpenter.k8s.aws/instance-generation operator: Gt values: [”6"] (中略) NodePool
  21. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. マルチアーキテクチャの コンテナイメージ 21
  22. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. コンテナイメージとは 22 • ベースイメージ Ø イメージの基礎 Ø Ubuntu, Debian, Amazon Linux, etc • イメージレイヤ Ø アプリケーションコードや 3rd party の ライブラリなど ベースイメージ イメージレイヤ イメージレイヤ イメージレイヤ イメージレイヤ U buntu /vendor/libA /vendor/libB /app/helper /app/m ain ベースイメージにイメージレイヤが積み重ねられたもの
  23. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. コンテナイメージの取得フロー (Amazon ECR の場合) 23 Amazon Elastic Container Registry (Amazon ECR) Amazon Simple Storage Service (Amazon S3) クライアント • Docker CLI • containerd • ..., etc 1. イメージマニフェストの取得 2. イメージレイヤの取得 JSON .tar.gz .tar.gz
  24. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. イメージマニフェスト 24 Amazon ECR リポジトリ Image Manifest Config Layers • Architecture • OS • ... Layer 0 Layer 1 Layer ...
  25. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. イメージマニフェスト 25 Amazon ECR リポジトリ Image Manifest Config Layers • Architecture • OS • ... Layer 0 Layer 1 Layer ... 1 つのコンテナイメージ (イメージマニフェスト) と CPU アーキテクチャが⼀対⼀で対応 例) linux, arm64 向け
  26. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ホストとコンテナの CPU アーキテクチャ対応 26 レジストリ OS (x86) Java (x86) App イメージ (x86) OS (arm) Java (arm) App イメージ (arm) sample-app:v1 sample-app:v1-arm コンテナホスト (x86) コンテナホスト (Graviton2 / arm) コンテナホストの CPU アーキテクチャごとにコンテナイメージが必要
  27. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. イメージインデックス: 複数イメージマニフェストへの参照 27 Amazon ECR リポジトリ Image Index Manifests Linux, amd64 Linux, arm64 Windows, amd64 Image Manifest Linux, amd64 Image Manifest Linux, arm64 Image Manifest Windows, amd64
  28. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. イメージレジストリのマルチアーキテクチャ対応 28 レジストリ コンテナホスト (x86) コンテナホスト (Graviton2 / arm) イメージ (x86) sample-app:v1-x86 OS, App, ... (x86) Image Index sample-app:v1 イメージ (arm) sample-app:v1-arm OS, App, ... (arm) docker docker 同じタグで、複数のコンテナイメージをサポート
  29. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. [補⾜] Open Container Initiative (OCI) と Docker 29 ⽂脈によっては OCI の⽤語が使われることも、Docker の⽤語が使われることもある Open Container Initiative - Open Container Initiative https://opencontainers.org/ Docker Manifest list application/vnd.docker.distribution. manifest.list.v2+json Image Manifest application/vnd.docker.distribution. manifest.v2+json OCI Image Index application/vnd.oci.image.index.v1+json Image Manifest application/vnd.oci.image.manifest.v1+json 互換性
  30. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 30 マルチアーキテクチャに対応した コンテナイメージのビルド
  31. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. マルチアーキテクチャ対応に必要な作業 31 1. イメージマニフェストの作成(コンテナイメージのビルド) Ø amd64 (x86)、arm64 など必要なコンテナイメージをそれぞれビルドする 2. イメージインデックスの作成 Ø 1 で作成したイメージマニフェストを参照リストに追加する
  32. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. CI/CD パイプラインの例 32 開発/テスト イメージのビルド/登録 デプロイ/運⽤ ソースコードの 単体テスト、Lint、etc ビルド & push (x86 ⽤) ビルド & push (Arm ⽤) イメージインデックス 作成 & push デプロイ
  33. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Multi-Arch Image Manifest – Method 1 $ docker manifest create myapp:abc1234 myapp:abc1234-arm64 myapp:abc1234-x86 $ docker manifest push myapp:abc1234 myapp:abc1234-arm64 myapp:abc1234-x86 myapp:abc1234
  34. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Multi-Arch Docker Manifest docker manifest inspect amazonlinux:2 { "schemaVersion": 2, "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json", "manifests": [ { "mediaType": "application/vnd.docker.distribution.manifest.v2+json", "size": 529, "digest": "sha256:c9ce7208912b7897c9a4cb273f20bbfd54fd745d1dd64f5e625fff6778469e69", "platform": { "architecture": "amd64", "os": "linux" } }, { "mediaType": "application/vnd.docker.distribution.manifest.v2+json", "size": 529, "digest": "sha256:06a218d9e2f63e912ffc51c7052c6e632e53dfcf8661f96b4028d7db08f931a3", "platform": { "architecture": "arm64", "os": "linux", "variant": "v8" } } ] }
  35. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Multi-arch using CodePipeline Code Repository Codebuild (x86) Codebuild (arm64) Create multi-arch manifest Deploy on EKS with mixed compute