本页面介绍了如何在部署中注册、编辑、列出和查看 Game Servers 配置 的详细信息。
此配置定义了 Agones 队列规范,其中包括游戏服务器计数、缓冲区和游戏服务器版本的相关信息。此配置还定义了扩缩配置 ,用于指定队列自动扩缩以满足需求的方式。
准备工作
开始之前,我们建议您先了解 Game Servers 概览 中的关键概念。请确保您已执行以下任务:
确保您已启用 Game Services API。
启用 Game Services API
选择安装了 gcloud CLI 的 shell,或者使用 API 客户端:
Cloud Shell 如需启动 Cloud Shell,请执行以下步骤:
转到 Google Cloud Console。
Google Cloud Console
在控制台的右上角,点击激活 Cloud Shell 按钮:
控制台下方的框架内会打开一个 Cloud Shell 会话。您可以使用此 shell 运行 gcloud
命令。
本地 shell 安装 gcloud CLI 。
确认您已为 Google Cloud CLI 设置所需的默认项目(否则,您稍后需要为每个命令明确指定 --project
标志):
gcloud config list project
如果无法运行以下命令来设置默认项目,请将 PROJECT_ID
替换为所需的项目 ID:
gcloud config set project PROJECT_ID
运行以下命令以验证 Google Cloud CLI 的版本。Game Servers 需要 306.0.0
或更高版本的 gcloud CLI。
gcloud version
如需更新安装,请运行以下命令:
gcloud components update
curl / PowerShell 如需将 REST API 与 curl
或 Windows PowerShell 搭配使用,请执行以下操作:
创建服务帐号。
以 JSON 文件的格式下载私钥。
将环境变量 GOOGLE_APPLICATION_CREDENTIALS
设置为包含您的服务帐号密钥的 JSON 文件的路径。
此变量仅适用于当前的 shell 会话,因此,如果您打开新的会话,请重新设置该变量。
示例 :Linux 或 macOS
export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH
"
将 KEY_PATH
替换为包含您的服务帐号密钥的 JSON 文件的路径。
例如:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
示例 :Windows
对于 PowerShell:
$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH
"
将 KEY_PATH
替换为包含您的服务帐号密钥的 JSON 文件的路径。
例如:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
对于命令提示符:
set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH
将 KEY_PATH
替换为包含您的服务帐号密钥的 JSON 文件的路径。
客户端库
您可以使用客户端库以编程方式控制 Google Cloud Game Servers。如需了解库和身份验证的使用说明,请参阅客户端库概览 。
创建配置
确保您已创建 Game Servers 部署 。
控制台
在 Cloud Console 中,转到游戏服务器部署 页面。
转到 Game Servers
在表格中查找部署。在表格的最后一列中,点击省略号 more_vert 并选择列出配置 。您可以通过此页面查看部署的活跃配置和无效配置。
要为部署创建新配置,请点击创建配置 。
在配置名称 框中,输入此配置的唯一标识符。
(可选)在说明 框中,输入此配置的说明。
在队列配置名称 框中,输入此队列配置的唯一标识符。
在队列规范 框中,输入队列规范 。
(可选)在扩缩配置 下,您可以指定一个或多个会自动纵向扩容或纵向缩容的配置。点击添加 以添加扩缩配置。
在扩缩配置名称 框中,输入此扩缩配置的唯一标识符。
(可选)在开始时间 和结束时间 框中,安排此扩缩配置将处于活跃状态的时间段。如需了解详情,请参阅创建扩缩时间表 。
在 Autoscape 规范 框中,输入扩缩规范 。
gcloud
您可以使用 Google Cloud CLI 在部署中创建 Game Servers 配置。
如需创建游戏服务器配置,请执行以下操作:
将此队列规范复制到一个文件中。
- name: fleet-spec-1
fleetSpec:
replicas: 10
scheduling: Packed
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
template:
metadata:
labels:
gameName: udp-server
spec:
ports:
- name: default
portPolicy: Dynamic
containerPort: 7654
protocol: UDP
health:
initialDelaySeconds: 30
periodSeconds: 60
sdkServer:
logLevel: Info
grpcPort: 9357
httpPort: 9358
template:
spec:
containers:
- name: dedicated
image: gcr.io/agones-images/udp-server:0.21
imagePullPolicy: Always
resources:
requests:
memory: 200Mi
cpu: 500m
limits:
memory: 200Mi
cpu: 500m
运行以下命令并将占位符值(例如 deploymentID 和 configID )替换为适当的值:
gcloud game servers configs create configID --deployment deploymentID --fleet-configs-file fleetSpecFile
变量占位符对应于以下说明:
configID 是您可以为此配置指定的唯一标识符。
deploymentID 是部署的唯一标识符。
fleetSpecFile 是具有 Agones 队列规范的文件的路径。
REST 和命令行
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID :IAM 设置 中列出的 Google Cloud 项目 ID
DEPLOYMENT_ID :用户定义的部署标识符
CONFIG_ID :用户定义的配置标识符
请求 JSON 正文:
{
"fleetConfigs": [
{
"name": "fleet-spec-1",
"fleetSpec": "{
\"replicas\": 10,
\"scheduling\": \"Packed\",
\"template\": {
\"metadata\": {
\"labels\": {
\"gameName\": \"udp-server\"
}
},
\"spec\": {
\"ports\": [
{
\"name\": \"default\",
\"portPolicy\": \"Dynamic\",
\"containerPort\": 7654,
\"protocol\": \"UDP\"
}
],
\"health\": {
\"periodSeconds\": 60,
\"initialDelaySeconds\": 30
},
\"sdkServer\": {
\"logLevel\": \"Info\",
\"grpcPort\": 9357,
\"httpPort\": 9358
},
\"template\": {
\"spec\": {
\"containers\": [
{
\"name\": \"dedicated\",
\"image\": \"gcr.io/agones-images/udp-server:0.21\",
\"imagePullPolicy\": \"Always\",
\"resources\": {
\"requests\": {
\"memory\": \"200Mi\",
\"cpu\": \"500m\"
},
\"limits\": {
\"memory\": \"200Mi\",
\"cpu\": \"500m\"
}
}
}
]
}
}
}
}
}"
}
]
}
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json
的文件中。复制以下命令,并在终端中运行它以创建此文件。注意 :以下命令将覆盖当前目录中任何现有的
request.json
文件。cat > request.json << 'EOF'
{
"fleetConfigs": [
{
"name": "fleet-spec-1",
"fleetSpec": "{
\"replicas\": 10,
\"scheduling\": \"Packed\",
\"template\": {
\"metadata\": {
\"labels\": {
\"gameName\": \"udp-server\"
}
},
\"spec\": {
\"ports\": [
{
\"name\": \"default\",
\"portPolicy\": \"Dynamic\",
\"containerPort\": 7654,
\"protocol\": \"UDP\"
}
],
\"health\": {
\"periodSeconds\": 60,
\"initialDelaySeconds\": 30
},
\"sdkServer\": {
\"logLevel\": \"Info\",
\"grpcPort\": 9357,
\"httpPort\": 9358
},
\"template\": {
\"spec\": {
\"containers\": [
{
\"name\": \"dedicated\",
\"image\": \"gcr.io/agones-images/udp-server:0.21\",
\"imagePullPolicy\": \"Always\",
\"resources\": {
\"requests\": {
\"memory\": \"200Mi\",
\"cpu\": \"500m\"
},
\"limits\": {
\"memory\": \"200Mi\",
\"cpu\": \"500m\"
}
}
}
]
}
}
}
}
}"
}
]
}
EOF
在终端运行以下命令。它引用您刚刚创建的 request.json
文件。
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://gameservices.googleapis.com/v1/projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs?configId=CONFIG_ID "
PowerShell (Windows)
将请求正文保存在名为 request.json
的文件中。复制以下命令,并在终端中运行它以创建此文件。注意 :以下命令将覆盖当前目录中任何现有的
request.json
文件。@'
{
"fleetConfigs": [
{
"name": "fleet-spec-1",
"fleetSpec": "{
\"replicas\": 10,
\"scheduling\": \"Packed\",
\"template\": {
\"metadata\": {
\"labels\": {
\"gameName\": \"udp-server\"
}
},
\"spec\": {
\"ports\": [
{
\"name\": \"default\",
\"portPolicy\": \"Dynamic\",
\"containerPort\": 7654,
\"protocol\": \"UDP\"
}
],
\"health\": {
\"periodSeconds\": 60,
\"initialDelaySeconds\": 30
},
\"sdkServer\": {
\"logLevel\": \"Info\",
\"grpcPort\": 9357,
\"httpPort\": 9358
},
\"template\": {
\"spec\": {
\"containers\": [
{
\"name\": \"dedicated\",
\"image\": \"gcr.io/agones-images/udp-server:0.21\",
\"imagePullPolicy\": \"Always\",
\"resources\": {
\"requests\": {
\"memory\": \"200Mi\",
\"cpu\": \"500m\"
},
\"limits\": {
\"memory\": \"200Mi\",
\"cpu\": \"500m\"
}
}
}
]
}
}
}
}
}"
}
]
}
'@ | Out-File -FilePath request.json -Encoding utf8
在终端运行以下命令。它引用您刚刚创建的 request.json
文件。$cred = gcloud auth application-default print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -InFile request.json ` -Uri "https://gameservices.googleapis.com/v1/projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs?configId=CONFIG_ID " | Select-Object -Expand Content
您应该会收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_ID /locations/global/operations/OPERATION_ID ",
"metadata": {
"@type": "type.googleapis.com/google.cloud.gaming.v1.OperationMetadata",
"createTime": CREATE_TIME ,
"target": "projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs/CONFIG_ID ",
"verb": "create",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
C#
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库 。
using Google.Cloud.Gaming.V1;
using Google.LongRunning;
using Newtonsoft.Json;
using System.Threading.Tasks;
public class CreateConfigSample
{
public async Task<GameServerConfig> CreateConfigAsync(
string projectId, string regionId, string deploymentId, string configId)
{
// Create the client.
GameServerConfigsServiceClient client = await GameServerConfigsServiceClient.CreateAsync();
GameServerConfig config = new GameServerConfig
{
GameServerConfigName = GameServerConfigName.FromProjectLocationDeploymentConfig(projectId, regionId, deploymentId, configId),
Description = "My Game Server Config",
FleetConfigs =
{
new FleetConfig
{
Name = "fleet-spec-1",
FleetSpec = JsonConvert.SerializeObject(new
{
replicas = 10,
scheduling = "Packed",
strategy = new
{
type = "RollingUpdate",
rollingUpdate = new
{
maxSurge = "25%",
maxUnavailable = "25%",
}
},
template = new
{
metadata = new
{
labels = new
{
gameName = "udp-server",
}
},
spec = new
{
ports = new [] {
new {
name = "default",
portPolicy = "Dynamic",
containerPort = 7654,
protocol = "UDP",
}
},
health = new
{
initialDelaySeconds = 30,
periodSeconds = 60,
},
sdkServer = new
{
logLevel = "Info",
grpcPort = 9357,
httpPort = 9358,
},
template = new
{
spec = new
{
containers = new [] {
new {
name = "dedicated",
image = "gcr.io/agones-images/udp-server:0.21",
imagePullPolicy = "Always",
resources = new
{
requests = new
{
memory = "200Mi",
cpu = "500m",
},
limits = new
{
memory = "200Mi",
cpu = "500m",
}
}
}
}
}
}
}
}
})
}
}
};
CreateGameServerConfigRequest request = new CreateGameServerConfigRequest
{
ParentAsGameServerDeploymentName = GameServerDeploymentName.FromProjectLocationDeployment(projectId, regionId, deploymentId),
ConfigId = configId,
GameServerConfig = config
};
// Make the request.
Operation<GameServerConfig, OperationMetadata> response = await client.CreateGameServerConfigAsync(request);
// Poll until the returned long-running operation is complete.
Operation<GameServerConfig, OperationMetadata> completedResponse = await response.PollUntilCompletedAsync();
// Retrieve the operation result.
return completedResponse.Result;
}
}
Go
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库 。
import (
"context"
"fmt"
"io"
gaming "cloud.google.com/go/gaming/apiv1"
gamingpb "google.golang.org/genproto/googleapis/cloud/gaming/v1"
)
// fleet is the spec portion of an agones Fleet. It must be in JSON format.
// See https://agones.dev/site/docs/reference/fleet/ for more on fleets.
const fleet = `
{
"replicas": 10,
"scheduling": "Packed",
"strategy": {
"type": "RollingUpdate",
"rollingUpdate": {
"maxSurge": "25%",
"maxUnavailable": "25%"
}
},
"template": {
"metadata": {
"labels": {
"gameName": "udp-server"
}
},
"spec": {
"ports": [
{
"name": "default",
"portPolicy": "Dynamic",
"containerPort": 2156,
"protocol": "TCP"
}
],
"health": {
"initialDelaySeconds": 30,
"periodSeconds": 60
},
"sdkServer": {
"logLevel": "Info",
"grpcPort": 9357,
"httpPort": 9358
},
"template": {
"spec": {
"containers": [
{
"name": "dedicated",
"image": "gcr.io/agones-images/udp-server:0.17",
"imagePullPolicy": "Always",
"resources": {
"requests": {
"memory": "200Mi",
"cpu": "500m"
},
"limits": {
"memory": "200Mi",
"cpu": "500m"
}
}
}
]
}
}
}
}
}
`
// createGameServerConfig creates a game server config.
func createGameServerConfig(w io.Writer, projectID, deploymentID, configID string) error {
// projectID := "my-project"
// deploymentID := "mydeployment"
// configID := "mydeployment"
ctx := context.Background()
client, err := gaming.NewGameServerConfigsClient(ctx)
if err != nil {
return fmt.Errorf("NewGameServerConfigsClient: %v", err)
}
defer client.Close()
req := &gamingpb.CreateGameServerConfigRequest{
Parent: fmt.Sprintf("projects/%s/locations/global/gameServerDeployments/%s", projectID, deploymentID),
ConfigId: configID,
GameServerConfig: &gamingpb.GameServerConfig{
FleetConfigs: []*gamingpb.FleetConfig{
{
Name: "fleet-spec-1",
FleetSpec: fleet,
},
},
Description: "My Game Server Config",
},
}
op, err := client.CreateGameServerConfig(ctx, req)
if err != nil {
return fmt.Errorf("CreateGameServerConfig: %v", err)
}
resp, err := op.Wait(ctx)
if err != nil {
return fmt.Errorf("Wait: %v", err)
}
fmt.Fprintf(w, "Config created: %v", resp.Name)
return nil
}
编辑配置
您无法编辑 Game Servers 配置,因为配置不可变。如果要编辑配置,请复制配置(查看配置详情 ),在本地修改配置,然后使用更新后的版本来创建新配置。
列出配置
控制台
在 Cloud Console 中,转到游戏服务器部署 页面。
转到 Game Servers
在表格中查找部署。在表格的最后一列中,点击省略号 more_vert 并选择列出配置 。您可以通过此页面查看部署的活跃配置和无效配置。
gcloud
您可以使用 Google Cloud CLI 列出配置。
如需列出所有配置,请运行以下命令:
gcloud game servers configs list
如需列出部署中的所有配置,请运行以下命令并将 deploymentID 替换为要在其中列出配置的父级部署 :
gcloud game servers configs list --deployment=deploymentID
REST 和命令行
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID :IAM 设置 中列出的 Google Cloud 项目 ID
DEPLOYMENT_ID :用户定义的部署标识符
根据您发送的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ "https://gameservices.googleapis.com/v1/projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs"
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth application-default print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method GET ` -Headers $headers ` -Uri "https://gameservices.googleapis.com/v1/projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs" | Select-Object -Expand Content
您应该会收到类似以下内容的 JSON 响应:
{
"gameServerConfigs": [
{
"name": "projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs/MY-CONFIG-1 ",
"createTime": CREATE_TIME ,
"updateTime": UPDATE_TIME ,
"fleetConfigs": [
{
"fleetSpec": ...
"name": "MY-CONFIG-1 "
}
],
"description": "Config 1"
},
{
"name": "projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs/MY-CONFIG-2 ",
"createTime": CREATE_TIME ,
"updateTime": UPDATE_TIME ,
"fleetConfigs": [
{
"fleetSpec": ...
"name": "MY-CONFIG-2 "
}
],
"description": "Config 2"
},
]
}
查看有关配置的详细信息
控制台
在 Cloud Console 中,转到游戏服务器部署 页面。
转到 Game Servers
在表格中查找部署。在表格的最后一列中,点击省略号 more_vert 并选择列出配置 。您可以通过此页面查看部署的活跃配置和无效配置。
在表格中查找配置。在表格的最后一列中,点击省略号 more_vert 并选择查看配置 。
gcloud
您可以使用 Google Cloud CLI 查找有关配置的详细信息,例如创建时间:
如需查看配置的详细信息,请运行以下命令,并将占位符值(如 deploymentID 和 configID )替换为适当的值:
gcloud game servers configs describe configID --deployment deploymentID
输出会为您提供配置的详细信息。
变量占位符对应于以下说明:
configID 是您为游戏服务器配置指定的唯一标识符。
deploymentID 是父级部署的唯一标识符。
REST 和命令行
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID :IAM 设置 中列出的 Google Cloud 项目 ID
DEPLOYMENT_ID :用户定义的部署标识符
CONFIG_ID :用户定义的配置标识符
根据您发送的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ "https://gameservices.googleapis.com/v1/projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs/CONFIG_ID "
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth application-default print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method GET ` -Headers $headers ` -Uri "https://gameservices.googleapis.com/v1/projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs/CONFIG_ID " | Select-Object -Expand Content
您应该会收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_ID /locations/global/gameServerDeployments/DEPLOYMENT_ID /configs/CONFIG_ID ",
"createTime": CREATE_TIME ,
"updateTime": UPDATE_TIME ,
"fleetConfigs": [
{
"fleetSpec": ...
"name": "CONFIG_ID "
}
],
"description": "My config"
}
后续步骤