[PYTHON] A story about a GCP beginner building a Minecraft server on GCE


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)




1. Create an instance for Minecraft server

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.

Preemptible Instance Limits

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

2. Install and run Minecraft server

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.

How to use Linux 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.

Server configuration file (server.properties)

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

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.

sudo screen -r mcs -X stuff '/stop\n'

3. Set up and execute regular backup

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

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

A story about a GCP beginner building a Minecraft server on GCE
A story about a Linux beginner putting Linux on a Windows tablet
Run a Linux server on GCP
A story about a 503 error on Heroku open
A story about operating a GCP instance from Discord
A story about running Python on PHP on Heroku
Building a Python environment on a Sakura VPS server
A story about building an IDE environment with WinPython on an old Windows OS.
The story of launching a Minecraft server from Discord
[Part 1] Let's set up a Minecraft server on Linux
A story about an engineer who came only on the server side created a portfolio
A story about building a PyPI cache server (with Docker) and making me a little happy again
[Introduction to AWS] A memorandum of building a web server on AWS
A story about a python beginner stuck with No module named'http.server'
A story about switching a personally developed Web service from a rental server to GCP (Google Cloud Platform)
[Windows] A story of a beginner who stumbles on Anaconda's PATH setting.
A story about wanting to think about garbled characters on GAE / P
A refreshing story about Python's Slice
Building a Python environment on Ubuntu
A sloppy story about Python's Slice
Run TensorFlow2 on a VPS server
A story about using Python's reduce
A story about trying to run JavaScripthon on Windows and giving up.
A story about a beginner making a VTuber notification bot from scratch in Python
A story about creating an anonymous channel on Slack from zero knowledge
A story of a deep learning beginner trying to classify guitars on CNN
A story about a beginner trying hard to set up CentOS 8 (procedure memo)
A story about deploying a Twitter-linked app created using Flask + gunicorn on Heroku
Build a Pypi cache server on QNAP
A story about remodeling Lubuntu into a Chromebook
Building a LaTeX environment on Chrome OS
Build a Samba server on Arch Linux
Build a web server on your Chromebook
A story about Python pop and append
A story about trying to install uwsgi on an EC2 instance and failing
A story about a programming beginner who made a business efficiency map application with GeoDjango
Automatic Zakuzaku, Bitcoin. A story about a Python beginner making a coin check 1-minute chart
A story about creating a web application that automatically generates Minecraft sound block performances