共計 6071 個字符,預計需要花費 16 分鐘才能閱讀完成。
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 autoscale
autoscale command allows user to list, create, delete autoscale rule for function on Kubeless
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 Kubeless
Use kubeless autoscale [command] --help for more information about a command.
kubeless autoscale create 命令幫助文檔如下:
$ kubeless autoscale create --help
automatically scale function based on monitored metrics
-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.yaml
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
serviceaccount/metrics-server created
deployment.apps/metrics-server created
service/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
4、再次確認 metrics-server 是否安裝成功:
$ kubectl api-versions|grep metrics
metrics.k8s.io/v1beta1
基于 cpu 進行自動伸縮
依舊使用那個熟悉的 Python 代碼:
# test.py
def 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 200M
INFO[0000] Deploying function...
INFO[0000] Function hello submitted for deployment
INFO[0000] Check the deployment status executing kubeless function ls hello
查看函數狀態:
$ kubeless function ls hello
NAME 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=60
INFO[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 -w
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hello Deployment/hello 0%/60% 1 20 1 30m
hello Deployment/hello 95%/60% 1 20 1 32m
hello Deployment/hello 95%/60% 1 20 2 33m
hello Deployment/hello 77%/60% 1 20 2 33m
hello Deployment/hello 77%/60% 1 20 3 34m
hello Deployment/hello 63%/60% 1 20 3 34m
hello Deployment/hello 62%/60% 1 20 3 36m
hello Deployment/hello 71%/60% 1 20 3 37m
hello Deployment/hello 71%/60% 1 20 4 37m
hello Deployment/hello 0%/60% 1 20 4 38m
hello Deployment/hello 0%/60% 1 20 4 42m
hello Deployment/hello 0%/60% 1 20 1 43m
使用 kubectl get pod -w 命令觀察也可以看到自動伸縮時 Pod 的數量及狀態變化:
$ kubectl get pod -w
NAME READY STATUS RESTARTS AGE
hello-67b44c7585-5t9g4 1/1 Running 0 21h
hello-67b44c7585-d9w7j 0/1 Pending 0 0s
hello-67b44c7585-d9w7j 0/1 Pending 0 0s
hello-67b44c7585-d9w7j 0/1 Init:0/1 0 0s
hello-67b44c7585-d9w7j 0/1 PodInitializing 0 2s
hello-67b44c7585-d9w7j 1/1 Running 0 6s
hello-67b44c7585-fctgq 0/1 Pending 0 0s
hello-67b44c7585-fctgq 0/1 Pending 0 0s
hello-67b44c7585-fctgq 0/1 Init:0/1 0 0s
hello-67b44c7585-fctgq 0/1 PodInitializing 0 2s
hello-67b44c7585-fctgq 1/1 Running 0 3s
hello-67b44c7585-ht784 0/1 Pending 0 0s
hello-67b44c7585-ht784 0/1 Pending 0 0s
hello-67b44c7585-ht784 0/1 Init:0/1 0 0s
hello-67b44c7585-ht784 0/1 PodInitializing 0 2s
hello-67b44c7585-ht784 1/1 Running 0 3s
hello-67b44c7585-wfcg9 0/1 Pending 0 0s
hello-67b44c7585-wfcg9 0/1 Pending 0 0s
hello-67b44c7585-wfcg9 0/1 Init:0/1 0 0s
hello-67b44c7585-wfcg9 0/1 PodInitializing 0 2s
hello-67b44c7585-wfcg9 1/1 Running 0 3s
hello-67b44c7585-fctgq 1/1 Terminating 0 8m53s
hello-67b44c7585-ht784 1/1 Terminating 0 7m52s
hello-67b44c7585-wfcg9 1/1 Terminating 0 5m50s
hello-67b44c7585-d9w7j 1/1 Terminating 0 9m54s
hello-67b44c7585-fctgq 0/1 Terminating 0 9m24s
hello-67b44c7585-ht784 0/1 Terminating 0 8m23s
hello-67b44c7585-fctgq 0/1 Terminating 0 9m25s
hello-67b44c7585-fctgq 0/1 Terminating 0 9m25s
hello-67b44c7585-fctgq 0/1 Terminating 0 9m25s
hello-67b44c7585-d9w7j 0/1 Terminating 0 10m
hello-67b44c7585-d9w7j 0/1 Terminating 0 10m
hello-67b44c7585-ht784 0/1 Terminating 0 8m24s
hello-67b44c7585-wfcg9 0/1 Terminating 0 6m22s
hello-67b44c7585-d9w7j 0/1 Terminating 0 10m
hello-67b44c7585-d9w7j 0/1 Terminating 0 10m
hello-67b44c7585-d9w7j 0/1 Terminating 0 10m
hello-67b44c7585-wfcg9 0/1 Terminating 0 6m29s
hello-67b44c7585-wfcg9 0/1 Terminating 0 6m29s
hello-67b44c7585-ht784 0/1 Terminating 0 8m31s
hello-67b44c7585-ht784 0/1 Terminating 0 8m31s
關于 Kubeless 如何基于 CPU 自動伸縮問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注丸趣 TV 行業資訊頻道了解更多相關知識。