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

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自動伸縮是 Serverless 的最大賣點之一。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kubless 的自動伸縮功能基於 Kubernetes 的 HPA(HorizontalPodAutoscaler)功能實現。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前,kubeless 中的函數支持基於 cpu 和 qps 這兩種指標進行自動伸縮。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文將演示基於 cpu 指標進行自動伸縮。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"環境說明"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"操作系統:macOS"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kubernetes 版本:v1.15.5"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kubeless 版本:v1.0.7"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"瞭解如何設置 autoscale"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以先通過 kubeless 命令行了解如何使用 autoscale。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"kubeless autoscale 命令幫助文檔如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubeless help autoscale\nautoscale command allows user to list, create, delete autoscale rule for function on Kubeless\n\n\nUsage:\n kubeless autoscale SUBCOMMAND [flags]\n kubeless autoscale [command]\n\n\nAvailable Commands:\n create automatically scale function based on monitored metrics\n delete delete an autoscale from Kubeless\n list list all autoscales in Kubeless\n\n\nFlags:\n -h, --help help for autoscale\n\n\nUse \"kubeless autoscale [command] --help\" for more information about a command.\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"kubeless autoscale create 命令幫助文檔如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubeless autoscale create --help\nautomatically scale function based on monitored metrics\n\n\nUsage:\n kubeless autoscale create FLAG [flags]\n\n\nFlags:\n -h, --help help for create\n --max int32 maximum number of replicas (default 1)\n --metric string metric to use for calculating the autoscale. Supported metrics: cpu, qps (default \"cpu\")\n --min int32 minimum number of replicas (default 1)\n -n, --namespace string Specify namespace for the autoscale\n --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\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"安裝 Metrics Server"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要使用 HPA,就需要在集羣中安裝 Metrics Server 服務,否則 HPA 無法獲取指標,自然也就無法進行擴容縮容。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以使用如下命令檢查是否安裝了 Metrics Server,如果沒有安裝,那麼需要安裝它。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubectl api-versions|grep metrics\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1、這裏要先下載 metrics-server 的 components.yaml:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ curl -L https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml --output components.yaml\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2、然後在 components.yaml 文件的 88行的 args 下面添加參數 --kubelet-insecure-tls,否則 metrics-server 啓動報錯:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b5/b53393cea20bdbeb1b52135860a20f6e.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3、最後再使用 kubectl apply 命令安裝 Metrics Server:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubectl apply -f components.yaml\nclusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created\nclusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created\nrolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created\napiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created\nserviceaccount/metrics-server created\ndeployment.apps/metrics-server created\nservice/metrics-server created\nclusterrole.rbac.authorization.k8s.io/system:metrics-server created\nclusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"4、再次確認 metrics-server 是否安裝成功:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubectl api-versions|grep metrics\nmetrics.k8s.io/v1beta1\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"基於 cpu 進行自動伸縮"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"依舊使用那個熟悉的 Python 代碼:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# test.py\ndef hello(event, context):\n print event\n return event['data']\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建 hello 函數,加上 cpu 參數和 memory 參數,以便 HPA 可以根據 cpu 指標進行擴容縮容:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubeless function deploy hello --runtime python2.7 --from-file test.py --handler test.hello --cpu 200m --memory 200M\nINFO[0000] Deploying function... \nINFO[0000] Function hello submitted for deployment \nINFO[0000] Check the deployment status executing 'kubeless function ls hello'\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查看函數狀態:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubeless function ls hello\nNAME NAMESPACE HANDLER RUNTIME DEPENDENCIES STATUS \nhello default test.hello python2.7 1/1 READY\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 kubeless 爲函數 hello 創建 autoscale:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubeless autoscale create hello --metric=cpu --min=1 --max=20 --value=60\nINFO[0000] Adding autoscaling rule to the function... \nINFO[0000] Autoscaling rule for hello submitted for deployment\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 kubectl proxy 創建反向代理,以便可以通過 http 訪問函數:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubectl proxy -p 8080\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來對函數進行壓力測試,這裏使用 ab,它是 apache 自帶的壓力測試工具,macOS 默認安裝了 apache,直接可以使用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 ab 工具進行壓力測試:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ 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/\"\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 kubectl get hpa -w 命令觀察 HPA 的狀態,可以看到副本數會根據指標的大小進行變化,壓力大的時候副本量會隨着遞增,等到壓力小了副本量會遞減:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubectl get hpa -w\nNAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE\nhello Deployment/hello 0%/60% 1 20 1 30m\nhello Deployment/hello 95%/60% 1 20 1 32m\nhello Deployment/hello 95%/60% 1 20 2 33m\nhello Deployment/hello 77%/60% 1 20 2 33m\nhello Deployment/hello 77%/60% 1 20 3 34m\nhello Deployment/hello 63%/60% 1 20 3 34m\nhello Deployment/hello 62%/60% 1 20 3 36m\nhello Deployment/hello 71%/60% 1 20 3 37m\nhello Deployment/hello 71%/60% 1 20 4 37m\nhello Deployment/hello 0%/60% 1 20 4 38m\nhello Deployment/hello 0%/60% 1 20 4 42m\nhello Deployment/hello 0%/60% 1 20 1 43m\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 kubectl get pod -w 命令觀察也可以看到自動伸縮時 Pod 的數量及狀態變化:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ kubectl get pod -w\nNAME READY STATUS RESTARTS AGE\nhello-67b44c7585-5t9g4 1/1 Running 0 21h\nhello-67b44c7585-d9w7j 0/1 Pending 0 0s\nhello-67b44c7585-d9w7j 0/1 Pending 0 0s\nhello-67b44c7585-d9w7j 0/1 Init:0/1 0 0s\nhello-67b44c7585-d9w7j 0/1 PodInitializing 0 2s\nhello-67b44c7585-d9w7j 1/1 Running 0 6s\nhello-67b44c7585-fctgq 0/1 Pending 0 0s\nhello-67b44c7585-fctgq 0/1 Pending 0 0s\nhello-67b44c7585-fctgq 0/1 Init:0/1 0 0s\nhello-67b44c7585-fctgq 0/1 PodInitializing 0 2s\nhello-67b44c7585-fctgq 1/1 Running 0 3s\nhello-67b44c7585-ht784 0/1 Pending 0 0s\nhello-67b44c7585-ht784 0/1 Pending 0 0s\nhello-67b44c7585-ht784 0/1 Init:0/1 0 0s\nhello-67b44c7585-ht784 0/1 PodInitializing 0 2s\nhello-67b44c7585-ht784 1/1 Running 0 3s\nhello-67b44c7585-wfcg9 0/1 Pending 0 0s\nhello-67b44c7585-wfcg9 0/1 Pending 0 0s\nhello-67b44c7585-wfcg9 0/1 Init:0/1 0 0s\nhello-67b44c7585-wfcg9 0/1 PodInitializing 0 2s\nhello-67b44c7585-wfcg9 1/1 Running 0 3s\nhello-67b44c7585-fctgq 1/1 Terminating 0 8m53s\nhello-67b44c7585-ht784 1/1 Terminating 0 7m52s\nhello-67b44c7585-wfcg9 1/1 Terminating 0 5m50s\nhello-67b44c7585-d9w7j 1/1 Terminating 0 9m54s\nhello-67b44c7585-fctgq 0/1 Terminating 0 9m24s\nhello-67b44c7585-ht784 0/1 Terminating 0 8m23s\nhello-67b44c7585-fctgq 0/1 Terminating 0 9m25s\nhello-67b44c7585-fctgq 0/1 Terminating 0 9m25s\nhello-67b44c7585-fctgq 0/1 Terminating 0 9m25s\nhello-67b44c7585-d9w7j 0/1 Terminating 0 10m\nhello-67b44c7585-d9w7j 0/1 Terminating 0 10m\nhello-67b44c7585-ht784 0/1 Terminating 0 8m24s\nhello-67b44c7585-wfcg9 0/1 Terminating 0 6m22s\nhello-67b44c7585-d9w7j 0/1 Terminating 0 10m\nhello-67b44c7585-d9w7j 0/1 Terminating 0 10m\nhello-67b44c7585-d9w7j 0/1 Terminating 0 10m\nhello-67b44c7585-wfcg9 0/1 Terminating 0 6m29s\nhello-67b44c7585-wfcg9 0/1 Terminating 0 6m29s\nhello-67b44c7585-ht784 0/1 Terminating 0 8m31s\nhello-67b44c7585-ht784 0/1 Terminating 0 8m31s\n"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"參考"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://kubeless.io/docs/autoscaling/","title":null},"content":[{"type":"text","text":"https://kubeless.io/docs/autoscaling/"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://github.com/mvranic/kubeless-apl-demo","title":null},"content":[{"type":"text","text":"https://github.com/mvranic/kubeless-apl-demo"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://github.com/kubernetes-sigs/metrics-server","title":null},"content":[{"type":"text","text":"https://github.com/kubernetes-sigs/metrics-server"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://stackoverflow.com/questions/54106725/docker-kubernetes-mac-autoscaler-unable-to-find-metrics","title":null},"content":[{"type":"text","text":"https://stackoverflow.com/questions/54106725/docker-kubernetes-mac-autoscaler-unable-to-find-metrics"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e8/e8b161c891e1578c2571fce5b0216806.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章