# Serverless 框架OpenFaas部署流程


方法1:

1.前置條件

1.安裝kubernetes
2.安裝kubectl

2.安裝OpenFaas

git clone https://github.com/openfaas/faas-netes
cd faas-netes
kubectl apply -f ./namespaces.yml

3.驗證是否成功創建openfass, openfaas-fn兩個namespaces

[root@node1 home] kubectl get namespaces
NAME              STATUS   AGE
default           Active   26h
kube-node-lease   Active   26h
kube-public       Active   26h
kube-system       Active   26h
openfaas          Active   26h
openfaas-fn       Active   26h

4.給openfaas創建密碼(必須)

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

5.安裝openfaas所有組件

git clone https://github.com/openfaas/faas-netes
cd faas-netes
kubectl apply -f ./yaml/

6.驗證是否安裝成功

root@node1 home]$ kubectl get pod -n openfaas
NAME                                READY   STATUS    RESTARTS   AGE
alertmanager-667c74c9c9-zsx66       1/1     Running   0          27h
basic-auth-plugin-76899dc95-78qjc   1/1     Running   0          27h
faas-idler-86b55ffcbf-k4xkd         1/1     Running   0          27h
gateway-8549c458c9-h6l6z            2/2     Running   1          27h
nats-6b6d549b56-rvgv4               1/1     Running   0          27h
prometheus-b4cbb9bdc-2q8dg          1/1     Running   0          27h
queue-worker-554946dc65-gsvcp       1/1     Running   0          27h

7.下載faas-cli工具

https://github.com/openfaas/faas-cli/releases
cp faas-cli /usr/local/bin

8.登錄docker hub (如果是hub拉鏡像必須登錄成功)或者使用本地倉庫(前提構建本地倉庫)

docker login -u username -p password

9.准備測試函數

mkdir -p /home/functions
cd /home/functions
faas-cli new --lang python hello-python

10.修改生成的handler.py實現自己的處理邏輯

11.登錄openfass

export OPENFAAS_URL=http://127.0.0.1:31112
echo -n $PASSWORD | faas-cli login --password-stdin  // admin admin

12.構建

faas-cli build -f ./hello-python.yml
# 如果超時, 嘗試restart docker

13.上傳Docker Hub(可選)

faas-cli push -f ./hello-python.yml

14.部署

faas-cli deploy -f ./hello-python.yml

方法2(helm方式):

1.安裝helm

參考鏈接:https://blog.csdn.net/bbwangj/article/details/81087911

2.Kubernetes 創建兩個命名空間一個用於OpenFaas核心服務,一個用於功能

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

3.驗證是否創建成功

kubectl get namespace
NAME              STATUS   AGE
default           Active   149m
kube-node-lease   Active   149m
kube-public       Active   149m
kube-system       Active   149m
openfaas          Active   25m
openfaas-fn       Active   25m

4.添加OpenFaas源

helm repo add openfaas https://openfaas.github.io/faas-netes/

5.創建密碼(必須)

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

6.部署OpenFaas

helm repo update \
 && helm upgrade openfaas --install openfaas/openfaas \
    --namespace openfaas  \
    --set basic_auth=true \
    --set functionNamespace=openfaas-fn

7.設置OPENFAAS_URL

export OPENFAAS_URL=http://127.0.0.1:31112

8.登錄並檢查是否連通

echo -n $PASSWORD | faas-cli login -g $OPENFAAS_URL -u admin -p admin

9.測試

mkdir -p /home/functions
cd /home/functions
faas-cli new --lang python3  hello-python  // 生成hello-python.yml和hello-python文件夾

10.編輯hello-python/handler.py,自定義函數

11.編輯hello-python.yml文件

version: 1.0
provider:
  name: openfaas
  gateway: http://127.0.0.1:31112  // OPENFAAS_URL
functions:
  hello-python:
    lang: python
    handler: ./hello-python
    image: 127.0.0.1:5000/v2/hello-python:latest //docker hub 地址或者本地倉庫地址,這里用的本地倉庫

12.編譯,上傳,部署

faas-cli build -f ./hello-python.yml
faas-cli push -f ./hello-python.yml
faas-cli deploy -f ./hello-python.yml
或者使用快捷命令完成上面三步
faas-cli up -f hello-python.yml

13.OK完成


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM