千家信息网

Kubeless如何基于CPU自动伸缩

发表于:2024-10-16 作者:千家信息网编辑
千家信息网最后更新 2024年10月16日,Kubeless如何基于CPU自动伸缩,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。自动伸缩是 Serverless 的最大卖点之一
千家信息网最后更新 2024年10月16日Kubeless如何基于CPU自动伸缩

Kubeless如何基于CPU自动伸缩,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

自动伸缩是 Serverless 的最大卖点之一。

Kubless 的自动伸缩功能基于 Kubernetes 的 HPA(HorizontalPodAutoscaler)功能实现。

目前,kubeless 中的函数支持基于 cpu 和 qps 这两种指标进行自动伸缩。

如何将演示基于 cpu 指标进行自动伸缩。

环境说明

操作系统:macOS

Kubernetes 版本:v1.15.5

Kubeless 版本:v1.0.7

了解如何设置 autoscale

可以先通过 kubeless 命令行了解如何使用 autoscale。

kubeless autoscale 命令帮助文档如下:

$ kubeless help autoscaleautoscale command allows user to list, create, delete autoscale rule for function on KubelessUsage:  kubeless autoscale SUBCOMMAND [flags]  kubeless autoscale [command]Available Commands:  create      automatically scale function based on monitored metrics  delete      delete an autoscale from Kubeless  list        list all autoscales in KubelessFlags:  -h, --help   help for autoscaleUse "kubeless autoscale [command] --help" for more information about a command.

kubeless autoscale create 命令帮助文档如下:

$ kubeless autoscale create --helpautomatically scale function based on monitored metricsUsage:  kubeless autoscale create  FLAG [flags]Flags:  -h, --help               help for create      --max int32          maximum number of replicas (default 1)      --metric string      metric to use for calculating the autoscale. Supported metrics: cpu, qps (default "cpu")      --min int32          minimum number of replicas (default 1)  -n, --namespace string   Specify namespace for the autoscale      --value string       value of the average of the metric across all replicas. If metric is cpu, value is a number represented as percentage. If metric is qps, value must be in format of Quantity

安装 Metrics Server

要使用 HPA,就需要在集群中安装 Metrics Server 服务,否则 HPA 无法获取指标,自然也就无法进行扩容缩容。

可以使用如下命令检查是否安装了 Metrics Server,如果没有安装,那么需要安装它。

$ kubectl api-versions|grep metrics

1、这里要先下载 metrics-server 的 components.yaml:

$ curl -L https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml --output components.yaml

2、然后在 components.yaml 文件的 88行的 args 下面添加参数 --kubelet-insecure-tls,否则 metrics-server 启动报错:

3、最后再使用 kubectl apply 命令安装 Metrics Server:

$ kubectl apply -f components.yamlclusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader createdclusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator createdrolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader createdapiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io createdserviceaccount/metrics-server createddeployment.apps/metrics-server createdservice/metrics-server createdclusterrole.rbac.authorization.k8s.io/system:metrics-server createdclusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created

4、再次确认 metrics-server 是否安装成功:

$ kubectl api-versions|grep metricsmetrics.k8s.io/v1beta1

基于 cpu 进行自动伸缩

依旧使用那个熟悉的 Python 代码:

# test.pydef hello(event, context):  print event  return event['data']

创建 hello 函数,加上 cpu 参数和 memory 参数,以便 HPA 可以根据 cpu 指标进行扩容缩容:

$ kubeless function deploy hello --runtime python2.7 --from-file test.py --handler test.hello --cpu 200m --memory 200MINFO[0000] Deploying function...                        INFO[0000] Function hello submitted for deployment      INFO[0000] Check the deployment status executing 'kubeless function ls hello'

查看函数状态:

$ kubeless function ls helloNAME     NAMESPACE    HANDLER       RUNTIME      DEPENDENCIES    STATUS   hello    default      test.hello    python2.7                    1/1 READY

使用 kubeless 为函数 hello 创建 autoscale:

$ kubeless autoscale create hello --metric=cpu --min=1 --max=20 --value=60INFO[0000] Adding autoscaling rule to the function...   INFO[0000] Autoscaling rule for hello submitted for deployment

使用 kubectl proxy 创建反向代理,以便可以通过 http 访问函数:

$ kubectl proxy -p 8080

接下来对函数进行压力测试,这里使用 ab,它是 apache 自带的压力测试工具,macOS 默认安装了 apache,直接可以使用。

使用 ab 工具进行压力测试:

$ ab -n 3000 -c 8 -t 300 -k -r "http://127.0.0.1:8080/api/v1/namespaces/default/services/hello:http-function-port/proxy/"

使用 kubectl get hpa -w 命令观察 HPA 的状态,可以看到副本数会根据指标的大小进行变化,压力大的时候副本量会随着递增,等到压力小了副本量会递减:

$ kubectl get hpa -wNAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGEhello   Deployment/hello   0%/60%    1         20        1          30mhello   Deployment/hello   95%/60%   1         20        1          32mhello   Deployment/hello   95%/60%   1         20        2          33mhello   Deployment/hello   77%/60%   1         20        2          33mhello   Deployment/hello   77%/60%   1         20        3          34mhello   Deployment/hello   63%/60%   1         20        3          34mhello   Deployment/hello   62%/60%   1         20        3          36mhello   Deployment/hello   71%/60%   1         20        3          37mhello   Deployment/hello   71%/60%   1         20        4          37mhello   Deployment/hello   0%/60%    1         20        4          38mhello   Deployment/hello   0%/60%    1         20        4          42mhello   Deployment/hello   0%/60%    1         20        1          43m

使用 kubectl get pod -w 命令观察也可以看到自动伸缩时 Pod 的数量及状态变化:

$ kubectl get pod -wNAME                     READY   STATUS    RESTARTS   AGEhello-67b44c7585-5t9g4   1/1     Running   0          21hhello-67b44c7585-d9w7j   0/1     Pending   0          0shello-67b44c7585-d9w7j   0/1     Pending   0          0shello-67b44c7585-d9w7j   0/1     Init:0/1   0          0shello-67b44c7585-d9w7j   0/1     PodInitializing   0          2shello-67b44c7585-d9w7j   1/1     Running           0          6shello-67b44c7585-fctgq   0/1     Pending           0          0shello-67b44c7585-fctgq   0/1     Pending           0          0shello-67b44c7585-fctgq   0/1     Init:0/1          0          0shello-67b44c7585-fctgq   0/1     PodInitializing   0          2shello-67b44c7585-fctgq   1/1     Running           0          3shello-67b44c7585-ht784   0/1     Pending           0          0shello-67b44c7585-ht784   0/1     Pending           0          0shello-67b44c7585-ht784   0/1     Init:0/1          0          0shello-67b44c7585-ht784   0/1     PodInitializing   0          2shello-67b44c7585-ht784   1/1     Running           0          3shello-67b44c7585-wfcg9   0/1     Pending           0          0shello-67b44c7585-wfcg9   0/1     Pending           0          0shello-67b44c7585-wfcg9   0/1     Init:0/1          0          0shello-67b44c7585-wfcg9   0/1     PodInitializing   0          2shello-67b44c7585-wfcg9   1/1     Running           0          3shello-67b44c7585-fctgq   1/1     Terminating       0          8m53shello-67b44c7585-ht784   1/1     Terminating       0          7m52shello-67b44c7585-wfcg9   1/1     Terminating       0          5m50shello-67b44c7585-d9w7j   1/1     Terminating       0          9m54shello-67b44c7585-fctgq   0/1     Terminating       0          9m24shello-67b44c7585-ht784   0/1     Terminating       0          8m23shello-67b44c7585-fctgq   0/1     Terminating       0          9m25shello-67b44c7585-fctgq   0/1     Terminating       0          9m25shello-67b44c7585-fctgq   0/1     Terminating       0          9m25shello-67b44c7585-d9w7j   0/1     Terminating       0          10mhello-67b44c7585-d9w7j   0/1     Terminating       0          10mhello-67b44c7585-ht784   0/1     Terminating       0          8m24shello-67b44c7585-wfcg9   0/1     Terminating       0          6m22shello-67b44c7585-d9w7j   0/1     Terminating       0          10mhello-67b44c7585-d9w7j   0/1     Terminating       0          10mhello-67b44c7585-d9w7j   0/1     Terminating       0          10mhello-67b44c7585-wfcg9   0/1     Terminating       0          6m29shello-67b44c7585-wfcg9   0/1     Terminating       0          6m29shello-67b44c7585-ht784   0/1     Terminating       0          8m31shello-67b44c7585-ht784   0/1     Terminating       0          8m31s

关于Kubeless如何基于CPU自动伸缩问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

0