Share via


在 Azure Kubernetes Service 上使用 OpenFaaS (AKS)

OpenFaaS 是一種架構,會使用容器來組建無伺服器函式。 由於是開放原始碼專案,它在社群內被廣泛採用。 本文件詳述在 Azure Kubernetes Service (AKS) 叢集上安裝和使用 OpenFaas 的做法。

開始之前

新增 OpenFaaS Helm 圖表存放庫

  1. 瀏覽至 Azure Cloud Shell

  2. 使用下列 helm 命令,將 OpenFaaS helm 圖表存放庫新增至最新版本。

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

部署 OpenFaaS

好的做法是 OpenFaaS 和 OpenFaaS 函式應該儲存在自己的 Kubernetes 命名空間中。

  1. 使用 kubectl apply 命令建立 OpenFaaS 系統和函式的命名空間。

    kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
    
  2. 使用下列命令產生 OpenFaaS UI 入口網站和 REST API 的密碼。 Helm 圖表會使用此密碼在 OpenFaaS 閘道上啟用基本身份驗證,而 OpenFaaS 閘道會透過雲端 LoadBalancer 公開給網際網路。

    # generate a random password
    PASSWORD=$(head -c 12 /dev/urandom | shasum| cut -d' ' -f1)
    
    kubectl -n openfaas create secret generic basic-auth \
    --from-literal=basic-auth-user=admin \
    --from-literal=basic-auth-password="$PASSWORD"
    
  3. 使用下列 echo 命令取得密碼的值。

    echo $PASSWORD
    
  4. 使用 helm upgrade 命令將 OpenFaaS 部署至 AKS 叢集。

    helm upgrade openfaas --install openfaas/openfaas \
        --namespace openfaas  \
        --set basic_auth=true \
        --set functionNamespace=openfaas-fn \
        --set serviceType=LoadBalancer
    

    您的輸出看起來應類似下列的緊縮範例輸出:

    NAME: openfaas
    LAST DEPLOYED: Tue Aug 29 08:26:11 2023
    NAMESPACE: openfaas
    STATUS: deployed
    ...
    NOTES:
    To verify that openfaas has started, run:
    
    kubectl --namespace=openfaas get deployments -l "release=openfaas, app=openfaas"
    ...
    
  5. 建立了用於存取 OpenFaaS 閘道的公用 IP 位址。 使用 kubectl get service 命令取得IP位址。

    kubectl get service -l component=gateway --namespace openfaas
    

    您的輸出看起來應類似下列的範例輸出:

    NAME               TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)          AGE
    gateway            ClusterIP      10.0.156.194   <none>         8080/TCP         7m
    gateway-external   LoadBalancer   10.0.28.18     52.186.64.52   8080:30800/TCP   7m
    
  6. 在此範例中,http://52.186.64.52:8080 瀏覽至連接埠 8080 上的外部 IP 位址,以測試 OpenFaaS 系統,系統會提示您登入。 預設使用者是 admin,您可以使用 echo $PASSWORD 擷取密碼。

    Screenshot of OpenFaaS UI.

  7. 設定 $OPENFAAS_URL 為連接埠 8080 上的外部 IP 位址 URL,並以下列命令使用 Azure CLI 登入。

    export OPENFAAS_URL=http://52.186.64.52:8080
    echo -n $PASSWORD | ./faas-cli login -g $OPENFAAS_URL -u admin --password-stdin
    

建立第一個函式

  1. 使用 OpenFaaS URL 瀏覽至 OpenFaaS 系統。

  2. 選取 [部署新函式] 並搜尋 Figlet,以使用 OpenFaas 入口網站建立函式。

  3. 選取 Figlet 函式,然後選取 [部署]

    Screenshot shows the Deploy A New Function dialog box with the text Figlet on the search line.

  4. 使用下列 curl 命令叫用函式。 請確定您將下列範例中的 IP 位址取代為 OpenFaaS 閘道位址。

    curl -X POST http://52.186.64.52:8080/function/figlet -d "Hello Azure"
    

    您的輸出看起來應類似下列的範例輸出:

     _   _      _ _            _
    | | | | ___| | | ___      / \    _____   _ _ __ ___
    | |_| |/ _ \ | |/ _ \    / _ \  |_  / | | | '__/ _ \
    |  _  |  __/ | | (_) |  / ___ \  / /| |_| | | |  __/
    |_| |_|\___|_|_|\___/  /_/   \_\/___|\__,_|_|  \___|
    

建立第二個函式

設定 Azure Cosmos DB 實例

  1. 瀏覽至 Azure Cloud Shell

  2. 使用 az group create 命令建立 Azure Cosmos DB 執行個體的新資源群組。

    az group create --name serverless-backing --location eastus
    
  3. 使用 az cosmosdb create 命令部署類型 MongoDB 的 Azure Cosmos DB 執行個體。 以您自己的唯一執行個體名稱取代 openfaas-cosmos

    az cosmosdb create --resource-group serverless-backing --name openfaas-cosmos --kind MongoDB
    
  4. 使用 az cosmosdb list 命令取得 Azure Cosmos DB 資料庫連接字串,並儲存在變數中。 確認將 --resource-group 參數的值替換為資源群組的名稱,並將 --name 參數替換為 Azure Cosmos DB 執行個體的名稱。

    COSMOS=$(az cosmosdb list-connection-strings \
      --resource-group serverless-backing \
      --name openfaas-cosmos \
      --query connectionStrings[0].connectionString \
      --output tsv)
    
  5. 在 Azure Cosmos DB 中建立名為 plans.json 的檔案,並在下列 json 中複製,以測試資料填入 Azure Cosmos DB。

    {
        "name" : "two_person",
        "friendlyName" : "Two Person Plan",
        "portionSize" : "1-2 Person",
        "mealsPerWeek" : "3 Unique meals per week",
        "price" : 72,
        "description" : "Our basic plan, delivering 3 meals per week, which will feed 1-2 people.",
        "__v" : 0
    }
    

建立函式

  1. 安裝 MongoDB 工具。 下列範例命令會使用 brew 安裝這些工具。 如需更多安裝選項,請參閱 MongoDB 文件

    brew install mongodb
    
  2. 使用 mongoimport 工具載入包含資料的 Azure Cosmos DB 執行個體。

    mongoimport --uri=$COSMOS -c plans < plans.json
    

    您的輸出看起來應類似下列的範例輸出:

    2018-02-19T14:42:14.313+0000    connected to: localhost
    2018-02-19T14:42:14.918+0000    imported 1 document
    
  3. 使用 faas-cli deploy 命令建立函式。 確認您將 -g 引數的值更新成您的 OpenFaaS 閘道位址。

    faas-cli deploy -g http://52.186.64.52:8080 --image=shanepeckham/openfaascosmos --name=cosmos-query --env=NODE_ENV=$COSMOS
    

    部署之後,您的輸出看起來應該類似下列範例輸出:

    Deployed. 202 Accepted.
    URL: http://52.186.64.52:8080/function/cosmos-query
    
  4. 使用下列 curl 命令測試函式。 請務必使用 OpenFaaS 閘道位址來更新 IP 位址。

    curl -s http://52.186.64.52:8080/function/cosmos-query
    

    您的輸出看起來應類似下列的範例輸出:

    [{"ID":"","Name":"two_person","FriendlyName":"","PortionSize":"","MealsPerWeek":"","Price":72,"Description":"Our basic plan, delivering 3 meals per week, which will feed 1-2 people."}]
    

    注意

    您也可以在 OpenFaaS 使用者介面中測試函式:

    Screenshot of OpenFaas UI.

下一步

繼續透過一套實作教室利用 OpenFaaS 工作坊學習,這涵蓋如何建立您自己的 GitHub 聊天機器人、取用祕密、檢視計量和自動縮放等主題。