基于阿里云ElasticWorkload的无限动态扩容

背景介绍

首先声明,不是在给阿里云做广告。不过在中国,如果你想上云,还是选择阿里云吧,不要问我为什么,说多了都是泪。

产线运行着一款及时聊天信息APP的后端服务,后端使用Spring Boot框架编写,部署在阿里云的K8S集群中。

APP的活跃用户数每天都有明显的波峰和波谷的情况,所以在波峰的时候需要更多的容器副本来支撑业务,如果通过增加K8S集群节点的方式来扩容容器,在波谷的时候其实是资源浪费的。

通过结合阿里云的ECI+ElasticWorkload可以很好的解决这个问题。如果对文档有任何的问题,可以给我留言,我会回复的。

方案总览

方案架构

方案说明

  1. 通过自己编写的exporter ,从阿里云的日志服务里面拉取活跃设备的值,并暴露成为metrics。https://github.com/aliyun/aliyun-log-python-sdk 这个是阿里云封装python版本的客户端
  2. Prometheus定时抓取exporter里面的活跃用户数,并保存在自己的数据库里面
  3. Prometheus-Adapter从Prometheus取活跃用户数据
  4. Prometheus-Adapter将活跃用户数据推送给metric-server,这样后面的HPA就可以根据metrics-server里面的度量值来进行决策判断
  5. HPA从metrics-server 检查活跃用户数
  6. HPA根据检测到的活跃用户数,根据HPA定义的策略控制后面的ElasticWorkload中定义的Deployment的副本数
  7. ElasticWorkload 监测Deployment的副本数,并根据定义的策略,决定是否需要创建新的副本,以及副本需要创建的目的地
  8. 少于ElasticWorkload 定义的最大副本数时,将副本调度到现有的包年包月的节点上
  9. 大于ElasticWorkload定义的最大副本数时,将副本调度到虚拟机节点中

实施步骤

PS:先下载所有的yaml文件,并修改为自己的镜像地址,服务名称和空间名称就不要修改了,想把一整套跑起来然后再按照自己的业务场景修改

链接:https://pan.baidu.com/s/11E4qF5WPpDtY7qkb_H8A5w 
提取码:ukxv 

  1. 阿里云K8S集群中安装虚拟机节点插件 , https://help.aliyun.com/document_detail/119207.html
  2. 阿里云K8S集群中安装ElasticWorkload插件,https://help.aliyun.com/document_detail/167814.html
  3. 部署自己编写的experter
    kubectl apply -f ali-exporter-deployment.yaml
    kubectl apply -f ali-exporter-svc.yaml
    kubectl apply -f ali-exporter-svc-monitor.yaml

     

  4. 验证Prometheus中已经取到了active_devices这个值
  5. 部署Prometheus-Adapter (如果你的集群里面没有prometheus,请自己先在集群里面部署,网上的资料非常多,我的集群是用prometheus-operator来部署的,空间使用的是monitoring)
    kubectl apply -f prometheus-adapter.yaml

     

  6.  部署业务,我这里使用nginx来模拟我的产线业务
    kubectl apply -f nginx-deployment.yaml

     

  7. 部署业务的service
    kubectl apply -f nginx-svc.yaml

     

  8. 部署业务的ServiceMonitor
    kubectl apply -f nginx-svc-monitor.yaml

     

  9. 部署业务的ElasticWorkload
    kubectl apply -f nginx-ew.yaml
     
  10. 部署业务的HPA
    kubectl apply -f nginx-hpa.yaml
     

方案验证 

  1. 首先查看自定义的用户活跃数是否已经注入到了metric-server中
    #kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq
    
    {
      "kind": "APIResourceList",
      "apiVersion": "v1",
      "groupVersion": "custom.metrics.k8s.io/v1beta1",
      "resources": [
        {
          "name": "namespaces/active_devices",
          "singularName": "",
          "namespaced": false,
          "kind": "MetricValueList",
          "verbs": [
            "get"
          ]
        },
        {
          "name": "services/active_devices",
          "singularName": "",
          "namespaced": true,
          "kind": "MetricValueList",
          "verbs": [
            "get"
          ]
        }
      ]
    }
    

     

  2. 查看HPA是否根据定义的规则控制ElasticWorkload中deployment的副本数, 我这里部署以后就触发了自动扩容。
    $ kubectl get hpa
    NAME    REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    nginx   ElasticWorkload/nginx   2721/3k   1         4         3          31m
    
    $ kubectl describe hpa nginx
    
    Name:                                                nginx
    Namespace:                                           default
    Labels:                                              <none>
    Annotations:                                         kubectl.kubernetes.io/last-applied-configuration:
                                                           {"apiVersion":"autoscaling/v2beta1","kind":"HorizontalPodAutoscaler","metadata":{"annotations":{},"name":"nginx","namespace":"default"},"s...
    CreationTimestamp:                                   Mon, 23 Nov 2020 17:59:35 +0800
    Reference:                                           ElasticWorkload/nginx
    Metrics:                                             ( current / target )
      "active_devices" on Service/nginx (target value):  2737 / 3k
    Min replicas:                                        1
    Max replicas:                                        4
    ElasticWorkload pods:                                3 current / 3 desired
    Conditions:
      Type            Status  Reason              Message
      ----            ------  ------              -------
      AbleToScale     True    ReadyForNewScale    recommended size matches current size
      ScalingActive   True    ValidMetricFound    the HPA was able to successfully calculate a replica count from Service metric active_devices
      ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
    Events:
      Type     Reason                        Age                    From                       Message
      ----     ------                        ----                   ----                       -------
      Normal   SuccessfulRescale             84s                    horizontal-pod-autoscaler  New size: 3; reason: Service metric active_devices above target
    
     
  3. HPA触发了扩容,并调度新的容器到虚拟节点中 ,新的容器为nginx-unit-virtual-kubelet-6f86f85f86-cgft8 ,在阿里云的ECI控制台中也可以看到这个容器。
    
    # kubectl get po
    
    NAME                                              READY   STATUS              RESTARTS   AGE
    ali-exporter-845647f6b9-tb5m8                     1/1     Running             0          70m
    nginx-85f6b67ff7-kfshb                            1/1     Running             0          10m
    nginx-85f6b67ff7-rczqf                            1/1     Running             0          8m45s
    nginx-unit-virtual-kubelet-6f86f85f86-cgft8       0/1     ContainerCreating   0          27s
    

  4. 如果活跃用户数下降了以后,就会自动缩容,而且是优先缩虚拟节点上面的副本。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值