CA 구성하기

Cluster Autoscaler (CA) 구성

AWS용 Cluster Autoscaler는 Auto Scaling 그룹(ASG)과의 통합을 제공합니다. 이를 통해 사용자는 다음 네 가지 배포 옵션 중에서 선택할 수 있습니다.

  • 1개의 Auto Scaling 그룹

  • 여러개의 Auto Scaling 그룹

  • Auto-Discovery

  • Control-plane 노드 설정

1. ASG 구성하기

최소, 최대 및 원하는 용량을 설정하여 Auto Scaling 그룹의 크기를 구성합니다. 클러스터를 만들 때 이러한 설정을 3으로 설정 했습니다.

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의 값을 다시 확인 합니다.

# 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 생성하기

Amazon EKS 클러스터의 service account에 대한 IAM 역할을 사용하려면, IAM 역할을 쿠버네티스 서비스 계정과 연결해야 합니다. 그러면 해당 service account을 사용하는 모든 파드의 컨테이너에 AWS 권한을 제공 할 수 있습니다. 이 기능을 사용하면 해당 노드의 파드가 AWS API를 호출 할 수 있도록 하여 더 이상 노드 IAM 역할에 확장 권한을 제공 할 필요가 없습니다.

클러스터의 서비스 계정에 대한 IAM 역할 활성화합니다.

eksctl utils associate-iam-oidc-provider \
    --cluster eksworkshop-eksctl \
    --approve
    

CA 파드가 Auto Scaling 그룹과 상호 작용할 수 있도록 서비스 계정에 대한 IAM 정책을 만듭니다.

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
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 역할을 생성합니다.

ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
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 되었는지 확인합니다.

kubectl -n kube-system describe sa cluster-autoscaler

3. CA 배포하기

다음 명령어를 사용하여 Cluster Autoscaler를 다운로드 후 클러스터에 배포 합니다.

curl https://www.eksworkshop.com/beginner/080_scaling/deploy_ca.files/cluster-autoscaler-autodiscover.yaml -o ~/environment/cluster-autoscaler-autodiscover.yaml
kubectl apply -f ~/environment/cluster-autoscaler-autodiscover.yaml

CA가 자체 파드가 실행중인 노드를 제거하지 못하도록 하기 위해 다음 명령을 사용하여 배포에 cluster-autoscaler.kubernetes.io/safe-to-evict annotation을 추가 합니다.

kubectl -n kube-system \
    annotate deployment.apps/cluster-autoscaler \
    cluster-autoscaler.kubernetes.io/safe-to-evict="false"
    

마지막으로 autoscaler 이미지를 업데이트 합니다.

# 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}
    

로그를 확인 합니다.

kubectl -n kube-system logs -f deployment/cluster-autoscaler

4. 샘플 애플리케이션 배포하기

예제 애플리케이션 nginx를 파드의 1 ReplicaSet으로 배포합니다. 그리고 deployments를 확인합니다.

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
kubectl apply -f ~/environment/cluster-autoscaler/nginx.yaml
kubectl get deployment/nginx-to-scaleout

5. ReplicaSet 확장하기

ReplicaSet를 10으로 확장 하겠습니다.

kubectl scale --replicas=10 deployment/nginx-to-scaleout

일부 포드는 Pending 상태가 되어 클러스터 autoscaler가 EC2 노드을 확장 하도록 트리거 합니다.

kubectl get pods -l app=nginx -o wide --watch

클러스터 autoscaler 로그를 확인합니다.

kubectl -n kube-system logs -f deployment/cluster-autoscaler

아래와 유사한 클러스터 autoscaler 처리 이벤트가 표시 됩니다.

EC2 AWS Management Console에서 Auto Scaling 그룹이 수요를 충족하도록 확장되는지 확인 합니다. 이 작업은 몇 분 정도 걸릴 수 있습니다. 명령 줄에서 파드 배포를 수행 할 수도 있습니다. 노드가 확장됨에 따라 파드가 pending에서 running으로 전환 되는 것을 확인 할 수도 있습니다.

또는 kubectl을 사용 합니다.

kubectl get nodes

다음과 같이 출력 됩니다.

Last updated