AWS용 Cluster Autoscaler는 Auto Scaling 그룹(ASG)과의 통합을 제공합니다. 이를 통해 사용자는 다음 네 가지 배포 옵션 중에서 선택할 수 있습니다.
1. ASG 구성하기
최소, 최대 및 원하는 용량을 설정하여 Auto Scaling 그룹의 크기를 구성합니다. 클러스터를 만들 때 이러한 설정을 3으로 설정 했습니다.
Copy aws autoscaling \
describe-auto-scaling-groups \
--query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='eksworkshop-eksctl']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" \
--output table
이제 최대 용량을 인스턴스 4개로 늘립니다.
EKS 노드 그룹의 AutoScalingGroup의 이름을 확인하고 최대 용량을 4개로 증가 시킵니다. 그리고 AutoScalingGroup의 값을 다시 확인 합니다.
Copy # we need the ASG name
export ASG_NAME=$(aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='eksworkshop-eksctl']].AutoScalingGroupName" --output text)
# increase max capacity up to 4
aws autoscaling \
update-auto-scaling-group \
--auto-scaling-group-name ${ASG_NAME} \
--min-size 3 \
--desired-capacity 3 \
--max-size 4
# Check new values
aws autoscaling \
describe-auto-scaling-groups \
--query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='eksworkshop-eksctl']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" \
--output table
2. IAM roles 생성하기
클러스터의 서비스 계정에 대한 IAM 역할 활성화합니다.
Copy eksctl utils associate-iam-oidc-provider \
--cluster eksworkshop-eksctl \
--approve
CA 파드가 Auto Scaling 그룹과 상호 작용할 수 있도록 서비스 계정에 대한 IAM 정책을 만듭니다.
Copy mkdir ~/environment/cluster-autoscaler
cat <<EoF > ~/environment/cluster-autoscaler/k8s-asg-policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"ec2:DescribeLaunchTemplateVersions"
],
"Resource": "*",
"Effect": "Allow"
}
]
}
EoF
Copy aws iam create-policy \
--policy-name k8s-asg-policy \
--policy-document file://~/environment/cluster-autoscaler/k8s-asg-policy.json
마지막으로 kube-system namespace에서 cluster-autoscaler 서비스 계정에 대한 IAM 역할을 생성합니다. Account ID가 설정되어 있지 않다면 다음의 명령을 실행한 후 IAM 역할을 생성합니다.
Copy ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
Copy eksctl create iamserviceaccount \
--name cluster-autoscaler \
--namespace kube-system \
--cluster eksworkshop-eksctl \
--attach-policy-arn "arn:aws:iam::${ACCOUNT_ID}:policy/k8s-asg-policy" \
--approve \
--override-existing-serviceaccounts
IAM 역할의 ARN이 있는 서비스 계정이 Annotation 되었는지 확인합니다.
Copy kubectl -n kube-system describe sa cluster-autoscaler
3. CA 배포하기
다음 명령어를 사용하여 Cluster Autoscaler를 다운로드 후 클러스터에 배포 합니다.
Copy curl https://www.eksworkshop.com/beginner/080_scaling/deploy_ca.files/cluster-autoscaler-autodiscover.yaml -o ~/environment/cluster-autoscaler-autodiscover.yaml
Copy kubectl apply -f ~/environment/cluster-autoscaler-autodiscover.yaml
CA가 자체 파드가 실행중인 노드를 제거하지 못하도록 하기 위해 다음 명령을 사용하여 배포에 cluster-autoscaler.kubernetes.io/safe-to-evict
annotation을 추가 합니다.
Copy kubectl -n kube-system \
annotate deployment.apps/cluster-autoscaler \
cluster-autoscaler.kubernetes.io/safe-to-evict="false"
마지막으로 autoscaler 이미지를 업데이트 합니다.
Copy # we need to retrieve the latest docker image available for our EKS version
export K8S_VERSION=$(kubectl version --short | grep 'Server Version:' | sed 's/[^0-9.]*\([0-9.]*\).*/\1/' | cut -d. -f1,2)
export AUTOSCALER_VERSION=$(curl -s "https://api.github.com/repos/kubernetes/autoscaler/releases" | grep '"tag_name":' | sed -s 's/.*-\([0-9][0-9\.]*\).*/\1/' | grep -m1 ${K8S_VERSION})
kubectl -n kube-system \
set image deployment.apps/cluster-autoscaler \
cluster-autoscaler=us.gcr.io/k8s-artifacts-prod/autoscaling/cluster-autoscaler:v${AUTOSCALER_VERSION}
로그를 확인 합니다.
Copy kubectl -n kube-system logs -f deployment/cluster-autoscaler
4. 샘플 애플리케이션 배포하기
예제 애플리케이션 nginx를 파드의 1 ReplicaSet
으로 배포합니다. 그리고 deployments
를 확인합니다.
Copy cat <<EoF> ~/environment/cluster-autoscaler/nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-to-scaleout
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
service: nginx
app: nginx
spec:
containers:
- image: nginx
name: nginx-to-scaleout
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 500m
memory: 512Mi
EoF
Copy kubectl apply -f ~/environment/cluster-autoscaler/nginx.yaml
Copy kubectl get deployment/nginx-to-scaleout
5. ReplicaSet 확장하기
ReplicaSet를 10으로 확장 하겠습니다.
Copy kubectl scale --replicas=10 deployment/nginx-to-scaleout
일부 포드는 Pending
상태가 되어 클러스터 autoscaler가 EC2 노드을 확장 하도록 트리거 합니다.
Copy kubectl get pods -l app=nginx -o wide --watch
클러스터 autoscaler 로그를 확인합니다.
Copy kubectl -n kube-system logs -f deployment/cluster-autoscaler
아래와 유사한 클러스터 autoscaler 처리 이벤트가 표시 됩니다.
또는 kubectl을 사용 합니다.
다음과 같이 출력 됩니다.