日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

k8s監控數據組件Pod自動化進行擴縮容HPA_服務器其它

作者:、重明 ? 更新時間: 2022-05-21 編程語言

自動擴縮容HPA:全稱是Horizontal Pod Autoscaler

我們安裝k8s集群的時候,安裝過一個metrics-server的組件,這是一個監控數據組件,提供HPA和基礎資源監控的能力。就是這面這個Pod:

[root@k8s-master01 ~]# kubectl get pod -n kube-system 
metrics-server-6bf7dcd649-5fhrw            1/1     Running   2 (3d5h ago)   8d

通過這個組件可以看到節點或者Pod的內存和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        

除了可以進行簡單的監控功能,還可以利用這個監控的數據做一些其他的操作。

比如我們可以給Pod的資源設定某個值,當資源的使用超過這個值,那么系統就會認為這個Pod當前存在壓力,從而就行擴容。

一般使用CPU和自定義指標進行擴容,內存相對較少。

HPA實踐:

注意事項:要想實現HPA的自動擴容,需要滿足以下幾個條件

  • 必須安裝metrics-server組件或其他自定義版本的metrics-server
  • 必須配置requests參數
  • 不能擴容無法縮放的對象,如DaemonSet

首先創建一個nginx的yaml文件:

kubectl create deployment hpa-nginx --image=nginx --dry-run=client -o yaml > hpa-nginx.yaml

然后進入yaml文件中進行配置:在配置鏡像那里進行配置,下列代碼的后三行,如果也想對基于內存擴容的話也可以將內存寫上。

resources:是資源的意思

requests:是請求的意思,這里應該是請求資源的意思

    spec:
      containers:
      - image: nginx
        name: nginx
        resources:
          requests:
            cpu: 10m

執行yaml文件創建副本:

[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           80/TCP    3m17s
kubernetes   ClusterIP   10.96.0.1               443/TCP   8d

訪問測試一下:證明這個Pod可以用了

[root@k8s-master01 ~]# curl 10.98.236.134



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

配置Hpa自動擴縮容的規則:這條命令是說當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的規則情況:

[root@k8s-master01 ~]# kubectl get hpa
NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-nginx   Deployment/hpa-nginx   0%/10%    1         10        1          2m38s

下面進行一個循環訪問hpa-nginx:觀察hpa的cpu值會不會上升

[root@k8s-master01 ~]# while true; do wget -q -O- http://10.98.236.134 >/dev/null; done

觀察是否已經進行擴容:可以看到hpa-nginx的副本數已經進行了自動擴容

[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   

那么,接下來將訪問測試停下,看副本是否會自動縮容到最初;等待一會發現副本回到了最原始的一個。注意這個時間可能會有點慢,稍微等一會,不是報錯了。

[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

這個功能雖然好用,但在實際生成中一定要結合實際的情況使用!!!

原文鏈接:https://yyang.blog.csdn.net/article/details/122861622

欄目分類
最近更新