“新史诗”。 我们部分吃掉大象


在本文中,我将为“ Epics”游戏的开发建立一个工作环境,并将该游戏分成适合在OpenFaaS中使用的部分。 我将在Linux上进行所有操作,Kubernetes将使用VirtualBox部署在minikube中。 在我的工作机中,有2个处理器核心和12GB RAM,我将SSD用作系统磁盘。 我将使用debian 8作为主要开发系统,并安装了emacs,sudo,git和virtualbox软件包,其余的将通过从GitHub和其他来源下载进行安装。 除非另有说明,否则我们将在/ usr / local / bin中安装这些应用程序。 让我们开始吧!


准备工作环境


去安装


请遵循官方网站上的说明:


$ 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, world\n") }' > 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 repo add openfaas https://openfaas.imtqy.com/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 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,其中包含游戏列表(游戏标识符,人类可读的名称等)。


游戏创作


该功能仅适用于授权用户,最大输入人数,以及游戏参数(例如,在此游戏中要激活的角色,最大玩家数等)在输入处被接受。 游戏的一个单独参数是是否存在加入密码,该密码使您可以创建非公开游戏。 默认情况下,会创建一个公共游戏。 函数执行的结果是JSON,其中包含创建成功字段,游戏的唯一标识符以及其他参数。


加入游戏


该功能仅适用于授权用户,在入口处-游戏的标识符及其密码(如果是非公开游戏),在输出处-带有游戏参数的JSON。 然后,将已加入游戏的授权用户以及游戏的创建者称为游戏的参与者。


查看游戏事件


任何未经授权的用户都可以请求不活动游戏的事件列表,而经过授权的用户可以获取事件和任何活动的游戏的列表。 该功能的附加参数可以是用户已经拥有的事件号。 在这种情况下,只有那些稍后发生的事件才会在列表中返回。 通过定期启动此功能,授权用户可以观看游戏中发生的事情。 该功能还返回对动作的请求,用户可以使用发送游戏事件的功能来响应该动作。


游戏活动派遣


该功能仅适用于游戏参与者:实现了启动游戏,进行移动,投票,编写出现在游戏事件列表中的短信等功能。
创建游戏的授权用户开始将角色分配给游戏中的所有参与者,包括他们自己,他们必须使用相同的功能来确认其角色。 确认所有角色后,游戏将自动进入夜间模式。


游戏统计


该功能仅适用于游戏的参与者,显示游戏的状态,玩家的列表和人数(别名),角色及其状态(完成或不完成)以及其他信息。 至于以前的功能,一切仅适用于游戏参与者。


定期运行功能


如果在创建游戏期间指定的时间内未启动游戏,则将使用清除功能将其自动从活动游戏列表中删除。


另一个定期任务是在转弯期间未发生的游戏中,强制将游戏模式从夜晚切换到白天,然后再切换回来(例如,需要响应游戏事件的玩家出于任何原因均未发送决定)。


公告公告


  • 引言
  • 设置开发环境,将任务划分为功能
  • 在后端工作
  • 前端工作
  • CI \ CD安装,测试组织
  • 开始游戏试用版
  • 总结

Source: https://habr.com/ru/post/zh-CN482752/


All Articles