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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章