The Essential Guide To Load-Balancing Minecraft Servers With Kong Gateway

The Essential Guide To Load-Balancing Minecraft Servers With Kong Gateway


Dev Spotlight is my company. We create tech content for tech companies. Email at [email protected]

It's time have some fun. Tell your family, friends, and coworkers that you're conducting research, or trying out new tech. But don't let your family see that you're playing Minecraft!

Here's the scenario: You're organizing a full-day Minecraft class for local STEM students. You need to run your own Minecraft servers to ensure a kid-friendly multiplayer environment, restricted only to your students. You won't have enough servers so you will need to run two servers simultaneously. Your load balancer will handle sending students to Server A and Server B depending on their load.

In this article, we'll explore port forwarding as well as load balancing using Kong Gateway. We're going to do this by spinning up multiple Minecraft servers, and then placing Kong Gateway in front of these upstream services to handle port forwarding and load balancing.

Before we get into the details, let's briefly discuss some important technology concepts.

Key Concepts

Port forwarding is the process of receiving network requests from a particular port on a machine and forwarding them onto a different port. https://raunge.com/ This task is typically handled by a router or firewall. You might have a webserver listening on port 3000, and a database server listening at port 5000. Your API gateway would listen to requests from outside your network. Requests addressed to port 80 would be forwarded by the gateway to your web server at port 3000. In the meantime, requests to port 80 would be forwarded by the gateway to your web server at port 3000.

Load Balancing

Load balancing is the task of distributing multiple requests to a server in a balanced manner across numerous replicas of that server. A specific piece of hardware or software called a load balancer usually handles this. The outside world does not know that there are multiple servers running. They think they are making requests to one server. The load balancer distributes the request loads to keep any one server from becoming overwhelmed. In the case of a replica failing completely, the load balancer ensures that requests only go to healthy nodes.

Kong Gateway

Kong Gateway is a thin API gateway layer that sits in front of upstream services, capable of performing these port forwarding and load balancing tasks. Kong Gateway is the front-door greeter for all requests, regardless of whether those upstream services include web servers, databases or Minecraft game servers. In addition to traffic control, Kong Gateway can manage authentication, request transformations, analytics, and logging.

Support for TCP Stream

One aspect of our Minecraft project that sets it apart from deploying a typical web server or database is that Minecraft requires an established connection between the Minecraft client (the gamer) and server. Instead of accepting stateless HTTP requests, we will be handling TCP connections with streaming data. TCP streaming is supported fully by Kong Gateway.

Our project approach

We'll walk you through this project step by step.

1. Spin up a single, local Minecraft server without any port forwarding. 2. Spin up a Minecraft server on a non-default port, configuring Kong Gateway to port forward requests to that server. 3. Spin up two Minecraft servers on different ports, configuring Kong Gateway to load balance and port forward connection requests.

As you can see we will start simple and slowly increase complexity.

Here are the essentials to get you started

This mini-project doesn't require a lot of Minecraft knowledge. Since it's easiest to spin up Minecraft servers within Docker containers, basic familiarity with Docker may be helpful.

Docker Engine needs to be installed on the local machine. If you want to check that our project results have been successful, you will need the Minecraft client installed and logged in as a paid user. Minecraft's free trial does not allow you to connect to multiplayer servers. This is what we will be using for our project.

Are you up for it? Here we go!

Step 1: Single Minecraft Server with Default Port

In this first step, we want to spin up a single Minecraft server on our local machine. We'll use the default port for the server, and then we'll connect our game client to the server. It's simple to deploy the Minecraft server as a Docker container, with the Docker image found here.

This command will be executed in a terminal window to pull down the server images and spin them up in a container.

As our container starts up, it downloads the Docker image for the Minecraft server. Once the image has been downloaded, the server is started up and the log messages are displayed. Here are the flags and options that we gave to docker run in this command:

-p specifies the port on the host (your machine) that Docker should bind with a port in the container. In this case, the container's port number 25565 will be mapped to our local machine's Port 25000. By default, Minecraft servers run on port 25565. You will typically bind to the container’s port 25565 regardless of which port you use on the host.

-e EULA=true provides an environment variable that the Docker container needs to use when starting up the server within the container. The Minecraft server application requires that you accept the EULA upon startup. This environment variable can be provided by Docker.

- Lastly, we specify the name of the Docker image (on DockerHub), which contains the Minecraft server.

Let's get our server up and running. Open up the Minecraft Launcher client and click on "Play".

You should see the actual Minecraft game launch. Click on "Multiplayer" for more game options.

Next, click the "Direct Connection" link

For server address, enter localhost:25000. Our local port 25000, of course, is bound to the container running our Minecraft server. Finally, click on "Join Server".

And... we're in!

If you look back at the terminal with the docker run command, you'll recall that it continues to output the log messages from the Minecraft server. It might look like this:

The server informs me that a new user (my username familycodingfun) has joined our game. Our single game server setup is complete. Now, let's add Kong Gateway and port forwarding to the mix. We will now exit the game, kill our Docker containers and send them to the server.

Step 2: Minecraft Server with Kong Gateway and Port Forwarding

Next, we'll put Kong Gateway in front of our Minecraft server and take advantage of port forwarding. If you were running a private network, you might forbid requests from outside the network to reach your Minecraft server port. You might also expose a single port to which Kong listens. Kong, as the API gateway, would listen to requests on that port and then forward those requests to your Minecraft server. Doing so ensures that any requests that want to go to a Minecraft server must go through Kong first.

We will work within localhost. However, we will set up port forwarding through Kong. Just like in our previous step, we want our Minecraft server to run on port 25000. Kong will listen to port 20000 while we wait. Kong will take TCP connection requests on port 20000 and forward them to the Minecraft server at port 25000.

Install and Setup Kong

Install Kong Gateway first. The installation steps vary depending on your unique setup. After installing Kong, it's time to set up the first configuration file. In your /etc/kong/ folder, you will find a template file named kong.conf.default. This file will be copied and renamed kong.conf. It will be used by Kong to configure its startup.

We will need to make these three edits in kong.conf

The stream_listen configuration tells Kong how to listen for streaming TCP data. We're telling Kong to listen on port 20000 for this. We can configure Kong using the DB-less or Declarative configuration styles to meet the requirements of this mini-project. Kong will not require a database (database=off), and all configurations for load balancing and port forwarding will be stored in a single YAML. This is the path to our declarative_config file.

Write Declarative Configuration File

Before we can launch Kong, we will need to create the minecraft.yml.yml file that contains our port forwarding configuration. Open minecraft.yml from a project folder.

This file will declare a new Service entity called Minecraft Server-A. The server uses the TCP protocol, listening on localhost port 25000, so we set these values together as the service's url. Next, we define a route for the service. This associates our server with a URL path, or an incoming connection destination, that Kong will listen to. We give Kong a route name, telling Kong that it will listen for requests using TCP/TLS to the destination specified in our kong.conf.

Start Up Minecraft Server and Kong

We've written all of our configuration for this step. Let's start up our Minecraft server in Docker. Remember, we want our host (our local machine) to be ready on port 25000, binding that port to the standard Minecraft server port of 25565 on the container:

As the server starts up, it might take some time for this command to run.

In a separate terminal window we'll now start Kong:

~/project$ sudo kong start

With our server up and running, we go back to our game client and, just like above, choose "Multiplayer" and try to establish a "Direct Connection" with a game server. We know that localhost:25000 is the host port bound to the container port. However, we want Kong to test Kong's forwarding. We want to connect to the supposed game server on localhost:20000, pretending that we're the casual user who is unaware that port 20000 points to a port forwarding gateway.

Click on "Join Server." You'll be able to enter the Minecraft world if your connection succeeds like Step 1. Our TCP connection request to localhost:20000 went to Kong Gateway, which then forwarded that request to port 25000, our actual Minecraft server. Port forwarding is now up and running!

Step 3: Load-Balancing Two Minecraft Servers

We will spin up two Minecraft servers for the final step in our mini-project, listening on ports 25000 and 26000. Previously, when we only had one Minecraft server, Kong would naturally forward TCP requests at port 20000 to that sole Minecraft server's port. Now, with two Minecraft server ports to choose from, we'll need to use port forwarding and load balancing. Kong Gateway will take TCP connection requests that come to port 20000 and distribute connections evenly between Minecraft Server A and Minecraft Server B.

Start Up Minecraft Servers

If you haven't done so already, terminate the single Minecraft server that was running in the previous step. We'll restart everything from a clean slate, spinning each server up in its own terminal window. Run the Docker container for Server A in your first terminal window. Bind the host's port 25000 to container's port25565

~/project$ docker run -p 25000:25565 -e EULA=true itzg/minecraft-server

In a separate terminal window we will then start Server B. This time, we will connect the host's port 26000 with the container's Port 25565.

~/project$ docker run -p 26000:25565 -e EULA=true itzg/minecraft-server

Now, Servers A andB are up and running at ports 25000 & 26000, respectively.

Edit Declarative Configuration File

Next, edit your declarative configuration file (minecraft_kong.yml) to configure Kong for load balancing. To reflect the following, edit your file.

Let's take a look at what we did. First, we created an upstream object (arbitrarily titled Minecraft Servers), which serves as a virtual hosting server for load balancing between multiple services. That's exactly what we need. Two Target Objects were added to our upstream service. Each target has an address with host and port; in our case, our two targets point to localhost:25000 (Minecraft Server A) and localhost:26000 (Minecraft Server B). The load balancer then uses this weight to distribute load. Even though we have specified that the weights should be evenly distributed to 100, the default is 100 for this optional configuration.

Next, we created our Service Object. This is our load balancer service. Requests that fulfill the routes we established will be forwarded to Minecraft-Servers host. This is our load balancing object. Similar to our previous step, we configured a route, telling Kong Gateway to listen for TCP/TLS requests destined for 127.0.0.1:20000.

Restart Kong

Because our Kong configuration has been changed, we must restart Kong in order for the changes to take place.

~/project$ sudo kong restart

Everything is now up. We have our two Minecraft servers (Server A and Server B) running in Docker containers in two separate terminal windows. Kong is configured to listen for TCP port 20000. This forwards those requests to our loadbalancer. We distribute connections across our two servers.

Open the Minecraft client again. Similar to previous steps, we will attempt to connect to the multiplayer server at localhost:20000 directly. Keep an eye on the two terminal windows of your server as you connect. You will see messages for Server A and Server B as you connect and disconnect from the game.

And just like that, we have set up our load balancer to distribute connection requests across our two Minecraft servers!

Ready to (Play) Work

We have now reached a certain level of complexity in our mini-project.

1. We started by simply spinning up a single Minecraft server in a Docker container, using port 25000 for accepting game client connections. 2. Next, we set Kong Gateway to be able to forward port requests to our single server. Kong listened on port 20000 for game client connections, forwarding those requests to the port on our host where the Minecraft server was accessible. 3. Lastly, we set up two Minecraft servers to run concurrently. Next, we set Kong Gateway up as a load balancer. Kong listened on port 20000 for game client connections, this time funneling them through its load balancing service to distribute connections across our two servers.

From here, you have many opportunities for adding complexity. You can add more game servers. You can add more game servers to your network. You can configure health check rules for Kong's load balancer to ensure requests are only forwarded to those servers which are presently healthy. You can also choose from a number of load balancing algorithms, other than the default "round-robin".

So far, we have had a lot of fun and learned some important tools and concepts. We have discovered that load balancing and port forwarding with Kong Gateway is simple and easy to set up. These features are still extremely powerful, even though they are simple to set up. Now that you have a good grasp of the basics, it's time for you to tackle the Ender Dragon.

Report Page