8.HPA自动扩缩容
生活随笔
收集整理的這篇文章主要介紹了
8.HPA自动扩缩容
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 什么是HPA?
-
HPA(Horizontal Pod Autoscaler,水平Pod自動伸縮器)可根據(jù)觀察到的CPU、內(nèi)存使用率或自定義度量標(biāo)準(zhǔn)來自動擴(kuò)展或縮容Pod的數(shù)量。HPA不適用于無法縮放的對象,比如DaemonSet。
-
HPA控制器會定期調(diào)整RC或Deployment的副本數(shù),以使觀察到的平均CPU利用率與用戶指定的目標(biāo)相匹配。
-
HPA需要metrics-server獲取度量指標(biāo),
2 HPA實(shí)踐
在生產(chǎn)環(huán)境中,總會有一些意想不到的事情發(fā)生,比如公司網(wǎng)站流量突然升高, 此時(shí)之前創(chuàng)建的Pod已不足以撐住所有的訪問,而運(yùn)維人員也不可能24小時(shí)守著業(yè)務(wù)服務(wù), 這時(shí)就可以通過配置HPA,實(shí)現(xiàn)負(fù)載過高的情況下自動擴(kuò)容Pod副本數(shù)以分?jǐn)偢卟l(fā)的流量, 當(dāng)流量恢復(fù)正常后,HPA會自動縮減Pod的數(shù)量。1 啟動一個(gè)Nginx服務(wù)
# 生成一個(gè)創(chuàng)建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: {}# 創(chuàng)建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# 創(chuàng)建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 創(chuàng)建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數(shù) # --max=10 最大pod數(shù)# 查看HPA狀態(tài) [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 持續(xù)請求nginx-hpa 使CPU占用超過閾值
while true; do wget -q -O- http://10.244.53.55 > /dev/null; done4 超過閾值自動擴(kuò)容
[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進(jìn)行自動縮容
總結(jié)
以上是生活随笔為你收集整理的8.HPA自动扩缩容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SL651-2014 水文监测 RTU
- 下一篇: 传感器自学笔记第五章——旋转编码器