Closed11

cluster-autoscaler

koizumi7010koizumi7010

以下、READMEから引用

Cluster Autoscaler is a tool that automatically adjusts the size of the Kubernetes cluster when one of the following conditions is true:
there are pods that failed to run in the cluster due to insufficient resources.
there are nodes in the cluster that have been underutilized for an extended period of time and their pods can be placed on other existing nodes.

まとめると、ノードが不足していて特定のPodが配置できないときにノードを自動でスケールアウトしてくれたり、ノードが余剰になっていてそのノードで稼働しているPodが別のノードで動作できる状況であれば自動でノードをスケールインしてくれるツール

koizumi7010koizumi7010

main関数
https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/main.go#L691-L795

まず、以下の部分でログなどの各種設定を初期化している
https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/main.go#L692-L701

最初の方では、Leader ElectionではNodeの増減を行うcluster-autoscaler君をどこのノードで動かすかを決めている。あとは各種フラグをコマンドラインに追加している。
https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/main.go#L701-L707

koizumi7010koizumi7010

https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/main.go#L729-L744
ここで運用に必要なメトリクスやヘルスチェックなどのエンドポイントを設定してサーバーを起動するgoroutineが作成されて並行実行されている
内容としては、以下のようなエンドポイントにリクエストが来た際にデータを返すように設定している

  • /metrics:メトリクスデータを返すエンドポイント
  • /snapshotz:デバッグ情報を返すエンドポイント(有効なら設定される)
  • /health-check:ヘルスチェックの結果を返すエンドポイント

最後に指定のアドレスでサーバーを起動して、リクエストを受け付ける

koizumi7010koizumi7010

そのrun()関数で実際にautoscalerの起動とスケールイン・スケールアウトの処理が行われている
https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/main.go#L651-L689

↓下記の部分でcluster-autoscalerを起動して、シグナルハンドラの設定と、ヘルスチェックが稼働している
https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/main.go#L656-L665

↓バックグラウンドタスクを実行
https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/main.go#L667-L670

frequentLoopsEnabledが有効な場合は、無限ループの中でRunAutoscalerOnceを前回実行時間を参照して実行する。無効な場合は、一定期間sleepしてRunAutoscalerOnceを実行する無限ループが走る。
https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/main.go#L672-L688

koizumi7010koizumi7010

Podの配置ロジック(ノードをいくつ増やせばいいか)にはBinpacking Algolithmを用いている

koizumi7010koizumi7010

Q. スケールアウトの際に、PodがUnscheduledになってからではタイミングとして遅くないのか。Nodeの空き容量が少なくなってきた時点で早めにスケールする(like HPA)ようなことはできないのか。
A. Cluster Autosaclerの場合は基本的にそういった仕組みはない。あくまでもPodのPendingを起点にトリガーされる。しかし、対処法はいくつか存在する。
有名な方法は、役割のないPodを用意する方法だ。
Nodeの利用率については、PodのCPU利用率ではなく、PodのRequest値によって決まるものである。そのため、ある程度のRequest値を持った空のPodをデプロイしておくことで、実際よりも多くのRequest値があるように見せることができる。

先のブログに上記のような記載があった
これによく使われるコンポーネントが cluster-overprovisioner
https://github.com/codecentric/cluster-overprovisioner
ベースになっているのは cluser-proportional-autoscaler(これもそのうちコード読みたい)
https://github.com/kubernetes-sigs/cluster-proportional-autoscaler

koizumi7010koizumi7010

ザッとこんな感じかな
また気になったことがあったら追記する

このスクラップは2025/01/25にクローズされました