# kubernetes\_vpa\_hpa\_ca
* [HPA](kubernetes_vpa_hpa_ca.md#hpa)
* [æ¦å¿µååç](kubernetes_vpa_hpa_ca.md#æ¦å¿µååç)
* [Install Metrics-Server](kubernetes_vpa_hpa_ca.md#install-metrics-server)
* [usage](kubernetes_vpa_hpa_ca.md#usage)
* [示ä¾é
ç½®](kubernetes_vpa_hpa_ca.md#示ä¾é
ç½®)
* [VPA](kubernetes_vpa_hpa_ca.md#vpa)
* [æ¦å¿µååç](kubernetes_vpa_hpa_ca.md#æ¦å¿µååç)
* [å®è£
](kubernetes_vpa_hpa_ca.md#å®è£
)
* [å
³äºååå
¼å®¹æ§ç注æäºé¡¹](kubernetes_vpa_hpa_ca.md#å
³äºååå
¼å®¹æ§ç注æäºé¡¹)
* [å
å³æ¡ä»¶](kubernetes_vpa_hpa_ca.md#å
å³æ¡ä»¶)
* [å®è£
](kubernetes_vpa_hpa_ca.md#å®è£
)
* [å¿«éå¼å§](kubernetes_vpa_hpa_ca.md#å¿«éå¼å§)
* [æµè¯æ¨çå®è£
](kubernetes_vpa_hpa_ca.md#æµè¯æ¨çå®è£
)
* [示ä¾é
ç½®](kubernetes_vpa_hpa_ca.md#示ä¾é
ç½®)
* [æ
éæé¤](kubernetes_vpa_hpa_ca.md#æ
éæé¤)
* [å é¤](kubernetes_vpa_hpa_ca.md#å é¤)
* [alphaçæ¬çå·²ç¥éå¶](kubernetes_vpa_hpa_ca.md#alphaçæ¬çå·²ç¥éå¶)
* [CA](kubernetes_vpa_hpa_ca.md#ca)
* [ä»ä¹æ¯cluster-autoscaler](kubernetes_vpa_hpa_ca.md#ä»ä¹æ¯cluster-autoscaler)
* [ä»ä¹æ¶åæ©ï¼](kubernetes_vpa_hpa_ca.md#ä»ä¹æ¶åæ©)
* [ä»ä¹æ¶å缩ï¼](kubernetes_vpa_hpa_ca.md#ä»ä¹æ¶å缩)
* [ä»ä¹æ ·çèç¹ä¸ä¼è¢«CAå é¤](kubernetes_vpa_hpa_ca.md#ä»ä¹æ ·çèç¹ä¸ä¼è¢«caå é¤)
* [å¦ä½é²æ¢node被Cluster Autoscalerå é¤](kubernetes_vpa_hpa_ca.md#å¦ä½é²æ¢node被cluster-autoscalerå é¤)
* [é¨ç½²æ¹å¼](kubernetes_vpa_hpa_ca.md#é¨ç½²æ¹å¼)
* [What does CA do?](kubernetes_vpa_hpa_ca.md#what-does-ca-do)
* [What CA doesn't do?](kubernetes_vpa_hpa_ca.md#what-ca-doesnt-do)
* [Cluster Autoscaleræ¶æ](kubernetes_vpa_hpa_ca.md#cluster-autoscaleræ¶æ)
* [æ©å®¹æµç¨](kubernetes_vpa_hpa_ca.md#æ©å®¹æµç¨)
* [å¦ä½çæ§Cluster Autoscaler](kubernetes_vpa_hpa_ca.md#å¦ä½çæ§cluster-autoscaler)
* [CAé群ç¶ææ£æ¥](kubernetes_vpa_hpa_ca.md#caé群ç¶ææ£æ¥)
* [é¨ç½²ä½¿ç¨CA](kubernetes_vpa_hpa_ca.md#é¨ç½²ä½¿ç¨ca)
* [CA代ç é»è¾è§£æ](kubernetes_vpa_hpa_ca.md#ca代ç é»è¾è§£æ)
* [CA æ¶åºå¾](kubernetes_vpa_hpa_ca.md#ca-æ¶åºå¾)
* [CA scaldownæµç¨å¾](kubernetes_vpa_hpa_ca.md#ca-scaldownæµç¨å¾)
* [CA scalupæµç¨å¾](kubernetes_vpa_hpa_ca.md#ca-scalupæµç¨å¾)
* [cloudprovider ç®åçæµç¨å¾](kubernetes_vpa_hpa_ca.md#cloudprovider-ç®åçæµç¨å¾)
* [kubernetes cluster autoscalerè°ç ä¸hpa/vpaèå¨](kubernetes_vpa_hpa_ca.md#kubernetes-cluster-autoscalerè°ç ä¸hpa/vpaèå¨)
* [横åæ©å®¹\(HPA\)](kubernetes_vpa_hpa_ca.md#横åæ©å®¹hpa)
* [纵åæ©å®¹\(VPA\)](kubernetes_vpa_hpa_ca.md#纵åæ©å®¹vpa)
* [é群æ©å®¹\(Cluster Autoscaler\)](kubernetes_vpa_hpa_ca.md#é群æ©å®¹cluster-autoscaler)
**HPA**
### æ¦å¿µååç
å©ç¨Horizontal Pod Autoscalingï¼HPAï¼ï¼kubernetesè½å¤æ ¹æ®çæµå°çCPUå©ç¨çèªå¨çæ©ç¼©å®¹ replication controllerï¼deploymentåreplica setä¸podçæ°éã
HPAä½ä¸ºkubernetes API resourceåcontroller çå®ç°ãResourceç¡®å®controllerçè¡ä¸ºãController ä¼æ ¹æ®çæµå°ç¨æ·æå®çç®æ ç CPU å©ç¨çå¨ææ§å°è°æ´ replication controller æ deployment ç replica æ°éã
![image](../.gitbook/assets/hpa-logic.png)
HPAç±ä¸ä¸ªæ§å¶å¾ªç¯å®ç°ï¼å¾ªç¯å¨æç±controller manager ä¸ç --horizontal-pod-autoscaler-sync-periodæ å¿æå®ãå¨æ¯ä¸ªå¨æå
ï¼controller managerä¼æ¥è¯¢HPAä¸å®ä¹çmetricçèµæºå©ç¨çãController manager ä» resource metric APIï¼æ¯ä¸ª pod ç resource metricï¼æè
èªå®ä¹ metric APIï¼ææçmetricï¼ä¸è·å metricã
### Install Metrics-Server
### usage
```text
# kubectl autoscale sts apache2 --cpu-percent=50 --min=1 --max=3
```
```text
# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
apache2 StatefulSet/apache2 5%/10% 1 2 2 33m
```
```text
# kubectl describe hpa apache2
Name: apache2
Namespace: default
Labels:
Annotations:
CreationTimestamp: Wed, 11 Sep 2019 10:47:42 +0800
Reference: StatefulSet/apache2
Metrics: ( current / target )
resource cpu on pods (as a percentage of request): 5% (14m) / 10%
Min replicas: 1
Max replicas: 2
StatefulSet pods: 2 current / 2 desired
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale True ScaleDownStabilized recent recommendations were higher than current one, applying the highest recent recommendation
ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
ScalingLimited False DesiredWithinRange the desired count is within the acceptable range
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulRescale 89s horizontal-pod-autoscaler New size: 2; reason: cpu resource utilization (percentage of request) above target
```
* AbleToScaleï¼è¡¨æHPAæ¯å¦ å¯ä»¥è·ååæ´æ°ä¼¸ç¼©ä¿¡æ¯ï¼ä»¥åæ¯å¦åå¨é»æ¢ä¼¸ç¼©çåç§åéæ¡ä»¶
* ScalingActiveï¼è¡¨æHPAæ¯å¦è¢«å¯ç¨ï¼å³ç®æ çå¯æ¬æ°éä¸ä¸ºé¶ï¼ 以åæ¯å¦è½å¤å®æ伸缩计ç®ãå½è¿ä¸ç¶æ为Falseæ¶ï¼é常表æè·å度éææ åå¨é®é¢ã
* ScalingLimittedï¼è¡¨ææé伸缩çå¼è¢«HorizontalPodAutoscaleræå®ä¹çæ大æè
æå°å¼æéå¶ï¼å³å·²ç»è¾¾å°æ大æè
æå°ä¼¸ç¼©å¼ï¼ãè¿é常表ææ¨å¯è½éè¦è°æ´HorizontalPodAutoscaler æå®ä¹çæ大æè
æå°å¯æ¬æ°éçéå¶äºã
### 示ä¾é
ç½®
第ä¸æ¥ï¼é¨ç½²podãserviceï¼
```text
$ kubectl run php-apache --image=gcr.io/google_containers/hpa-example --requests=cpu=200m --expose --port=80
service "php-apache" created
deployment "php-apache" created
```
第äºæ¥ï¼å建Horizontal Pod Autoscalerï¼
```text
$ kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
deployment "php-apache" autoscaled
```
```text
$ kubectl get hpa
NAME REFERENCE TARGET MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache/scale 0% / 50% 1 10 1
```
第ä¸æ¥ï¼å¢å è´è½½ï¼
```text
$ kubectl run -i --tty load-generator --image=busybox /bin/sh
Hit enter for command prompt
$ while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done
```
```text
$ kubectl get hpa
NAME REFERENCE TARGET CURRENT MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache/scale 305% / 50% 305% 1 10 1 3m
```
```text
$ kubectl get deployment php-apache
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
php-apache 7 7 7 7 19m
```
第åæ¥ï¼åæ¢è´è½½ï¼
```text
$ kubectl get hpa
NAME REFERENCE TARGET MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache/scale 0% / 50% 1 10 1 11m
$ kubectl get deployment php-apache
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
php-apache 1 1 1 1 27m
```
## VPA
### æ¦å¿µååç
Vertical Pod Autoscalerï¼VPAï¼ä½¿ç¨æ·æ é为å
¶podsä¸ç容å¨è®¾ç½®ææ°çèµæºrequestãé
ç½®åï¼å®å°æ ¹æ®ä½¿ç¨æ
åµèªå¨è®¾ç½®requestï¼ä»èå
许å¨èç¹ä¸è¿è¡éå½çè°åº¦ï¼ä»¥ä¾¿ä¸ºæ¯ä¸ªpodæä¾éå½çèµæºéã
使ç¨å为VerticalPodAutoscalerçèªå®ä¹èµæºå®ä¹å¯¹è±¡é
ç½®èªå¨ç¼©æ¾ ãå®å
许æå®åç´èªå¨ç¼©æ¾çpod以åæ¯å¦/å¦ä½åºç¨èµæºå»ºè®®ã
è¦å¨ç¾¤éä¸å¯ç¨vpaï¼è¯·æç
§ä¸é¢ä»ç»çå®è£
æ¥éª¤è¿è¡æä½ã
### å®è£
#### å
³äºååå
¼å®¹æ§ç注æäºé¡¹
å¨alphaæé´ï¼VPA CRD对象å¯è½ä»¥ä¸åçæ¬ä¹é´çå
¼å®¹æ¹å¼åå±ãå¦æå®è£
æ°çæ¬çVPAï¼æå®å
¨çåæ³æ¯å é¤ç°æçVPA CRD对象ã请注æï¼å¦ææ¨åªæ¯ä½¿ç¨vpa-down.shèæ¬æé¤æ§çVPAå®è£
ï¼åä¼èªå¨æ§è¡æ¤æä½ã
#### å
å³æ¡ä»¶
* 强ç建议使ç¨Kubernetes 1.9ææ´é«çæ¬ãæ¨ç群éå¿
é¡»æ¯æMutatingAdmissionWebhooksï¼é»è®¤æ
åµä¸èª1.9ï¼\#58255ï¼å¯ç¨ãäºè§£æå
³VPA Admission Webhookçæ´å¤ä¿¡æ¯ã
* kubectl åºè¯¥è¿æ¥å°è¦å®è£
VPAç群éã
* å¿
é¡»å¨ç¾¤éä¸é¨ç½²Metrics Serverãé
读æå
³Metrics Serverçæ´å¤ä¿¡æ¯ã
* å¦ææ¨ä½¿ç¨çæ¯GKE Kubernetes群éï¼åéè¦æäºæ¨å½åçGoogle身份 cluster-adminè§è²ãå¦åï¼æ¨å°æ ææäºVPAç³»ç»ç»ä»¶é¢å¤çæéã
```text
$ gcloud info | grep Account # get current google identity
Account: [[email protected]]
$ kubectl create clusterrolebinding myname-cluster-admin-binding --clusterrole=cluster-admin [email protected]
Clusterrolebinding "myname-cluster-admin-binding" created
```
* å¦ææ¨ç群éä¸å·²å®è£
äºå¦ä¸çæ¬çVPAï¼åå¿
é¡»é¦å
å é¤ç°æå®è£
ï¼
```text
./hack/vpa-down.sh
```
#### å®è£
è¦å®è£
VPAï¼è¯·ä¸è½½VPAçæºä»£ç ï¼ä¾å¦ä½¿ç¨ git clone [https://github.com/kubernetes/autoscaler.gitï¼å¹¶å¨vertical-pod-autoscalerç®å½ä¸è¿è¡ä»¥ä¸å½ä»¤ï¼](https://github.com/kubernetes/autoscaler.gitï¼å¹¶å¨vertical-pod-autoscalerç®å½ä¸è¿è¡ä»¥ä¸å½ä»¤ï¼)
```text
./hack/vpa-up.sh
```
注æï¼èæ¬å½å读åç¯å¢åéï¼$REGISTRYå$TAGãé¤éæ¨è¦ä½¿ç¨éé»è®¤çæ¬çVPAï¼å¦å请确ä¿ä¸è®¾ç½®å®ä»¬ã
该èæ¬kubectlåé群ååºå¤ä¸ªå½ä»¤ï¼è¿äºå½ä»¤æå
¥é
置并å¨kube-systemå½å空é´ä¸å¯å¨ææéè¦çpodï¼è¯·åé
ä½ç³»ç»æï¼ãå®è¿ä¼çæ并ä¸è½½VPA Admission Controllerå¨ä¸APIæå¡å¨éä¿¡æ¶ä½¿ç¨çæºå¯ï¼CAè¯ä¹¦ï¼ã
#### å¿«éå¼å§
å®è£
å®æåï¼ç³»ç»å°±å¯ä»¥ä¸ºæ¨çpod建议å设置èµæºè¯·æ±ã为äºä½¿ç¨å®ï¼æ¨éè¦ä¸ºå
·æç¸ä¼¼èµæºè¦æ±çæ¯ä¸ªé»è¾podç»æå
¥Vertical Pod Autoscalerèµæºãæ们建议为æ¯ä¸ªè¦èªå¨æ§å¶çé¨ç½²æå
¥VPAï¼å¹¶ä½¿ç¨ä¸é¨ç½²ä½¿ç¨çç¸åçæ ç¾éæ©å¨ãVPAæä¸ç§è¿ä½æ¨¡å¼ï¼
* "Auto"ï¼VPAå¨å建podæ¶åé
èµæºè¯·æ±ï¼å¹¶ä½¿ç¨é¦éæ´æ°æºå¶å¨ç°æpodä¸æ´æ°å®ä»¬ãç®åè¿ç¸å½äº"Recreate"ï¼è§ä¸æï¼ãä¸æ¦éå¯å
è´¹ï¼âin-placeâï¼ï¼pod请æ±çæ´æ°å¯ç¨ï¼"Auto"模å¼å¯ä»¥è¢«ç¨ä½ä¼éçæ´æ°æºå¶ã
* "Recreate"ï¼VPAå¨å建podæ¶åé
èµæºè¯·æ±ï¼å¹¶å¨è¯·æ±çèµæºä¸æ°å»ºè®®ææ¾ä¸åæ¶ï¼å¦æå·²å®ä¹ï¼åéµå¾ªPodä¸æé¢ç®ï¼ï¼éè¿éåºé©±å¨æ¥æ´æ°å®ä»¬ãåªæå¨éè¦ç¡®ä¿å¨èµæºè¯·æ±åçæ´æ¹æ¶éæ°å¯å¨podæ¶ï¼æåºè¯¥å¾å°ä½¿ç¨æ¤æ¨¡å¼ãå¦åæ´å欢"Auto"å¯ä»¥å©ç¨å
éå¯çæ´æ°æ¨¡å¼ã
* "Initial"ï¼VPAä»
å¨å建podæ¶åé
èµæºè¯·æ±ï¼å¹¶ä¸ä»¥åæ°¸è¿ä¸ä¼æ´æ¹å®ä»¬ã
* "Off"ï¼VPAä¸ä¼èªå¨æ´æ¹podsçèµæºè¦æ±ã计ç®å»ºè®®å¹¶å¯å¨VPA对象ä¸æ£æ¥ã
#### æµè¯æ¨çå®è£
æ£æ¥Vertical Pod Autoscalerå¨é群ä¸æ¯å¦å®å
¨å¯æä½çä¸ç§ç®åæ¹æ³æ¯å建示ä¾é¨ç½²åç¸åºçVPAé
ç½®ï¼
```text
kubectl create -f examples/hamster.yaml
```
ä¸é¢çå½ä»¤å建äºä¸ä¸ªå
å«2个podçdeploymentï¼æ¯ä¸ªpodè¿è¡ä¸ä¸ªè¯·æ±100 millicores ç容å¨ï¼å¹¶å°è¯ä½¿ç¨ç¥é«äº500millicoresç容å¨ã该å½ä»¤è¿ä¼å建ä¸ä¸ªVPAé
ç½®ï¼å
¶ä¸å
å«ä¸é¨ç½²ä¸çpodå¹é
çéæ©å¨ãVPAå°è§å¯podçè¡ä¸ºï¼å¤§çº¦5åéåï¼ä»ä»¬åºè¯¥ä½¿ç¨æ´é«çCPU请æ±è¿è¡æ´æ°ï¼è¯·æ³¨æï¼VPAä¸ä¼ä¿®æ¹é¨ç½²ä¸ç模æ¿ï¼ä½ä¼æ´æ°podçå®é
请æ±ï¼ãè¦æ¥çVPAé
ç½®åå½å建议çèµæºè¯·æ±ï¼è¯·æ§è¡:
```text
kubectl describe vpa
```
注æï¼å¦ææ¨ç群éçå¯ç¨å®¹éå¾å°ï¼åè¿äºå®¹å¨å¯è½æ æ³å®æãæ¨å¯è½éè¦æ·»å æ´å¤èç¹æè°æ´examples / hamster.yaml以使ç¨æ´å°çCPUã
#### 示ä¾é
ç½®
```text
apiVersion: poc.autoscaling.k8s.io/v1alpha1
kind: VerticalPodAutoscaler
metadata:
name: my-app-vpa
spec:
selector:
matchLabels:
app: my-app
updatePolicy:
updateMode: "Auto"
```
#### æ
éæé¤
è¦è¯æVPAå®è£
é®é¢ï¼è¯·æ§è¡ä»¥ä¸æ¥éª¤ï¼
æ£æ¥ææç³»ç»ç»ä»¶æ¯å¦æ£å¨è¿è¡ï¼
```text
kubectl --namespace=kube-system get pods|grep vpa
```
ä¸é¢çå½ä»¤åºè¯¥ååºç¶æ为Runningç3个podï¼æ¨èè
ï¼æ´æ°è
ååå
¥æ§å¶å¨ï¼ã
æ£æ¥ç³»ç»ç»ä»¶æ¯å¦è®°å½ä»»ä½é误ã对äºä¸ä¸ä¸ªå½ä»¤è¿åçæ¯ä¸ªpodï¼æ§è¡ä»¥ä¸æä½ï¼
```text
kubectl --namespace=kube-system logs [pod name]| grep -e '^E[0-9]\{4\}'
```
æ£æ¥æ¯å¦å·²å建VPAèªå®ä¹èµæºå®ä¹ï¼
```text
kubectl get customresourcedefinition|grep verticalpodautoscalers
```
VPAçç»æé¨å 该项ç®å
æ¬3个ç»æé¨åï¼
* Recommender - å®çè§å½ååè¿å»çèµæºæ¶èï¼å¹¶æ ¹æ®å®æä¾æ¨èå¼å®¹å¨çCPUåå
å请æ±ã
* Updater - å®æ£æ¥åªäºæ管çªæ ¼å
·ææ£ç¡®çèµæºéï¼å¦æ没æï¼åæ£æ¥å®ä»¬ï¼ä»¥ä¾¿æ§å¶å¨å¯ä»¥ä½¿ç¨æ´æ°ç请æ±éæ°å建å®ä»¬ã
* Admission Plugin - å®å¨æ°podä¸è®¾ç½®æ£ç¡®çèµæºè¯·æ±ï¼ç±äºUpdaterçæ´»å¨èååç±å
¶æ§å¶å¨å建æéæ°å建ï¼ã
#### å é¤
请注æï¼å¦ææ¨åæ¢å¨ç¾¤éä¸è¿è¡VPAï¼åVPA已修æ¹çpodçèµæºè¯·æ±å°ä¸ä¼æ´æ¹ï¼ä½ä»»ä½æ°podå°è·åæ§å¶å¨ä¸å®ä¹çèµæºï¼å³é¨ç½²æå¤å¶ï¼ï¼èä¸æ¯æ ¹æ®å
åç建议ç±VPAã
è¦åæ¢å¨ç¾¤éä¸ä½¿ç¨Vertical Pod Autoscalingï¼
å¦æå¨GKEä¸è¿è¡ï¼è¯·æ¸
é¤å¨å
å³æ¡ä»¶ä¸å建çè§è²ç»å®ï¼
```text
kubectl delete clusterrolebinding myname-cluster-admin-binding
```
å é¤VPAç»ä»¶ï¼
```text
./hack/vpa-down.sh
```
#### alphaçæ¬çå·²ç¥éå¶
* æ¯å½VPAæ´æ°podèµæºæ¶ï¼é½ä¼éæ°å建podï¼è¿ä¼å¯¼è´éæ°å¯å¨æææ£å¨è¿è¡ç容å¨ãå¯ä»¥å¨ä¸åèç¹ä¸éæ°å建podã
* vpaä¸åºä¸CPUæå
åä¸çHorizoâântal Pod Autoscaler\(HPA\)ä¸èµ·ä½¿ç¨ãä½æ¯ï¼æ¨å¯ä»¥å¨èªå®ä¹åå¤é¨ææ ä¸ä½¿ç¨VPAåHPAã
* Auto模å¼ä¸çVPA åªè½ç¨äºå¨æ§å¶å¨ï¼ä¾å¦é¨ç½²ï¼ä¸è¿è¡çpodï¼åè
è´è´£éæ°å¯å¨å·²å é¤çpodã å¨Auto模å¼ä¸ï¼æ²¡æå¨ä»»ä½æ§å¶å¨ä¸è¿è¡çpodç模å¼ä¸ä½¿ç¨VPA å°å¯¼è´å é¤è¯¥pod并ä¸ä¸ä¼éæ°å建该podã
* VPAåå
¥æ§å¶å¨æ¯ä¸ä¸ªadmission webhookãå¦ææ¨å群éæ·»å å
¶ä»admission webhookï¼åå¿
é¡»åæå®ä»¬ä¹é´ç交äºæ¹å¼ä»¥åå®ä»¬æ¯å¦å¯è½ç¸äºå²çªãåå
¥æ§å¶å¨ç顺åºç±APIserverä¸çæ å¿å®ä¹ã
* VPAä¼å¯¹æäºå
åä¸è¶³äºä»¶ååºååºï¼ä½å¹¶éå¨æææ
åµä¸é½ä¼åçã
* VPAæ§è½å°æªå¨å¤§åé群ä¸è¿è¡æµè¯ã
* VPA建议å¯è½ä¼è¶
åºå¯ç¨èµæºï¼ä¾å¦èç¹å¤§å°ï¼å¯ç¨å¤§å°ï¼å¯ç¨é
é¢ï¼å¹¶å¯¼è´podè¿å
¥å¾
å¤çç¶æãè¿å¯ä»¥éè¿å°VPAä¸Cluster Autoscalerä¸èµ·ä½¿ç¨æ¥è§£å³ã
* ä¸åä¸ä¸ªpodå¹é
çå¤ä¸ªVPAèµæºå
·ææªå®ä¹çè¡ä¸ºã
ç¸å
³é¾æ¥
[FQA](https://github.com/kubernetes/autoscaler/blob/master/vertical-pod-autoscaler/FAQ.md)
[设计æ¹æ¡](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/autoscaling/vertical-pod-autoscaler.md)
[APIå®ä¹](https://github.com/kubernetes/autoscaler/blob/master/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1beta2/types.go)
[GitHub](https://github.com/kubernetes/autoscaler/blob/master/vertical-pod-autoscaler/README.md)
## CA
### ä»ä¹æ¯cluster-autoscaler
CA[cluster-autoscaler](https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler)æ¯ç¨æ¥å¼¹æ§ä¼¸ç¼©kubernetesé群çãæ们å¨ä½¿ç¨kubernetesé群ç»å¸¸é®å°çä¸ä¸ªé®é¢æ¯ï¼æåºè¯¥ä¿æå¤å¤§çèç¹è§æ¨¡æ¥æ»¡è¶³åºç¨éæ±å¢ï¼ cluster-autoscalerçåºç°è§£å³äºè¿ä¸ªé®é¢ï¼å®å¯ä»¥èªå¨çæ ¹æ®é¨ç½²çåºç¨æ请æ±çèµæºéæ¥å¨æç伸缩é群
#### ä»ä¹æ¶åæ©ï¼
ç±äºèµæºä¸è¶³ï¼podè°åº¦å¤±è´¥ï¼å¯¼è´podå¤äºpendingç¶ææ¶
#### ä»ä¹æ¶å缩ï¼
nodeçèµæºå©ç¨çè¾ä½æ¶ï¼ä¸æ¤nodeä¸åå¨çpodé½è½è¢«éæ°è°åº¦å°å
¶ä»èç¹
#### ä»ä¹æ ·çèç¹ä¸ä¼è¢«CAå é¤
* èç¹ä¸æpod被PodDisruptionBudgetæ§å¶å¨éå¶ã
* èç¹ä¸æå½å空é´æ¯kube-systemçpodsã
* èç¹ä¸çpodä¸æ¯è¢«æ§å¶å¨å建ï¼ä¾å¦ä¸æ¯è¢«deployment, replica set, job, stateful setå建ã
* èç¹ä¸æpod使ç¨äºæ¬å°åå¨
* èç¹ä¸pod驱éåæ å¤å¯å»ï¼å³æ²¡æå
¶ä»nodeè½è°åº¦è¿ä¸ªpod
* èç¹æ注解ï¼âcluster-autoscaler.kubernetes.io/scale-down-disabledâ: âtrueâ
#### å¦ä½é²æ¢node被Cluster Autoscalerå é¤
kubectl annotate node cluster-autoscaler.kubernetes.io/scale-down-disabled=true
#### é¨ç½²æ¹å¼
deployment
#### What does CA do?
Monitor the state of the cluster Look for pending pods Simulate scheduler Resize NodeGroups based on result of simulations
#### What CA doesn't do?
Register nodes in kubernetes Configure nodes in any way Put any labels or taints on new nodes Look at actual resource usage Support custom scheduling Predictive autoscaling
#### Cluster Autoscaleræ¶æ
![k8s-ca](../.gitbook/assets/k8s-ca.png)
autoscalerï¼æ ¸å¿æ¨¡åï¼è´è´£æ´ä½æ©ç¼©å®¹åè½ Estimatorï¼è´è´£è¯ä¼°è®¡ç®æ©å®¹ Simulatorï¼è´è´£æ¨¡æè°åº¦ï¼è®¡ç®ç¼©å®¹ Cloud Providerï¼æ½è±¡äºCloudProvideråNodeGroupçç¸å
³æ¥å£ï¼ä¸äºAPI交äº
#### æ©å®¹æµç¨
![k8s-ca-up](../.gitbook/assets/k8s-ca-up.png)
Cluster autoscaleræ¯10sæ£æ¥æ¯å¦æpodå¤äºpendingç¶æ å½åç°æpodç±äºèµæºä¸è¶³èå¤äºpendingç¶æï¼Cluster Autoscaleråå¤è¿è¡nodeçå¨å¤ ç±cloud providerå建nodeï¼nodeå å
¥é群ï¼è¿å
¥readyç¶æ
Scale-upå¯å¨ä¸ä¸ªAPIçå¬çæå¡ï¼æ¯10s\(--scan-interval\)æ£æ¥å 为æ nodeå¯è°åº¦å¯¼è´pendingçpodsï¼å³unschedulable podsï¼PodCondition=falseï¼reason=unschedulableãCAå设é群æ¯åºäºnode groupsï¼å个node groupéçæºå¨å
·æç¸åçæ§è½ååæ ·çlabesléãåºäºè¿ä¸ªè¢è£ï¼CA为æ¯ä¸ªnode groupå建ä¸ä¸ªtemplate nodesã Expanderï¼ç¨äºæ ¹æ®çç¥å³å®scale upåªä¸ªnode groupã æ°nodeæåä¹åéè¦ä¸äºæ¶é´æè½è¢«æ³¨åå°kubernetesï¼ä¾èµäºcloud provideråé¨ç½²çæ¶é´ãCAå¸ænodeå¨é群ä¸ççå¾
æ¶é´å¨15mä¹å
ï¼max-node-provision-timeï¼ãå¦ææªå¦æ注åï¼scale upä¼æèµ·æ°node并移é¤æªæ³¨åçnode
#### å¦ä½çæ§Cluster Autoscaler
CA metrics /metrics /health-check
#### CAé群ç¶ææ£æ¥
![k8s-cluster](../.gitbook/assets/k8s-cluster.png)
/var/log/cluster-autoscaler.log
kubectl get configmap cluster-autoscaler-status -n kube-system -o yaml
Events
on pods \(ç¹å«æ¯unscheduled pods\) on nodes on kube-system/cluster-autoscaler-status config map
kubectl get events -n kube-system \| grep cluster-autoscaler-status
#### é¨ç½²ä½¿ç¨CA
ç´æ¥å¨é群ä¸é¨ç½²å³å¯ï¼ç®åçyamlå¦ä¸æ示ï¼å¯å¨åæ°æéæ·»å ï¼å
¶ä¸æ¯æå°èç¹æ°ï¼æ¯æ大èç¹æ°
```text
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: cluster-autoscaler
labels:
k8s-app: cluster-autoscaler
spec:
replicas: 1
selector:
matchLabels:
k8s-app: cluster-autoscaler
template:
metadata:
labels:
k8s-app: cluster-autoscaler
spec:
containers:
- image: cluster-autoscaler:latest
name: cluster-autoscaler
command:
- ./cluster-autoscaler
- --nodes={{MIN}}:{{MAX}}:k8s-worker-asg-1
```
### CA代ç é»è¾è§£æ
#### CA æ¶åºå¾
![ca](../.gitbook/assets/k8s-ca-logic.png)
#### CA scaldownæµç¨å¾
![ScaleDown](../.gitbook/assets/k8s-scaledown-2.png)
#### CA scalupæµç¨å¾
![ScaleUp](../.gitbook/assets/k8s-scale-up.png)
#### cloudprovider ç®åçæµç¨å¾
![CA](../.gitbook/assets/paas-ca.png)
## kubernetes cluster autoscalerè°ç ä¸hpa/vpaèå¨
Kubernetesä½ä¸ºå®¹å¨ç¼æå·¥å
·ï¼åºç¨é¨ç½²å¨é群ä¸ï¼åºç¨çè´è½½æ¬èº«æ¯ä¼éçæ¶é´å¨æåçååçï¼ä¸ºäºæ´å¥½ç平衡èµæºä½¿ç¨ç以åæ§è½ï¼kuberneteså¼å
¥äºautoscalerãå¯ä»¥æ ¹æ®åºç¨è´è½½çæ
åµå¨æçæ©ç¼©å®¹èµæº Kubernetesçautoscaleråæ两个å±æ¬¡:
* pod级å«çæ©å®¹ï¼å
å«æ¨ªåæ©å®¹\(HPA\)以å纵åæ©å®¹\(VPA\),æ©å®¹å®¹å¨å¯ç¨çèµæºä½¿ç¨éã
* é群级å«çæ©å®¹ï¼éè¿CA\(Cluster Autoscaler\)æ¥æ§å¶æ©å®¹æè
缩å°é群ä¸Nodeçæ°éãé群级å«çæ©å®¹ï¼éè¿CA\(Cluster Autoscaler\)æ¥æ§å¶æ©å®¹æè
缩å°é群ä¸Nodeçæ°éã
### 横åæ©å®¹\(HPA\)
æ©å®¹podçå¯æ¬æ°ï¼éè¿å®¹å¨çCPU以åï¼emoryæ¥è§¦åæ©å®¹æè
缩容æä½ï¼å¹¶ä¸æ¯æèªå®ä¹ææ ãå¤ä¸ªææ çè³æ¯å¤é¨çææ æ¥ä½ä¸ºè§¦åæ©å®¹æè
缩容æä½çæ¡ä»¶ã HPAçå·¥ä½æµ
![hpa](../.gitbook/assets/hpa.png)
* HPAæ¯é30secæ¥æ£æ¥ææ çå¼
* å¦æSPECIFIFD éå¼æ»¡è¶³æ¡ä»¶å°ä¼å¢å podå¯æ¬çæ°é
* HPA主è¦æ´æ°deployment/replication controlleræ§å¶å¨å¯¹è±¡çå¯æ¬æ°
* Deployment/replication controllerå°ä¼å建åºæ¥é¢å¤éè¦çpods
å½ä½¿ç¨HPAçæ¶åéè¦æ³¨æçå°æ¹
* HPAæ£æ¥å¨æ为30så¯ä»¥éè¿è®¾ç½®controller managerçhorizontal-pod-autoscaler-sync-periodåæ°æ¥æ¹å
* é»è®¤çHPAç¸å¯¹ææ å
¬å·®ä¸º10%
* HPAå¨æåä¸æ¬¡æ©å®¹äºä»¶åçå¾
3åéï¼ä»¥ä½¿ææ 稳å®ä¸æ¥ãå¯éè¿ - horizontal-pod-autoscaler-upscale-delayåæ°æ¥é
ç½®
* HPAä»æåä¸æ¬¡ç¼©å®¹äºä»¶å¼å§çå¾
5åéï¼ä»¥é¿å
èªå¨è°èå¨æå¨ãå¯éè¿ - horizontal-pod-autoscaler-downscale-delayåæ°æ¥é
ç½®
* ç¸å¯¹äºreplication controllerèè¨ï¼ï½ï½ï½æ´å éåä¸deploymentä¸èµ·é
置工ä½
### 纵åæ©å®¹\(VPA\)
Vertical Pods Autoscalerï¼VPAï¼ä¸ºç°æpodåé
æ´å¤ï¼ææ´å°ï¼çCPUæå
åãå®å¯ä»¥éç¨äºæç¶æåæ ç¶æçpodï¼ä½å®ä¸»è¦æ¯ä¸ºæç¶ææå¡èæ建çãä½æ¯ï¼å¦ææ¨å¸æå®ç°æå为podåé
çèµæºçèªå¨æ´æ£ï¼åå¯ä»¥å°å
¶ç¨äºæ ç¶æ容å¨ãVPAè¿å¯ä»¥å¯¹OOMï¼å
åä¸è¶³ï¼äºä»¶ååºååºãVPAå½åè¦æ±éæ°å¯å¨pod以æ´æ¹å·²åé
çCPUåå
åãå½VPAéæ°å¯å¨podæ¶ï¼å®ä¼èèpodsååé¢ç®ï¼PDBï¼ä»¥ç¡®ä¿å§ç»å
·ææéçæå°podæ°ãæ¨å¯ä»¥è®¾ç½®VPAå¯ä»¥åé
ç»ä»»ä½podçèµæºçæå°å¼åæ大å¼ãä¾å¦ï¼æ¨å¯ä»¥å°æ大å
åéå¶éå¶ä¸ºä¸è¶
è¿8 GBãå½æ¨ç¥éå½åèç¹æ æ³ä¸ºæ¯ä¸ªå®¹å¨åé
è¶
è¿8 GBæ¶ï¼è¿å°¤å
¶æç¨ã
VPAè¿æä¸ä¸ªå为VPA Recommenderçæ趣åè½ãå®çè§ææpodçåå²èµæºä½¿ç¨æ
åµåOOMäºä»¶ï¼ä»¥å»ºè®®requestèµæºçæ°å¼ãæ¨èå¨ä½¿ç¨ä¸äºæºè½ç®æ³æ¥æ ¹æ®åå²ææ 计ç®å
ååCPUå¼ãå®è¿æä¾äºä¸ä¸ªAPIï¼éè¿å®å¯ä»¥è·åpodæ述符并æä¾å»ºè®®çrequestå¼ã
å¼å¾ä¸æçæ¯ï¼VPAæ¨èè
ä¸ä¼è®¾ç½®èµæºçlimitå¼ãè¿å¯è½å¯¼è´podåæèç¹å
çèµæºãå»ºè®®ä½ å¨namespac级å«è®¾ç½®ä¸ä¸ªâéå¶âå¼ï¼ä»¥é¿å
ç¯çæ¶èå
åæCPU
VPAå·¥ä½æµ
![vpa](../.gitbook/assets/vpa.png)
VPAæ¯éï¼ï¼ï½æ£æ¥ææ çå¼
* å½éå¼è¾¾å°çæ¶åï¼VPAå°è¯ä¿®æ¹åé
çmemoryåCPU
* VPA主è¦æ¯æ´æ°deploymentæè
replication controller specsä¸çresourceså®ä¹
* å½Podéå¯çæ¶åï¼ææ请æ±çèµæºå¾å°è°æ´ 使ç¨VPAçæ¶åéè¦æ³¨æç¹
* å¦æä¸éæ°å¯å¨podï¼åæ æ³è¿è¡èµæºæ´æ¹ãå°ç®å为æ¢ä¸»è¦çæ§ï¼å°±æ¯è¿ç§ååå¯è½ä¼é æå¾å¤ä¸ç¨³å®ãå æ¤ï¼æ³è¦éæ°å¯å¨podå¹¶æ ¹æ®æ°åé
çèµæºè¿è¡è°åº¦ã
* VPAåHPAå°æªç¸äºå
¼å®¹ï¼æ æ³å¨ç¸åçpodä¸è¿è¡ãå¦ææ¨å¨åä¸ç¾¤éä¸ä½¿ç¨å®ä»¬ï¼è¯·ç¡®ä¿å°å®ä»¬çèå´åå¼ã
* VPAä»
æ ¹æ®è§å¯å°çè¿å»åå½åèµæºä½¿ç¨æ
åµè°æ´å®¹å¨çèµæºè¯·æ±ãå®æ²¡æ设置èµæºéå¶ã对äºè¡ä¸ºä¸ç«¯çåºç¨ç¨åºèè¨ï¼è¿å¯è½ä¼åºç°é®é¢ï¼è¿äºåºç¨ç¨åºå¼å§ä½¿ç¨è¶æ¥è¶å¤çèµæºå¯¼è´pod被Kubernetesææ»ã
### é群æ©å®¹\(Cluster Autoscaler\)
Cluster Autoscalerï¼CAï¼æ ¹æ®pendingç¶æçpodæ¥æ©å±æ¨ç群éèç¹ãå®ä¼å®ææ£æ¥æ¯å¦æpendingç¶æçpodï¼å¦æéè¦æ´å¤èµæºå¹¶ä¸æ©å±åç群éä»å¨ç¨æ·æä¾ç约æèå´å
ï¼åä¼å¢å 群éç大å°ãCAä¸äºæä¾åæ¥å£ä»¥è¯·æ±æ´å¤èç¹æéæ¾ç©ºé²èç¹ãå®éç¨äºGCPï¼AWSåAzureãçæ¬1.0ï¼GAï¼ä¸kubernetes 1.8ä¸èµ·åå¸ã
CAå·¥ä½æµ ![ca](../.gitbook/assets/ca.png)
* CAæ¯é10sæ£æ¥ä»¥ä¸pendingç¶æç容å¨
* å¦æåå¨å 为èµæºä¸è¶³å¯¼è´pendingç¶æçpodåå¨çæ¶åï¼å°è¯å建ä¸ä¸ªæå¤ä¸ªnodes
* å½nodeæ¯è¢«cloud provideræ管ççï¼nodeå°ä¼è¢«æ·»å å°é群ä¸ï¼æ为readyçèç¹æ¥å建pod
* Kubernetesè°åº¦å¨åé
pendingç¶æçpodså°æ°çnodeèç¹ä¸ãå¦æä¸äºpodä»ç¶å¤äºpendingç¶æï¼è¿ä¸ªè¿ç¨å°ä¼ç»§ç»ï¼å°ä¼ææ´å¤çnodesæ·»å å°é群ä¸
CA使ç¨çæ¶å注æäºé¡¹
* Cluster Autoscalerç¡®ä¿ç¾¤éä¸çææpodé½æä¸ä¸ªå¯ä»¥è¿è¡çä½ç½®ï¼æ 论æ¯å¦æä»»ä½CPUè´è½½ãæ¤å¤ï¼å®ä¼å°è¯ç¡®ä¿ç¾¤éä¸æ²¡æä¸éè¦çèç¹ãï¼èµæºï¼
* CAå¨å¤§çº¦30ç§å
å®ç°äºå¯æ©å±æ§éæ±ã
* å¨èç¹å为ä¸éè¦ä¹åï¼CAé»è®¤çå¾
10åéï¼ç¶åå缩å°èç¹ã
* CAå
·ææ©å±å¨çæ¦å¿µãæ©å±å¨æä¾äºä¸åççç¥æ¥éæ©è¦æ·»å æ°èç¹çèç¹ç»ã
* è´è´£ä»»å°ä½¿ç¨"cluster-autoscaler.kubernetes.io/safe-to-evict"ï¼"true"ãå¦ææ¨è®¾ç½®äºææèç¹ä¸ç许å¤podæ足å¤çpodï¼åä¼å¤±å»å¾å¤§ç缩å°çµæ´»æ§ã
* 使ç¨PodDisruptionBudgetså¯ä»¥é²æ¢å é¤pod并使åºç¨ç¨åºçä¸é¨åå®å
¨æ æ³è¿è¡ã
Kubernetes autoscalers交äºä¸èµ·æä¹å·¥ä½ å¦ææ¨å¸æèªå¨æ©å±æ¨çKubernetesé群ï¼åéè¦å¨CAä¸ä½¿ç¨podå±èªå¨ç¼©æ¾å¨ãä»ä»¬å½¼æ¤åä½çæ¹å¼ç¸å¯¹ç®åï¼å¦ä¸å¾æ示ã
![ca-hpa-vpa](../.gitbook/assets/ca-hpa-vpa.png)
* HPAæè
VPAæ¥æ´æ°å·²ç»åå¨çpodå¯æ¬æ°æè
使ç¨çresources
* å¦æ没æ足å¤çèç¹å¨å¯ä¼¸ç¼©æ§äºä»¶åè¿è¡podï¼åCAä¼åç°é¨åæå
¨é¨å·²ç¼©æ¾çpodå¤äºæèµ·ç¶æçäºå®ã
* CAæ©å®¹æ°çnodeå°é群ä¸
* Podså°ä¼è¢«è°åº¦å°è¢«æ°ç®¡ççnodeä¸
常è§çé误 æå¨ä¸åç论åä¸çè¿ï¼æ¯å¦Kubernetes slackåStackOverflowé®é¢ï¼ç±äºä¸äºäºå®å¯¼è´ç常è§é®é¢ï¼è®¸å¤DevOpséè¿äºèªå¨ç¼©æ¾å¨ã HPAåVPAä¾èµäºææ åä¸äºåå²æ°æ®ãå¦ææ¨æ²¡æåé
足å¤çèµæºï¼æ¨çpodå°è¢«OOMææ»ï¼å¹¶ä¸æ°¸è¿ä¸ä¼ææºä¼çæææ ãå¨è¿ç§æ
åµä¸ï¼podsä¸çæ©å±å¨å¯è½æ°¸è¿ä¸ä¼åçãæ©å®¹æ¯æ¶é´ææçæä½ãå¨ç¨æ·éå°åºç¨ç¨åºä¸çä»»ä½ä¸ææå´©æºä¹åï¼æ¨å¸ææ¨çpodå群éè½å¤ç¸å½å¿«å°æ©å±ãæ¨åºè¯¥èè容å¨å群éæ©å±çå¹³åæ¶é´ã
æä½³æ¡ä¾åºæ¯ï¼ï¼åé
* 30ç§ - ç®æ ææ å¼æ´æ°ï¼30-60ç§
* 30ç§ - HPAæ£æ¥ææ å¼ï¼30ç§ - >30ç§ - HPAæ£æ¥ææ å¼ï¼30ç§ - >
* <2ç§ - Podså建ä¹åè¿å
¥pendingç¶æ<2ç§ãï¼Podså建ä¹åè¿å
¥pendingç¶æ
* <2ç§ - CAçå°pendingç¶æçpodsï¼ä¹åè°ç¨æ¥å建node 1ç§<2ç§ãï¼CAçå°pendingç¶æçpodsï¼ä¹åè°ç¨æ¥å建node 1ç§
* 3åé - cloud providerå建nodeï¼ä¹åå å
¥k8sä¹åçå¾
nodeåæready,ä¸çº¿æ¯10åé \(åç\)æç³ç³çæ
åµ - 12åé
* 60 ç§ âç®æ ææ å¼æ´æ°
* 30 ç§âââHPAæ£æ¥ææ å¼
* < 2 ç§âââPodså建ä¹åè¿å
¥pendingç¶æ
* < 2 ç§ââCAçå°pendingç¶æçpodsï¼ä¹åè°ç¨æ¥å建node 1ç§
* 10 åé â cloud providerå建ï½ï½ï½ï½
ï¼ä¹åå å
¥ï½8sä¹åçå¾
nodeåæready,ä¸çº¿æ¯10åé
ä¸è¦å°äºæä¾ç¨åºå¯ä¼¸ç¼©æ§æºå¶ä¸CAæ··æ·ãCAå¨é群å
é¨å·¥ä½ï¼èäºæä¾åçå¯æ©å±æ§æºå¶ï¼ä¾å¦AWSå
é¨çASGï¼åºäºèç¹åé
å·¥ä½ãå®ä¸ç¥éæ¨çpodæåºç¨ç¨åºæ£å¨åçä»ä¹ãä¸èµ·ä½¿ç¨å®ä»¬ä¼ä½¿æ¨ç群éä¸ç¨³å®å¹¶ä¸é¾ä»¥é¢æµè¡ä¸ºã
ç¸å
³é¾æ¥
[CA Events](https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/FAQ.md#what-events-are-emitted-by-ca)
[åæ°è§£æ](https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/FAQ.md#what-are-the-parameters-to-ca)
[å¦ä½å¼åCA](https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/FAQ.md#what-events-are-emitted-by-ca)