• 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

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

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

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

    6. 部署

    1. [root@localhost openfaas]# kubectl apply -f ./yaml
    2. configmap/alertmanager-config created
    3. deployment.apps/alertmanager created
    4. service/alertmanager created
    5. deployment.apps/basic-auth-plugin created
    6. service/basic-auth-plugin created
    7. deployment.apps/cron-connector created
    8. serviceaccount/openfaas-controller created
    9. role.rbac.authorization.k8s.io/openfaas-controller created
    10. role.rbac.authorization.k8s.io/openfaas-profiles created
    11. rolebinding.rbac.authorization.k8s.io/openfaas-controller created
    12. rolebinding.rbac.authorization.k8s.io/openfaas-profiles created
    13. deployment.apps/gateway created
    14. service/gateway-external created
    15. service/gateway created
    16. namespace/openfaas unchanged
    17. namespace/openfaas-fn unchanged
    18. deployment.apps/nats created
    19. service/nats created
    20. customresourcedefinition.apiextensions.k8s.io/profiles.openfaas.com created
    21. configmap/prometheus-config created
    22. deployment.apps/prometheus created
    23. serviceaccount/openfaas-prometheus created
    24. role.rbac.authorization.k8s.io/openfaas-prometheus created
    25. role.rbac.authorization.k8s.io/openfaas-prometheus-fn created
    26. rolebinding.rbac.authorization.k8s.io/openfaas-prometheus created
    27. rolebinding.rbac.authorization.k8s.io/openfaas-prometheus-fn created
    28. service/prometheus created
    29. deployment.apps/queue-worker created

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

    1. [root@localhost ~]# kubectl get pod -n openfaas
    2. NAME READY STATUS RESTARTS AGE
    3. alertmanager-c7d4dd89b-gr6sf 1/1 Running 1 62m
    4. basic-auth-plugin-86d54f7c5f-rcht6 1/1 Running 1 62m
    5. cron-connector-b747cb4b9-g6lrs 1/1 Running 1 53m
    6. gateway-557854c544-swfb8 2/2 Running 4 62m
    7. nats-76844df8b4-ts5zd 1/1 Running 1 62m
    8. prometheus-5498dc8757-gvfch 1/1 Running 1 62m
    9. 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,其安装也简单:

    生产环境中一般需要将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

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

    OPENFAAS_URL写入环境变量中

    验证faas-cli是否可执行

    1. xudb@LAPTOP-VKV71JIE MINGW64 /e/xuexi/works/openfaas/bin
    2. $ faas-cli
    3. ___ _____ ____
    4. / _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
    5. | | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
    6. | |_| | |_) | __/ | | | _| (_| | (_| |___) |
    7. \___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
    8. |_|
    9. Manage your OpenFaaS functions from the command line
    10. Usage:
    11. faas-cli [flags]
    12. faas-cli [command]
    13. Available Commands:
    14. auth Obtain a token for your OpenFaaS gateway
    15. build Builds OpenFaaS function containers
    16. cloud OpenFaaS Cloud commands
    17. completion Generates shell auto completion
    18. deploy Deploy OpenFaaS functions
    19. describe Describe an OpenFaaS function
    20. generate Generate Kubernetes CRD YAML file
    21. help Help about any command
    22. invoke Invoke an OpenFaaS function
    23. list List OpenFaaS functions
    24. login Log in to OpenFaaS gateway
    25. logout Log out from OpenFaaS gateway
    26. logs Fetch logs for a functions
    27. namespaces List OpenFaaS namespaces
    28. new Create a new template in the current folder with the name given as name
    29. publish Builds and pushes multi-arch OpenFaaS container images
    30. push Push OpenFaaS functions to remote registry (Docker Hub)
    31. registry-login Generate and save the registry authentication file
    32. remove Remove deployed OpenFaaS functions
    33. secret OpenFaaS secret commands
    34. store OpenFaaS store commands
    35. template OpenFaaS template store and pull commands
    36. up Builds, pushes and deploys OpenFaaS function containers
    37. version Display the clients version information
    38. Flags:
    39. --filter string Wildcard to match with function names in YAML file
    40. -h, --help help for faas-cli
    41. --regex string Regex to match with function names in YAML file
    42. -f, --yaml string Path to YAML file describing function(s)
    43. Use "faas-cli [command] --help" for more information about a command.

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

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

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

    1. $ faas-cli list
    2. Function Invocations Replicas

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

  • 相关阅读:
    程序员过不去的坎-算法篇
    MySQL 表分区简介
    暑期备考2024年汉字小达人:吃透18道选择题真题(持续)
    PEP 257 - 文档字符串约定
    SpringMVC文件上传
    Linux·软中断&tasklet
    【踩坑】POST 方法的基于摘要的协商缓存实现
    C语言之字符串函数二
    SpringBoot整合Mybatisplus配置多数据源
    Chrome漏洞分析与利用(十三)——issue-1182647(CVE 2021-21195)漏洞分析
  • 原文地址:https://blog.csdn.net/akenseren/article/details/126804129