두번 째 포스트 주제는 Node Autoscaling이다.
Node Autoscaling이란 Kubernetes 클러스터에 과도한 부하 및 이벤트가 발생하여 Pod가 Node에 할당되지 못하여 Pending상태가 되는 경우 추가 Node를 생성하여 해당 Node에 Pod를 배치하는 것을 의미한다.
즉 Node의 Scale In/Out, Scale Up/Down을 의미
CA: Cluster Autoscaler
EKS의 경우 AWS Auto Scaling Group를 사용하여 Cluster Autoscaler에 적용
- EKS 배포 시 Tag 설정
- EC2 Autoscaling 내 ASG 생성 확인
- awscli명령어를 통해 클러스터 노드의 수량 조절 / MaxSize 6개로 변경
aws autoscaling describe-auto-scaling-groups \
--query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" \
--output table
-----------------------------------------------------------------
| DescribeAutoScalingGroups |
+------------------------------------------------+----+----+----+
| eks-ng1-44c41109-daa3-134c-df0e-0f28c823cb47 | 3 | 3 | 3 |
+------------------------------------------------+----+----+----+
# MaxSize 6개로 수정
export ASG_NAME=$(aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].AutoScalingGroupName" --output text)
aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${ASG_NAME} --min-size 3 --desired-capacity 3 --max-size 6
# 확인
aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" --output table
-----------------------------------------------------------------
| DescribeAutoScalingGroups |
+------------------------------------------------+----+----+----+
| eks-ng1-c2c41e26-6213-a429-9a58-02374389d5c3 | 3 | 6 | 3 |
+------------------------------------------------+----+----+----+
CA 배포 및 확인
# 배포 : Deploy the Cluster Autoscaler (CA)
curl -s -O https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml
sed -i "s/<YOUR CLUSTER NAME>/$CLUSTER_NAME/g" cluster-autoscaler-autodiscover.yaml
kubectl apply -f cluster-autoscaler-autodiscover.yaml
# 확인
kubectl get pod -n kube-system | grep cluster-autoscaler
kubectl describe deployments.apps -n kube-system cluster-autoscaler
kubectl describe deployments.apps -n kube-system cluster-autoscaler | grep node-group-auto-discovery
--node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/myeks
# (옵션) cluster-autoscaler 파드가 동작하는 워커 노드가 퇴출(evict) 되지 않게 설정
kubectl -n kube-system annotate deployment.apps/cluster-autoscaler cluster-autoscaler.kubernetes.io/safe-to-evict="false"
실습시나리오
1. nginx.yaml을 배포 (Web Server Pod)
2. Deployment의 replicas 수를 증가 → 현재 클러스터 내 Worker Node에 더이상 Pod를 할당 할 수 없게 설정
3. CA를 통해 자동으로 Worker Node 생성 및 Pending Pod가 신규 Node에 배치되는 것을 확인
1. nginx.yaml을 배포 (Web Server Pod)
- resource requests를 높게 설정 → CPU: 500m / MEM: 512mi
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
kubectl apply -f nginx.yaml
2. Deployment의 replicas 수를 15로 증가
kubectl scale --replicas=15 deployment/nginx-to-scaleout && date
3. Node 생성 확인
- replicas 수를 증가시켜 Pod가 더이상 Node에 할당되지 못하여 Pending 상태
- eks-node-viewer에서 Node의 CPU/MEM 사용률 확인
- CA에 의해 Node 3대가 생성된 것을 확인
- 신규 Node 생성에 따라 Pending된 Pod가 모두 Node에 할당된 것을 확인
- ASG 확인 시 desire 수량이 6으로 변경된 것을 확인
CPA: Cluster Proportional Autoscaler
Node 수 증가에 비례하여 성능 처리가 필요한 애플리케이션(컨테이너/파드)를 수평으로 자동 확장 ex. coredns
실습시나리오
1. CPA 설치
2. cpa-nginx.yaml을 배포
3. CPA 규칙 설정
4. Node 수량 증가
5. Node 증가에 따른 Pod 생성 확인
1. CPA 설치
helm repo add cluster-proportional-autoscaler https://kubernetes-sigs.github.io/cluster-proportional-autoscaler
# CPA규칙을 설정하고 helm차트를 릴리즈 필요
helm upgrade --install cluster-proportional-autoscaler cluster-proportional-autoscaler/cluster-proportional-autoscaler
2. cpa-nginx.yaml을 배포
# nginx 디플로이먼트 배포
cat <<EOT > cpa-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
resources:
limits:
cpu: "100m"
memory: "64Mi"
requests:
cpu: "100m"
memory: "64Mi"
ports:
- containerPort: 80
EOT
kubectl apply -f cpa-nginx.yaml
3. CPA 규칙 설정
- nodeToReplicas: 노드 수량 증가에 따른 Pod 증가 수량 설정
# CPA 규칙 설정
cat <<EOF > cpa-values.yaml
config:
ladder:
nodesToReplicas:
- [1, 1]
- [2, 2]
- [3, 3]
- [4, 3]
- [5, 5]
options:
namespace: default
target: "deployment/nginx-deployment"
EOF
kubectl describe cm cluster-proportional-autoscaler
4. Node 수량 증가 / 5개
# helm 업그레이드
helm upgrade --install cluster-proportional-autoscaler -f cpa-values.yaml cluster-proportional-autoscaler/cluster-proportional-autoscaler
# 노드 5개로 증가
export ASG_NAME=$(aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].AutoScalingGroupName" --output text)
aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${ASG_NAME} --min-size 5 --desired-capacity 5 --max-size 5
aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" --output table
5. Node 증가에 따른 Pod 생성 확인
- 노드가 5개 증가로 인하여 nginx-pod가 5개 증가한 것을 확인
5-1. Node 증가에 따른 Pod 생성 확인
- Node 수량을 4개로 감소 시켰을 경우 nignx-pod가 3개 감소 확인
# 노드 4개로 축소
aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${ASG_NAME} --min-size 4 --desired-capacity 4 --max-size 4
aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='myeks']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" --output table
'AEWS Study' 카테고리의 다른 글
6주차 - EKS Security - 사용자 인증/인가 (0) | 2024.04.09 |
---|---|
5주차 - EKS Autoscaling - Karpenter (1) | 2024.04.07 |
5주차 - EKS Autoscaling - (Pod Autoscaling) (0) | 2024.04.05 |
4주차 - EKS Observability (0) | 2024.03.31 |
3주차 - EKS Storage & Nodegroup (0) | 2024.03.24 |