NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

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

Dev Spotlight, which I run, is tech content written for tech companies. Email at [email protected]



It's time to have some fun. You can tell your family and colleagues that you are doing research or experimenting with new tech, but don't let them know that you're 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.



In this article, we're going to explore port forwarding and load balancing 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 dive in, let's briefly cover a few important technology concepts.



Key Concepts



Port forwarding involves receiving network requests on one port of a machine, and forwarding those requests onto another 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 to requests from outside your network. The gateway would forward all requests addressed to port80 to your webserver at ports 3000 and 3000. Meanwhile, requests addressed to port 5432 would be forwarded to your database server at port 5000.



Load Balancing



Load balancing refers to the process of distributing multiple requests on a server in a balanced way across many replicas of that server. This is usually done by a piece of hardware or software known as a load balancer. The outside world is unaware that there are multiple replicas of a server running. They believe they are making requests to only one server. The load balancer distributes the request load to ensure that no one server is overwhelmed. The load balancer ensures all requests are sent to healthy nodes in the unlikely event that a replica fails completely.



Kong Gateway



Kong Gateway is a thin API gateway that sits between upstream services. It's capable of performing load balancing and port forwarding tasks. Kong Gateway is the front door greeter to all requests, no matter if those upstream services are web server or database servers or Minecraft game servers. Kong Gateway can manage traffic control and authentication as well as request transformations, analytics, logging, and log.



TCP Stream Support



Minecraft, unlike other web servers and databases, requires a connection between the Minecraft client (the user) and the server. Rather than expecting stateless HTTP requests, we'll need to handle TCP connections with streaming data. Fortunately, Kong Gateway fully supports TCP streaming.



Our project approach



We'll walk you through the 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, our approach is simple, but we'll gradually increase our complexity.



How to get 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. Finally, to verify that our project results are accurate, you will need to install Docker Engine on your local machine. Log in as a paid owner to the Minecraft client. The free trial of Minecraft doesn't allow connecting to multiplayer servers, which is what we'll be running for our project.



Are you ready for this? 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 will use the default port to connect to the server. It's simple to deploy the Minecraft server as a Docker container, with the Docker image found here.



In a terminal window, we'll run this command to pull down the server image and spin it up in a container:



As our container starts up, it downloads the Docker image for the Minecraft server. After the image is downloaded, it launches the server and displays the log messages. Here's a list of flags and options which we gave docker run when we ran our command.



-p is a port on your local machine (host port) that Docker should connect to a container port. In this example, the port 25000 on our local machine will point to port 25565 on the container. By default, Minecraft servers run on port 25565. Regardless of the port on your host, you will bind to the container's port 25655.


-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 game should launch. For game options, click on "Multiplayer".



Next, click "Direct Connection".



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



The server notifies me that a new player (my username: familycodingfun) has joined the game. Our single game server setup has been completed. Now, let's add Kong Gateway and port forwarding to the mix. We'll close the game now and kill the Docker container that we have with 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 one port to Kong that Kong listens to. 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'll be working in localhost but we'll setup 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



The first step is to install Kong Gateway. The steps of installation will vary depending on the configuration. After installing Kong, you will need to create the initial configuration file. You'll find a template file called kong.conf.default in your /etc/kong directory. This file will be copied to kong.conf and renamed as kong.conf. Kong will use this file for its startup configuration.



We'll need to make three edits to kong.conf:



The stream_listen configuration tells Kong that it will listen for streaming TCP traffic. We tell Kong to listen at port 20000. We can configure Kong with its DBless or Declarative configuration style to suit the needs of this small project. Kong will not need a database (database=off), as all configurations for port forwarding, load balancing, and load balancing can be stored in one YAML file. 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. These values will be used as the service's web address. The server uses TCP protocol. It is listening on localhost port 250000. Next, we define a Route for the service, which associates our service with a URL path or an incoming connection destination that Kong will listen for. We give Kong a name for our route. This tells Kong to listen for TCP/TLS requests on the destination we have specified in our kong.conf file.



Start Up Minecraft Server and Kong



We have all the configuration needed 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:



This command may take a while to execute as the server is starting up.

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 with the game server on localhost.20000 pretending we are a casual user who doesn't know that port 20000 is a port forwarding gateway.



Click on "Join Server." Like Step 1, your connection should be successful, and you'll have entered the Minecraft world. 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 working!



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 will start everything from scratch, spinning up each server in its respective terminal window. In the first terminal window, run Docker container Server A. Bind the host port 25000 to the container port 25565.



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



In a separate terminal, we will launch Server B. This time, the host's port 26000 is bound to the container’s port 25565.



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



Now, we have Servers A and B running, accessible at ports 25000 and 26000, respectively.



Modify Declarative Configuration File



Next, we will edit our declarative file (minecraft-kong.yml), configuring Kong to load balance. 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. This is exactly the functionality we require. We added two Target Objects 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 declared our Service Object, which in this case 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 and running. We have our two Minecraft servers (Server A and Server B) running in Docker containers in two separate terminal windows. We have Kong configured to listen for TCP on port 20000, forwarding those requests to our load balancer, distributing connections across our two servers.



Open the Minecraft game 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 repeatedly connect to the game, disconnect, and then reconnect, you will at times see a connection log message for Server A, and then at other times a message for Server 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



To recap, we slowly progressed in complexity for 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 installed Kong Gateway on our single server to perform port forwarding. 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. Minecraft servers Then, Kong Gateway was configured to act as 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 additional game servers. For example, if some servers run on machines that can handle a heavier load of connections than others, then you can configure the load balancer to distribute the load unevenly. You can set health check rules to ensure that requests are only forwarded on servers that are healthy. There are many load balancing options available, including the "round-robin" default strategy.



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.


Read More: https://minecraft-servers.cc/
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.