自动扩缩容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   <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 href="http://nginx.org/" rel="external nofollow"   >nginx.org</a>.<br/>
commercial support is available at
<a href="http://nginx.com/" rel="external nofollow"   >nginx.com</a>.</p>

<p><em>thank you for using nginx.</em></p>
</body>
</html>

配置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

这个功能虽然好用,但在实际生成中一定要结合实际的情况使用!!!

以上就是监控数据组件pod自动化进行扩缩容-hpa的详细内容,更多关于pod自动化扩缩容hpa的资料请关注其它相关文章!