本文将介绍 Kubeless 的安装以及如何使用 Kubeless CLI 部署和管理函数。
操作系统:macOS
Kubernetes 版本:v1.15.5
Kubeless 版本:v1.0.7
目前 Kubeless 同时支持安装在 Kubernetes 和 OpenShift (1.5+),其中 Kubernetes 环境支持 RBAC Kubernetes 集群和非 RBAC Kubernetes 集群。
Kubeless 安装比较简单,通过 kubectl create 命令即可快速安装:
目前 Kubeless 最新的版本为 v1.0.7,在 RBAC Kubernetes 集群安装 Kubeless,命令及输出如下所示:
192:~ donghuiexport RELEASE=exportRELEASE=(curl -s https://api.github.com/repos/kubeless/kubeless/releases/latest | grep tag_name | cut -d '"' -f 4) 192:~ donghuiechoechoRELEASE v1.0.7 192:~ donghui$ kubectl create ns kubeless namespace/kubeless created 192:~ donghuikubectl create -f https://github.com/kubeless/kubeless/releases/download/kubectlcreate−fhttps://github.com/kubeless/kubeless/releases/download/RELEASE/kubeless-$RELEASE.yaml clusterrole.rbac.authorization.k8s.io/kubeless-controller-deployer created clusterrolebinding.rbac.authorization.k8s.io/kubeless-controller-deployer created customresourcedefinition.apiextensions.k8s.io/functions.kubeless.io created customresourcedefinition.apiextensions.k8s.io/httptriggers.kubeless.io created customresourcedefinition.apiextensions.k8s.io/cronjobtriggers.kubeless.io created configmap/kubeless-config created deployment.apps/kubeless-controller-manager created serviceaccount/controller-acct created
Kubeless Controller Manager 是 Kubeless 的控制中枢。查看 Kubeless 相关的 Pod 和 Deployment:
192:~ donghui$ kubectl get pods -n kubeless NAME READY STATUS RESTARTS AGE kubeless-controller-manager-cd68f56c4-cjbnz 3/3 Running 0 6m38s 192:~ donghui$ kubectl get deployment -n kubeless NAME READY UP-TO-DATE AVAILABLE AGE kubeless-controller-manager 1/1 1 1 6m50s
Kubeless 基于 Kubernetes CRD 进行了扩展,查看 相关的 CRD :
192:~ donghui$ kubectl get customresourcedefinition NAME CREATED AT cronjobtriggers.kubeless.io 2020-09-26T15:41:05Z functions.kubeless.io 2020-09-26T15:41:05Z httptriggers.kubeless.io 2020-09-26T15:41:05Z
也可以通过 kubectl 访问这些 CRD,如下所示:
192:~ donghui$ kubectl get functions No resources found. 192:~ donghui$ kubectl get cronjobtriggers No resources found. 192:~ donghui$ kubectl get httptriggers No resources found.
查看与 controller 相关的 docker 镜像,可以发现上面每个 CRD 都对应一个 docker 镜像:
192:kubeless_demo donghui$ docker images|grep kubeless|grep controller kubeless/function-controller v1.0.7 94e7c07e8bd3 2 months ago 85.3MB kubeless/http-trigger-controller v1.0.1 d6f09f3299d9 15 months ago 83.4MB kubeless/cronjob-trigger-controller v1.0.1 aec1dd30bb57 15 months ago 77.1MB
Kubeless 提供了命令行工具 kubeless,用于对 Kubeless 的函数进行部署以及管理。安装 kubeless CLI,Linux 和 macOS 可以使用下面的命令:
export OS=$(uname -s| tr '[:upper:]' '[:lower:]')curl -OL https://github.com/kubeless/kubeless/releases/download/$RELEASE/kubeless_$OS-amd64.zip && \ unzip kubeless_$OS-amd64.zip && \ sudo mv bundles/kubeless_$OS-amd64/kubeless /usr/local/bin/
Windows 则需要下载安装包、解压并将 kubeless 命令添加到系统 PATH 环境变量中。
使用 kubeless -h 可以查看 kubeless CLI 帮助文档,了解 kubeless 相关命令:
192:kubeless_demo donghui$ kubeless --help Serverless framework for Kubernetes Usage: kubeless [command] Available Commands: autoscale manage autoscale to function on Kubeless completion Output shell completion code for the specified shell. function function specific operations get-server-config Print the current configuration of the controller help Help about any command topic manage message topics in Kubeless trigger trigger specific operations version Print the version of Kubeless Flags: -h, --help help for kubeless Use "kubeless [command] --help" for more information about a command.
创建一个 Python 文件 test.py,内容如下:
def hello(event, context): print event return event['data']
Kubeless 中的函数具有相同的格式,而与函数的语言或事件源无关。通常,每个函数:
部署函数:
192:kubeless_demo donghui$ kubeless function deploy hello --runtime python2.7 --from-file test.py --handler test.hello INFO[0000] Deploying function... INFO[0000] Function hello submitted for deployment INFO[0000] Check the deployment status executing 'kubeless function ls hello'
让我们分析下上面的命令:
提示:更多选项可以通过 kubeless function deploy --help 命令查看。
查看函数部署状态:
192:kubeless_demo donghui$ kubeless function ls hello NAME NAMESPACE HANDLER RUNTIME DEPENDENCIES STATUS hello default test.hello python2.7 0/1 NOT READY 192:kubeless_demo donghui$ kubeless function ls hello NAME NAMESPACE HANDLER RUNTIME DEPENDENCIES STATUS hello default test.hello python2.7 1/1 READY 192:kubeless_demo donghui$ kubectl get functions NAME AGE hello 3m25s
调用函数,通过 kubeless function call 调用函数:
192:kubeless_demo donghui$ kubeless function call hello --data 'Hello world!' Hello world!
或者可以使用 kubectl proxy 创建反向代理,然后通过 curl 访问函数:
192:kubeless_demo donghui$ kubectl proxy -p 8080 & [1] 7166 192:kubeless_demo donghui$ Starting to serve on 127.0.0.1:8080 192:kubeless_demo donghui$ curl -L --data '{"Another": "Echo"}' \ --header "Content-Type:application/json" \ localhost:8080/api/v1/namespaces/default/services/hello:http-function-port/proxy/ {"Another": "Echo"}
修改代码,重新部署,然后调用:
修改 test.py: def hello(event, context): print event return event['data'] + " updated"
修改代码后,重新部署,然后调用,可以发现代码已生效:
192:kubeless_demo donghui$ kubeless function update hello -f test.py INFO[0000] Redeploying function... INFO[0000] Function hello submitted for deployment INFO[0000] Check the deployment status executing 'kubeless function ls hello' 192:kubeless_demo donghui$ kubeless function call hello --data 'Hello world!' Hello world! updated
删除函数:
192:kubeless_demo donghui$ kubeless function delete hello 192:kubeless_demo donghui$ kubeless function ls NAME NAMESPACE HANDLER RUNTIME DEPENDENCIES STATUS
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。