Benvenuto, ospite! [ Registrati | Login

A proposito di conevest9

Descrizione:

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


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. You can't run more than one server so you will need to host two servers simultaneously. Your loadbalancer will send students to Server A, or Server B, depending upon the load.


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 get into the details, let's briefly discuss some important 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 usually handled by a router, firewall, or API gateway. You might have a webserver listening on port 3000, and a database server listening at port 5000. Your API gateway would listen out for requests from outside of your network. Requests addressed to port 80 would be forwarded by the gateway to your web server at port 3000. Port 5432 requests would be forwarded to the gateway 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. Outsiders are unaware that there are many replicas of a server. They think they are making requests to one server. The load balancer distributes the request load to ensure that no one server is overwhelmed. The load balancer ensures requests only reach healthy nodes in case of a total failure of the 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 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 is capable of managing traffic control as well as authentication, request transformations (analytics), and logging.


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


You don't actually need a lot of familiarity with Minecraft to progress through this mini-project. 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. Minecraft's free trial doesn't allow you to connect with multiplayer servers, but that's what we'll use 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. First, we will use the default port of the server. Then, we will connect our game client with 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. Once the image is downloaded, it starts the server. We can see the log messages for the server startup. Here's an explanation of flags and options we provided to docker run in our command:


-p indicates a port on a host (your local machine) that Docker should associate with 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. 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. 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.


With our server running, let's see if we can connect to the server at localhost:25000. Open up the Minecraft Launcher client and click on "Play".


You should see the actual Minecraft game launch. 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 has been completed. 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 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 first. The steps of installation will vary depending on the configuration. 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. We will copy the file and rename it kong.conf. This is the file Kong will use to configure its startup.


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


The stream_listen configuration tells Kong it to listen for streaming TCP trafic. For this, we tell Kong that it should 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 for declarative_config files that we've given above.


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 the Route for the service. This associates your service with a URL or an incoming destination destination that Kong will hear. We provide a name for our route, telling Kong to listen for requests using TCP/TLS on the destination that we specified in our kong.conf file: ip 127.0.0.1 and port 20000.


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:


It might take some time to execute this command as the server boots 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 can connect directly with localhost:25000 because that's actually the host port bound for the container's Port; but 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. minecraft servers 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


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, we have Servers A and B running, accessible at ports 25000 and 26000, respectively.


Edit Declarative configuration File


Next, edit your declarative configuration file (minecraft_kong.yml) to configure Kong for load balancing. Modify your file to reflect these changes:


Let's walk through what we've done here. 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. 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). Next, we assign a weight to each target. This is what the load balancer uses for load distribution. 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 previous step we created a route telling Kong Gateway that it will listen for TCP/TLS request destined for 127.0.0.1.20000.


Restart Kong


Our Kong configuration has changed. We must restart Kong to allow the changes to take affect.


~/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 game client again. Similar to the previous steps we will connect to the multiplayer server 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


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 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. Then, we configured Kong Gateway 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.


There are many ways to add complexity from here. You can add additional 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. 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. Kong Gateway makes load balancing, port forwarding simple and easy. These features are powerful even though they are so simple. Now that you know what it is, it's time get to work and face off against the Ender Dragon.

Siamo spiacenti, non sono stati trovati annunci.