使用游戏服务器软件准备容器镜像 - Amazon GameLift

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用游戏服务器软件准备容器镜像

本文档适用于公开预览版中的一项功能。本文档随时可能更改。

集装箱是 Amazon 集装 GameLift 箱船队中最基本的组成部分。您的容器包括您的游戏服务器及其依赖项,例如 SDK、软件、目录和文件。

要在容器队列中运行,您的游戏服务器必须在 Linux 上运行,并与服务器 SDK 5.x 集成。

设置你的工作目录

您的工作目录是您存放构建容器映像和定义 Amazon 如何 GameLift 运行容器映像所需的所有文件的地方。

设置您的容器工作目录
  1. 创建您要在其中使用 Amazon GameLift 容器映像的目录。

    例如:

    [~/]$ mkdir -p work/glc/gamebuild && cd work && find . . ./glc ./glc/gamebuild
  2. 克隆亚马逊 GameLift 代理

    例如:

    [~/work]$ git clone https://github.com/aws/amazon-gamelift-agent.git Cloning into 'amazon-gamelift-agent'...
  3. GameLiftAgent 使用 Maven 进行构建

    例如:

    [~/work]$ cd amazon-gamelift-agent

    [~/work/amazon-gamelift-agent]$ mvn clean compile assembly:single && \ mv target ../glc && cd .. && find glc
  4. 添加已与服务器 SDK 5.x 集成、构建并打包成.ZIP文件的游戏服务器。

  5. 将您的.ZIP文件复制到~/work/glc/gamebuild/

如果您没有 SDK 5.x 游戏服务器,则可以下载并使用我们的示例SimpleServer游戏来尝试使用容器舰队。

[~/work]$ curl -o glc/gamebuild/SimpleServer.zip \ 'https://ws-assets-prod-iad-r-iad-ed304a55c2ca1aee.s3.us-east-1.amazonaws.com/086bb355-4fdc-4e63-8ca7-af7cfc45d4f2/AmazonGameLiftSampleServerBinary.zip' && % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 5140k 100 5140k 0 0 12.3M 0 --:--:-- --:--:-- --:--:-- 12.3M glc glc/target glc/target/GameLiftAgent-1.0.jar glc/gamebuild glc/gamebuild/SimpleServer.zip

构建您的容器镜像

您的 Dockerfile 指定了构建容器的环境、软件和说明。

创建你的 Dockerfile
  1. 移至glc子目录。

    [~/work]$ cd glc && find . ./target ./target/GameLiftAgent-1.0.jar ./gamebuild
  2. 创建并打开一个新的 Dockerfile。

    例如:

    [~/work/glc]$ nano Dockerfile
  3. 从以下模板之一复制,然后将内容粘贴到您的 Dockerfile 中。

此模板包含容器在 Amazon GameLift 车队中使用所需的最低限度说明。根据需要修改游戏服务器的内容。

# Base image # ---------- # Add the base image that you want to use over here, # Make sure to use an image with the same architecture as the # Instance type you are planning to use on your fleets. # We require JDK to be installed in the base image, so that # it can be used to run the &AGS; Agent FROM public.ecr.aws/amazoncorretto/amazoncorretto:17-amd64 # # Game build directory # -------------------- # Add your game build to gamebuild directory and add the zip file name in the 'GAME_BUILD_ZIP' env variable below. # The game build provided over here needs to be integrated with gamelift server sdk. # This template assumes that the game build is in a zip format. ENV GAME_BUILD_ZIP="<ADD_GAME_BUILD_ZIP_FILE_NAME>" \ # # Default directory # ----------------- # Default directory, the value provided here should be where the game executable exists. # Provide this same value as your launch path in RuntimeConfiguration when creating a fleet. # Ref: https://docs.aws.amazon.com/gamelift/latest/apireference/API_ServerProcess.html GAME_EXECUTABLE="<ADD NAME OF EXECUTABLE WITHIN THE GAME BUILD>" \ HOME_DIR="/local/game" \ # # Registered compute in anywhere fleet (not used in container fleets) # ------------------------------------------------------------------- # Add the name for the registered compute in an anywhere fleet. # This environment variable is required only for anywhere fleets, but not for container fleets. # If it is set for container fleets, it will be overridden by Gamelift. GAMELIFT_COMPUTE_NAME="<ADD_COMPUTE_NAME>" \ # # Default Gamelift Agent jar # -------------------------- GAMELIFT_AGENT_EXEC="GameLiftAgent-1.0.jar" \ # # This env variable defines the name of the S3 bucket that stores the GameLift Agent logs. # This S3 bucket should exist in the customer AWS account. # In order to allow GameLift agent to upload logs to this s3 bucket, customers would need to # include s3:PutObject permission in the IAM role provided as instanceRoleArn during CreateFleet operation. GAMELIFT_AGENT_LOGS_BUCKET_NAME="<ADD NAME OF GAMELIFT AGENT LOGS S3 BUCKET>" \ # # ----------- # This env variable defines the name of the S3 bucket that stores the game session logs. # This S3 bucket should exist in the customer AWS account. # In order to allow GameLift agent to upload logs to this s3 bucket, customers would need to # include s3:PutObject permission in the IAM role provided as instanceRoleArn during CreateFleet operation. # ----------- GAME_SESSION_LOGS_BUCKET_NAME="<ADD NAME OF GAME SESSION LOGS S3 BUCKET>" \ # # ----------- GAMELIFT_AGENT_LOGS_PATH="/local/game/agentlogs/" \ # # NOT USED in container fleets - USED in Anywhere fleets # ---------------------------------------------------------- # Specifiy the type of compute resource used to host the game servers. # This env variable is required only for anywhere fleets, but not for container fleets. # If it is set for container fleets, it will be overridden by Gamelift. # # ----------- COMPUTE_TYPE="ANYWHERE" \ # # Specify the credential to be used for creating the client. # This env variable is required only for anywhere fleets, but not for container fleets. # If it is set for container fleets, it will be overridden by Gamelift. # # ----------- CREDENTIAL_PROVIDER="environment-variable" USER root # intall dependencies as necessary RUN yum install -y sudo \ unzip \ git \ shadow-utils \ iputils \ tar \ gcc \ make \ openssl-devel \ zlib-devel \ vim \ net-tools \ nc \ procps # Set up the ground for 'gamescale' user RUN groupadd -r gamescale -g 500 && \ useradd -u 500 -r -g gamescale -m -s /sbin/nologin -c "Gamescale user" gamescale && \ echo "gamescale ALL=(ALL) NOPASSWD: ALL" | (EDITOR="tee -a" visudo) && \ mkdir -p $HOME_DIR && \ mkdir $HOME_DIR/mono && \ chown -R gamescale:gamescale $HOME_DIR WORKDIR $HOME_DIR # extract game build as necessary COPY ./gamebuild/$GAME_BUILD_ZIP . RUN unzip ./$GAME_BUILD_ZIP -d ./ # copy Gamelift Agent jar COPY ./gameliftAgent/$GAMELIFT_AGENT_EXEC ./ # Add permissions to game build and gamelift agent jar RUN chmod +x ./$GAME_EXECUTABLE RUN chmod +x ./$GAMELIFT_AGENT_EXEC # Check if java is installed on the image, if not then the Agent will not be able to run RUN java --version USER gamescale ENV PATH="$PATH:$HOME_DIR/bin:$JAVA_HOME" # Change directory to bin WORKDIR $HOME_DIR # check path before starting the container RUN echo $PATH # Create logs directory for GameLift Agent & server processes RUN mkdir logs RUN mkdir agentlogs # Start the GameLift Agent ENTRYPOINT sleep 90 && java -jar $GAMELIFT_AGENT_EXEC -ip "192.168.1.1" -gslb "$GAME_SESSION_LOGS_BUCKET_NAME" -galb "$GAMELIFT_AGENT_LOGS_BUCKET_NAME" -galp "$GAMELIFT_AGENT_LOGS_PATH" -glc environment-variable
# Base image # ---------- # Add the base image that you want to use over here, # Make sure to use an image with the same architecture as the # Instance type you are planning to use on your fleets. # We require JDK to be installed in the base image, so that # it can be used to run the &AGS; Agent FROM public.ecr.aws/amazoncorretto/amazoncorretto:17-amd64 # # Game build directory # -------------------- # Add your game build to gamebuild directory and add the zip file name in the 'GAME_BUILD_ZIP' env variable below. # The game build provided over here needs to be integrated with gamelift server sdk. # This template assumes that the game build is in a zip format. ENV GAME_BUILD_ZIP="SimpleServer.zip" \ # # Default directory # ----------------- # Default directory, the value provided here should be where the game executable exists. # Provide this same value as your launch path in RuntimeConfiguration when creating a fleet. # Ref: https://docs.aws.amazon.com/gamelift/latest/apireference/API_ServerProcess.html GAME_EXECUTABLE="GameLiftSampleServer" \ HOME_DIR="/local/game" \ # # Registered compute in anywhere fleet (not used in container fleets) # ------------------------------------------------------------------- # Add the name for the registered compute in an anywhere fleet. # This environment variable is required only for anywhere fleets, but not for container fleets. # If it is set for container fleets, it will be overridden by Gamelift. GAMELIFT_COMPUTE_NAME="<ADD_COMPUTE_NAME>" \ # # Default Gamelift Agent jar # -------------------------- GAMELIFT_AGENT_EXEC="GameLiftAgent-1.0.jar" \ # # This env variable defines the name of the S3 bucket that stores the GameLift Agent logs. # This S3 bucket should exist in the customer AWS account. # In order to allow GameLift agent to upload logs to this s3 bucket, customers would need to # include s3:PutObject permission in the IAM role provided as instanceRoleArn during CreateFleet operation. GAMELIFT_AGENT_LOGS_BUCKET_NAME="<ADD NAME OF GAMELIFT AGENT LOGS S3 BUCKET>" \ # # ----------- # This env variable defines the name of the S3 bucket that stores the game session logs. # This S3 bucket should exist in the customer AWS account. # In order to allow GameLift agent to upload logs to this s3 bucket, customers would need to # include s3:PutObject permission in the IAM role provided as instanceRoleArn during CreateFleet operation. # ----------- GAME_SESSION_LOGS_BUCKET_NAME="<ADD NAME OF GAME SESSION LOGS S3 BUCKET>" \ # # ----------- GAMELIFT_AGENT_LOGS_PATH="/local/game/agentlogs/" \ # # NOT USED in container fleets - USED in Anywhere fleets # ---------------------------------------------------------- # Specifiy the type of compute resource used to host the game servers. # This env variable is required only for anywhere fleets, but not for container fleets. # If it is set for container fleets, it will be overridden by Gamelift. # # ----------- COMPUTE_TYPE="ANYWHERE" \ # # Specify the credential to be used for creating the client. # This env variable is required only for anywhere fleets, but not for container fleets. # If it is set for container fleets, it will be overridden by Gamelift. # # ----------- CREDENTIAL_PROVIDER="environment-variable" USER root # intall dependencies as necessary RUN yum install -y sudo \ unzip \ git \ shadow-utils \ iputils \ tar \ gcc \ make \ openssl-devel \ zlib-devel \ vim \ net-tools \ nc \ procps # Set up the ground for 'gamescale' user RUN groupadd -r gamescale -g 500 && \ useradd -u 500 -r -g gamescale -m -s /sbin/nologin -c "Gamescale user" gamescale && \ echo "gamescale ALL=(ALL) NOPASSWD: ALL" | (EDITOR="tee -a" visudo) && \ mkdir -p $HOME_DIR && \ mkdir $HOME_DIR/mono && \ chown -R gamescale:gamescale $HOME_DIR WORKDIR $HOME_DIR # extract game build as necessary COPY ./gamebuild/$GAME_BUILD_ZIP . RUN unzip ./$GAME_BUILD_ZIP -d ./ # copy Gamelift Agent jar COPY ./target/$GAMELIFT_AGENT_EXEC ./ # Add permissions to game build and gamelift agent jar RUN chmod +x ./$GAME_EXECUTABLE RUN chmod +x ./$GAMELIFT_AGENT_EXEC # Check if java is installed on the image, if not then the Agent will not be able to run RUN java --version USER gamescale ENV PATH "$PATH:$HOME_DIR/bin:$JAVA_HOME" # Change directory to bin WORKDIR $HOME_DIR # check path before starting the container RUN echo $PATH # Create logs directory for GameLift Agent & server processes RUN mkdir logs RUN mkdir agentlogs # Start the GameLift Agent ENTRYPOINT sleep 90 && java -jar $GAMELIFT_AGENT_EXEC -ip "192.168.1.1" -gslb "$GAME_SESSION_LOGS_BUCKET_NAME" -galb "$GAMELIFT_AGENT_LOGS_BUCKET_NAME" -galp "$GAMELIFT_AGENT_LOGS_PATH" -glc environment-variable
注意

注意:Dockerfile 中的某些环境变量可以被覆盖。ContainerDefinition

构建您的容器镜像
  1. 构建您的容器镜像。

    如果你使用的是自己的 SDK 5.x 服务器

    你可以指定任何你想要的本地存储库名称。

    [~/work/glc]$ docker build -t <local repository name>:<optional tag> .
    如果你正在使用我们的SimpleServer样本
    [~/work/glc]$ docker build -t simple-server:version-1 . Successfully built 0123456789012 Successfully tagged simple-server:version-1
    注意

    在以下示例中,我们使用 s impler-server 作为初始REPOSITORY值和version-1值。TAG

  2. 查看图像列表并记下REPOSITORY和的IMAGE ID值。你需要在下面的步骤中使用它们。

    [~/work/glc]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE simple-server version-1 0123456789012 14 minutes ago 1.24GB

将您的容器镜像推送到 Amazon ECR

将您的容器映像上传到 Amazon ECR 中的私有存储库。创建容器组定义时,您需要引用此存储库位置,以便 Amazon GameLift 可以拍摄您的容器映像的快照并在部署容器队列时使用它。

注意

如果您还没有 Amazon ECR 私有存储库,请创建一个

获取您的 Amazon ECR 凭证
  • 在将容器映像推送到 Amazon ECR 之前,您必须以临时形式获取 AWS 凭证并将其提供给 Docker。获取您的亚马逊 ECR 凭证,这样 Docker 就可以登录了。

    [~/work/glc]$ aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.us-west-2.amazonaws.com WARNING! Your password will be stored unencrypted in /home/user-name/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
将您的容器镜像推送到 Amazon ECR
  1. 复制您要使用的 Amazon ECR 私有存储库的 URI。

  2. 将 Amazon ECR 标签应用于您的容器映像。

    [~/work/glc]$ docker tag <IMAGE ID from above> <Amazon ECR private repository URI>:<optional tag>
  3. 将您的容器镜像推送到 Amazon ECR

    [~/work/glc]$ docker image push <Amazon ECR private repository URI>