混合云K8s容器化应用弹性伸缩实战
1. 前提條件
本最佳實(shí)踐的軟件環(huán)境要求如下:
應(yīng)用環(huán)境:
①容器服務(wù)ACK基于專有云V3.10.0版本。
②公共云云企業(yè)網(wǎng)服務(wù)CEN。
③公共云彈性伸縮組服務(wù)ESS。
配置條件:
1)使用專有云的容器服務(wù)或者在ECS上手動部署敏捷PaaS。
2)開通云專線,打通容器服務(wù)所在VPC與公共云上的VPC。
3)開通公共云彈性伸縮組服務(wù)(ESS)。
2. 背景信息
本實(shí)踐基于K8s的業(yè)務(wù)集群運(yùn)行在專有云上,對測試業(yè)務(wù)進(jìn)行壓力測試,主要基于以下三種產(chǎn)品和能力:
①利用阿里云的云企業(yè)網(wǎng)專線打通專有云和公共云,實(shí)現(xiàn)兩朵云上VPC網(wǎng)絡(luò)互通。
②利用K8s(Kubernetes)的HPA能力,實(shí)現(xiàn)容器的水平伸縮。
③利用K8s的Cluster Autoscaler和阿里云彈性伸縮組ESS能力實(shí)現(xiàn)節(jié)點(diǎn)的自動伸縮。
HPA(Horizontal Pod Autoscaler)是K8s的一種資源對象,能夠根據(jù)CPU、內(nèi)存等指標(biāo)對statefulset、deployment等對象中的pod數(shù)量進(jìn)行動態(tài)伸縮,使運(yùn)行在上面的服務(wù)對指標(biāo)的變化有一定的自適應(yīng)能力。
當(dāng)被測試業(yè)務(wù)指標(biāo)達(dá)到上限時(shí),觸發(fā)HPA自動擴(kuò)容業(yè)務(wù)pod;當(dāng)業(yè)務(wù)集群無法承載更多pod時(shí),觸發(fā)公共云的ESS服務(wù),在公共云內(nèi)擴(kuò)容出ECS并自動添加到專有云的K8s集群。
圖 1:架構(gòu)原理圖
3. 配置HPA
本示例創(chuàng)建了一個(gè)支持HPA的nginx應(yīng)用,創(chuàng)建成功后,當(dāng)Pod的利用率超過本例中設(shè)置的20%利用率時(shí),則會進(jìn)行水平擴(kuò)容,低于20%的時(shí)候會進(jìn)行縮容。
1.若使用自建K8s集群,則通過yaml文件配置HPA
1)創(chuàng)建一個(gè)nginx應(yīng)用,必須為應(yīng)用設(shè)置request值,否則HPA不會生效。
apiVersion: app/v1beta2 kind: Deployment spec:template:metadata:creationTimestamp: nulllabels:app: hpa-testspec:dnsPolicy: ClusterFirst terminationGracePeriodSeconds:30 containers:image: '192.168.**.***:5000/admin/hpa-example:v1'imagePullPolicy: IfNotPresentterminationMessagePolicy:FileterminationMessagePath:/dev/termination-logname: hpa-testresources:requests:cpu: //必須設(shè)置request值securityContext: {}restartPolicy:AlwaysschedulerName:default-schedulerreplicas: 1selector: matchLabels:app: hpa-testrevisionHistoryLimit: 10strategy: type: RollingUpdaterollingUpdate:maxSurge: 25%maxUnavailable: 25%progressDeadlineSeconds: 6002)創(chuàng)建HPA。
apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata:annotations:autoscaling.alpha.kubernetes.io/conditions:'[{"type":"AbleToScale","status":"True","lastTransitionTime":"2020-04-29T06:57:28Z","reason":"ScaleDownStabilized","message":"recentrecommendations were higher than current one, applying the highest recentrecommendation"},{"type":"ScalingActive","status":"True","lastTransitionTime":"2020-04-29T06:57:28Z","reason":"ValidMetricFound","message":"theHPAwas able to successfully calculate a replica count from cpu resourceutilization(percentage ofrequest)"},{"type":"ScalingLimited","status":"False","lastTransitionTime":"2020-04-29T06:57:28Z","reason":"DesiredWithinRange","message":"thedesiredcount is within the acceptable range"}]'autoscaling.alpha.kubernetes.io/currentmetrics:'[{"type":"Resource","resource":{"name":"cpu","currentAverageUtilization":0,"currentAverageValue":"0"}}]'creationTimestamp: 2020-04-29T06:57:13Z name: hpa-test namespace: default resourceVersion: "3092268" selfLink: /apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/hpa01 uid: a770ca26-89e6-11ea-a7d7-00163e0106e9 spec:maxReplicas: //設(shè)置pod數(shù)量 minReplicas: 1scaleTargetRef:apiVersion: apps/v1beta2kind: Deploymentname: centos targetCPUUtilizationPercentage://設(shè)置CPU閾值2.若使用阿里云容器服務(wù),需要在部署應(yīng)用時(shí)選擇配置HPA
圖2:訪問設(shè)置
4. 配置Cluster Autoscaler
資源請求(Request)的正確、合理設(shè)置,是彈性伸縮的前提條件。節(jié)點(diǎn)自動伸縮組件基于K8s資源調(diào)度的分配情況進(jìn)行伸縮判斷,節(jié)點(diǎn)中資源的分配通過資源請(Request)進(jìn)行計(jì)算。
當(dāng)Pod由于資源請求(Request)無法滿足并進(jìn)入等待(Pending)狀態(tài)時(shí),節(jié)點(diǎn)自動伸縮組件會根據(jù)彈性伸縮組配置信息中的資源規(guī)格以及約束配置,計(jì)算所需的節(jié)點(diǎn)數(shù)目。
如果可以滿足伸縮條件,則會觸發(fā)伸縮組的節(jié)點(diǎn)加入。而當(dāng)一個(gè)節(jié)點(diǎn)在彈性伸縮組中且節(jié)點(diǎn)上Pod的資源請求低于閾值時(shí),節(jié)點(diǎn)自動伸縮組件會將節(jié)點(diǎn)進(jìn)行縮容。
1.配置彈性伸縮組ESS
1)創(chuàng)建ESS彈性伸縮組,記錄最小實(shí)例數(shù)和最大實(shí)例數(shù)。
圖3:修改伸縮組
2)創(chuàng)建伸縮配置,記錄伸縮配置的id。
圖4:伸縮配置
2.K8s集群部署autoscaler
kubectl apply -f ca.yml
參考ca.yml創(chuàng)建autoscaler,注意修改如下配置與實(shí)際環(huán)境相對應(yīng)。
access-key-id: "TFRBSWlCSFJyeHd2QXZ6****" access-key-secret: "bGIyQ3NuejFQOWM0WjFUNjR4WTVQZzVPRXND****" region-id: "Y24taHVoZWhh****"ca.yal代碼如下:
--- apiVersion: v1 kind: ServiceAccount metadata:labels:k8s-addon: cluster-autoscaler.addons.k8s.iok8s-app: cluster-autoscalername: cluster-autoscalernamespace: kube-system --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata:name: cluster-autoscalerlabels:k8s-addon: cluster-autoscaler.addons.k8s.iok8s-app: cluster-autoscaler rules: - apiGroups: [""]resources: ["events","endpoints"]verbs: ["create", "patch"] - apiGroups: [""]resources: ["pods/eviction"]verbs: ["create"] - apiGroups: [""]resources: ["pods/status"]verbs: ["update"] - apiGroups: [""]resources: ["endpoints"]resourceNames: ["cluster-autoscaler"]verbs: ["get","update"] - apiGroups: [""]resources: ["nodes"]verbs: ["watch","list","get","update"] - apiGroups: [""]resources: ["pods","services","replicationcontrollers","persistentvolumeclaims","persistentvolumes"]verbs: ["watch","list","get"] - apiGroups: ["extensions"]resources: ["replicasets","daemonsets"]verbs: ["watch","list","get"] - apiGroups: ["policy"]resources: ["poddisruptionbudgets"]verbs: ["watch","list"] - apiGroups: ["apps"]resources: ["statefulsets"]verbs: ["watch","list","get"] - apiGroups: ["storage.k8s.io"]resources: ["storageclasses"]verbs: ["watch","list","get"]--- apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata:name: cluster-autoscalernamespace: kube-systemlabels:k8s-addon: cluster-autoscaler.addons.k8s.iok8s-app: cluster-autoscaler rules: - apiGroups: [""]resources: ["configmaps"]verbs: ["create","list","watch"] - apiGroups: [""]resources: ["configmaps"]resourceNames: ["cluster-autoscaler-status", "cluster-autoscaler-priority-expander"]verbs: ["delete","get","update","watch"]--- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata:name: cluster-autoscalerlabels:k8s-addon: cluster-autoscaler.addons.k8s.iok8s-app: cluster-autoscaler roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-autoscaler subjects:- kind: ServiceAccountname: cluster-autoscalernamespace: kube-system--- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata:name: cluster-autoscalernamespace: kube-systemlabels:k8s-addon: cluster-autoscaler.addons.k8s.iok8s-app: cluster-autoscaler roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: cluster-autoscaler subjects:- kind: ServiceAccountname: cluster-autoscalernamespace: kube-system --- apiVersion: v1 kind: Secret metadata:name: cloud-confignamespace: kube-system type: Opaque data:access-key-id: "TFRBSWlCSFJyeHd2********"access-key-secret: "bGIyQ3NuejFQOWM0WjFUNjR4WTVQZzVP*********"region-id: "Y24taHVoZW********"--- apiVersion: apps/v1 kind: Deployment metadata:name: cluster-autoscalernamespace: kube-systemlabels:app: cluster-autoscaler spec:replicas: 1selector:matchLabels:app: cluster-autoscalertemplate:metadata:labels:app: cluster-autoscalerspec:dnsConfig:nameservers:- 100.XXX.XXX.XXX- 100.XXX.XXX.XXXnodeSelector:ca-key: ca-valuepriorityClassName: system-cluster-criticalserviceAccountName: admincontainers:- image: 192.XXX.XXX.XXX:XX/admin/autoscaler:v1.3.1-7369cf1name: cluster-autoscalerresources:limits:cpu: 100mmemory: 300Mirequests:cpu: 100mmemory: 300Micommand:- ./cluster-autoscaler- '--v=5'- '--stderrthreshold=info'- '--cloud-provider=alicloud'- '--scan-interval=30s'- '--scale-down-delay-after-add=8m'- '--scale-down-delay-after-failure=1m'- '--scale-down-unready-time=1m'- '--ok-total-unready-count=1000'- '--max-empty-bulk-delete=50'- '--expander=least-waste'- '--leader-elect=false'- '--scale-down-unneeded-time=8m'- '--scale-down-utilization-threshold=0.2'- '--scale-down-gpu-utilization-threshold=0.3'- '--skip-nodes-with-local-storage=false'- '--nodes=0:5:asg-hp3fbu2zeu9bg3clraqj'imagePullPolicy: "Always"env:- name: ACCESS_KEY_IDvalueFrom:secretKeyRef:name: cloud-configkey: access-key-id- name: ACCESS_KEY_SECRETvalueFrom:secretKeyRef:name: cloud-configkey: access-key-secret- name: REGION_IDvalueFrom:secretKeyRef:name: cloud-configkey: region-id5. 執(zhí)行結(jié)果
模擬業(yè)務(wù)訪問:
啟動busybox鏡像,在pod內(nèi)執(zhí)行如下命令訪問以上應(yīng)用的service,可以同時(shí)啟動多個(gè)pod增加業(yè)務(wù)負(fù)載。while true;do wget -q -O- http://hpa-test/index.html;done
觀察HPA:
加壓前
圖 5:加壓前
加壓后
當(dāng)CPU值達(dá)到閾值后,會觸發(fā)pod的水平擴(kuò)容。
圖 6:加壓后1
圖 7:加壓后2
觀察Pod:
當(dāng)集群資源不足時(shí),新擴(kuò)容出的pod處于pending狀態(tài),此時(shí)將觸發(fā)cluster autoscaler,自動擴(kuò)容節(jié)點(diǎn)。
圖8:伸縮活動
我們是阿里云智能全球技術(shù)服務(wù)-SRE團(tuán)隊(duì),我們致力成為一個(gè)以技術(shù)為基礎(chǔ)、面向服務(wù)、保障業(yè)務(wù)系統(tǒng)高可用的工程師團(tuán)隊(duì);提供專業(yè)、體系化的SRE服務(wù),幫助廣大客戶更好地使用云、基于云構(gòu)建更加穩(wěn)定可靠的業(yè)務(wù)系統(tǒng),提升業(yè)務(wù)穩(wěn)定性。我們期望能夠分享更多幫助企業(yè)客戶上云、用好云,讓客戶云上業(yè)務(wù)運(yùn)行更加穩(wěn)定可靠的技術(shù),您可用釘釘掃描下方二維碼,加入阿里云SRE技術(shù)學(xué)院釘釘圈子,和更多云上人交流關(guān)于云平臺的那些事。
原文鏈接:https://developer.aliyun.com/article/783989?
版權(quán)聲明:本文內(nèi)容由阿里云實(shí)名注冊用戶自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,阿里云開發(fā)者社區(qū)不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。具體規(guī)則請查看《阿里云開發(fā)者社區(qū)用戶服務(wù)協(xié)議》和《阿里云開發(fā)者社區(qū)知識產(chǎn)權(quán)保護(hù)指引》。如果您發(fā)現(xiàn)本社區(qū)中有涉嫌抄襲的內(nèi)容,填寫侵權(quán)投訴表單進(jìn)行舉報(bào),一經(jīng)查實(shí),本社區(qū)將立刻刪除涉嫌侵權(quán)內(nèi)容。 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的混合云K8s容器化应用弹性伸缩实战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国内唯一入选云厂商——阿里云入选 202
- 下一篇: 宜搭数字工厂,让订单周期缩减三分之一