
在本文中,我將建立一個用於開發遊戲「Bylina」的工作環境,並將遊戲本身拆分成適合在 OpenFaaS 中使用的部分。所有操作都將使用 OpenFaaS 完成。 Linux我將使用 VirtualBox 在 minikube 中部署 Kubernetes。我的工作機有 2 個 CPU 核心和 12GB 內存,系統盤是 SSD。我將把它用作主要的開發系統。 debian 系統版本 8,已安裝 emacs、sudo、git 和 virtualbox 軟體包。其他軟體我將從 GitHub 和其他來源下載安裝。除非另有說明,這些應用程式將安裝在 /usr/local/bin 目錄下。讓我們開始吧!
準備工作環境
安裝Go
按照官方網站的說明進行操作:
$ curl -L0 https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz -o go.tar.gz
$ sudo tar -C /usr/local -xzf go.tar.gz
$ echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile我們檢查功能:
$ mkdir -p ~/go/src/hello && cd ~/go/src/hello
$ echo 'package main
import "fmt"
func main() {
fmt.Printf("hello, worldn")
}' > hello.go
$ go build
$ ./hello
hello, world安裝 faas-cli
按照官方網站的說明進行操作:
$ curl -sSL https://cli.openfaas.com | sudo -E sh
x86_64
Downloading package https://github.com/openfaas/faas-cli/releases/download/0.11.3/faas-cli as /tmp/faas-cli
Download complete.
Running with sufficient permissions to attempt to move faas-cli to /usr/local/bin
New version of faas-cli installed to /usr/local/bin
Creating alias 'faas' for 'faas-cli'.
___ _____ ____
/ _ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ / _ '_ | |_ / _` |/ _` ___
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
___/| .__/ ___|_| |_|_| __,_|__,_|____/
|_|
CLI:
commit: 73004c23e5a4d3fdb7352f953247473477477a64
version: 0.11.3此外,您還可以連接 bash-completion:
faas-cli completion --shell bash | sudo tee /etc/bash_completion.d/faas-cli安裝並設定 Kubernetes
對於開發來說,minikube已經足夠了,所以我們在/usr/local/bin中安裝它和kubelet,同時我們還要安裝helm來安裝應用程式:
$ curl https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 -o minikube && chmod +x minikube && sudo mv minikube /usr/local/bin/
$ curl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl -o kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
$ curl https://get.helm.sh/helm-v3.0.2-linux-amd64.tar.gz | tar -xzvf - linux-amd64/helm --strip-components=1; sudo mv helm /usr/local/bin運行 minikube:
$ minikube start
minikube v1.6.2 on Debian 8.11
Automatically selected the 'virtualbox' driver (alternates: [])
Downloading VM boot image ...
> minikube-v1.6.0.iso.sha256: 65 B / 65 B [--------------] 100.00% ? p/s 0s
> minikube-v1.6.0.iso: 150.93 MiB / 150.93 MiB [-] 100.00% 5.67 MiB p/s 27s
Creating virtualbox VM (CPUs=2, Memory=8192MB, Disk=20000MB) ...
Preparing Kubernetes v1.17.0 on Docker '19.03.5' ...
Downloading kubeadm v1.17.0
Downloading kubelet v1.17.0
Pulling images ...
Launching Kubernetes ... Waiting for cluster to come online ...
Done! kubectl is now configured to use "minikube"我們檢查:
$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-6955765f44-knlcb 1/1 Running 0 29m
kube-system coredns-6955765f44-t9cpn 1/1 Running 0 29m
kube-system etcd-minikube 1/1 Running 0 28m
kube-system kube-addon-manager-minikube 1/1 Running 0 28m
kube-system kube-apiserver-minikube 1/1 Running 0 28m
kube-system kube-controller-manager-minikube 1/1 Running 0 28m
kube-system kube-proxy-hv2wc 1/1 Running 0 29m
kube-system kube-scheduler-minikube 1/1 Running 0 28m
kube-system storage-provisioner 1/1 Running 1 29m安裝 OpenFaaS
開發人員建議為工作創建兩個命名空間:
$ kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
namespace/openfaas created
namespace/openfaas-fn created新增 helm 的儲存庫:
$ helm repo add openfaas https://openfaas.github.io/faas-netes/
"openfaas" has been added to your repositories圖表具有在安裝之前設定密碼的功能,讓我們使用它並將存取資料儲存為 k8s secret:
$ PASSWORD=verysecurerandompasswordstring
$ kubectl -n openfaas create secret generic basic-auth --from-literal=basic-auth-user=admin --from-literal=basic-auth-password="$PASSWORD"
secret/basic-auth created我們進行部署:
$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "openfaas" chart repository
Update Complete. Happy Helming!
$ helm upgrade openfaas --install openfaas/openfaas --namespace openfaas --set functionNamespace=openfaas-fn --set generateBasicAuth=false
Release "openfaas" does not exist. Installing it now.
NAME: openfaas
LAST DEPLOYED: Fri Dec 25 10:28:22 2019
NAMESPACE: openfaas
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
To verify that openfaas has started, run:
kubectl -n openfaas get deployments -l "release=openfaas, app=openfaas"過了一會兒,我們運行建議的命令:
$ kubectl -n openfaas get deployments -l "release=openfaas, app=openfaas"
NAME READY UP-TO-DATE AVAILABLE AGE
alertmanager 1/1 1 1 114s
basic-auth-plugin 1/1 1 1 114s
faas-idler 1/1 1 1 114s
gateway 1/1 1 1 114s
nats 1/1 1 1 114s
prometheus 1/1 1 1 114s
queue-worker 1/1 1 1 114s我們檢查功能:
$ kubectl rollout status -n openfaas deploy/gateway
deployment "gateway" successfully rolled out
$ kubectl port-forward -n openfaas svc/gateway 8080:8080 &
[1] 6985
Forwarding from 127.0.0.1:8080 -> 8080
$ echo -n $PASSWORD | faas-cli login --username admin --password-stdin
Calling the OpenFaaS server to validate the credentials...
Handling connection for 8080
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
credentials saved for admin http://127.0.0.1:8080
$ faas-cli list
Function Invocations Replicas安裝 MongoDB
我們使用 helm 安裝所有內容:
$ helm repo add stable https://kubernetes-charts.storage.googleapis.com/
"stable" has been added to your repositories
$ helm install stable/mongodb --generate-name
NAME: mongodb-1577466908
LAST DEPLOYED: Fri Dec 25 11:15:11 2019
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
** Please be patient while the chart is being deployed **
MongoDB can be accessed via port 27017 on the following DNS name from within your cluster:
mongodb-1577466908.default.svc.cluster.local
To get the root password run:
export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace default mongodb-1577466908 -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)
To connect to your database run the following command:
kubectl run --namespace default mongodb-1577466908-client --rm --tty -i --restart='Never' --image bitnami/mongodb --command -- mongo admin --host mongodb-1577466908 --authenticationDatabase admin -u root -p $MONGODB_ROOT_PASSWORD
To connect to your database from outside the cluster execute the following commands:
kubectl port-forward --namespace default svc/mongodb-1577466908 27017:27017 &
mongo --host 127.0.0.1 --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD我們檢查:
kubectl run --namespace default mongodb-1577466908-client --rm --tty -i --restart='Never' --image bitnami/mongodb --command -- mongo admin --host mongodb-1577466908 --authenticationDatabase admin -u root -p $(kubectl get secret --namespace default mongodb-1577466908 -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)
If you don't see a command prompt, try pressing enter.
> db.version();
4.0.14按 ctrl+D 退出容器。
設定 emacs
基本上一切都已經設定好了 ,所以我就不細說了。
將遊戲分解成功能
與函數的交互透過 http 協定進行,不同函數之間的端對端身份驗證由 JWT 提供。 MongoDB 用於儲存令牌以及遊戲狀態、玩家資料、所有遊戲的移動序列和其他資訊。讓我們仔細看看其中最有趣的函數。
註冊
此函數的輸入是包含遊戲暱稱和密碼的 JSON 資料。呼叫此函數時,會檢查資料庫中是否存在此暱稱,如果檢查成功,則將暱稱和密碼雜湊值插入資料庫。需要註冊才能參與遊戲。
入口
此函數的輸入是 JSON,包含遊戲暱稱和密碼;如果暱稱在資料庫中,且密碼與先前儲存在資料庫中的密碼核對成功,則傳回 JWT,呼叫其他函數時必須傳遞該 JWT。各種服務記錄也會插入到資料庫中,例如上次登入的時間等。
查看遊戲列表
任何未經授權的用戶都可以要求除活躍遊戲之外的所有遊戲清單。授權用戶也可以看到活躍遊戲清單。此函數的傳回結果為 JSON 格式,其中包含遊戲清單(遊戲 ID、使用者可讀的名稱等)。
遊戲創作
此函數僅適用於授權用戶,輸入時會接受最大玩家數量以及遊戲參數(例如,遊戲中要啟動的角色、最大玩家數量等)。一個單獨的遊戲參數是加入密碼,該密碼允許您創建非公開遊戲。預設情況下,將創建一個公開遊戲。此函數的傳回值為 JSON,其中包含建立成功的欄位、唯一的遊戲識別碼和其他參數。
加入遊戲
此函數僅適用於授權使用者。輸入為遊戲 ID 及其密碼(如果是私人遊戲),輸出為包含遊戲參數的 JSON。加入遊戲的授權使用者以及遊戲的創建者在下文中稱為遊戲參與者。
查看遊戲事件
任何未經授權的用戶都可以要求非活躍遊戲的事件列表,而授權用戶可以獲得任何活躍遊戲的事件列表。此函數的附加參數可以是使用者已有的事件編號。在這種情況下,只有後續發生的事件才會回到清單中。透過定期運行此函數,授權使用者可以查看遊戲中正在發生的事情。此函數也會傳回操作請求,使用者可以使用遊戲事件發送函數來回應該操作。
發送遊戲事件
該功能僅適用於遊戲參與者:它允許您開始遊戲、採取行動、投票、編寫顯示在遊戲事件列表中的文字訊息等。
創建遊戲的授權使用者開始向所有遊戲參與者(包括自己)分配角色,所有參與者必須使用相同的功能來確認自己的角色。所有角色確認後,遊戲將自動切換到夜間模式。
遊戲統計
此功能僅對遊戲參與者有效,顯示遊戲狀態、玩家清單和數量(假名)、角色及其狀態(是否已移動)以及其他資訊。與上一個功能一樣,所有內容僅對遊戲參與者有效。
定期觸發的功能
如果遊戲在創建遊戲時指定的一段時間內尚未啟動,則將使用清理功能自動將其從活躍遊戲清單中刪除。
另一個定期任務是強制將遊戲模式從夜晚切換到白天,對於在某一回合中沒有這樣做的遊戲(例如,需要對遊戲事件做出反應的玩家由於某種原因沒有發送他們的決定),再切換回來。
公告
- 設定開發環境,將任務分解為功能
- 後端工作
- 前端工作
- 設定CICD,組織測試
- 啟動試玩遊戲
- 結果
來源: www.habr.com
