k8s監(jiān)控數(shù)據(jù)組件Pod自動化進行擴縮容HPA
自動擴縮容HPA:全稱是Horizontal Pod Autoscaler
我們安裝k8s集群的時候,安裝過一個metrics-server的組件,這是一個監(jiān)控數(shù)據(jù)組件,提供HPA和基礎資源監(jiān)控的能力。就是這面這個Pod:
[root@k8s-master01 ~]# kubectl get pod -n kube-system metrics-server-6bf7dcd649-5fhrw 1/1 Running 2 (3d5h ago) 8d
通過這個組件可以看到節(jié)點或者Pod的內(nèi)存和CPU的使用率:
[root@k8s-master01 ~]# kubectl top pod -A NAMESPACE NAME CPU(cores) MEMORY(bytes) default busybox 0m 0Mi kube-system calico-kube-controllers-5dffd5886b-4blh6 3m 18Mi kube-system calico-node-fvbdq 42m 135Mi kube-system calico-node-g8nqd 52m 73Mi
除了可以進行簡單的監(jiān)控功能,還可以利用這個監(jiān)控的數(shù)據(jù)做一些其他的操作。
比如我們可以給Pod的資源設定某個值,當資源的使用超過這個值,那么系統(tǒng)就會認為這個Pod當前存在壓力,從而就行擴容。
一般使用CPU和自定義指標進行擴容,內(nèi)存相對較少。
HPA實踐:
注意事項:要想實現(xiàn)HPA的自動擴容,需要滿足以下幾個條件
- 必須安裝metrics-server組件或其他自定義版本的metrics-server
- 必須配置requests參數(shù)
- 不能擴容無法縮放的對象,如DaemonSet
首先創(chuàng)建一個nginx的yaml文件:
kubectl create deployment hpa-nginx --image=nginx --dry-run=client -o yaml > hpa-nginx.yaml
然后進入yaml文件中進行配置:在配置鏡像那里進行配置,下列代碼的后三行,如果也想對基于內(nèi)存擴容的話也可以將內(nèi)存寫上。
resources:是資源的意思
requests:是請求的意思,這里應該是請求資源的意思
spec:
containers:
- image: nginx
name: nginx
resources:
requests:
cpu: 10m
執(zhí)行yaml文件創(chuàng)建副本:
[root@k8s-master01 ~]# kubectl create -f hpa-nginx.yaml deployment.apps/hpa-nginx created
暴露出一個service端口:
[root@k8s-master01 ~]# kubectl expose deployment hpa-nginx --port=80 [root@k8s-master01 ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hpa-nginx ClusterIP 10.98.236.134 <none> 80/TCP 3m17s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8d
訪問測試一下:證明這個Pod可以用了
[root@k8s-master01 ~]# curl 10.98.236.134
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a rel="external nofollow" >nginx.org</a>.<br/>
Commercial support is available at
<a rel="external nofollow" >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
配置Hpa自動擴縮容的規(guī)則:這條命令是說當hpa-nginx這個Pod的cpu值達到10的時候,將進行自動擴容,最小擴容1個,最大擴容10個。
[root@k8s-master01 ~]# kubectl autoscale deployment hpa-nginx --cpu-percent=10 --min=1 --max=10 horizontalpodautoscaler.autoscaling/hpa-nginx autoscaled
看一下hpa的規(guī)則情況:
[root@k8s-master01 ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE hpa-nginx Deployment/hpa-nginx 0%/10% 1 10 1 2m38s
下面進行一個循環(huán)訪問hpa-nginx:觀察hpa的cpu值會不會上升
[root@k8s-master01 ~]# while true; do wget -q -O- http://10.98.236.134 >/dev/null; done
觀察是否已經(jīng)進行擴容:可以看到hpa-nginx的副本數(shù)已經(jīng)進行了自動擴容
[root@k8s-master01 ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE hpa-nginx Deployment/hpa-nginx 640%/10% 1 10 1 7m14s [root@k8s-master01 ~]# kubectl top pod NAME CPU(cores) MEMORY(bytes) busybox 0m 0Mi hpa-nginx-bd88bdd8f-7gdwq 1m 3Mi hpa-nginx-bd88bdd8f-8c6j6 1m 3Mi hpa-nginx-bd88bdd8f-cfcjs 1m 7Mi hpa-nginx-bd88bdd8f-h8vx7 74m 7Mi hpa-nginx-bd88bdd8f-kpgl8 2m 3Mi hpa-nginx-bd88bdd8f-lpf45 1m 3Mi hpa-nginx-bd88bdd8f-lwc2h 1m 3Mi hpa-nginx-bd88bdd8f-qkgfd 1m 3Mi hpa-nginx-bd88bdd8f-t9fj9 1m 3Mi hpa-nginx-bd88bdd8f-tbrl4 1m 7Mi
那么,接下來將訪問測試停下,看副本是否會自動縮容到最初;等待一會發(fā)現(xiàn)副本回到了最原始的一個。注意這個時間可能會有點慢,稍微等一會,不是報錯了。
[root@k8s-master01 ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE hpa-nginx Deployment/hpa-nginx 2%/10% 1 10 10 11m [root@k8s-master01 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE busybox 1/1 Running 26 (46m ago) 8d hpa-nginx-bd88bdd8f-h8vx7 1/1 Running 0 27m
這個功能雖然好用,但在實際生成中一定要結合實際的情況使用?。。?/p>
以上就是監(jiān)控數(shù)據(jù)組件Pod自動化進行擴縮容-HPA的詳細內(nèi)容,更多關于Pod自動化擴縮容HPA的資料請關注腳本之家其它相關文章!
相關文章
Apache,IIS下Discuz x1.5偽靜態(tài)設置方法
有時候我們在假設論壇的時候,為了優(yōu)化搜索引擎收錄效果,需要設為偽靜態(tài),除了后臺的設置,也需要服務器支持,下面的具體的實現(xiàn)方法,2011-05-05
MongoDB學習筆記(四) 用MongoDB的文檔結構描述數(shù)據(jù)關系
MongoDB的集合(collection)可以看做關系型數(shù)據(jù)庫的表,文檔對象(document)可以看做關系型數(shù)據(jù)庫的一條記錄。但兩者并不完全對等2013-07-07

