日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

混合云K8s容器化应用弹性伸缩实战

發布時間:2024/9/3 编程问答 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 混合云K8s容器化应用弹性伸缩实战 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡介:混合云K8s容器化應用彈性伸縮實戰

1. 前提條件

本最佳實踐的軟件環境要求如下:
應用環境:
①容器服務ACK基于專有云V3.10.0版本。
②公共云云企業網服務CEN。
③公共云彈性伸縮組服務ESS。
配置條件:
1)使用專有云的容器服務或者在ECS上手動部署敏捷PaaS。
2)開通云專線,打通容器服務所在VPC與公共云上的VPC。
3)開通公共云彈性伸縮組服務(ESS)。

2. 背景信息

本實踐基于K8s的業務集群運行在專有云上,對測試業務進行壓力測試,主要基于以下三種產品和能力:
①利用阿里云的云企業網專線打通專有云和公共云,實現兩朵云上VPC網絡互通。
②利用K8s(Kubernetes)的HPA能力,實現容器的水平伸縮。
③利用K8s的Cluster Autoscaler和阿里云彈性伸縮組ESS能力實現節點的自動伸縮。

HPA(Horizontal Pod Autoscaler)是K8s的一種資源對象,能夠根據CPU、內存等指標對statefulset、deployment等對象中的pod數量進行動態伸縮,使運行在上面的服務對指標的變化有一定的自適應能力。

當被測試業務指標達到上限時,觸發HPA自動擴容業務pod;當業務集群無法承載更多pod時,觸發公共云的ESS服務,在公共云內擴容出ECS并自動添加到專有云的K8s集群。


圖 1:架構原理圖

3. 配置HPA

本示例創建了一個支持HPA的nginx應用,創建成功后,當Pod的利用率超過本例中設置的20%利用率時,則會進行水平擴容,低于20%的時候會進行縮容。

1.若使用自建K8s集群,則通過yaml文件配置HPA

1)創建一個nginx應用,必須為應用設置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: //必須設置request值securityContext: {}restartPolicy:AlwaysschedulerName:default-schedulerreplicas: 1selector: matchLabels:app: hpa-testrevisionHistoryLimit: 10strategy: type: RollingUpdaterollingUpdate:maxSurge: 25%maxUnavailable: 25%progressDeadlineSeconds: 600

2)創建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: //設置pod數量 minReplicas: 1scaleTargetRef:apiVersion: apps/v1beta2kind: Deploymentname: centos targetCPUUtilizationPercentage://設置CPU閾值
2.若使用阿里云容器服務,需要在部署應用時選擇配置HPA


圖2:訪問設置

4. 配置Cluster Autoscaler

資源請求(Request)的正確、合理設置,是彈性伸縮的前提條件。節點自動伸縮組件基于K8s資源調度的分配情況進行伸縮判斷,節點中資源的分配通過資源請(Request)進行計算。

當Pod由于資源請求(Request)無法滿足并進入等待(Pending)狀態時,節點自動伸縮組件會根據彈性伸縮組配置信息中的資源規格以及約束配置,計算所需的節點數目。

如果可以滿足伸縮條件,則會觸發伸縮組的節點加入。而當一個節點在彈性伸縮組中且節點上Pod的資源請求低于閾值時,節點自動伸縮組件會將節點進行縮容。

1.配置彈性伸縮組ESS

1)創建ESS彈性伸縮組,記錄最小實例數和最大實例數。


圖3:修改伸縮組

2)創建伸縮配置,記錄伸縮配置的id。


圖4:伸縮配置

#!/bin/sh yum install -y ntpdate && ntpdate -u ntp1.aliyun.com && curl http:// example.com/public/hybrid/attach_local_node_aliyun.sh | bash -s -- --docker-version 17.06.2-ce-3 --token 9s92co.y2gkocbumal4fz1z --endpoint 192.168.**.***:6443 --cluster-dns 10.254.**.** --region cn-huhehaote echo "{" > /etc/docker/daemon.json echo "\"registry-mirrors\": [" >> /etc/docker/daemon.json echo "\"https://registry-vpc.cn-huhehaote.aliyuncs.com\"" >> /etc/docker/daemon.json echo "]," >> /etc/docker/daemon.json echo "\"insecure-registries\": [\"https://192.168.**.***:5000\"]" >> /etc/docker/daemon.json echo "}" >> /etc/docker/daemon.json systemctl restart docker
2.K8s集群部署autoscaler
kubectl apply -f ca.yml

參考ca.yml創建autoscaler,注意修改如下配置與實際環境相對應。

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-id

5. 執行結果

模擬業務訪問:

啟動busybox鏡像,在pod內執行如下命令訪問以上應用的service,可以同時啟動多個pod增加業務負載。while true;do wget -q -O- http://hpa-test/index.html;done

觀察HPA:

加壓前


圖 5:加壓前

加壓后
當CPU值達到閾值后,會觸發pod的水平擴容。


圖 6:加壓后1

圖 7:加壓后2

觀察Pod:

當集群資源不足時,新擴容出的pod處于pending狀態,此時將觸發cluster autoscaler,自動擴容節點。


圖8:伸縮活動

我們是阿里云智能全球技術服務-SRE團隊,我們致力成為一個以技術為基礎、面向服務、保障業務系統高可用的工程師團隊;提供專業、體系化的SRE服務,幫助廣大客戶更好地使用云、基于云構建更加穩定可靠的業務系統,提升業務穩定性。我們期望能夠分享更多幫助企業客戶上云、用好云,讓客戶云上業務運行更加穩定可靠的技術,您可用釘釘掃描下方二維碼,加入阿里云SRE技術學院釘釘圈子,和更多云上人交流關于云平臺的那些事。

原文鏈接:https://developer.aliyun.com/article/783989?

版權聲明:本文內容由阿里云實名注冊用戶自發貢獻,版權歸原作者所有,阿里云開發者社區不擁有其著作權,亦不承擔相應法律責任。具體規則請查看《阿里云開發者社區用戶服務協議》和《阿里云開發者社區知識產權保護指引》。如果您發現本社區中有涉嫌抄襲的內容,填寫侵權投訴表單進行舉報,一經查實,本社區將立刻刪除涉嫌侵權內容。 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的混合云K8s容器化应用弹性伸缩实战的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。