This article will teach us to deploy an Agones Game Server in Kubernetes Cluster. Previously, we learned to integrate Agones SDK into the Gameserver. We will learn to deploy standalone Gameservers, Gameserver Fleets, etc., on a Kubernetes Cluster.

By the end of this article, you will learn to deploy your Agones Game Servers anywhere a Kubernetes Cluster can run.

Prerequisites

  • Kubernetes Cluster
  • Agones SDK Integrated Game Server

Starting Up

Let's start by making a docker image of the previously integrated code. You can learn more about the integration process in this article. We'll perform docker build and then push my docker image into Dockerhub. Dockerhub is a free platform that can be used to publish our docker images for free.

💡
It's always a good idea to setup your own docker registry and it is quite easy. You can learn more about it in this article.
cd nodejs-chat-app

Copy the following Dockerfile into a file inside the directory,

#Builds
FROM node:lts-alpine3.16 AS build-env
WORKDIR /usr/src/alpine/app
COPY package.json yarn.lock /usr/src/alpine/app/
RUN yarn install
COPY . /usr/src/alpine/app/
RUN yarn build
#Serve
FROM gcr.io/distroless/nodejs:16 AS server
COPY --from=build-env /usr/src/alpine/app /usr/src/app
WORKDIR /usr/src/app
EXPOSE 3400
CMD [ "dist/main.js"]
Dockerfile

Now build it using the command below,

docker build -t example-user/agones-dummy:v1 . -f Dockerfile
💡
If you're running the build on an ARM-based system append the following parameter into the build command above --platform linux/amd64.

This ensures that your image is built for the amd64 architecture which is what we'll be using for our Kubernetes cluster, but if your cluster is ARM-based, then you don't need the platform argument.

Now that our Docker Image has been built, we'll publish it to our dockerhub account. You can either use a private repository or a public one. I'm using a public repository. (You're only allowed a maximum of one private repository with a free dockerhub account).

docker push example-user/agones-dummy:v1

Now, we're ready to deploy our Game Server with Agones.

Deploying with Agones

In a previous article, we discussed deploying Agones on a Kubernetes Cluster. If you have not already set up Agones, follow the article here.

Before going ahead with our Game Server (GS) deployment, let's learn a little about the components that make Agones, which are a group of CRDs

What's a CRD?

CRDs, also known as Custom Resource Definitions, are a staple of Kubernetes. These are extensions on the core Kubernetes made according to specific use cases. The developers of Agones have created CRDs that work with the core Kubernetes components to enable the features that Agones provides us.

Listed below are the CRDs used by Agones.

1. Fleet Autoscalers

The fleet auto scaler is used for scaling game servers up and down according to the induced load. It runs with two different logic buffers and a webhook. Buffer logic keeps a certain amount of buffer servers used as the load progresses.

Webhook logic allows us to provide our own logic and is useful for specific use cases where buffer autoscaling is not feasible.

2. Fleets

Fleet consists of game servers that are ready to be used. The Agones controller changes the status of game servers in a fleet when game servers are allocated /de-allocated.

3. Gameserver Allocation Policies

Gameserver Allocation is used to allocate Gameservers from a Fleet. The allocation policy defines how game servers are organized throughout a cluster.

There are two modes available: packed and distributed.
The distributed option works well with clusters where resources are distributed across the whole cluster, whereas packed mode is good for clusters provided by cloud providers where all resources are packed tightly within a small group of clusters.

4. Gameservers

Gameservers are dedicated instances of a game server running in a Kubernetes cluster. The game server has a pod resource where the Gameserver container runs. Using the Gameserver CRD, we can create Gameservers and gather details of the Gameserver like status, IP address, ports exposed, etc.

5. Game Server Sets

The game server set is similar to a Fleet, i.e., a group of Gameservers controllable through a single config. But gameserver sets cannot be scaled up and down compared to fleets. The number of game servers available in nameservers is fixed.

So all these CRDs are what make Agones up. Visit the Agones Docs if you would like more info on them.

Now that we have more knowledge on the CRDs and have a fundamental idea of how Agones works under the hood, we can start deploying our game server.

The Game Server can be deployed as a single unit (standalone mode) or in a group (fleet). We'll deploy our GS in both modes, Standalone and Fleet.

Deploying in Standalone Mode

Copy the following configuration into a new file called. standaloneconfig.yml

apiVersion: "agones.dev/v1"
kind: GameServer
metadata:
  name: dummy-gameserver
spec:
  players:
    initialCapacity: 101
  health:
    initialDelaySeconds: 300
    periodSeconds: 25
  sdkServer:
    logLevel: Debug
  ports:
    - name: dummy-gameserver
      portPolicy: Dynamic
      containerPort: 8818
      protocol: TCP
  template:
    spec:
      containers:
        - name: dummy-gameserver
          image: example-user/agones-dummy:v1.5
          imagePullPolicy: IfNotPresent
Agones Standalone Game Server Config

Deploy it using the command below,

kubectl apply -f standaloneconfig.yml

Check the deployment status using the command below,

kubectl get gs -n agones

Deploying in Fleet Mode

Copy the following configuration into a new file called. fleetconfig.yml

apiVersion: "agones.dev/v1"
kind: Fleet
metadata:
  name: dummy-fleet
spec:
  strategy:
    type:
      Recreate
  replicas: 1
  template:
    metadata:
      annotations:
      	dummy: fleet
    spec:
      players:
        initialCapacity: 101
      health:
        initialDelaySeconds: 300
        periodSeconds: 25
      ports:
        - name: dummy-gameserver
          portPolicy: Dynamic
          containerPort: 8818
          protocol: TCP
      container: dummy-gameserver
      template:
        spec:
          containers:
            - name: dummy-gameserver
              image: example-user/agones-dummy:v1
              imagePullPolicy: IfNotPresent
              securityContext:
                allowPrivilegeEscalation: false
Fleet Game Server Config

Deploy it using the command below,

kubectl apply -f fleetconfig.yml

Check the fleet deployment status using the command below,

kubectl get fleet -n agones

You can check the logs to see if everything is working or not by using the command below,

kubectl logs -f {use_pod/gameserver_id}

Accessing the Game Servers

You can also open the game servers in the browser using Kubernetes port forwarding. Run the command below,

kubectl port-forward dummy-gameserver -n agones

You should be able to access the Game Server if you access the URL.

Summary

We've learned about the components that make up Agones, and the configurations required to host Game Servers and deployed our Game Server container into a Kubernetes Cluster.

In the next article, we will learn to expose Agones Game Servers on the public internet and access them.

Thank You reading the article, please comment below if you have any queries.

I update my articles periodically to ensure legibility and continuity.