Slide 1

Slide 1 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Announcing Karpenter v1.0.0 Discover how Karpenter works Ryota Yamada Global Solutions Architect Amazon Web Services Inc. Kubernetes Meetup Tokyo #67

Slide 2

Slide 2 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 好きなAWSのサービス / OSS 趣味: 謎解き、サッカー観戦、フットサル 経歴: Ø Engineer, CTO, TechAdvisor@startups Ø TechTrainingSpecialist@AWS Japan Ø Solutions Archietct@AWS Ryota Yamada / riita10069 Amazon Web Services / Global Solutions Architect

Slide 3

Slide 3 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3 https://aws.amazon.com/jp/blogs/containers/announcing-karpenter-1-0/

Slide 4

Slide 4 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 4 https://github.com/kubernetes/org/issues/4258

Slide 5

Slide 5 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Karpenter はなぜ登場したのか 5

Slide 6

Slide 6 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Cluster Autoscaler with AWS の課題 6 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 • CA は “node group” に対してスケーリングを実施 Ø AWS では「node group」=「Auto Scaling グループ」 • CA が Node を追加する際のシミュレーションの関係から、 ASG の属性(例: vCPU, memory)は揃えることが推奨 • 多様なワークロード に対応するためには、 多くの Auto Scaling グループを予め⽤意する Ø インスタンスタイプ Ø 購⼊オプション(オンデマンド、スポットインスタンス) Ø アベイラビリティーゾーン Ø ..., etc AWS では node group の実態が Auto Scaling グループだがこれは AWS の概念 Kubernetes のオブジェクトでも、Kubernetesのコントローラーでもない

Slide 7

Slide 7 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Kubernetes の仕組み(カスタムリソース、コントローラー)で スケーリングのみならず Node Lifecycle そのものを管理したい 7

Slide 8

Slide 8 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Karpenter is no mere node autoscaler 8 CA ASG EC2 API Cluster Auto- scaler Auto Scaling Group Pod Autoscaling Pending pods EC2 Fleet (instance) “node group” ではなく “instance” を直接管理 Kubernetes ネイティブの Node Lifecycle Manager

Slide 9

Slide 9 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Karpenter の機能 9

Slide 10

Slide 10 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 10 Node Launch Karpenter の役割は⼤きく分けて 2 つ Disruption

Slide 11

Slide 11 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 11 Node Launch Karpenter の役割は⼤きく分けて 2 つ Disruption

Slide 12

Slide 12 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Node Launch 12 • Scheduling (kube-scheduler) • Batching • Binpacking

Slide 13

Slide 13 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Node Launch 13 • Scheduling (kube-scheduler) • Batching • Binpacking

Slide 14

Slide 14 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Karpenter の Scheduling への誤解 14 Karpenter って Pod を kube-scheduler ではなく Karpenter ⾃⾝が binding するんですよね︖ No. Karpenter リリース当初はそのような動きでしたが、現在は改善され kube-scheduler を⽤いています。 https://github.com/aws/karpenter-provider-aws/pull/1856

Slide 15

Slide 15 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 15 Sample well-known labels added to nodes for scheduling Label Example topology.kubernetes.io/zone us-east-2a node.kubernetes.io/instance-type g4dn.8xlarge kubernetes.io/os linux kubernetes.io/arch amd64 karpenter.sh/capacity-type spot karpenter.k8s.aws/instance-hypervisor nitro karpenter.k8s.aws/instance-encryption-in- transit-supported true karpenter.k8s.aws/instance-category g nodeSelector, nodeAffinity を利⽤して Pod は選択された Node に Scheduling される

Slide 16

Slide 16 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Node Launch 16 • Scheduling (kube-scheduler) • Batching • Binpacking

Slide 17

Slide 17 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Batcher – expanding window 17 https://github.com/kubernetes-sigs/karpenter/blob/633135958bc4584cee99e7956c82efcca761e9a2/pkg/controllers/provisioning/batcher.go#L50-L76 Pending Pod を収集する Window を決定する仕組み Pending Pod を⾒たら即座に Node の Provisioning を起動す ると1 Pod のみを⾒て Node 選択を⾏うことになり⾮効率 素早い起動とある程度まとまった Pod を⾒るバランス 1 * time.Second で他の Pod が現れなければ終了 他の Pod が現れた場合は、 idle をリセットし、 BatchIdleDuration の時間が経過すればアイドルとなり終了 ⼤規模なクラスタでは、 BatchIdleDuration が経たない BatchMaxDuration の時間が経過すればタイムアウトとなり終了 https://github.com/aws/karpenter-provider-aws/blob/15eb36f1b980be941434bf447be6dbf7ec6bb09e/charts/karpenter/values.yaml#L150-L156 BatchMaxDuration と BatchIdleDuration は Helm で設定可能 デフォルトでは、Max が10s, Idle が 1s https://karpenter.sh/v1.0/reference/settings/

Slide 18

Slide 18 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Node Launch 18 • Scheduling (kube-scheduler) • Batching • Binpacking

Slide 19

Slide 19 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Bin-packing 19 ひとつの Window の Pod をみてどの Node を起動するか︖利⽤率の最⼤化ではなく、コストの最⼩化 仮想ノードを作成し シミュレーションする Launch Decision NodeSelector, NodeAffinity, Taint/Toleration, PodAffinity VolumeTopology(PVC), Available ENI, DaemonSet, Spot/Ondemand EC2 Fleet API を⽤いて price-capacity-optimized 戦略(Spot) lowest-price戦略(ondemand)に基づき EC2 インスタンスを決定 利⽤可能なインスタンスタイプを列挙 NodePool の Requrement と 合致するものに限定 CPUアーキテクチャ、オペレーティングシステム インスタンス(category, family, size, hypervisor, generation) EC2 describe instances を利⽤して列挙

Slide 20

Slide 20 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 20 Node Launch Karpenter の役割は⼤きく分けて 2 つ Disruption

Slide 21

Slide 21 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Disruption 21 • Manual • NodeClaim を直接的に削除することで手動でノードの削除を行う • Expiration • Karpenter は NodePool の spec.template.spec.expireAfter の値に基づいて、設定された秒 数を経過したノードを Expiration として Annotations を付与し、ノードを中断します • Consolidation • Karpenter は様々な状況に応じて、クラスターのコストを削減します • Drift • 望ましい仕様からドリフトした (離れた) ノードを中断します • Interruption • Karpenter はノードに影響を与える可能性のある中断イベント (スポット中断など) を監視 し、イベントに先⽴ってノード上の Pod を退避し、ノードを終了させます 原則、 Eviction API 経由で Pod を終了してからノードの削除を⾏う

Slide 22

Slide 22 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Expiration – Forceful Expiration 22 PDB や do-not-disruptアノテーション が付与された Pod のある Node が終了できない。 TerminationGracePeriod が経過した場合、セーフガードを無視して、期限切れノードの終了プロセスを開始します 状態1 → 状態2 : 中断、不健全、または期限切れのノードを検出(Nodeのタイムスタンプで判断) すると Karpenter は即座に Gracefully Terminating を開始 状態4: ノードにNoSchedule Taint を追加し、NodeClaim に終了猶予期間 (TerminationGracePeriod) のタイムスタンプを設定 この時点でノードのドレイニングを開始しますが、PDB と do-not-disrupt アノテーションは尊重 状態4 → 状態5: ドレイン可能な Pod がなくなるか、終了猶予期間 (TerminationGracePeriod)に達すると、 Karpenter は直接的にインスタンスを終了し、強制的な終了とクリーンアップを開始 状態5 → 状態6: 終了が完了し、ネットワーク接続とボリュームがクリーンアップされると、ノードをクラスターから削除

Slide 23

Slide 23 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Drift 23 Amazon Elastic Compute Cloud (Amazon EC2) (m5.large) Amazon Elastic Compute Cloud (Amazon EC2) (c5.large) Drifted apiVersion: karpenter.sh/v1 kind: NodePool metadata: name: reserved-instance spec: template: spec: requirements: - key: node.kubernetes.io/instance-type operator: In values: ["c5.large"] • Spec hashing: Taints, Labels • 単純に⽂字列を⽐較して、仕様がノードの起動時から変更されたかどうかを判断し、 変更されていればそのノードをドリフトしたものとして指名する • Periodic polling: Requirements, AMI • Karpenterは AWS Systems Manager パラメータストアを継続的にポーリングし、最新のAMIを発⾒ • ワーカーノードの AMI version up が⾃動で⾏われる(Default AMI で latest 利⽤時のみ、固定可能)

Slide 24

Slide 24 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 24 Consolidation – for Cost Optimization コストを最適化するため、 不要なノードを disruption する

Slide 25

Slide 25 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Consolidation – for Cost Optimization 25 GetCandidates Disruption cost で並び替え Simulate Consolidation Execute Command コストを最適化するため、不要なノードを disruption する Pending Pods がこれから起動する予定のノードやなどを除外する Empty Node が⾒つかった場合、即座に Consolidationを実⾏ DisruptionCost = ReschedulingCost * LifetimeRemaining Eviction が容易でなおかつTTL が近いものが低コストになる 中断対象の Node にNoScedule Taint を付与し、 先に代替ノードを起動し、完了してから古いノードを削除する Consolidation を実⾏した場合に PDB や taints などを考慮し evict された Pod が新規の Node に⽴ち上がるかをシミュレーション

Slide 26

Slide 26 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 26 LifetimeRemaining https://github.com/kubernetes-sigs/karpenter/blob/633135958bc4584cee99e7956c82efcca761e9a2/pkg/utils/disruption/disruption.go#L34-L46 Appendix Expiration time に近づくほど⼩さな値になっていく [0.0. 1.0] に収まる値

Slide 27

Slide 27 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 27 ReschedulingCost Reschedule 対象のすべての Pod の EvictionCost の総和 https://github.com/kubernetes-sigs/karpenter/blob/633135958bc4584cee99e7956c82efcca761e9a2/pkg/utils/disruption/disruption.go#L48-L70 Appendix

Slide 28

Slide 28 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Interruption 28 • Spot Interruption Warnings • Scheduled Change Health Events (Maintenance Events) • Instance Terminating Events • Instance Stopping Events

Slide 29

Slide 29 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 29 Node Launch Karpenter の役割は⼤きく分けて 2 つ Disruption

Slide 30

Slide 30 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Thank you! Ryota Yamada Global Solutions Architect [email protected] 30