A Minecraft BungeeCord plugin which automates a Google Cloud Compute Minecraft server start/stop based on usage
If the VM instance is not running and a player connects, the instance will be started and the player is held waiting in the loading screen. When the instance is up, the player is automatically sent to the server.
It also automatically monitors the Minecraft server, and when the last player leaves the VM instance will be stopped again after a short timer.
If you don't already have one, create a Google Cloud project and setup billing for it.
- In the cloud console, go to VM instances. Click Create Instance.
- Name your instance something, such as
minecraft-1
. - Select the region and zone you want. Preferably, the region should be as close as possible to the BungeeCord server to minimize latency.
- Select the machine you want, you can pick any here depending on your performance needs. Some types will be harder to get hold of (remember you're sharing the resources from a pool), so if you find that the pool is often exhausted it might be worth looking into switching type.
- Under Boot disk, select what type of storage you want. A 10GB SSD is a good start for a small server. Image should be regular Debian GNU/Linux 10.
- Under Networking tab, add a new Network tag called
minecraft-server
. Under the default networking interface, make sure Ephermal is selected for External IP. - Click Create when you're finished setting up the VM.
- Start the instance if it isn't already started.
- Press the SSH button once its up to open its terminal.
- First we need to install some programs necessary to run the server. Run:
sudo apt-get update
sudo apt-get install -y default-jre-headless wget
- Now you need to download the Minecraft server. This is done like any regular unix Minecraft server install. I recommend using https://github.com/GameServerManagers/LinuxGSM .
- When finished installing the server, make sure
enable-query
is set to true, andonline-mode
set to false in server.properties. - In order to easily start and stop the server, we are going to make two bash scripts. The contents of these depends on if you use LGSM or some other method of hosting, but here I assume you use LGSM, installed with a user named minecraft.
startup.sh
#!/bin/bash
/home/minecraft/mcserver start
shutdown.sh
#!/bin/bash
/home/minecraft/mcserver stop
Now we are going to make a startup and shutdown script so the Minecraft server is automatically started when the VM instance is, and safely stopped before the VM instance stops.
- Click your VM instance in Cloud Console, click Edit. In the Custom metadata section, add a new key called
startup-script
and copy the following there:
#!/bin/bash
sudo -u minecraft bash /home/minecraft/startup.sh
- Add another key called
shutdown-script
and copy the following there:
#!/bin/bash
sudo -u minecraft bash /home/minecraft/shutdown.sh
By default the firewall for VM instances is very restrictive. We want to open the 25565 port towards the BungeeCord host server so it can proxy players there.
- Open the VPC Firewall page by searching for it at the top.
- Create a new Firewall rule. Set the following:
- Name:
minecraft-rule
- Direction of traffic: Ingress
- Action on match: Allow
- Targets: Specified target tags
- Target tags:
minecraft-server
- Source filter: IP ranges
- Source IP ranges:
<your BungeeCord host>
- Protocols and ports: Check TCP and UDP and type 25565 in both boxes.
- Save
You need to create a Service Account for the plugin to have access to control your VM.
- Open IAM Service Accounts by searching for it at the top of Cloud Console.
- Create Service Account. Name it something like
Minecraft BungeeGCloud
. Give it Compute Admin role. Click Create. - In the IAM Service Accounts main menu again, click the triple dots button on your newly made service account. Click Create Key. Type JSON. The downloaded json file should be placed in BungeeGCloud's data folder, as explained soon.
Install BungeeCord/Waterfall or some other derivative on your other host like usual. Add the built BungeeGCloud.jar to the plugins folder. Create a folder inside plugins folder called BungeeGCloud
. Inside, place the IAM service account json file, named exactly credentials.json
. Also create a file called config.yml
. The contents of the config should be as followed:
idle_server_stopwait: 180 # How many seconds to wait after last player leaves until the instance is stopped
refresh_players_period: 5 # How often we should refresh the player count from the Minecraft server
compute:
project_id: minecraft-123456 # Your GCloud project id
instance_id: minecraft-1 # Your GCloud instance name
minecraft:
port: 25565 # The Minecraft server port
default:
motd: "Welcome to Exhale!" # Default MOTD to show if we haven't seen the Minecraft server up yet
max_players: 20 # Default max players to show if we haven't seen the Minecraft server up yet