「新史詩」。 我們把大象分成幾部分吃

「新史詩」。 我們把大象分成幾部分吃

在本文中,我將建立一個開發遊戲「Epics」的工作環境,並將遊戲本身分解成適合在 OpenFaaS 中使用的部分。我將在 Linux 上完成所有操作,我將使用 VirtualBox 在 minikube 中部署 Kubernetes。我的工作機器有2個處理器核心和12GB RAM;我使用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 就足夠了,所以將它和 kubelet 安裝在 /usr/local/bin 中,並安裝 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

開發人員建議創建 2 個命名空間來使用:

$ 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 機密:

$ 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,在其他函數呼叫時必須將其傳遞給其他函數叫。各種服務記錄也被插入到資料庫中,例如上次登入時間等。

查看遊戲列表

任何未經授權的用戶都可以請求除活動遊戲之外的所有遊戲的清單。授權使用者也可以看到活動遊戲的清單。此函數的結果是包含遊戲清單(遊戲 ID、人類可讀名稱等)的 JSON。

遊戲創作

此功能僅適用於授權使用者;輸入時接受最大玩家數量以及遊戲參數(例如,在該遊戲中啟動哪些角色、最大玩家數量等)。遊戲的一個單獨參數是加入密碼的存在,它允許您創建非公開遊戲。預設情況下,會創建一個公共遊戲。該函數的結果是JSON,其中包含建立成功欄位、唯一遊戲識別碼和其他參數。

加入遊戲

此函數僅適用於授權用戶,輸入是遊戲ID和密碼,如果這是非公開遊戲,則輸出是帶有遊戲參數的JSON。加入遊戲的授權使用者以及遊戲的創建者在下文中被稱為遊戲參與者。

查看遊戲事件

任何未經授權的使用者都可以要求不活動遊戲的事件列表,而授權使用者可以接收任何活動遊戲的事件列表。此函數的附加參數可以是使用者已有的事件編號。在這種情況下,清單中只會傳回後來發生的事件。透過定期啟動此功能,授權使用者可以觀察遊戲中發生的情況。該函數還傳回一個操作請求,使用者可以使用遊戲的事件調度函數來回應該請求。

發送遊戲事件

此功能僅適用於遊戲參與者:可以開始遊戲、採取行動、投票、編寫顯示在遊戲事件清單中的簡訊等。
創建遊戲的授權使用者開始將角色分配給遊戲中的所有參與者,包括自己,他們必須使用相同的功能來確認自己的角色。一旦所有角色都被確認,遊戲會自動切換到夜間模式。

遊戲統計

此功能僅適用於遊戲參與者;它顯示遊戲狀態、玩家清單和數量(暱稱)、角色及其狀態(已進行或未進行)以及其他資訊。與先前的功能一樣,一切僅適用於遊戲參與者。

定期推出功能

如果遊戲在創建遊戲時指定的一段時間內沒有啟動,則會使用清除功能自動將其從活動遊戲清單中刪除。

另一個週期性任務是強制將遊戲模式從夜間切換到白天,並在回合期間沒有發生這種情況的遊戲(例如,需要對遊戲事件做出反應的玩家由於某種原因沒有發送他的解決方案) )。

公告

  • 介紹
  • 設定開發環境,將任務分解為功能
  • 後端工作
  • 前端工作
  • 建立CICD,組織測試
  • 開始試玩遊戲
  • 結果

來源: www.habr.com

添加評論