brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Sep 14. 2021

EKS 4탄-14.  EKS AutoScaling 실습

실습 4탄 =  14/17

<1> 오토 스케일링 3가지

<2> 실습1 - EKS 운영 뷰  - KUBERNETES OPERATIONAL VIEW 설치

<3> 실습2  Node 수를 늘리는 CA  (Cluster AutoScaler) 설정

<4> 배포 , CA

<5> 삭제

<6> Pod 수를 늘리는  HPA (Horizontal Pod Autoscaler) 실습

<7> Pod 사양을 높이는 VPA( Vertical Pod Autoscaler) 실습

<8> Node 수를 늘리는 CA  (Cluster AutoScaler) 실습

<10> 다음편 - EKS-Fargate




<1> 오토 스케일링 3가지


Pod 수를 늘리는  HPA (Horizontal Pod Autoscaler)

Pod 사양을 높이는 VPA( Vertical Pod Autoscaler)

Work Node 수를 늘리는 CA  (Cluster AutoScaler) = 물리서버 증가



<2> 실습1 EKS 운영 뷰  - KUBERNETES OPERATIONAL VIEW 설치


변경되는 부분을 쉽게 확인하도록 VIEW를 설치한다.


1

curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash



2

helm repo add stable https://charts.helm.sh/stable

helm install kube-ops-view stable/kube-ops-view --set service.type=LoadBalancer --set rbac.create=True



3

AWS CLB 생성 후 대상(인스턴스) 헬스체크 까지 2~3분 정도 시간 소요

kubectl get svc kube-ops-view | tail -n 1 | awk '{ print "Kube-ops-view URL = http://"$4 }'



4

http url을 웹브라우저로 접속  (3분 걸림)




<3> 실습2 -  Node 수를 늘리는 CA  (Cluster AutoScaler) 설정


1) Cluster-autoscaler 파드가 필요하다.

2) IRSA 계정 권한도 필요하다.

이미지 출처

https://aws-eks-web-application.workshop.aws/ko/100-scaling/200-cluster-scaling.html



1

CA는 리소스 부족으로 Pod가 시작 되지 않으면, Node수를 자동으로 늘리는것.

Cluster Autoscale 동작하기 위해 Cluster-autoscaler 파드(디폴로이먼트)를 배치해야 한다.


1) Cluster-autoscaler 파드가 Autoscaler에서 상태 정보를 전달해 늘린다. 

2) IRSA  권한도 필요하다.



2

CA는 pending 상태인 Pod가 존재할때 , Work node를 늘린다.



3

특정 시간을 간격으로 사용률을 확인하여 스케일 인/아웃을 수행합니다. 

그리고 AWS에서는 Auto Scaling Group(ASG)을 사용하여 Cluster Autoscaler를 적용



4

현재 autoscaling(ASG) 정보 확인


현재 오토 스케일링 그룹으로 묶여 있다.

콘솔 > EC2 > 오토 스케일링 가서 확인 가능 하다.


// aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='클러스터이름']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" --output table

클러스터 이름 

myeks 

first-eks


aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" --output table


2개 , 최대 6개, 

-------------------------------------------------------------------------

|                       DescribeAutoScalingGroups                       |

+--------------------------------------------------------+----+----+----+

|  eks-ng-ddcb067-b135ec0-a13f-e9b6fb6306fd  |  2 |  6 |  2 |

+--------------------------------------------------------+----+----+----+


AWS 콘솔  > EC2 > Autoscaling group에서도 볼수 있다.



5

MaxSize 수정

export ASG_NAME=$(aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].AutoScalingGroupName" --output text)

or

first-eks



MaxSize 를 4로 수정

aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${ASG_NAME} --min-size 2 --desired-capacity 2 --max-size 4



6

확인

aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" --output table


eks-ng-e3a4e584-f6bdf0da-af32-8f6c-3d0d-c26aa60a7089  |  2 |  4 |  2 |



7

Policy 파일 생성


권한 필요

워커 노드 2개 있는데, SetDesiredCapacity 를 늘리는 권한이 있어야 한다.


cat <<EoF > 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

// autoscaling 정보를 조회하고,  DesiredCapacity  상태 세팅하는 것등  권한이 필요하다.



8

# IAM Policy 생성

aws iam create-policy --policy-name k8s-asg-policy --policy-document file://k8s-asg-policy.json



9

IRSA  생성  - iamserviceaccount  생성

echo $CLUSTER_NAME

ACCOUNT_ID=`aws sts get-caller-identity --query 'Account' --output text`

echo $ACCOUNT_ID


eksctl create iamserviceaccount --name cluster-autoscaler --namespace kube-system --cluster $CLUSTER_NAME --attach-policy-arn "arn:aws:iam::${ACCOUNT_ID}:policy/k8s-asg-policy" --approve --override-existing-serviceaccounts



10

배포


디플로이 먼트로 배포

Deploy the Cluster Autoscaler (CA)


curl -O https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml


cat cluster-autoscaler-autodiscover.yaml | sed "s/<YOUR CLUSTER NAME>/$CLUSTER_NAME/g" | kubectl apply -f -



11

확인?


kubectl get pod -n kube-system | grep cluster-autoscaler

cluster-autoscaler-5549447d45-x79rl   0/1     CrashLoopBackOff   8          23m

모니터링용 pod  1개가 생겼다.

컨트롤러 역할을 한다. 

부족하다고 판단이 되면, 오토 스케일러에 추가하라고 명령을 내린다.



12

kubectl describe deployments.apps -n kube-system cluster-autoscaler



13

cluster-autoscaler 파드가 동작하는 워커 노드가 퇴출(evict) 되지 않게 설정 (옵션)

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



14

cluster-autoscaler 버전 확인 ?

kubectl describe deployments.apps -n kube-system cluster-autoscaler | grep cluster-autoscaler:

  cluster-autoscaler:

    Image:      k8s.gcr.io/autoscaling/cluster-autoscaler:v1.17.3

// cluster-autoscaler 이미지 버전이 1.17.3 이다.   

cluster-autoscaler 1.21  이미지 버전으로 올리자.



15

autoscaler image 업데이트 하는 법 ?


export K8S_VERSION=$(kubectl version --short | grep 'Server Version:' | sed 's/[^0-9.]*\([0-9.]*\).*/\1/' | cut -d. -f1,2)


echo ${K8S_VERSION}


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 describe deployments.apps -n kube-system cluster-autoscaler | grep cluster-autoscaler:

  cluster-autoscaler:

    Image:      us.gcr.io/k8s-artifacts-prod/autoscaling/cluster-autoscaler:v1.21.0

// 버전 1.21  확인


지금까지는  cluster-autoscaler 파드 를 만들었다.




<4> 배포 , CA


1

샘플 배포 

# We will deploy an sample nginx application as a ReplicaSet of 1 Pod


cat <<EoF> 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




watch -d kubectl get pods,deployment


kubectl apply -f nginx.yaml


kubectl get deployment/nginx-to-scaleout




2

현재 node수 2개인것 확인 

k get nodes

(seo-eks@myeks:default) [root@myeks-host ~]# k get nodes

NAME                                               STATUS   ROLES    AGE   VERSION

ip-192-168-1-167.ap-northeast-1.compute.internal   Ready    <none>   23h   v1.21.2-eks-55daa9d

ip-192-168-2-85.ap-northeast-1.compute.internal    Ready    <none>   23h   v1.21.2-eks-55daa9d



3

pod  를  10개로 늘려보자~  

펜딩 되는게 보인다~


# Scale our ReplicaSet

# Let’s scale out the replicaset to 10

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




확인

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

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




3

# 노드 자동 증가 확인


aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" --output table


-------------------------------------------------------------------------

|                       DescribeAutoScalingGroups                       |

+--------------------------------------------------------+----+----+----+

|  eks-ng-ddcb00bb-a47--a13f-e9b6fb6306fd  |  2 |  6 |  4 |

+--------------------------------------------------------+----+----+----+



4

콘솔에서 확인 가능

 EC2 > Auto Scaling  그룹



5

디플로이먼트 삭제

kubectl delete -f nginx.yaml



6

노드 수 축소

기본은 10분 후 scale down 됨

아래 flag 로 시간 수정 가능

디플로이먼트 삭제 후 10분 기다리고 나서 보자!

# By default, cluster autoscaler will wait 10 minutes between scale down operations, 

# you can adjust this using the --scale-down-delay-after-add, --scale-down-delay-after-delete, 

# and --scale-down-delay-after-failure flag. 

# E.g. --scale-down-delay-after-add=5m to decrease the scale down delay to 5 minutes after a node has been added.


# 터미널1

watch -d kubectl get node



7

AWS 콘솔에서 확인


배포 참고

https://www.eksworkshop.com/beginner/080_scaling/test_ca/




<5> 삭제


cluster-autoscaler 삭제

cat cluster-autoscaler-autodiscover.yaml | sed "s/<YOUR CLUSTER NAME>/$CLUSTER_NAME/g" | kubectl delete -f -



2

irsa 삭제

eksctl delete iamserviceaccount --name cluster-autoscaler --namespace kube-system --cluster $CLUSTER_NAME --wait




<6> Pod 수를 늘리는  HPA (Horizontal Pod Autoscaler) 실습


https://docs.aws.amazon.com/ko_kr/eks/latest/userguide/horizontal-pod-autoscaler.html


https://aws-eks-web-application.workshop.aws/ko/100-scaling/100-pod-scaling.html


https://www.eksworkshop.com/beginner/080_scaling/deploy_hpa/


https://www.stacksimplify.com/aws-eks/aws-eks-kubernetes-autoscaling/learn-to-master-horizontal-pod-autoscaling-on-aws-eks/




<7> Pod 사양을 높이는 VPA( Vertical Pod Autoscaler) 실습


https://docs.aws.amazon.com/ko_kr/eks/latest/userguide/vertical-pod-autoscaler.html


https://www.stacksimplify.com/aws-eks/aws-eks-kubernetes-autoscaling/learn-to-master-vertical-pod-autoscaling-on-aws-eks/




<8> Node 수를 늘리는 CA  (Cluster AutoScaler) 실습


https://www.eksworkshop.com/beginner/080_scaling/deploy_ca/


https://aws-eks-web-application.workshop.aws/ko/100-scaling/200-cluster-scaling.html


https://www.eksworkshop.com/beginner/080_scaling/deploy_ca/


https://www.stacksimplify.com/aws-eks/aws-eks-kubernetes-autoscaling/learn-to-master-cluster-autoscaler-on-aws-eks/


https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler/cloudprovider/aws




<10> 다음편 - EKS-Fargate


https://brunch.co.kr/@topasvga/1889



감사합니다.

매거진의 이전글 EKS 4탄-13.CloudWatch Container

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari