참고 - https://www.eksworkshop.com/beginner/080_scaling/deploy_ca/
참고 - https://docs.aws.amazon.com/ko_kr/eks/latest/userguide/autoscaling.html
Cluster AutoScaler는 특정 Tag가 있는 Node를 스케줄링 하므로, 특정 Node Group을 스케줄링 하고 싶다면
태그를 지정합니다. 즉, Autoscaling Group에 수동으로 해줘야합니다.
CA같은 경우는 해당 Pod가 Pending 상태에 있어, Node를 Scale_Out 해준다. 그런데 그럼 Scale In은 어떻게 가능할까? 즉, Pod에 CPU 사용상태가 0m가 되어야한다는 점이다. 이것을 꼭 명심하자
aws autoscaling \
describe-auto-scaling-groups \
--query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='eksworkshop-eksctl']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" \
--output table
export ASG_NAME=$(aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='$ClusterName']].AutoScalingGroupName" --output text)
aws autoscaling \
update-auto-scaling-group \
--auto-scaling-group-name ${ASG_NAME} \
--min-size 2 \
--desired-capacity 2 \
--max-size 4
aws autoscaling \
describe-auto-scaling-groups \
--query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='eksworkshop-eksctl']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" \
--output table
mkdir cluster-autoscaler && cd cluster-autoscaler
IAM roles for service accounts 시작
2. CA 포드가 자동 크기 조정 그룹과 상호 작용하도록 허용하는 서비스 계정에 대한 IAM 정책 생성
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
aws iam create-policy \
--policy-name k8s-asg-policy \
--policy-document file://k8s-asg-policy.json
eksctl create iamserviceaccount \
--name cluster-autoscaler \
--namespace kube-system \
--cluster wsi-eks-cluster \
--attach-policy-arn "arn:aws:iam::040217728499:policy/k8s-asg-policy" \
--approve \
--override-existing-serviceaccounts
kubectl -n kube-system describe sa cluster-autoscaler
Output(위에 명령어 입력시 출력)
Name: cluster-autoscaler
Namespace: kube-system
Labels: app.kubernetes.io/managed-by=eksctl
Annotations: eks.amazonaws.com/role-arn: arn:aws:iam::040217728499:role/eksctl-wsi-eks-cluster-addon-iamserviceaccou-Role1-GULTKT4L2KUR
Image pull secrets: <none>
Mountable secrets: cluster-autoscaler-token-h56mz
Tokens: cluster-autoscaler-token-h56mz
Events: <none>
curl -o cluster-autoscaler-autodiscover.yaml https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml
sed -i 's|<YOUR CLUSTER NAME>|wsi-eks-cluster|g' ./cluster-autoscaler-autodiscover.yaml
kubectl apply -f cluster-autoscaler-autodiscover.yaml
kubectl annotate serviceaccount cluster-autoscaler \
-n kube-system \
eks.amazonaws.com/role-arn=arn:aws:iam::040217728499:role/eksctl-wsi-eks-cluster-addon-iamserviceaccou-Role1-GULTKT4L2KUR
kubectl -n kube-system \
annotate deployment.apps/cluster-autoscaler \
cluster-autoscaler.kubernetes.io/safe-to-evict="false"
또는,
kubectl patch deployment cluster-autoscaler \
-n kube-system \
-p '{"spec":{"template":{"metadata":{"annotations":{"cluster-autoscaler.kubernetes.io/safe-to-evict": "false"}}}}}'
kubectl -n kube-system edit deployment./cluster-autoscaler
:wq
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
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 ~/environment/cluster-autoscaler/nginx.yaml
kubectl get deployment/nginx-to-scaleout
kubectl scale --replicas=50 deployment/nginx-to-scaleout
kubectl get pods -l app=nginx -o wide --watch
kubectl -n kube-system logs -f deployment/cluster-autoscaler
kubectl get nodes
다시 Pod 갯수 줄이기
kubectl scale --replicas=1 deployment/nginx-to-scaleout
그럼 Node가 수명주기후크로 인해서 6분뒤에 다시 Scale In 합니다.