在k8s上安装OpenFaaS

安装OpenFaaS(总览)

此教程包含以下步骤:

  • 安装OpenFaaS CLI
  • 使用helm 部署OpenFaaS
  • 获得OpenFaaS gateway 地址
  • 记住登录gateway 的密码凭证
  • 登录、部署函数,试用web UI

准备k8s集群

开始之前,你需要拥有一个k8s集群。

我这里是使用minikube 搭建的单节点k8s。参考 minikube部署k8s

安装faas-cli

faas-cli 是OpenFaaS的命令行客户端。

在window下,mimikube启动一个linux虚拟机来部署k8s。

该教程所有命令操作都将在linux下进行。

1)下载faas-cli 二进制包

从github 下载最新版(当前0.13.11)。

地址 https://github.com/openfaas/faas-cli/releases

2)增加执行权限

chmod +x faas-cli

3)放入可执行文件目录

mv faas-cli /usr/bin

使用helm 安装openfaas

1)安装helm

跟faas-cli 类似,从官网下载二进制包,放入/usr/bin 即可。

2)添加OpenFaaS helm chart 仓库:

# helm repo add openfaas https://openfaas.github.io/faas-netes/
"openfaas" has been added to your repositories

3)更新helm charts

helm repo update

4)创建两个namespace,一个留给OpenFaaS 组件服务,一个留给函数。

# kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml

可能会出现如下报错:

The connection to the server raw.githubusercontent.com was refused - did you specify the right host or port?

据称是dns污染问题,查询域名ip修改/etc/hosts即可。

参考https://blog.csdn.net/weixin_38074756/article/details/109231865

5)创建一个secret,用户密码都设置为admin

kubectl -n openfaas create secret generic basic-auth --from-literal=basic-auth-user=admin --from-literal=basic-auth-password=admin

6)使用chart安装OpenFaaS.

# helm upgrade openfaas --install openfaas/openfaas --namespace openfaas --set functionNamespace=openfaas-fn --set basic_auth=true
Release "openfaas" does not exist. Installing it now.

NAME: openfaas
LAST DEPLOYED: Fri Jun 11 03:11:32 2021
NAMESPACE: openfaas
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
To verify that openfaas has started, run:

  kubectl -n openfaas get deployments -l "release=openfaas, app=openfaas"

稍等几分钟。可以按提示检查下openfaas启动状态。

获得OpenFaaS gateway 地址

# kubectl get svc -n openfaas gateway-external
NAME               TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
gateway-external   NodePort   10.111.117.113   <none>        8080:31112/TCP   69m

由此可知,gateway 可以使用(host ip):31112 访问。

为方便命令行工具faas-cli的使用,将地址加入环境变量。

# echo "export OPENFAAS_URL=192.168.99.100:31112" >> ~/.bashrc
# source .bashrc 

faas-cli登录测试

前面我们设置用户密码为admin/admin

# faas-cli login -u admin -p admin
credentials saved for admin http://192.168.99.100:31112

已经可以列出函数了,当然我还没有部署函数,所以是空的。

# faas-cli list  
Function                      	Invocations    	Replicas
#

部署一个函数

1)新建函数,选择使用python,将根据语言将生成模板

# faas-cli new func-test --lang python
2021/06/11 04:32:09 No templates found in current directory.
2021/06/11 04:32:09 Attempting to expand templates from https://github.com/openfaas/templates.git
2021/06/11 04:32:11 Fetched 13 template(s) : [csharp dockerfile go java11 java11-vert-x node node12 node14 php7 python python3 python3-debian ruby] from https://github.com/openfaas/templates.git
Folder: func-test created.
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
      |_|


Function created in folder: func-test
Stack file written: func-test.yml

可以看到模板里的处理函数:接收请求,然后原封不动返回响应。

# cat func-test/handler.py 
def handle(req):
    """handle a request to the function
    Args:
        req (str): request body
    """

    return req

2)将函数构建为镜像

# faas-cli build -f func-test.yml

func-test.yml 由模板生成。

3)将镜像推送到 Docker registry

需要指定一个镜像仓库,将镜像推送保存。

例如可以自己注册docker hub账号,修改yml文件image字段为 dockerhub仓库地址。

# cat func-test.yml
...
image: yourName/func-test(这个地址是dockerhub地址)

然后执行faas-cli push。

# faas-cli push -f func-test.yml

在这里记得先docker login一下。否则分发时会提示你没有权限push。

4)部署函数

# faas-cli deploy -f func-test.yml 
Deploying: func-test.

Deployed. 202 Accepted.
URL: http://192.168.99.100:31112/function/func-test

检查k8s,发现相应资源已经就位了。

# kubectl get all -n openfaas-fn
NAME                             READY   STATUS    RESTARTS   AGE
pod/func-test-5f665849c9-86crp   1/1     Running   0          115s

NAME                TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
service/func-test   ClusterIP   10.99.136.17   <none>        8080/TCP   115s

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/func-test   1/1     1            1           115s

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/func-test-5f665849c9   1         1         1       115s

5)触发函数调用

# curl -X POST -d @name.json http://192.168.99.100:31112/function/func-test
{  "name": "jon"}

试用web UI

本地浏览器打开gateway网址,例如http://192.168.99.100:31112/。登录之后,就进入了web 客户端。可操作的功能类似CLI,那么,自己开始探索吧!

参考文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值