Function 建立時,系統會同步建立其指定的應用程式容器,並且建立相關的水平擴展、監控、日誌等功能。

如何建立 Function

容器映象檔 (Container Image)

Syntixi 支援使用容器映像檔來建立 Function,以幫助使用 Docker 等一般容器執行環境的使用者能簡單且快速地進行服務移植。

這裡我們使用 NGINX 容器映像檔來當例子建立 Function,並透過 --port 來指定揭露的 port

  syntixi function create --name fn-demo --image nginx --port=tcp=80=http
  

建立成功後,系統會開始將應用程式部署至 Kubernetes 內,根據網路狀況下載容器映像檔等因素可能需等待一段時間後才會正式運作。

當完成時您可以使用 test 指令來測試您建立的 Function

  syntixi function test --name fn-demo
  

預設,Syntixi 會使用容器映像檔的入口點(Entrypoint)來啟動 Function。如果未指定入口點,您需要在建立 Function 時加上 --entry 參數。

  syntixi function create --name fn-demo --image nginx --entry "nginx -g daemon off;"
  

Bundle (使用原始碼或執行檔)

如果你偏好使用 Function-as-a-Service (FaaS) 方式來建立 Function,則可以使用 Bundle 上傳程式碼壓縮檔來建立 Function。

假設您已經使用 Node.js 原始碼來建立 Bundle,接下來我們使用官方提供的 Node.js 容器映像檔來建立 Function。

  syntixi function create --name hello \
    --image node:15.14.0-alpine3.10 \
    --bundle hello \ 
    --entry "node hello.js" \
    --port=tcp=80=http
  

在 Function 初始化階段時,Bundle 會被解壓縮放到 /userfunc 底下,同時容器預設的工作目錄也是 /userfunc

因此您可以將 Function 入口點參數設定為

  node hello.js
  

或使用絕對路徑

  node /userfunc/hello.js
  

如何讓應用被其他服務所存取

為提高服務可用性與存取安全控管,Syntixi 只會在 Function 有宣告開放存取的埠號 (Port Number) 時,才會建立對應內部 DNS 記錄供其他 Function 存取。

你可以在建立 Function 使用 --port 參數來指定要開放存取的埠號,以下是 --port 參數介紹

  --port=<protocol>=<port_number>=<port_name>
  

如 Function 有多個埠號開放存取,可以使用多個 --port 參數來指定,比如:

  syntixi function create --name hello \
    --image node:15.14.0-alpine3.10 \
    --bundle hello \ 
    --entry "node hello.js" \
    --port=tcp=80=http \
    --port=tcp=443=http
  

要使用容器映像檔還是 Bundle?

以下是一些建議,可以幫助您決定哪種更適合您的情況。

  • 可攜性

    從可攜性的角度來看,容器映像檔比起 Bundle 具有更好的可攜性,因為容器映像檔包含了所有必要的執行環境和相關套件。

  • Function 執行過程中所依賴的東西是否為應用程式的一部分

    假設您正在建立一個 RESTful API 服務,其中一個 API 使用到機器學習模型進行推論。 由於機器學習模型並非 HTTP 服務程式的一部分且經常更改,在此種狀況下將 HTTP 服務和機器學習模型分開管理是很正常的。
    在這種情況下,您可以使用帶有 HTTP 服務的容器映像檔,並使用包含機器學習模型的 Bundle 來建立 Function。 一旦 Bundle 的內容有所異動將觸發 Syntixi 內部更新機制,將相關服務進行滾動更新。

Was this page helpful?