It is a story that a beginner who is less than half a year old for the first time to build GCP built a Minecraft server for studying.
--Building a Minecraft server with GCE --Controlling an instance from Discord --Get used to operating GCP
--VM instance 1 (Micra server) --VM instance 2 (discord bot) --Cloud storage (for backup) --Service account 1 (for control from discord)
Anyway, nothing will start unless you create an instance for the Minecraft server. Basically, we will create it by referring to the following official document. GCP has a fairly easy-to-understand official document, which was a great help for beginners.
Set up Minecraft server on Compute Engine (https://cloud.google.com/solutions/gaming/minecraft-server?hl=ja#automate_start_up_and_shutdown_procedures)
For the instance I created it with this setting.
item | Contents |
---|---|
Instance name | mineserver |
region | us-central1 |
zone | us-central1-f |
series | N1 |
Machine type | n1-standard-2 |
Boot disk | CentOS 7 |
Preemptive | Effectiveness |
Network interface external IP | Create an IP address |
** Preemptible instance limits ** The benefit of enabling preemptible instances is simply that the cost of the instance is one-third that of normal. Regarding the Minecraft server this time, I thought that there was almost no situation where I kept logging in, so I chose a preemptible instance with the priority of significantly reducing costs. As mentioned in the goal, I think that there is not much obstacle because the instance is started only when playing the game from discord and stopped when it is finished.
Please refer to the official document for details on the disadvantages.
** Add persistent disk ** Create additional discs as follows.
item | Contents |
---|---|
Disk name | minecraft-disk |
Disc type | SSD persistent type |
Source type | Empty type |
size | 50GB |
Basically, we will proceed based on the official document described above, but Officially, the OS uses Debian, and I'm using CentOS, so some commands are different. Since the differences are described, we would appreciate it if you could read it together with the official document.
** Set up Java Runtime Environment (JRE) **
#First, update to the latest version of the existing package
$ yum update
#Install JRE
$ yum install -y default-jre-headless
** Download and install Minecraft server **
Officially, it is downloaded using wget, but centOS does not have wget
by default, so install it.
For the download link of Micra server, please copy and replace the latest link from the download page of minecraft.
#wget installation
$ yum install wget
#Minecraft server installation
$ wget https://launcher.mojang.com/v1/objects/f1a0073671057f01aa843443fef34330281333ce/server.jar
** First server startup ** The command below says "-Xms1G -Xmx3G", but this one is allocating memory. It can be adjusted according to the machine specifications.
#Run minecraft server
$ java -Xms1G -Xmx3G -d64 -jar server.jar nogui
** Agreement terms for using Minecraft server **
After the first startup, several files will be created in the same directory, and there is a file called ʻeula.txt in it, so open it with the following command and edit it. If you agree with the EULA terms, change the value of ʻeula
from false
to true
, save and exit.
$ vi eula.txt
#Below eula.Contents of txt
#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).
#Thu Nov 21 18:02:54 UTC 2019
eula=true <--Change here from "false" to "true"
At this point, the minimum required setup has been completed.
However, if you leave this as it is, if you log out from the server, the session will be cut off and the Minecraft server will stop.
Use screen
to solve this problem.
First, install screen
.
#screen installation
$ yum install screen
Run the Minecraft server using the installed screen
.
Please refer to the following page for how to use the screen command.
#Run minecraft server using screen
$ screen -S mcs java -Xms1G -Xmx3G -d64 -jar server.jar nogui
** Allow clients to log in to Minecraft server **
As stated in the official documentation, the Minecraft server uses 25565 as the default listening port, so create a firewall rule to allow it.
If you want to change the port, you can change it by editing server.properties
in the same directory.
In addition, server.properties
allows you to change various other settings of the Minecraft server.
** Automate instance start and stop ** If you plan to stop the server on a regular basis, add startup and shutdown scripts to your instance to automate frequently used startups and shutdowns.
It is necessary when controlling the instance from discord later, so set it now.
In the VM Instance-> Click Instance Name-> Edit-> Custom Metadata section of the GCP Console, add a new key named startup-script
and add the following script to the Value ] Copy to the field.
#!/bin/bash
mount /dev/disk/by-id/google-minecraft-disk /home/minecraft
(crontab -l | grep -v -F "/home/minecraft/backup.sh" ; echo "0 */4 * * * /home/minecraft/backup.sh")| crontab -
cd /home/minecraft
screen -d -m -S mcs java -Xms1G -Xmx3G -d64 -jar server.jar nogui
To automate the shutdown procedure, add another key, shutdown-script
, and copy the following script into its Value field.
#!/bin/bash
/home/minecraft/backup.sh
sudo screen -r mcs -X stuff '/stop\n'
I was playing Minecraft and suddenly crashed and the data flew away. What a sad thing I have had several times. It is essential to make regular backups for such a case.
Basically, proceed according to the item "Schedule regular backup" in the official document.
First, create a new Cloud Storage Regional bucket as per the official documentation. Replace the region where you create the bucket if you like. I created it in ʻasia-northeast1 (Tokyo)`.
Create a backup script and run it regularly using cron
.
#Creating a backup script
$ vi /home/minecraft/backup.sh
#Below backup.contents of sh
#!/bin/bash
screen -r mcs -X stuff '/save-all\n/save-off\n'
/usr/bin/gsutil cp -R ${BASH_SOURCE%/*}/world gs://[BUCKET_NAME]/$(date "+%Y%m%d-%H%M%S")-world
screen -r mcs -X stuff '/save-on\n'
#Change the permissions of the created script
$ chmod 755 /home/minecraft/backup.sh
To schedule a cron
job, you need to write to crontab
.
$ crontab -e
#Below is the contents of crontab
0 */4 * * * /home/minecraft/backup.sh
With the above settings, backup.sh
will be executed every 4 hours.
The official documentation says it takes 4 hours, but when I crash and recover, it's mentally difficult to get back to the previous state 4 hours, so I did it every hour (laughs).
** Save backups for generations **
If you just keep backing up, the files will become huge, so set to always keep only a certain generation.
Cloud Storage
has a feature called object lifecycle management.
Simply put, it will automatically delete your old backups.
For the settings, proceed according to the item "Automatically delete old backups" in the official document. Items with settings different from the 1-point official document are In the Select Object Condition section, select Age. I changed my age to 3 days instead of 7 days. In my case, I make backups every hour, so 24 times a day, and if I leave for 7 days, 168 backups will remain. As expected, it was set to 3 days in consideration of cost.
How was it? This is the end of building the Minecraft server. Next time, we will be able to control the Minecraft server created this time from Discord.
Next time-> A story about operating a GCP instance from Discord
Recommended Posts