彈性伸縮主要有三個(gè)維度:
HPA,根據(jù)利用率,自動(dòng)伸縮 Pod 數(shù)量
VPA,根據(jù)歷史數(shù)據(jù),自動(dòng)設(shè)置 Pod 的 Request、Limit
CA,根據(jù)使用率,自動(dòng)伸縮 Node 數(shù)量
EKS集群的彈性擴(kuò)縮容是一項(xiàng)功能(這里指node的),可以自動(dòng)上下伸縮您的資源以滿足不斷變化的需求。若沒有此項(xiàng)重要的 Kubernetes 功能,則需要耗費(fèi)大量的人力資源來手動(dòng)執(zhí)行這些工作。
Amazon EKS 支持兩款彈性縮放產(chǎn)品:Kubernetes???Cluster Autoscaler????和???Karpenter????開源自動(dòng)縮放項(xiàng)目。Cluster Autoscaler 使用 AWS 縮放組,而 Karpenter 則直接使用 Amazon EC2 Fleet。(參考 ???https://docs.aws.amazon.com/zh_cn/eks/latest/userguide/autoscaling.html??)
EKS Cluster Autoscaler的配置實(shí)戰(zhàn)
信息:
EKS集群名稱:pre-production??????????
地域: ap-southeast-1??? 亞太地區(qū) (新加坡)
登錄ID: 75xxxxxxx533
1、前置條件:一個(gè)AutoScaling Group???? [Cluster Autoscaler=CA 和 Auto Scaling組 是2個(gè)不同的概念,CA控制AS實(shí)現(xiàn)彈性伸縮。]?
2、創(chuàng)建向 IAM 角色授予 Cluster Autoscaler 所需權(quán)限的 IAM 策略,使得 Cluster Autoscaler 能夠具備操作 AWS AutoScaling 的權(quán)限。
- 首先在群集上為服務(wù)帳戶啟用IAM角色
eksctl utils associate-iam-oidc-provider \
--cluster pre-production \
--approve --region ap-southeast-1
- 編輯文件IAM權(quán)限描述文件
cat <<EoF > /home/ec2-user/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 CLI命令創(chuàng)建IAM權(quán)限?
aws iam create-policy \
--policy-name k8s-asg-policy \
--policy-document file:///bsn/k8s/Cluster-Autoscaler/k8s-asg-policy.json --region ap-southeast-1
3、使用 eksctl 創(chuàng)建 Service Account 以及 IAM 角色,并將上一步創(chuàng)建的 IAM 策略附加到該角色 如果您自定義了集群名稱,請(qǐng)將–cluster xxxx 替換為您的集群名稱(若您沒有使用默認(rèn)配置) 請(qǐng)將 iam::xxxxxxxxxxxx:替換為您的賬戶ID(在您的aws控制臺(tái)點(diǎn)擊右上角可查看),例如263168716248(必須替換)
eksctl create iamserviceaccount \
--name cluster-autoscaler \
--namespace kube-system \
--cluster pre-production \
--attach-policy-arn "arn:aws:iam::75xxxxxx533:policy/k8s-asg-policy" \
--approve \
--override-existing-serviceaccounts --region ap-southeast-1
特別注意:中國(guó)地域的arn格式會(huì)多一個(gè)-cn,寧夏為例:arn:aws-cn:iam::984xxxx4388:
此過程需要花一點(diǎn)時(shí)間,eksctl 會(huì)通過 CloudFormation 創(chuàng)建相關(guān)的權(quán)限資源
通過以下describe命令進(jìn)行驗(yàn)證
kubectl -n kube-system describe sa cluster-autoscaler
4、部署 Cluster Autoscaler,請(qǐng)直接復(fù)制執(zhí)行下面命令
kubectl apply -f https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml
輸出
serviceaccount/cluster-autoscaler created
clusterrole.rbac.authorization.k8s.io/cluster-autoscaler created
role.rbac.authorization.k8s.io/cluster-autoscaler created
clusterrolebinding.rbac.authorization.k8s.io/cluster-autoscaler created
rolebinding.rbac.authorization.k8s.io/cluster-autoscaler created
deployment.apps/cluster-autoscaler created
5,執(zhí)行以下命令將cluster-autoscaler.kubernetes.io/safe-to-evict批注添加到部署中。
為了防止 CA 刪除運(yùn)行它自己的 pod 的節(jié)點(diǎn), 添加注釋cluster-autoscaler.kubernetes.io/safe-to-evict
kubectl -n kube-system \
annotate deployment.apps/cluster-autoscaler \
cluster-autoscaler.kubernetes.io/safe-to-evict="false"
6,通過kubectl edit編輯 Cluster Autoscaler 部署 (Deployment) 在 “spec: container: - command” 部分,將 (包括 <>)替換為當(dāng)前 EKS 集群的名稱(默認(rèn)為odoo),并添加 “–balance-similar-node-groups"以及”–skip-nodes-with-system-pods=false"選項(xiàng) 示范如下: 執(zhí)行命令,翻到后半段
修改部分如下所示(下面3行)
spec:
containers:
- command:
- ./cluster-autoscaler
- --v=4
- --stderrthreshold=info
- --cloud-provider=aws
- --skip-nodes-with-local-storage=false
- --expander=least-waste
- --node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/pre-production
- --balance-similar-node-groups
- --skip-nodes-with-system-pods=false
完成修改后通過 esc鍵 輸入 :wq 回車 保存并退出。
7,修改 Cluster Autoscaler 的 image 配置,復(fù)制執(zhí)行以下命令
kubectl -n kube-system set image deployment.apps/cluster-autoscaler cluster-autoscaler=k8s.gcr.io/autoscaling/cluster-autoscaler:v1.20.0
這個(gè)版本貌似可以用這2個(gè)命令確定,供參考
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})
8,查看 Cluster Autoscaler 日志,確認(rèn)部署成功,復(fù)制執(zhí)行以下命令
kubectl -n kube-system logs -f deployment.apps/cluster-autoscaler
9,您也可以通過命令autoscaling命令查看當(dāng)前您的兩個(gè)彈性擴(kuò)展組狀態(tài)。正常情況下,您現(xiàn)在擁有兩個(gè)工作負(fù)載組,一個(gè)是onDemand的集群,最大節(jié)點(diǎn)數(shù)是1,一個(gè)是Spot的集群,最大值是4。當(dāng)大規(guī)模負(fù)載發(fā)生時(shí),會(huì)在Spot的集群進(jìn)行擴(kuò)容。平時(shí)的正常運(yùn)行狀態(tài)是1臺(tái)Stop加2臺(tái)onDemand
請(qǐng)將Value=='xxxx'的odoo替換為您的集群名稱(默認(rèn)為odoo),然后執(zhí)行查看
aws autoscaling \
describe-auto-scaling-groups \
--query "AutoScalingGroups[? Tags[? (Key=='eks:cluster-name') && Value=='pre-production']].[AutoScalingGroupName, MinSize, MaxSize,DesiredCapacity]" \
--output table --region ap-southeast-1
輸出
-----------------------------------------------------------------------------------
| DescribeAutoScalingGroups |
+------------------------------------------------------------------+----+----+----+
| eks-pre-production-gnode1-b8c3ba17-d029-f1cb-839b-46478531d530 | 3 | 5 | 3 |
+------------------------------------------------------------------+----+----+----+
?10、部署測(cè)試應(yīng)用,增加副本測(cè)試,或者并發(fā)壓力測(cè)試
參考:
??https://graviton2.awspsa.com/3-%E5%AE%9E%E9%AA%8C3%E9%85%8D%E7%BD%AEeks%E5%BC%B9%E6%80%A7%E9%9B%86%E7%BE%A4%E5%B9%B6%E8%BF%90%E8%A1%8C%E5%8E%8B%E5%8A%9B%E6%B5%8B%E8%AF%95.html??文章來源:http://www.zghlxwxcb.cn/news/detail-842950.html
??https://blog.csdn.net/weixin_41335923/article/details/121486673??文章來源地址http://www.zghlxwxcb.cn/news/detail-842950.html
到了這里,關(guān)于EKS集群的彈性擴(kuò)縮容CA的配置實(shí)戰(zhàn) ·『云原生品鑒與布道』·的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!