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


I run Dev Spotlight - we write tech content for tech companies. Email at [email protected]



It's time to have fun. It's okay to tell your family or colleagues that research is being done, that you're experimenting with new tech-because that's what we'll do-but don't let anyone see you playing Minecraft!



Let's say you're organizing a 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. Two servers won't suffice so you'll need to run them both simultaneously. Your load balancer should be able to send students to Server A or Server B depending on how busy they are.



This article will discuss port forwarding and load balance with 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 started, let's briefly outline some key 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. This task is usually handled by a router, firewall, or API gateway. A web server might listen on port 3000 while a database server may be listening on port 5500. Your API gateway would listen for outside requests. Requests addressed to port 80 would be forwarded by the gateway to your web server at port 3000. In the meantime, port 5432 requests will be forwarded by your gateway to your web server at Port 3000.



Load Balancing



Load-balancing is the process of distributing multiple requests across multiple servers in a balanced fashion. This task is typically handled by a particular piece of hardware, or software, called a "load balancer". The outside world is unaware that there are multiple replicas of a server running. They think they are requesting information from one server. The load balancer, however, distributes the request load to prevent any one server from being overwhelmed. The load balancer ensures requests only reach healthy nodes in case of a total failure of the replica.



Kong Gateway



Kong Gateway is an API gateway layer that sits in front upstream services and can perform load balancing and port forwarding 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. Kong Gateway can manage traffic control and authentication as well as request transformations, analytics, logging, and log.



TCP Stream Support



Our Minecraft project is different from other web servers or databases in that Minecraft requires an established connection between the Minecraft client (the player) and the server. Rather than expecting stateless HTTP requests, we'll need to handle TCP connections with streaming data. TCP streaming is supported by Kong Gateway.



Our project approach



We're going to walk through this project step-by-step:



1. Minecraft-servers.cc 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 clearly see, we'll start with simplicity and gradually build up complexity.



How to get started



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



Docker Engine will need to be installed on your local machine. To verify that our project is successful, you will need to install the Minecraft client and log in to the game as a paid owner. 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 ready for this? Let's 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. The default port will be used for the server. Next, we'll connect the 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 indicates a port on a host (your local machine) that Docker should associate with a container port. In this example, our local machine's port 25000 points to the container's ports 25565. By default, Minecraft servers run on port 25565. Typically, you will always bind to the container's port 25565, regardless of the port on the host that you choose to use.


-e EULA=true sets an environment variable that Docker container uses to start the container's server. The Minecraft server application requires that you accept the EULA upon startup. Providing this environment variable is the Docker way to do that.


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


Now that our server is up, let's connect to localhost:25000. Open up the Minecraft Launcher client and click on "Play".



The actual Minecraft application should launch. Click on "Multiplayer" to see 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, we click on the "Join Server" button.



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 something like this:



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



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. At the same time, you might expose a single port on 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. Meanwhile, Kong will listen on port 20000. 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 we will need to set up the initial configuration. You will find the template file kong.conf.default within your /etc/kong directories. We will copy the file and rename it kong.conf. This is the file Kong will use to configure its startup.



In kong.conf , we'll need to make the following three edits:



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



Write Declarative Configuration File



Before we start Kong, we need the minecraft.yml file to contain our port forwarding configuration. Open minecraft.yml, a file that corresponds to the path you specify above, in a project folder.



This file will declare a new Service entity called Minecraft Server-A. This server uses TCP protocol, listening at localhost port 25000. We set these values together to make the service's address. Next, we define the Route for the service. This associates your service with a URL or an incoming destination destination that Kong will hear. 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 already written all the configuration. 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:



That command might take a little time to run as the server starts up.

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


~/project$ sudo kong start



Once our server is up and running, we return to our game client and choose "Multiplayer" to try to establish a "Direct Connection". We know that we could connect directly to localhost:25000, since that's the actual host port bound to the container's port; rather, we want to test Kong's port 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 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



Next, we will open Server B in a separate terminal and bind the host's port 26000 the container's ports 25565.



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



Now, Servers A & B are available at ports 25000 and 26000, respectively.



Edit Declarative configuration File



Next, we want to edit our declarative configuration file (minecraft-kong.yml ), configuring Kong for load balancing. Make sure your file reflects the following:



Let's see what we've done. First, we created an upstream object (arbitrarily titled Minecraft Servers), which serves as a virtual hosting server for load balancing between multiple services. This is exactly the functionality we require. Two Target Objects were also added to our upstream services. 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 uses the weights 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 declared our Service Object, which in this case is our load balancer service. Requests that satisfy the routes we establish will be forwarded to the Minecraft-Servers host, our load balancing upstream object. Similar to the previous step, we set up a route and told Kong Gateway to listen to TCP/TLS requests 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 the previous steps, we will try to connect directly to the multiplayer server located at localhost:20000. Keep an eye on both your server terminal windows as you connect. As you connect to the server, disconnect and reconnect repeatedly, you will sometimes see a connection message for Server A and a message for Client B.



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



Ready to (Play!) Work



In summary, we gradually increased the complexity of 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 up to run port forwarding on 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. We then set Kong Gateway up to act as a load-balancer. Kong Gateway received game client connections on port 20000, and channeled them through its load balancer service to distribute them across the two servers.


From here, you have many opportunities for adding complexity. You can add game servers. If some servers can handle a higher load than others, you can set up the load balancer to distribute that load unevenly. 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 even select from a handful of load balancing algorithms besides the default "round-robin" strategy.



So, we've had a little fun and learned to use some important tools and concepts along the way. We have discovered that load balancing and port forwarding with Kong Gateway is simple and easy to set up. Yet, even with such ease, these features are extremely powerful. Now that you have a good grasp of the basics, it's time for you to tackle the Ender Dragon.


Created: 08/08/2022 18:22:15
Page views: 15
CREATE NEW PAGE