OpenFaaS梳理之一:部署

关于《OpenFaaS梳理》系列

《OpenFaaS梳理》系列是最近在做的一个项目需要用到openfaas,该项目基本的实现是通过利用openfaas将Python基础镜像结合用户自定义的Python脚本,并通过发布服务和调度任务等方式,实现将一个个机器学习任务隔离在不同的容器服务中执行,实现现场人员机器学习任务的利用。本系列从最基础的部署开始,希望通过对之前开发中的细节和实现的回顾,浅入深逐渐熟悉OpenFaaS的高级功能,并将整个研发的思路过程做一次回顾。

环境信息
整个实战系列用到的OpenFaaS都是部署在Kubernetes环境下,因此请小伙伴自行提前准备好Kubernetes环境;

我这里由于是开发环境,因此并未部署Kubernetes集群,而是在单机上部署的Kubernetes;
下面给出整个系列涉及到的环境和软件的版本信息,仅供参考:

  1. 操作系统:Linux version 3.10.0-1160.el7.x86_64(CentOS-7-x86_64-DVD-2009)
  2. Kubernetes:1.21.2
  3. OpenFaaS gateway:0.21.1
  4. OpenFaaS faas-netes:0.14.1

部署

接下来下载faas-netes的源码,使用其中的kubernetes资源配置文件进行部署;

1. 安装git命令

yum install -y git

 2. 下载源码

git clone https://github.com/openfaas/faas-netes

 3. 进入下载的文件夹

cd faas-netes

4. 创建namespace

[root@localhost openfaas]# kubectl apply -f namespaces.yml
namespace/openfaas created
namespace/openfaas-fn created

5. 设置登录web控制台的账号和密码,设置账号密码

[root@localhost openfaas]# kubectl -n openfaas create secret generic basic-auth \
>     --from-literal=basic-auth-user=admin \
> --from-literal=basic-auth-password=openfaas
secret/basic-auth created

6. 部署

[root@localhost openfaas]# kubectl apply -f ./yaml
configmap/alertmanager-config created
deployment.apps/alertmanager created
service/alertmanager created
deployment.apps/basic-auth-plugin created
service/basic-auth-plugin created
deployment.apps/cron-connector created
serviceaccount/openfaas-controller created
role.rbac.authorization.k8s.io/openfaas-controller created
role.rbac.authorization.k8s.io/openfaas-profiles created
rolebinding.rbac.authorization.k8s.io/openfaas-controller created
rolebinding.rbac.authorization.k8s.io/openfaas-profiles created
deployment.apps/gateway created
service/gateway-external created
service/gateway created
namespace/openfaas unchanged
namespace/openfaas-fn unchanged
deployment.apps/nats created
service/nats created
customresourcedefinition.apiextensions.k8s.io/profiles.openfaas.com created
configmap/prometheus-config created
deployment.apps/prometheus created
serviceaccount/openfaas-prometheus created
role.rbac.authorization.k8s.io/openfaas-prometheus created
role.rbac.authorization.k8s.io/openfaas-prometheus-fn created
rolebinding.rbac.authorization.k8s.io/openfaas-prometheus created
rolebinding.rbac.authorization.k8s.io/openfaas-prometheus-fn created
service/prometheus created
deployment.apps/queue-worker created

7. 等待镜像下载、pod创建、启动等操作完成:

[root@localhost ~]# kubectl get pod -n openfaas
NAME                                 READY   STATUS    RESTARTS   AGE
alertmanager-c7d4dd89b-gr6sf         1/1     Running   1          62m
basic-auth-plugin-86d54f7c5f-rcht6   1/1     Running   1          62m
cron-connector-b747cb4b9-g6lrs       1/1     Running   1          53m
gateway-557854c544-swfb8             2/2     Running   4          62m
nats-76844df8b4-ts5zd                1/1     Running   1          62m
prometheus-5498dc8757-gvfch          1/1     Running   1          62m
queue-worker-5bb684c788-jcslt        1/1     Running   2          62m

至此,部署完成,接下来验证环境是否可用 。

验证

1. 当前K8S宿主机IP地址是192.168.79.139,因此浏览器访问192.168.79.139:31112,如下图,会弹出账号密码输入窗口,账号admin,密码是openfaas

 2. 登录成功:

OpenFaaS梳理之一:部署_第1张图片

部署和配置命令行工具

我们可以通过命令行工具远程访问openfaas,其安装也简单:

生产环境中一般需要将faas-cli安装到应用服务器上,开发环境也需要,我本地将其安装到了windowds下。

1. Linux下安装

  • 拉取faas-cli
curl -sL https://cli.openfaas.com | sh
  •  执行如下命令完成配置,将OPENFAAS_URL写入环境变量中,192.168.133.187是K8S宿主机IP:
echo export OPENFAAS_URL=192.168.79.131:31112 >> ~/.bashrc
  •  执行source ~/.bashrc让前面的配置立即生效;
source ~/.bashrc
  • 执行以下命令,用账号密码登录:
faas-cli login -u admin -p openfaas

2.windows下安装

  • 从官网离线下载faas-cli客户端​​​​​​

faas-cli

将安装路径添加到环境变量path中

OpenFaaS梳理之一:部署_第2张图片

OPENFAAS_URL写入环境变量中

OpenFaaS梳理之一:部署_第3张图片

验证faas-cli是否可执行

xudb@LAPTOP-VKV71JIE MINGW64 /e/xuexi/works/openfaas/bin
$ faas-cli
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
      |_|


Manage your OpenFaaS functions from the command line

Usage:
  faas-cli [flags]
  faas-cli [command]

Available Commands:
  auth           Obtain a token for your OpenFaaS gateway
  build          Builds OpenFaaS function containers
  cloud          OpenFaaS Cloud commands
  completion     Generates shell auto completion
  deploy         Deploy OpenFaaS functions
  describe       Describe an OpenFaaS function
  generate       Generate Kubernetes CRD YAML file
  help           Help about any command
  invoke         Invoke an OpenFaaS function
  list           List OpenFaaS functions
  login          Log in to OpenFaaS gateway
  logout         Log out from OpenFaaS gateway
  logs           Fetch logs for a functions
  namespaces     List OpenFaaS namespaces
  new            Create a new template in the current folder with the name given                                                                                                                                   as name
  publish        Builds and pushes multi-arch OpenFaaS container images
  push           Push OpenFaaS functions to remote registry (Docker Hub)
  registry-login Generate and save the registry authentication file
  remove         Remove deployed OpenFaaS functions
  secret         OpenFaaS secret commands
  store          OpenFaaS store commands
  template       OpenFaaS template store and pull commands
  up             Builds, pushes and deploys OpenFaaS function containers
  version        Display the clients version information

Flags:
      --filter string   Wildcard to match with function names in YAML file
  -h, --help            help for faas-cli
      --regex string    Regex to match with function names in YAML file
  -f, --yaml string     Path to YAML file describing function(s)

Use "faas-cli [command] --help" for more information about a command.

 然后再bash下用账号密码登录:

$ faas-cli login -u admin -p openfaas
WARNING! Using --password is insecure, consider using: cat ~/faas_pass.txt | faas-cli login -u user --password-stdin
Calling the OpenFaaS server to validate the credentials...
WARNING! You are not using an encrypted connection to the gateway, consider using HTTPS.
credentials saved for admin http://192.168.79.139:31112

登录成功,可以执行客户端命令了,list命令的结果如下

$ faas-cli list
Function                        Invocations     Replicas

至此,openfaas的安装部署和客户端的安装介绍完毕。

你可能感兴趣的:(openfaas,kubernetes,容器,云原生,serverless)