What you need

To complete this lab, you need:

Internet access

Access to a supported Internet browser:

What you do

What you learn

You will setup a game application - a Minecraft Server.

The Minecraft server software will run on a Compute Engine instance.

You will use an n1-standard-1 machine type includes a 10 GB boot disk, 1 virtual CPU (vCPU), and 3.75 GB of RAM. This machine type runs Debian Linux by default.

To make sure there is plenty of room for the Minecraft server's world data, you'll also attach a high performance 50 GB persistent solid-state drive (SSD) to the instance. This dedicated Minecraft server can support up to 50 players.

General activities:

Step 1 Create a Project

Select an existing project or create a new google cloud project.

Remember the project ID, a unique name across all Google Cloud projects. It will be referred to later in this lab as PROJECT_ID.

Step 1 Define a VM using advanced options

Console: Products and Services > Compute Engine > VM instances

Click on [Create instance].

Property

Value

Name:

mc-server

Zone:

us-central1-f

Machine type:

1 vCPU (n1-standard-1)

You shouldn't need to change the following settings, just verify them.

Boot disk:

New 10GB, Debian Linux

Under Identity and API access:

Service account:

don't change

Compute Engine default service account

Firewall:

don't change

Step 2 Add API access for Cloud Storage

  1. In the Identity and API access section, find Access Scopes.
  2. Select Set access for each API. This will open another menu.

  1. Scroll down the menu and find Storage.
  1. Pull down the associated menu with Storage and set it to Read Write.

Don't click [Create] yet! This VM requires advanced options!

Step 3 Open the tabbed menu for advanced options

Open the advanced options menu by clicking on Management, disk, networking, SSH keys.

You will be using these tabs in the coming steps:

Step 4 Add a Tag for a firewall rule

Click on the Management tab. Add a tag that will be used for firewall rules.

Property

Value

Tag:

minecraft-server

Step 5 Create an additional data disk

Click on the Disks tab. Add a disk that will be used for game storage.

  1. Click [ + Add Item ]
  2. Under Additional Disks, click on the menu and select Add Item. A new menu will open. Use the following information to specify the disk.

Property

Value

Name:

minecraft-disk

Disk Type:

SSD Persistent Disk

Source type:

None (blank disk)

Size (GB):

50

Encryption:

Automatic (recommended)

Click [Create] on the disk menu. This will create the disk and automatically attach it to the VM when the VM is created.

Step 6 Create an External static IP

Click on the Networking tab.

Under External IP, select New Static IP Address...

Property

Value

Name:

mc-server-ip

Click the [Reserve] button.

Step 7 Create the VM

Click the [Create] button

The disk is attached to the instance, but it is not yet mounted or formatted.

From the VM Instances page, click on SSH to open a terminal to the VM.

Step 1 Create a directory

This directory will serve at the mount point for the data disk.

$ sudo mkdir -p /home/minecraft

Step 2 Format the disk

$ sudo mkfs.ext4 -F -E lazy_itable_init=0,\
lazy_journal_init=0,discard \
/dev/disk/by-id/google-minecraft-disk
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   

Step 3 Mount the disk

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

The Minecraft server runs on top of the Java Virtual Machine (JVM), so it requires the Java Runtime Environment (JRE) to run. Because the server doesn't need a graphical user interface, you use the headless version of the JRE. This reduces the JRE's resource usage on the machine, helping ensure that the Minecraft server has of room to expand its own resource usage if needed.

Step 1 Install the Java Runtime Environment (JRE)

To download and install the headless version of the JRE, you will first need to update the Debian repositories on the VM.

$ sudo apt-get update

After the repositories are updated, install the headless JRE.

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

Step 2 Install the Minecraft Server

Move to the directory where the persistent disk is mounted.

$ cd /home/minecraft

Download the current Minecraft Server JAR file. You will use the 1.11.2 JAR.

$ sudo wget https://s3.amazonaws.com/Minecraft.Download/versions/1.11.2/minecraft_server.1.11.2.jar
$ sudo java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.11.2.jar nogui

Step 3 Initialize the Minecraft Server

00:28:34] [Server thread/INFO]: Starting minecraft server version 1.11.2
[00:28:34] [Server thread/INFO]: Loading properties
[00:28:34] [Server thread/WARN]: server.properties does not exist
[00:28:34] [Server thread/INFO]: Generating new properties file
[00:28:34] [Server thread/WARN]: Failed to load eula.txt
[00:28:34] [Server thread/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more 
info.
[00:28:34] [Server thread/INFO]: Stopping server

The Server won't run unless you accept the terms of the EULA.

Take a look at some of the files the first run created.

$ sudo ls -l

One file is the server.properties which you could edit to change the default behavior of the minecraft server.

Use your favorite text editor to edit the eula.txt file. Remember to use sudo so you can save the edit. Change the last line that says eula=false to eula=true.

Step 4 Create a virtual terminal screen

If you start the Minecraft server again at this point, it will be tied to the life of your SSH session—that is, if you close your SSH terminal, the server will be terminated as well. To get around this issue, you can use screen, an application that allows you to create a virtual terminal that can be "detached," becoming a background process, or "reattached," becoming a foreground process. When a virtual terminal is detached to the background, it will run whether you are logged in or not.

In your SSH terminal, run the following to install screen:

$ sudo apt-get install -y screen

Step 5 Start the Minecraft Server in a screen

Start your Minecraft server in a screen virtual terminal. Use the -S flag to name your terminal mcs:

$ sudo screen -S mcs java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.11.2.jar nogui
08:34:57] [Server thread/INFO]: Starting minecraft server version 1.11.2
[08:34:57] [Server thread/INFO]: Loading properties
[08:34:57] [Server thread/INFO]: Default game type: SURVIVAL
[08:34:57] [Server thread/INFO]: Generating keypair
[08:34:57] [Server thread/INFO]: Starting Minecraft server on *:25565
[08:34:57] [Server thread/INFO]: Using epoll channel type
[08:34:57] [Server thread/INFO]: Preparing level "world"
[08:34:58] [Server thread/INFO]: Preparing start region for level 0
[08:34:59] [Server thread/INFO]: Preparing spawn area: 0%
[08:35:00] [Server thread/INFO]: Preparing spawn area: 5%
[08:35:01] [Server thread/INFO]: Preparing spawn area: 74%
[08:35:01] [Server thread/INFO]: Done (3.886s)! For help, type "help" or "?"

Step 6 Detach from the screen

Detach the screen terminal by pressing [Ctrl]+[a], then typing [d]. The terminal will continue to run in the background. To reattach the terminal, run screen -r <terminal_name> as follows:

$ sudo screen -r mcs

Step 7 close the SSH session

  1. If necessary, exit the screen by pressing [Ctrl][a][d].
  2. Exit the SSH session.

Congratulations! You've set up and customized a VM and installed and configured application software -- a Minecraft Server!

Up to this point, the server has an external static IP address, but it can receive no traffic because there is no firewall rule in place. Minecraft server uses TCP port 25565 by default. So you will need to configure a firewall rule to allow these connections.

Step 1 Create a firewall rule

Console: Products and Services > Networks > Firewall rules

Click on [New firewall rule].

Property

Value

Name:

minecraft-rule

Source Filter:

Allow from any source (0.0.0.0/0)

Allowed protocols or ports:

tcp:25565

Target tags:

minecraft-server

Click the [Create] button.

Users can now access your server from their Minecraft clients.

Step 2 Verify server availability

You can use on of these Minecraft Server verification sites to test the server:

https://dinnerbone.com/minecraft/tools/status/

http://www.serversminecraft.net/minecraft-server-status-checker.html

Or if you are using Chrome, there is an app for that:

A common activity is to backup your application data. In this case you will configure the system to backup Minecraft world data to Cloud Storage.

Step 1 Create a Cloud Storage bucket

  1. SSH to the VM from the VM instances page.
  2. Create the bucket using the gsutil tool, part of the Cloud SDK. The bucket name must be globally unique. To make it unique, you can prepend the name with your Project ID.
$ sudo gsutil mb gs://<project_id>-minecraft-backup

Step 2 Create a backup script

Create the script in your /home/minecraft directory

$ sudo nano /home/minecraft/backup.sh

Copy and paste this script into the file:

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

Replace <bucket> in the script with your Cloud Storage bucket.

Make the script executable

$ sudo chmod 755 /home/minecraft/backup.sh

Step 2 Test the backup script

$ sudo /home/minecraft/backup.sh

After the script finishes. Verify that the backup file was written using the Cloud Storage browser in the console.

Console: Products and Services > Storage > Browser

Click on the backup bucket name.

You should see a folder with a date-time stamp name.

Step 3 Schedule cron job

Now that you have verified that backups are working, you can schedule a cron job to automate this task.

Open the cron table for editing:

$ sudo crontab -e

Scroll to the bottom of the page and enter paste the following line. It instructs cron to run backups every 4 hours.

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

Save and exit.

To perform server maintenance you need to shutdown the server.

Step 1 SSH to the server and stop it

Enter the following command:

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

Step 2 Shutdown the VM

Console: Products and Services > Compute Engine > VM instances

Select the VM instance.

Click the [Stop] button

You will be logged out of your SSH session.

Step 3 Automate server maintenance with startup and shutdown scripts

Rather than going through 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 things for you.

Console: Products and Services > Compute Engine > VM instances

Click on the VM instance.

Click [Edit].

In the Custom metadata section, add a new key called startup-script and copy the following script into its Value field:

#!/bin/bash
mount /dev/disk/by-id/google-minecraft-disk /home/minecraft
(crontab -l ; echo "0 */4 * * * /home/minecraft/backup.sh")| crontab -
cd /home/minecraft
screen -d -m -S mcs java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.11.2.jar nogui

To automate your shutdown procedure, add another key called shutdown-script and copy the following into its Value field:

#!/bin/bash
sudo screen -r -X stuff '/stop\n'

These scripts will run automatically when you start or stop your instance.

When you restart your instance, the startup-script will automatically mount the Minecraft disk to the appropriate directory, reinstall your cron job, start your Minecraft server in a screen session, and detach the session.

When you stop the instance, the shutdown-script will shut down your Minecraft server before the instance shuts down.

©Google, Inc. or its affiliates. All rights reserved. Do not distribute.