INTRODUCTION

Learn how to set up your game application (Minecraft server) on Google Cloud using a powerful e2-medium machine with 2 CPUs, 4 GB of RAM, and a 50 GB SSD for extra storage. Perfect for hosting up to 50 players, this guide makes it easy to create your high-performance gaming world.

OBJECTIVES

In this article, you learn how to perform the following tasks:

  • Customize an application server
  • Install and configure the necessary software
  • Configure network access
  • Schedule regular backups

PROCEDURE

  1. Start the Lab:
    • Open Qwiklabs in an incognito window and note the time limit.
    • Click Start Lab to begin—remember, there’s no pause feature.
  2. Access Google Cloud:
    • Use the provided lab credentials (Username and Password) to log in.
    • Click Open Google Console and select Use another account to enter the credentials.
  3. Begin Working:
    • Accept the terms and skip the recovery page to avoid errors or extra charges.

Start your lab and ensure you complete it within the given time.

Cloud overview

VIDEO TUTORIAL

Task 1. Create the VM

Define a VM using advanced options

  1. Click Create Instance.
  2. Specify and leave the remaining settings as their defaults
  3. Click Advanced options.
  4. Click Disks and backups. You will add a disk to be used for game storage.
  5. Click Add new disk.
  6. Specify and leave the remaining settings as their defaults
  7. Click Save. This creates the disk and automatically attaches it to the VM when the VM is created.
  8. Click Networking.
  9. Specify and leave the remaining settings as their defaults
  10. Click Reserve>Done>Create.

In the Cloud Console, on the Navigation menu, click Compute Engine > VM instances.

VM instance

Task 2. Prepare the data disk

Create a directory and format and mount the disk
The disk is attached to the instance, but it is not yet mounted or formatted.

  1. To create a directory that serves as the mount point for the data disk, run the following command:

For mc-server, click SSH to open a terminal and connect.

mc-server
sudo mkdir -p /home/Minecraft

3.To format the disk, run the following command:

sudo mkfs.ext4 -F -E lazy_itable_init=0,\
lazy_journal_init=0,discard \
/dev/disk/by-id/google-minecraft-disk

Result (this is example output):

mke2fs 1.42.12 (29-Aug-2014)
Discarding device blocks: done
Creating filesystem with 13107200 4k blocks and 3276800 inodes
Filesystem UUID: 3d5b0563-f29e-4107-ad1a-ba7bf11dcf7c
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

4.To mount the disk, run the following command:

sudo mount -o discard,defaults /dev/disk/by-id/google-minecraft-disk /home/minecraft

No output is displayed after the disk is mounted.

Task 3. Install and run the application

The Minecraft server runs on the Java Virtual Machine (JVM) and needs the Java Runtime Environment (JRE). Using the headless JRE reduces resource usage, ensuring the server has enough capacity to expand as needed.

Install the Java Runtime Environment (JRE) and the Minecraft server
1.In the SSH terminal for mc-server, to update the Debian repositories on the VM, run the following command:

sudo apt-get update

2.After the repositories are updated, to install the headless JRE, run the following command:

sudo apt-get install -y default-jre-headless

3.To navigate to the directory where the persistent disk is mounted, run the following command:

cd /home/minecraft

4.To install wget, run the following command:

sudo apt-get install wget

5.If prompted to continue, type Y.

6.To download the current Minecraft server JAR file (1.11.2 JAR), run the following command:

sudo wget https://launcher.mojang.com/v1/objects/d0d0fe2b1dc6ab4c65554cb734270872b72dadd6/server.jar

Initialize the Minecraft server
1.To initialize the Minecraft server, run the following command:

sudo java -Xmx1024M -Xms1024M -jar server.jar nogui

Result (example output):

[21:01:54] [main/ERROR]: Failed to load properties from file: server.properties
[21:01:54] [main/WARN]: Failed to load eula.txt
[21:01:54] [main/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.

2.To see the files that were created in the first initialization of the Minecraft server, run the following command:

sudo ls -l

3.To edit the EULA, run the following command:

sudo nano eula.txt

4.Change the last line of the file from eula=false to eula=true.
5.Press Ctrl+O, ENTER to save the file and then press Ctrl+X to exit nano.

Create a virtual terminal screen to start the Minecraft server
Restarting the Minecraft server ties it to your SSH session, meaning it stops if the terminal closes. To prevent this, use screen to create a virtual terminal that can run in the background or be reattached later, ensuring the server continues running even when you're logged out.
1.To install screen, run the following command:

sudo apt-get install -y screen

2.To start your Minecraft server in a screen virtual terminal, run the following command (using the -S flag to name your terminal mcs):
sudo screen -S mcs java -Xmx1024M -Xms1024M -jar server.jar nogui
Result (example output):

...
[21:06:06] [Server-Worker-1/INFO]: Preparing spawn area: 83%
[21:06:07] [Server-Worker-1/INFO]: Preparing spawn area: 85%
[21:06:07] [Server-Worker-1/INFO]: Preparing spawn area: 86%
[21:06:08] [Server-Worker-1/INFO]: Preparing spawn area: 88%
[21:06:08] [Server-Worker-1/INFO]: Preparing spawn area: 89%
[21:06:09] [Server-Worker-1/INFO]: Preparing spawn area: 91%
[21:06:09] [Server-Worker-1/INFO]: Preparing spawn area: 93%
[21:06:10] [Server-Worker-1/INFO]: Preparing spawn area: 95%
[21:06:10] [Server-Worker-1/INFO]: Preparing spawn area: 98%
[21:06:11] [Server-Worker-1/INFO]: Preparing spawn area: 99%
[21:06:11] [Server thread/INFO]: Time elapsed: 55512 ms
[21:06:11] [Server thread/INFO]: Done (102.484s)! For help, type "help"

Detach from the screen and close your SSH session

  1. To detach the screen terminal, press Ctrl+A, Ctrl+D. The terminal continues to run in the background. To reattach the terminal, run the following command: sudo screen -r mcs
  2. If necessary, exit the screen terminal by pressing Ctrl+A, Ctrl+D.
  3. To exit the SSH terminal, run the following command: exit

Task 4. Allow client traffic

The server has a static IP but can't receive traffic without a firewall rule. Configure the rule to allow TCP port 25565, which Minecraft uses by default.

Create a firewall rule

  1. In the Cloud Console, on the Navigation menu, click VPC network > Firewall.
  2. Click Create firewall rule.
  3. Specify and leave the remaining settings as their defaults
  4. For tcp, specify port 25565.
  5. Click Create. Users can now access your server from their Minecraft clients.

Verify server availability

  1. Navigate to VPC network.
  2. In the left pane, click IP addresses.
  3. Locate and copy the External IP address for the mc-server VM.
  4. Use Minecraft Server Status to test your Minecraft server.

Task 5. Schedule regular backups

Backing up your application data is a common activity. In this case, you configure the system to back up Minecraft world data to Cloud Storage.

Create a Cloud Storage bucket

  1. On the Navigation menu, click Compute Engine > VM instances.
  2. For mc-server, click SSH.
  3. Create a globally unique bucket name, and store it in the environment variable YOUR_BUCKET_NAME. To make it unique, you can use your Project ID. Run the following command:
export YOUR_BUCKET_NAME=<Enter your bucket name here>

4.Verify it with echo:

echo $YOUR_BUCKET_NAME

5.To create the bucket using the gcloud storage tool, part of the Cloud SDK, run the following command:

gcloud storage buckets create gs://$YOUR_BUCKET_NAME-minecraft-backup

Create a backup script
1.In the mc-server SSH terminal, navigate to your home directory:

cd /home/minecraft

2.To create the script, run the following command:

sudo nano /home/minecraft/backup.sh

3.Copy and paste the following script into the file:

#!/bin/bash
screen -r mcs -X stuff '/save-all\n/save-off\n'
/usr/bin/gcloud storage cp -R ${BASH_SOURCE%/*}/world gs://${YOUR_BUCKET_NAME}-minecraft-backup/$(date "+%Y%m%d-%H%M%S")-world
screen -r mcs -X stuff '/save-on\n'

4.Press Ctrl+O, ENTER to save the file, and press Ctrl+X to exit nano.
5.To make the script executable, run the following command:

sudo chmod 755 /home/minecraft/backup.sh

Test the backup script and schedule a cron job
1.In the mc-server SSH terminal, run the backup script:

. /home/minecraft/backup.sh

2.After the script finishes, return to the Cloud Console.
3.To verify that the backup file was written, on the Navigation menu, click Cloud Storage > Buckets.
4.Click on the backup bucket name. You should see a folder with a date-time stamp name. Now that you've verified that the backups are working, you can schedule a cron job to automate the task.
5.In the mc-server SSH terminal, open the cron table for editing:

sudo crontab -e

6.When you are prompted to select an editor, type the number corresponding to nano, and press ENTER.
7.At the bottom of the cron table, paste the following line:

0 */4 * * * /home/minecraft/backup.sh

Note: That line instructs cron to run backups every 4 hours.
8.Press Ctrl+O, ENTER to save the cron table, and press Ctrl+X to exit nano.

Task 6. Server maintenance

To perform server maintenance, you need to shut down the server.
Connect via SSH to the server, stop it and shut down the VM
1.In the mc-server SSH terminal, run the following command:

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

2.In the Cloud Console, on the Navigation menu, click Compute Engine > VM instances.
3.Select mc-server and click Stop.
4.In the confirmation dialog, click Stop to confirm. You will be logged out of your SSH session.

Automate server maintenance with startup and shutdown scripts
Instead of following the manual process to mount the persistent disk and launch the server application in a screen, you can use metadata scripts to create a startup script and a shutdown script to do this for you.

1.Click mc-server and click Edit.
2.For Metadata, click + Add Item and specify the following:

Key: startup-script-url
Value: https://storage.googleapis.com/cloud-training/archinfra/mcserver/startup.sh
Key: shutdown-script-url
Value: https://storage.googleapis.com/cloud-training/archinfra/mcserver/shutdown.sh
3.Click Save

CONCLUSION
In this article, you created a virtual machine, installed a headless JRE and Minecraft server, added a high-speed SSD, and reserved a static IP. You verified the server online, set up and tested backups to Cloud Storage, automated them with cron, and used metadata to script graceful startup and shutdown.

Author Of article : Oluwatobiloba Akinbobola Read full article