Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
I run Dev Spotlight - we write tech content for tech companies. 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 what it looks like: You are organizing a Minecraft class to teach local STEM students. You need to run your own Minecraft servers to ensure a kid-friendly multiplayer environment, restricted only to your students. One server won't be enough, so you'll run two servers simultaneously, expecting your load balancer to handle sending students to Server A or Server B, depending on the 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 dive in, let us briefly review 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. A router, firewall or API gateway usually handles this task. For example, you might have a web server listening on port 3000 and a database server listening on port 5000. Your API gateway would listen for requests from outside your network. The gateway would forward any requests addressed to port 80 to your webserver 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. The outside world doesn't know that there are multiple copies of a server. 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. In the case of a replica failing completely, the load balancer ensures that requests only go to healthy nodes.
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 is capable of managing traffic control as well as authentication, request transformations (analytics), and logging.
Support for TCP Stream
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. Instead of expecting stateless HTTP requests we will need to handle TCP connections using streaming data. TCP streaming is supported by Kong Gateway.
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, we'll start simple, and we'll slowly build on complexity.
What you'll need to get started
To complete this mini-project, you don't need to be familiar with Minecraft. 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 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 will use the default port to connect to the server. don't even mess with me 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. Once the image downloads, it starts up the server, and we see the log messages of the server startup. Here are the flags and options that we gave to docker run in this command:
-p specifies a port on the host (your local machine) that Docker should bind to a port on 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. Regardless of the port on your host, you will bind to the container's port 25655.
-e EULA=true is an environment variable that Docker container needs when starting up the server. The Minecraft server application requires that you accept the EULA upon startup. This environment variable can also be done using Docker.
- Lastly, we specify the name of the Docker image (on DockerHub), which contains the Minecraft server.
Let's start our server and see if it can connect to the localhost.25000 server. 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 "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. 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. 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.
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
Install Kong Gateway is the first step. The installation steps vary depending on your unique setup. After installing Kong we will need to set up the initial configuration. In your /etc/kong/ folder, you will find a template file named kong.conf.default. We will copy this file and rename it as kong.conf , which is the file that Kong will look to for its startup configuration:
The following three edits will be required in 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. That is the declarative_config file path that we've set above.
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. 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. 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:
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
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 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 on behalf of the casual user to the game server at localhost:20000.
Click on "Join Server." Your connection should work as it did in Step 1. You will then be able enter 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. We have port forwarding 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
Then, in a separate terminal window, we will start up Server B, this time binding the host's port 26000 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.
Edit Declarative Configuration File
Next, we will edit our declarative file (minecraft-kong.yml), configuring Kong to load balance. 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. This is exactly what we need. 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've explicitly set the weights evenly to 100, the default for this optional configuration is 100.
Next, we created our Service Object. This 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 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
We need to restart Kong because our Kong configuration has changed.
~/project$ sudo kong restart
At this point, everything is 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 to listen to TCP port 20000 and forward those requests to our loadbalancer, distributing connections between 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 out for the messages in your server terminal windows while 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!
Are you 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 Gateway received game client connections on port 20000, and channeled them through its load balancer service to distribute them across the two servers.
There are many ways to add complexity from here. You can add more 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. To ensure that only healthy servers are forwarding requests to Kong's load-balancer, you can create health check rules. 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. It's easy to set Kong Gateway up for load balancing or port forwarding. These features are very powerful, even though it is so easy. Now that you've got a handle on it, it's time to get to work and face the Ender Dragon.
Read More: https://hbl.info/
![]() |
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