Skip to content

Commit

Permalink
OKG add doc of ID awareness (#159)
Browse files Browse the repository at this point in the history
Signed-off-by: ChrisLiu <[email protected]>
  • Loading branch information
chrisliu1995 authored Jan 19, 2024
1 parent 42fe1d5 commit b89972b
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 部署游戏服
## 功能概述

## "Hello World" of OKG
您可以使用GameServerSet进行游戏服的部署,一个简单的部署案例如下:

```bash
Expand Down Expand Up @@ -30,14 +31,71 @@ NAME AGE
minecraft 9s

kubectl get gs
NAME STATE OPSSTATE DP UP
minecraft-0 Ready None 0 0
minecraft-1 Ready None 0 0
minecraft-2 Ready None 0 0
NAME STATE OPSSTATE DP UP AGE
minecraft-0 Ready None 0 0 10s
minecraft-1 Ready None 0 0 10s
minecraft-2 Ready None 0 0 10s

kubectl get pod
NAME READY STATUS RESTARTS AGE
minecraft-0 1/1 Running 0 10s
minecraft-1 1/1 Running 0 10s
minecraft-2 1/1 Running 0 10s
```
```

## ID业务感知

游戏服由于有状态的特性,通常需要唯一标识来区别彼此,这也是GameServerSet管理的GameServer的名称会以ID序号结尾的原因。

在有些情况下,游戏服业务自身需要感知到自己的ID,以作为区服属性标志或配置管理的依据等。
这个时候,通过 DownwardAPI 可以实现将对应标识ID下沉至容器中。下面是一个示例:

部署一个GameServerSet,其生成的游戏服容器中的环境变量GS_NAME会以对应名称为值:

```yaml
apiVersion: game.kruise.io/v1alpha1
kind: GameServerSet
metadata:
name: minecraft
namespace: default
spec:
replicas: 3
updateStrategy:
rollingUpdate:
podUpdatePolicy: InPlaceIfPossible
gameServerTemplate:
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/acs/minecraft-demo:1.12.2
name: minecraft
env:
- name: GS_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
```
同样会生成3个游戏服:
```bash
kubectl get gs
NAME STATE OPSSTATE DP UP AGE
minecraft-0 Ready None 0 0 31s
minecraft-1 Ready None 0 0 31s
minecraft-2 Ready None 0 0 31s
```

分别查看这三个游戏服的GS_NAME环境变量,发现三个游戏服的GS_NAME与自身的名称一一对应。

```bash
kubectl exec minecraft-0 -- env | grep GS_NAME
GS_NAME=minecraft-0

kubectl exec minecraft-1 -- env | grep GS_NAME
GS_NAME=minecraft-1

kubectl exec minecraft-2 -- env | grep GS_NAME
GS_NAME=minecraft-2
```

这样一来,业务程序在启动时可以通过解析GS_NAME环境变量来进行配置管理等操作了。
68 changes: 64 additions & 4 deletions kruisegame/user-manuals/deploy-gameservers.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Deploy Gameservers

## “Hello World” of OKG
You can use GameServerSet to deploy game servers. A simple deployment case is as follows:

```bash
Expand Down Expand Up @@ -29,14 +31,72 @@ NAME AGE
minecraft 9s

kubectl get gs
NAME STATE OPSSTATE DP UP
minecraft-0 Ready None 0 0
minecraft-1 Ready None 0 0
minecraft-2 Ready None 0 0
NAME STATE OPSSTATE DP UP AGE
minecraft-0 Ready None 0 0 10s
minecraft-1 Ready None 0 0 10s
minecraft-2 Ready None 0 0 10s

kubectl get pod
NAME READY STATUS RESTARTS AGE
minecraft-0 1/1 Running 0 10s
minecraft-1 1/1 Running 0 10s
minecraft-2 1/1 Running 0 10s
```


## ID awareness

Due to the stateful nature of game servers, they usually need unique identifiers to distinguish each other. This is why the name of the GameServer managed by GameServerSet ends with an ID number.

In some cases, the game server business itself needs to be aware of its own ID, which can be used as a regional server attribute mark or a basis for configuration management.
At this time, the corresponding identification ID can be sunk into the container through DownwardAPI. Here's an example:

Deploy a GameServerSet, and the environment variable GS_NAME in the generated game server container will have the corresponding name as its value:

```yaml
apiVersion: game.kruise.io/v1alpha1
kind: GameServerSet
metadata:
name: minecraft
namespace: default
spec:
replicas: 3
updateStrategy:
rollingUpdate:
podUpdatePolicy: InPlaceIfPossible
gameServerTemplate:
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/acs/minecraft-demo:1.12.2
name: minecraft
env:
- name: GS_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
```
3 game servers will be generated:
```bash
kubectl get gs
NAME STATE OPSSTATE DP UP AGE
minecraft-0 Ready None 0 0 31s
minecraft-1 Ready None 0 0 31s
minecraft-2 Ready None 0 0 31s
```

Check the GS_NAME environment variables of these three game servers separately and find that the GS_NAME of the three game servers corresponds to their own names.

```bash
kubectl exec minecraft-0 -- env | grep GS_NAME
GS_NAME=minecraft-0

kubectl exec minecraft-1 -- env | grep GS_NAME
GS_NAME=minecraft-1

kubectl exec minecraft-2 -- env | grep GS_NAME
GS_NAME=minecraft-2
```

In this way, the game program can perform configuration management and other operations by parsing the GS_NAME environment variable when it is started.

0 comments on commit b89972b

Please sign in to comment.