Amazon EKS で Cluster Autoscaler を設定するには、どうすればよいですか?
最終更新日: 2020 年 8 月 13 日
Amazon Elastic Kubernetes Service (Amazon EKS) に Cluster Autoscaler を設定したいです。
簡単な説明
Kubernetes Cluster Autoscaler は、以下のいずれかの条件に該当する場合に Kubernetes クラスターのサイズを自動的に調整します。
- リソース不足のため、クラスター内で実行に失敗したポッドがある。
- クラスター内に長期間使用されていないノードがあり、それらのポッドを他の既存のノードに配置できる。
Cluster Autoscaler は、指定された任意の Auto Scaling グループ内のワーカーノードを拡張し、クラスター内のデプロイメントとして実行されます。
注意: 以下の解決方法は、AWS CloudFormation テンプレートによって作成された関連するワーカーノードがある有効な Amazon EKS クラスターがあることを前提としています。解決方法によれば、自動検出セットアップが使用されますが、1 つまたは複数の Auto Scaling グループを指定して Cluster Autoscaler を設定することもできます。
解決方法
自動検出の設定
1. AWS CloudFormation コンソールを開いて、スタックを選択してから、[Resources] タブを選択します。
2. スタックによって作成された Auto Scaling グループのリソースを見つけるには、[論理 ID] 列で NodeGroup を探します。詳細については、セルフマネージド Amazon Linux 2 ノードの起動を参照してください。
3. [Amazon Elastic Compute Cloud (Amazon EC2) コンソール] を開き、ナビゲーションペインで [Auto Scaling グループ] を選択します。
4. [Tags] タブを選択し、 [Add/Edit tags] を選択します。
5. [Auto Scaling グループタグの追加/編集] ウィンドウで、awsExampleClusterName を Amazon EKS クラスターの名前に置き換えて、次のタグを入力します。その後、[保存] を選択します。
Key: k8s.io/cluster-autoscaler/enabled
Key: k8s.io/cluster-autoscaler/awsExampleClusterName
注意: 入力したタグのキーには値がありません。Cluster Autoscaler は、キーに設定された値を無視します。
IAM ポリシーの作成
1. 次の例に基づいて、ClusterAutoScaler という AWS Identity and Access Management (IAM) ポリシーを作成します。これにより、Cluster Autosaler を実行しているワーカーノードが、必要なリソースとアクションにアクセスできるようになります。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"ec2:DescribeLaunchTemplateVersions"
],
"Resource": "*"
}
]
}
注意: 上記のポリシーをワーカーノードのロールに追加することで、それぞれの EC2 インスタンスで実行されているすべてのポッドまたはアプリケーションは追加の IAM アクセス許可を使用できるようになります。
2. Amazon EKS ワーカーノードに関連付けられているインスタンスロールに新しいポリシーをアタッチします。
Cluster Autoscaler のデプロイ
1. GitHub の Cluster Autoscaler プロジェクトで提供されているデプロイのサンプルファイルをダウンロードするには、次のコマンドを実行します。
wget https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml
2. ダウンロードした YAML ファイルを開き、次の例に基づいてクラスター名 (awsExampleClusterName) を設定します。そして、変更を保存します。
...
command:
- ./cluster-autoscaler
- --v=4
- --stderrthreshold=info
- --cloud-provider=aws
- --skip-nodes-with-local-storage=false
- --expander=least-waste
- --node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/awsExampleClusterName
...
3. Cluster Autoscaler デプロイを作成するには、次のコマンドを実行します。
kubectl apply -f cluster-autoscaler-autodiscover.yaml
4. Cluster Autoscaler のデプロイログでデプロイエラーを確認するには、次のコマンドを実行します。
kubectl logs -f deployment/cluster-autoscaler -n kube-system
ワーカーノードのスケールアウトのテスト
1. 現在のワーカーノードの数を表示するには、次のコマンドを実行します。
kubectl get nodes
2. ワーカーノードの数を増やすには、次のコマンドを実行します。
kubectl create deployment autoscaler-demo --image=nginx
kubectl scale deployment autoscaler-demo --replicas=50
注意: 上記のコマンドは、Kubernetes クラスタで直接 NGINX イメージを使用して autoscaler-demo という名前のデプロイを作成し、50 のポッドを起動します。
3. デプロイのステータスを確認し、増えているポッドの数を表示するには、次のコマンドを実行します。
kubectl get deployment autoscaler-demo --watch
4. 使用可能なポッドの数が 50 になったら、次のコマンドを実行してワーカーノードの数を確認します。
kubectl get nodes
テストデプロイのクリーンアップ
1. 以前に作成したデプロイ autoscaler-demo を削除してワーカーノードをスケールダウンするには、次のコマンドを実行します。
kubectl delete deployment autoscaler-demo
2. ワーカーノードの数を確認するには、約 10 分待ってから、次のコマンドを実行します。
kubectl get nodes