# AWS - EKS Cluster Autoscaler


# Cluster Autoscaler

AWS Cluster Autoscaler 是 Kubernetes 的自動擴展組件,主要用於 Amazon EKS


# 主要功能

  1. 自動擴展節點
    • 根據需求增加節點
    • 移除未使用的節點
    • 優化資源使用
    • 降低成本
  2. Pod 調度
    • 檢測無法調度的 Pod
    • 自動增加節點容量
    • 確保 Pod 正常運行

# 工作原理

  1. 檢測 Pending Pods
  2. 評估需要的資源
  3. 向 Auto Scaling Group 請求新節點
  4. 節點就緒後調度 Pod
  5. 定期檢查未使用節點
  6. 縮減不需要的節點

# 部署配置

# IAM 權限設置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"ec2:DescribeLaunchTemplateVersions"
],
"Resource": "*"
}
]
}

# Deployment YAML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apiVersion: apps/v1
kind: Deployment
metadata:
name: cluster-autoscaler
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: cluster-autoscaler
template:
metadata:
labels:
app: cluster-autoscaler
spec:
serviceAccountName: cluster-autoscaler
containers:
- image: k8s.gcr.io/autoscaling/cluster-autoscaler:v1.21.0
name: cluster-autoscaler
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/<cluster-name>

# Auto Scaling Group 必要標籤

1
2
k8s.io/cluster-autoscaler/<cluster-name> = owned
k8s.io/cluster-autoscaler/enabled = true

# 擴展策略

  • Scale Up(擴展)
    • 檢測間隔:10 秒
    • 觸發條件:Pending Pods
    • 評估時間:~30 秒
    • 節點就緒:2-5 分鐘
  • Scale Down(縮減)
    • 檢測間隔:10 秒
    • 等待時間:10 分鐘(默認)
    • 條件:節點利用率 < 50%
    • 保護:系統 Pod、本地存儲
1
2
3
4
5
6
--scale-down-delay-after-add=10m
--scale-down-unneeded-time=10m
--scale-down-utilization-threshold=0.5
--max-node-provision-time=15m
--scan-interval=10s
--balance-similar-node-groups=true

# Reference

  • AWS - Cluster Autoscaler