Agones是为了游戏服在Kubernetes上部署、运行、伸缩而设计的框架。具体相关介绍可参考官方网站和Github。本文介绍了在阿里云ACK上部署并使用Agones的全过程。
Install Agones on ACK
-
首先创建需要的Agones命名空间
$ kubectl create namespace agones-system
-
通过配置文件安装对应版本的Agones,这里我选择的是1.23版本
$ kubectl apply -f https://raw.githubusercontent.com/googleforgames/agones/release-1.23.0/install/yaml/install.yaml
安装完成后,命名空间下有以下资源:
get deploy -n agones-system
NAME READY UP-TO-DATE AVAILABLE AGE
agones-allocator 3/3 3 3 3d
agones-controller 1/1 1 1 3d
agones-ping 2/2 2 2 3d
kubectl get po -n agones-system
NAME READY STATUS RESTARTS AGE
agones-allocator-554c79cffc-6dpzp 1/1 Running 0 3d
agones-allocator-554c79cffc-jlp5t 1/1 Running 0 3d
agones-allocator-554c79cffc-kbn5w 1/1 Running 0 3d
agones-controller-864dd7dbc4-2ghs8 1/1 Running 0 3d
agones-ping-77b7dc578c-9dfk7 1/1 Running 0 3d
agones-ping-77b7dc578c-hhvff 1/1 Running 0 3d
kubectl get svc -n agones-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
agones-allocator LoadBalancer 172.16.157.137 47.105.53.1 443:30302/TCP 3d
agones-allocator-metrics-service ClusterIP 172.16.238.137
8080/TCP 3d
agones-controller-service ClusterIP 172.16.150.98
443/TCP,8080/TCP 3d
agones-ping-http-service LoadBalancer 172.16.253.193 118.190.206.167 80:30770/TCP 3d
agones-ping-udp-service LoadBalancer 172.16.10.63 47.104.240.179 50000:32691/UDP 3d
需要额外注意的是阿里云上无法连接gcr.io,我在节点上提前pull了个人制作的相同镜像,再打上gcr重名tag,绕过了这个问题。
Deploy a game server on Agones
在这里,我们使用Agones官方Github中的Unity示例(path: agones/examples/unity-sample),将项目导入Unity Editor。分别build server与client
使用项目中的Dockerfile制作服务镜像:
FROM ubuntu:18.04
WORKDIR /unity
# Need to build with UnityEditor in advance.
COPY Builds/Server/ ./
# [Workaround] Wait until the sidecar is ready.
CMD sleep 2 && ./UnitySimpleServer.x86_64
将其上传至镜像仓库后我们开始部署game server。部署的方式也很简单,直接apply yaml即可:
apiVersion: "agones.dev/v1"
kind: GameServer
metadata:
name: "unity-simple-server"
spec:
ports:
- name: default
portPolicy: Dynamic
containerPort: 7777
template:
spec:
containers:
- name: unity-simple-server
# 使用刚刚制作的game server镜像
image: xxx:xx
很快,gs将完成部署:
kubectl get gs
NAME STATE ADDRESS PORT NODE AGE
unity-simple-server Ready 120.27.21.131 7050 cn-xxx.192.xx.xx.48 2d
通过ADDRESS和PORT,我们使用客户端来连接game server,看看效果如何:
输入的句子成功在屏幕上显示了,说明游戏服务正常运行中