前言

本文记录一下本人在搭建完成k8s的基础上,搭建serverless框架openfaas的过程,参考了两篇博客,记录了在搭建过程中遇到的问题与解决方案。本人的系统是centos8。


一、OpenFaaS部署

1、获取OpenFaaS资源

下载 faas-netes,首先是官方的

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

要是网络不好,使用下面的国内镜像

git clone https://gitee.com/mirrors/faas-netes.git

下载成功后进入 faas-netes 文件夹

cd faas-netes

2、创建依赖的 namespace

通过应用 namespaces.yml 来创建 namespace/openfaas 以及 namespace/openfaas-fn

kubectl apply -f namespaces.yml

创建完成如下

namespace/openfaas created
namespace/openfaas-fn created

3、设置登录web控制台的账号和密码,这里账号密码都用admin

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

显示创建完成如下:

secret/basic-auth created

4、部署

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
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
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

部署过程时间可能较长,可以多次查看pod,pod会自动的部署

kubectl get pods -n openfaas

等待较长时间后,再次查看,发现pod都已经READY了

NAME                                READY   STATUS    RESTARTS   AGE
alertmanager-7b566d8d4c-8qwzj       1/1     Running   0          7h6m
basic-auth-plugin-98cd9746c-j6wnt   1/1     Running   0          7h6m
gateway-77f7d67cbc-8wkth            2/2     Running   1          7h6m
nats-697d4bd9fd-k5qtg               1/1     Running   0          7h6m
prometheus-7ff7fb4897-lgt4x         1/1     Running   0          7h6m
queue-worker-7579944c7d-9rpqv       1/1     Running   0          7h6m

5、成功后查看service

成功后可以查看service,发现除了gateway-external,其他service都是ClusterIP类型的,gateway-external是NodePort类型,service映射到了31222端口,之后可以在集群外部访问。

kubectl get service -n openfaas

service查看如下图:

NAME                TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)          AGE
alertmanager        ClusterIP   192.168.241.8     <none>        9093/TCP         7h11m
basic-auth-plugin   ClusterIP   192.168.250.208   <none>        8080/TCP         7h11m
gateway             ClusterIP   192.168.246.81    <none>        8080/TCP         7h11m
gateway-external    NodePort    192.168.242.183   <none>        8080:31112/TCP   7h11m
nats                ClusterIP   192.168.248.154   <none>        4222/TCP         7h11m
prometheus          ClusterIP   192.168.250.237   <none>        9090/TCP         7h11m

二、验证是否成功

浏览器访问:K8S宿主机IP地址:31112,会弹出账号密码输入窗口,按照你之前设置的,本人设置的账号admin,密码也是admin,成功进入界面如下:

点击 DEPLOY NEW FUNCTION 部署一个新的函数
在这里插入图片描述
可以选择商店已经有的 NodeInfo ,它的作用是获取部署的主机的信息,(1)选择NodeInfo,(2)点击部署DEPLOY。
在这里插入图片描述
等待一段时间,(1)状态Status变成Ready,(2)在输入文本txt,(3)点击INVOKE,(4)观察到返回值,则部署成功。
在这里插入图片描述

三、安装faas-cli

faas-cli 是 OpenFaaS 的命令行。

官方命令安装

curl -sL cli.openfaas.com | sudo sh

镜像加速安装

curl -sSL https://gitee.com/llcloud/llfaas/raw/master/get.sh | sudo sh

手动安装

我命令安装未能成功,报错如下:

Failed while attempting to install faas-cli. Please manually install:

1. Open your web browser and go to https://github.com/openfaas/faas-cli/releases
2. Download the latest release for your platform. Call it 'faas-cli'.
3. chmod +x ./faas-cli
4. mv ./faas-cli /usr/local/bin
5. ln -sf /usr/local/bin/faas-cli /usr/local/bin/faas

于是手动下载,自己在电脑上下载网址如下:
https://github.com/openfaas/faas-cli/releases,下载最新版的faas-cli即可,命名为 ‘faas-cli’,下载完成后上传到linux系统中,之后执行如下命令即可。

chmod +x ./faas-cli
mv ./faas-cli /usr/local/bin
ln -sf /usr/local/bin/faas-cli /usr/local/bin/faas

注:若遇到 mv 移动时可以会问是否要覆盖,输入y,选择覆盖,之后执行 ln 。

检查是否成功

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.

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐