Categories
程式開發

Keubeless 如何基於CPU 自動伸縮? | 玩轉Kubeless


自動伸縮是Serverless 的最大賣點之一。

Kubless 的自動伸縮功能基於Kubernetes 的HPA(Horizo​​ntalPodAutoscaler)功能實現。

目前,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

Usage:
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

Flags:
-h, --help help for autoscale

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

Usage:
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

要使用HP​​A,就需要在集群中安裝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 啟動報錯:

Keubeless 如何基於CPU 自動伸縮?  | 玩轉Kubeless 1

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

參考

https://kubeless.io/docs/autoscaling/

https://github.com/mvranic/kubeless-apl-demo

https://github.com/kubernetes-sigs/metrics-server

https://stackoverflow.com/questions/54106725/docker-kubernetes-mac-autoscaler-unable-to-find-metrics

Keubeless 如何基於CPU 自動伸縮?  | 玩轉Kubeless 2