8.HPA自动扩缩容
生活随笔
收集整理的這篇文章主要介紹了
8.HPA自动扩缩容
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 什么是HPA?
-
HPA(Horizontal Pod Autoscaler,水平Pod自動伸縮器)可根據觀察到的CPU、內存使用率或自定義度量標準來自動擴展或縮容Pod的數量。HPA不適用于無法縮放的對象,比如DaemonSet。
-
HPA控制器會定期調整RC或Deployment的副本數,以使觀察到的平均CPU利用率與用戶指定的目標相匹配。
-
HPA需要metrics-server獲取度量指標,
2 HPA實踐
在生產環境中,總會有一些意想不到的事情發生,比如公司網站流量突然升高, 此時之前創建的Pod已不足以撐住所有的訪問,而運維人員也不可能24小時守著業務服務, 這時就可以通過配置HPA,實現負載過高的情況下自動擴容Pod副本數以分攤高并發的流量, 當流量恢復正常后,HPA會自動縮減Pod的數量。1 啟動一個Nginx服務
# 生成一個創建deploy的yaml文件 kubectl create deployment nginx-hpa \--image=nginx:1.15.4 \--port=80 \--dry-run=client \-o yaml > nginx-hpa.yaml# 修改yaml文件 [root@k8s-master-01 k8s_test]# kubectl delete deployments.apps nginx deployment.apps "nginx" deleted [root@k8s-master-01 k8s_test]# kubectl create -f nginx-hpa.yaml deployment.apps/nginx-hpa created [root@k8s-master-01 k8s_test]# cat nginx-hpa.yaml apiVersion: apps/v1 kind: Deployment metadata:creationTimestamp: nulllabels:app: nginx-hpaname: nginx-hpa spec:replicas: 1selector:matchLabels:app: nginx-hpastrategy: {}template:metadata:creationTimestamp: nulllabels:app: nginx-hpaspec:containers:- image: nginx:1.15.4name: nginxports:- containerPort: 80resources: requests: # 新增cpu配置, cpu: 10m # CPU大小, 1核cpu等于1000m status: {}# 創建deploy [root@k8s-master-01 k8s_test]# kubectl create -f nginx-hpa.yaml deployment.apps/nginx-hpa created [root@k8s-master-01 k8s_test]# kubectl get po -l app=nginx-hpa NAME READY STATUS RESTARTS AGE nginx-hpa-7cd85dbf7-7xc7j 1/1 Running 0 2m33s# 創建service暴露端口 [root@k8s-master-01 k8s_test]# kubectl expose deployment nginx-hpa --port=80 service/nginx-hpa exposed [root@k8s-master-01 k8s_test]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.244.0.1 <none> 443/TCP 7d2h nginx-hpa ClusterIP 10.244.53.55 <none> 80/TCP 44s2 創建HPA
[root@k8s-master-01 k8s_test]# kubectl autoscale deployment nginx-hpa --cpu-percent=10 --min=1 --max=10 horizontalpodautoscaler.autoscaling/nginx-hpa autoscaled# --cpu-percent=10 CPU占用超過deploy的requests.cpu配置的百分比,10就是10% # --min=1 最小pod數 # --max=10 最大pod數# 查看HPA狀態 [root@k8s-master-01 ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-hpa Deployment/nginx-hpa 0%/10% 1 10 1 9m40s3 持續請求nginx-hpa 使CPU占用超過閾值
while true; do wget -q -O- http://10.244.53.55 > /dev/null; done4 超過閾值自動擴容
[root@k8s-master-01 ~]# kubectl get hpa -w NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-hpa Deployment/nginx-hpa 0%/10% 1 10 1 21m nginx-hpa Deployment/nginx-hpa 430%/10% 1 10 1 21m[root@k8s-master-01 ~]# kubectl top po NAME CPU(cores) MEMORY(bytes) busybox 0m 1Mi nginx-hpa-7cd85dbf7-8rhds 65m 3Mi [root@k8s-master-01 ~]# kubectl get po -w NAME READY STATUS RESTARTS AGE busybox 1/1 Running 13 (5m33s ago) 25h nginx-hpa-7cd85dbf7-68j75 1/1 Running 0 18s nginx-hpa-7cd85dbf7-6jj4f 1/1 Running 0 3s nginx-hpa-7cd85dbf7-8rhds 1/1 Running 0 19m nginx-hpa-7cd85dbf7-fmgpw 1/1 Running 0 3s nginx-hpa-7cd85dbf7-hfwvt 1/1 Running 0 18s nginx-hpa-7cd85dbf7-skh6d 1/1 Running 0 18s nginx-hpa-7cd85dbf7-vr4dj 1/1 Running 0 3s nginx-hpa-7cd85dbf7-xlxrv 1/1 Running 0 3s nginx-hpa-7cd85dbf7-24t85 0/1 Pending 0 0s nginx-hpa-7cd85dbf7-lxrxb 0/1 Pending 0 0s nginx-hpa-7cd85dbf7-24t85 0/1 Pending 0 0s nginx-hpa-7cd85dbf7-lxrxb 0/1 Pending 0 0s nginx-hpa-7cd85dbf7-24t85 0/1 ContainerCreating 0 0s nginx-hpa-7cd85dbf7-lxrxb 0/1 ContainerCreating 0 0s nginx-hpa-7cd85dbf7-24t85 0/1 ContainerCreating 0 0s nginx-hpa-7cd85dbf7-lxrxb 0/1 ContainerCreating 0 0s nginx-hpa-7cd85dbf7-lxrxb 1/1 Running 0 1s nginx-hpa-7cd85dbf7-24t85 1/1 Running 0 1s- CPU占用降下來5分鐘左右開始刪除多余的pod進行自動縮容
總結
以上是生活随笔為你收集整理的8.HPA自动扩缩容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SL651-2014 水文监测 RTU
- 下一篇: 传感器自学笔记第五章——旋转编码器