其他分享
首页 > 其他分享> > |NO.Z.00168|——————————|CloudNative|——|KuberNetes&服务发布.V19|------------------------------------------

|NO.Z.00168|——————————|CloudNative|——|KuberNetes&服务发布.V19|------------------------------------------

作者:互联网



[CloudNative:KuberNetes&服务发布.V19]                                                               [Applications.KuberNetes][|DevOps|k8s|服务发布|什么是HPA|自动扩缩容HPA实践|]








一、自动扩缩容HPA实践
### --- 创建deployment,添加内存参数
~~~     首先需要创建一个deployment

[root@k8s-master01 ~]# kubectl get deploy
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
demo-nginx   2/2     2            2           85s
### --- 添加触发条件,触发扩容
~~~     添加它的内存参数

[root@k8s-master01 ~]# kubectl set resources deploy demo-nginx -c demo-nginx --limits=cpu=200m,memory=128Mi --requests=cpu=10m,memory=16Mi
deployment.apps/nginx resource requirements updated
[root@k8s-master01 ~]# kubectl get deploy demo-nginx -oyaml
~~~     # 更改CPU参数,把它的Requests更改的小一些,

[root@k8s-master01 ~]# kubectl edit deploy demo-nginx 
        resources:
          limits:
            cpu: 1000m
            memory: 170Mi
          requests:
            cpu: 10m
            memory: 70Mi
[root@k8s-master01 ~]#  kubectl get deploy
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
demo-nginx   2/2     2            2           4m14s
~~~     # 到100%的时候给他扩容,现在它的状态是0m

[root@k8s-master01 ~]# kubectl top po                               
NAME                        CPU(cores)   MEMORY(bytes)   
busybox                     0m           0Mi             
demo-nginx-59569f79-2bmnd   2m           2Mi             
demo-nginx-59569f79-44z9c   1m           2Mi   
二、为pod添加触发条件
### --- 添加触发条件
~~~     --cpu-percent=20:cpu达到CPU的20就扩容 --min=2:最小1个 --max=5:最大5个

[root@k8s-master01 ~]# kubectl autoscale deploy demo-nginx --cpu-percent=20 --min=2 --max=5         
horizontalpodautoscaler.autoscaling/nginx autoscaled   
### --- 查看它的监控状态,

[root@k8s-master01 ~]# kubectl get hpa
NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
demo-nginx   Deployment/demo-nginx   0%/20%    2         5         2          82s
### --- 当前CPU的占比是0%,当达到20%的时候触发;20%是扩容的指标
~~~     测试,不下载页面,只是做请求

[root@k8s-master01 ~]# kubectl describe hpa
Name:                                                  demo-nginx
Namespace:                                             default
Labels:                                                <none>
Annotations:                                           <none>
CreationTimestamp:                                     Fri, 23 Apr 2021 15:20:33 +0800
Reference:                                             Deployment/demo-nginx
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  0% (0) / 20%
Min replicas:                                          2
Max replicas:                                          5
Deployment pods:                                       2 current / 2 desired
### --- 找到这个service的地址

[root@k8s-master01 ~]# kubectl get svc
NAME                 TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)                      AGE
my-service           ClusterIP      10.97.126.187   <none>          80/TCP,443/TCP               99s
nginx-svc            NodePort       10.101.145.83   <none>          80:31000/TCP,443:32765/TCP   2d20h
三、触发扩容;扩容至5个节点
### --- 触发扩容,当它的CPU达到100%的时候,就会触发扩容,扩容最大的pod数量为5个
~~~     直接请求去下载它的主页(在不同的节点都启动,增加它的触发效果)   
~~~     请求它的Nginx,但是不下载

[root@k8s-master01 ~]# while true; do wget -q -o- http://10.97.126.187 > /dev/null ;done
### --- 查看pod的访问日志,看它是否被请求

[root@k8s-master01 ~]# kubectl get po -owide
NAME                        READY   STATUS    RESTARTS   AGE     IP               NODE           NOMINATED NODE   READINESS GATES
busybox                     1/1     Running   52         3d18h   172.25.244.211   k8s-master01   <none>           <none>
demo-nginx-59569f79-2bmnd   1/1     Running   0          123m    172.25.244.218   k8s-master01   <none>           <none>
demo-nginx-59569f79-7wzrp   1/1     Running   0          31s     172.18.195.19    k8s-master03   <none>           <none>
[root@k8s-master01 ~]# kubectl logs -f demo-nginx-59569f79-2bmnd 
### --- 查看po的CPU参数的变化

[root@k8s-master01 ~]# kubectl top po
NAME                        CPU(cores)   MEMORY(bytes)   
busybox                     0m           0Mi             
demo-nginx-59569f79-2bmnd   9m           3Mi             
demo-nginx-59569f79-7wzrp   25m          4Mi 
### --- 查看它的hpa状态,是否达到100%,若是达到100%就会自动扩容
~~~     CPU的占比已经达到170%了,开始扩容副本,最大扩容数量为5个

[root@k8s-master01 ~]# kubectl get hpa                                          
NAME         REFERENCE               TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
demo-nginx   Deployment/demo-nginx   170%/20%   2         5         5          116m
### --- 最多扩容pod节点数为5个,我们当时定义的
~~~     这时候它会把流量分摊到5个节点上面

[root@k8s-master01 ~]# kubectl get po
[root@k8s-master01 ~]# kubectl get po -owide
NAME                        READY   STATUS    RESTARTS   AGE     IP               NODE           NOMINATED NODE   READINESS GATES
busybox                     1/1     Running   53         3d18h   172.25.244.211   k8s-master01   <none>           <none>
demo-nginx-59569f79-2bmnd   1/1     Running   0          136m    172.25.244.218   k8s-master01   <none>           <none>
demo-nginx-59569f79-4b2nc   1/1     Running   0          11m     172.25.92.77     k8s-master02   <none>           <none>
demo-nginx-59569f79-7wzrp   1/1     Running   0          13m     172.18.195.19    k8s-master03   <none>           <none>
demo-nginx-59569f79-vtct4   1/1     Running   0          3m32s   172.17.125.17    k8s-node01     <none>           <none>
demo-nginx-59569f79-vwf8l   1/1     Running   0          3m32s   172.27.14.212    k8s-node02     <none>           <none>
### --- 新扩的节点,建议是分摊到不同的宿主机上。

[root@k8s-master01 ~]# kubectl top po
四、关闭触发条件,恢复节点
### --- 关闭触发条件,等到他的CPU占比恢复到20%的时候,pod节点的数量最少恢复到2个 

[root@k8s-master01 ~]# kubectl get po -owide
NAME                        READY   STATUS    RESTARTS   AGE     IP               NODE           NOMINATED NODE   READINESS GATES
busybox                     1/1     Running   53         3d18h   172.25.244.211   k8s-master01   <none>           <none>
demo-nginx-59569f79-2bmnd   1/1     Running   0          138m    172.25.244.218   k8s-master01   <none>           <none>
demo-nginx-59569f79-4b2nc   1/1     Running   0          13m     172.25.92.77     k8s-master02   <none>           <none>
demo-nginx-59569f79-7wzrp   1/1     Running   0          15m     172.18.195.19    k8s-master03   <none>           <none>
demo-nginx-59569f79-vtct4   1/1     Running   0          5m33s   172.17.125.17    k8s-node01     <none>           <none>
demo-nginx-59569f79-vwf8l   1/1     Running   0          5m33s   172.27.14.212    k8s-node02     <none>           <none>
### --- 退出访问
~~~     访问量下降后,pod节点数量变为最少个数

[root@k8s-master01 ~]# while true; do wget -q -o- http://10.97.126.187 > /dev/null ;done
[1]+  Stopped                 wget -q -o- http://10.97.126.187 > /dev/null
[root@k8s-master01 ~]# kubectl get po                                           
NAME                        READY   STATUS        RESTARTS   AGE
busybox                     1/1     Running       53         3d18h
demo-nginx-59569f79-2bmnd   1/1     Running       0          139m
demo-nginx-59569f79-4b2nc   0/1     Terminating   0          14m
demo-nginx-59569f79-7wzrp   1/1     Running       0          15m
demo-nginx-59569f79-vtct4   0/1     Terminating   0          6m4s
demo-nginx-59569f79-vwf8l   0/1     Terminating   0          6m4s 
### --- 查看pod缩容节点数量

[root@k8s-master01 ~]# kubectl get po
NAME                        READY   STATUS    RESTARTS   AGE
busybox                     1/1     Running   53         3d18h
demo-nginx-59569f79-2bmnd   1/1     Running   0          140m
demo-nginx-59569f79-7wzrp   1/1     Running   0          16m 








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                   ——W.S.Landor



来自为知笔记(Wiz)

标签:缩容,demo,master01,59569f79,------------------------------------------------------
来源: https://www.cnblogs.com/yanqivip/p/16076561.html