去年八月,我寫了一篇文章,展示了如何在5分鐘內(nèi)使用Kubernetes或Docker Swarm在DigitalOcean上設(shè)置OpenFaaS。本周,DigitalOcean與OpenFaaS社區(qū)之間的合作達到了頂峰,這使得OpenFaaS一鍵式Droplet圖像得以普遍提供。這使得通過DigitalOcean以最具成本效益的方式部署OpenFaaS變得更加容易。
現(xiàn)在,我們有三種方法有據(jù)可查,可以在DigitalOcean的基礎(chǔ)架構(gòu)上為您的團隊設(shè)置OpenFaaS:
- 將OpenFaaS部署到DigitalOcean的Kubernetes服務(wù)(DOKS)
- 通過Kubernetes或Swarm與Ansible一起部署OpenFaaS
- 使用今天可用的新的一鍵式Droplet圖像(本文)
在這篇文章中,我們將指導(dǎo)您使用一鍵式Droplet映像創(chuàng)建第一個OpenFaaS函數(shù),該映像使用單節(jié)點Docker Swarm集群。
先決條件
- DigitalOcean帳戶
- OpenFaaS CLI
現(xiàn)在,OpenFaaS CLI(或faas-cli)是OpenFaaS開發(fā)人員經(jīng)驗的核心部分,我們的用戶在本地及其CI作業(yè)中運行它。我們將使用它來創(chuàng)建功能并將其部署到您的一鍵式Droplet上。OpenFaaS文檔提供了獲取CLI所需的所有詳細信息。
創(chuàng)建Droplet
訪問您的Droplet
準備好Droplet后,它將顯示在DigitalOcean資源窗格內(nèi)的Droplet列表中。

復(fù)制IP地址,然后使用您喜歡的終端登錄到Droplet,使用 root@<IP address>
注意:請確保您已為正在使用的客戶端提供了適當(dāng)?shù)乃借€。
登錄后,請按照說明進行操作;在撰寫本文時,只需按Enter。
將為您執(zhí)行許多配置操作,結(jié)果將是運行中的基于Swarm的OpenFaaS實例。記下憑據(jù)-特別是最后一行將設(shè)置憑據(jù)的行faas-cli-在部署功能時將需要此憑據(jù)?,F(xiàn)在,我們可以關(guān)閉與Droplet的連接。
在本地計算機上設(shè)置OPENFAAS_URL:
$ export OPENFAAS_URL=http://159.65.92.17:8080
要保留此設(shè)置,請將其添加到您的.bashrc文件或中.bash_profile。
運行CLI登錄命令:
echo -n 6acf4e5dc4997530666fece7be3e15e5a1b7ba9572163aa2beb8ff9449ee1911 | faas-cli login --username=admin --password-stdin
當(dāng)我們將功能部署到Droplet時,這將節(jié)省我們的時間和精力。
創(chuàng)建一個功能
我們將創(chuàng)建一個簡單的Python函數(shù),該函數(shù)在調(diào)用時將回顯字符串。
- 為我們的功能創(chuàng)建一個工作區(qū):
$ mkdir -p ~/openfaas/functions/ && cd ~/openfaas/functions/
-
hello-digitalocean使用CLI 創(chuàng)建一個稱為的函數(shù):
將
rgee0前綴替換為您自己的Docker Hub帳戶名,以啟用后續(xù)push操作。
$ faas-cli new --lang=python3 --prefix=rgee0 hello-digitalocean
faas-cli new --lang=python3 --prefix=rgee0 hello-digitalocean
2019/02/02 11:42:08 No templates found in current directory.
2019/02/02 11:42:08 Attempting to expand templates from https://github.com/openfaas/templates.git
2019/02/02 11:42:10 Fetched 15 template(s) : [csharp csharp-armhf dockerfile go go-armhf java8 node node-arm64 node-armhf php7 python python-armhf python3 python3-armhf ruby] from https://github.com/openfaas/templates.git
Folder: hello-digitalocean created.
___ _____ ____
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|
Function created in folder: hello-digitalocean
Stack file written: hello-digitalocean.yml
- 重命名
hello-digitalocean.yml為stack.yml:
$ mv hello-digitalocean.yml stack.yml
- 編輯
hello-digitalocean/handler.py以添加我們函數(shù)的代碼:
def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""
return 'Hello from OpenFaaS on DigitalOcean!'
- 保存處理程序并運行
faas-cli up以構(gòu)建,推送和部署:
$ faas-cli up
[0] > Building hello-digitalocean.
Clearing temporary build folder: ./build/hello-digitalocean/
Preparing ./hello-digitalocean/ ./build/hello-digitalocean/function
Building: rgee0/hello-digitalocean:latest with python template. Please wait..
Sending build context to Docker daemon 8.192kB
Step 1/25 : FROM python:2.7-alpine
...
Successfully built 724a2e96754b
Successfully tagged rgee0/hello-digitalocean:latest
Image: rgee0/hello-digitalocean:latest built.
[0] < Building hello-digitalocean done.
[0] worker done.
[0] > Pushing hello-digitalocean [rgee0/hello-digitalocean:latest].
The push refers to repository [docker.io/rgee0/hello-digitalocean]
...
latest: digest: sha256:03ec049a59bd784a152ca3df8fcdffd97ab0dcb4392e0c2811ee5dcb4180ca2d size: 3655
[0] < Pushing hello-digitalocean [rgee0/hello-digitalocean:latest] done.
[0] worker done.
Deploying: hello-digitalocean.
Deployed. 202 Accepted.
URL: http://159.65.92.17:8080/function/hello-digitalocean
正如我們先前將CLI登錄到我們的Droplet并設(shè)置一樣
OPENFAAS_URL,CLI將無縫地以Droplet為目標。為了推送到Docker Hub,您還需要在此登錄到您的帳戶。
- 使用以下命令調(diào)用功能
faas-cli:
$ echo '' | faas-cli invoke hello-digitalocean
Hello from OpenFaaS on DigitalOcean!
通過訪問訪問UI以查看已部署的功能http://159.65.92.17:8080/ui/。您需要以前的憑據(jù)才能訪問該網(wǎng)站。

從這里您還可以調(diào)用函數(shù)-只需單擊invoke按鈕;您會注意到調(diào)用計數(shù)隨您的增加而增加。您還可以通過OpenFaaS商店從一組現(xiàn)成的功能中進行部署-單擊Deploy New Function以查看可用功能。
包起來
我們已經(jīng)開始使用DigitalOcean的OpenFaaS一鍵式應(yīng)用程序創(chuàng)建函數(shù)的第一步。內(nèi)置配置創(chuàng)建了一個基于Docker Swarm的可公開訪問的實例,默認情況下啟用了基本身份驗證。我們已經(jīng)研究了如何通過設(shè)置來使用OpenFaaS CLI構(gòu)建功能并將其部署到遠程實例OPENFAAS_URL。
這是快速入門OpenFaaS的良好起點。實例啟動并運行后,您可以理想地通過OpenFaaS Workshop進行工作。如果您已完全按照本文進行操作,那么您應(yīng)該可以直接跳至實驗2-記住將所有研討會參考資料替換127.0.0.1為Droplet的IP地址。