NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

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

Dev Spotlight is a tech content writing company that I manage. Email at [email protected]



It's time for 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 how it works: You organize a Minecraft class for local students in STEM. 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 dive in, let us briefly review some key technology concepts.



Key Concepts



Port forwarding refers to receiving network requests on a specific port of a computer and forwarding them to another port. This task is typically handled by a router or firewall. A web server might listen on port 3000 while a database server may be listening on port 5500. Your API gateway would listen out for requests from outside of your network. The gateway would forward requests to port 80 to your web server at Port 3000. Port 5432 requests would be forwarded to the gateway at port 3000.



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. A specific piece of hardware or software called a load balancer usually handles this. Outsiders are unaware that there are many replicas of a server. They believe they are making requests to only one server. The load balancedr, however, distributes request load to prevent any server from being overwhelmed. The load balancer ensures that requests go only to healthy nodes in the event of a complete failure of a replica.



Kong Gateway



Kong Gateway is a thin API gateway layer which sits in front off upstream services. It can perform port forwarding and load balancencing tasks. Kong Gateway can greet all requests regardless of whether they are web servers, databases, or Minecraft game server servers. Kong Gateway can also manage traffic control, authentication, request transformations and analytics.



TCP Stream Support



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 request, we'll be handling TCP connections that use streaming data. Fortunately, Kong Gateway fully supports TCP streaming.



Our project approach



We'll walk 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, we'll start simple, and we'll slowly build on complexity.



What you'll need 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. 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? 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. 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.



In a terminal, run the following command to pull down the image server and spin it up into 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 are the flags and options that we gave to docker run in this command:



-p is a port on your local machine (host port) that Docker should connect to 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. Regardless of the port on your host, you will bind to the container's port 25655.


-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 is what Docker does.


- 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".



The actual Minecraft game should launch. For game options, click on "Multiplayer".



Next, click on "Direct Connection".



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 could look something like this.



The server notifies me that a new player (my username: familycodingfun) has joined the game. Our single game server setup is complete. Let's add Kong Gateway, port forwarding, and more. 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 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.



Although we'll be working within localhost, we'll set up this kind of 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



The first step is to install Kong Gateway. The installation steps vary depending on your unique setup. After installing Kong, we'll need to set up the initial configuration file. In your /etc/kong/ folder, you will find a template file named kong.conf.default. This file will be copied to kong.conf and renamed as kong.conf. Kong will use this file for its startup configuration.



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



The stream_listen configuration tells Kong how to listen for streaming TCP data. This tells Kong to listen on 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 start up Kong, we need to write that minecraft-kong.yml file with our port forwarding configuration. Open minecraft-kong.yml in a folder that matches the path you have specified.



This file will declare a new Service entity called Minecraft Server-A. These values are used as the service's URL. The server uses TCP protocol and is listening on localhost port 25000. 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



This step is complete. 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.

Now, we'll open Kong in a separate terminal.

~/project$ sudo kong start



After our server is up and running we go back into our game client. We choose "Multiplayer" as before and attempt to establish a direct connection with a game server. We know we can connect directly to localhost.25000, as that's the port bound to the container's ports; however, we are testing Kong's port 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 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 start everything up again from a clean state, spinning up each server in its own terminal window. In the first terminal, run the Docker Container for Server A. Bind Server 25000 to the container’s port 25565.



~/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



Servers A, and B, are now available at ports 25000 to 26000, respectively.



Edit Declarative Configuration



Next, we need to edit our declarativeconfig file (minecraft_kong.yml), configuring Kong for load balancencing. Modify your file to reflect these changes:



Let's look at what we have done. 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). Next, we assign a weight to each target. This is what the load balancer uses for load distribution. Although we have explicitly set the weights equally to 100, the default configuration for this option is 100.



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 the last step, we created a route to tell Kong Gateway to listen out for TCP/TLS queries destined for 127.0.0.1.20000.



Restart Kong



Since our Kong configuration has changed, we need to restart Kong for the changes to take effect:



~/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. Kong is configured on port 20000 to listen for TCP, forwarding these requests to our loadbalancer. This distributes connections across the two servers.



Open the Minecraft client again. Minecraft servers list Similar to previous steps, we will attempt to connect to the multiplayer server at localhost:20000 directly. As you connect, keep an eye on your two server terminal windows. 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



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 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. Then, Kong Gateway was configured to act as load balancer. Kong Gateway then listened on port 20000 to game client connection requests, redirecting them through its load-balancing service.


This is where you can add complexity. You can add game servers. You can add more game servers to your network. To ensure that only healthy servers are forwarding requests to Kong's load-balancer, you can create health check rules. You can also choose from a number of load balancing algorithms, other than the default "round-robin".



So far, we've had some fun, and have learned some important concepts and tools. 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 know what it is, it's time get to work and face off against the Ender Dragon.


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

Notes is a web-based application for online 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 14 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.