Deploy Minecraft Server in GCP Compute Engine

google compute engine



Task 1: Create the VM

Define a VM using advanced options

| Property                                | Value                  |
| Name | mc-server |
| Region | us-central1 |
| Zone | us-central1-a |
| Identity and API access > Access scopes | Set access for each API| | Storage | Read/Write |
| Property    | Value (type value or select option as specified) |
| Name | minecraft-disk |
| Disk type | SSD Persistent Disk |
| Source type | None (blank disk) |
| Size (GB) | 50 |
| Encryption | Google-managed key |

Create an External static IP

| Property           | Value (type value or seleion as specified) |
| Networks tags | minecraft-server |
| Network interfaces | Click default to edit the interface |
| External IP | Create IP Address |
| Name | mc-server-ip |
| Encryption | Google-managed key |

Task 2: Prepare the data disk

Create a directory and format and mount the disk

sudo mkfs.ext4 -F -E lazy_itable_init=0,\
lazy_journal_init=0,discard \
: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

Task 3: Install and run the application

Install the Java Runtime Environment (JRE) and the Minecraft server

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]: 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
[00:28:34] [Server thread/INFO]: Stopping server
he Minecraft server won't run unless you accept the terms of the End User Licensing Agreement (EULA).
You could edit the file to change the default behavior of the Minecraft server.
Don't try to restart the Minecraft server yet. You use a different technique in the next procedure.

Create a virtual terminal screen to start the Minecraft server

[14:52:12] [Server thread/INFO]: Preparing level "world"
[14:52:14] [Server thread/INFO]: Preparing start region for level 0
[14:52:15] [Server thread/INFO]: Preparing spawn area: 6%
[14:52:16] [Server thread/INFO]: Preparing spawn area: 10%
[14:52:17] [Server thread/INFO]: Preparing spawn area: 15%
[14:52:18] [Server thread/INFO]: Preparing spawn area: 21%
[14:52:19] [Server thread/INFO]: Preparing spawn area: 26%
[14:52:20] [Server thread/INFO]: Preparing spawn area: 32%
[14:52:21] [Server thread/INFO]: Preparing spawn area: 39%
[14:52:22] [Server thread/INFO]: Preparing spawn area: 46%
[14:52:23] [Server thread/INFO]: Preparing spawn area: 53%
[14:52:24] [Server thread/INFO]: Preparing spawn area: 58%
[14:52:25] [Server thread/INFO]: Preparing spawn area: 66%
[14:52:26] [Server thread/INFO]: Preparing spawn area: 74%
[14:52:27] [Server thread/INFO]: Preparing spawn area: 81%
[14:52:28] [Server thread/INFO]: Preparing spawn area: 89%
[14:52:29] [Server thread/INFO]: Preparing spawn area: 99%
[14:52:29] [Server thread/INFO]: Done (16.831s)! For help, type "help" or "?"

Detach from the screen and close your SSH session

Congratulations! You set up and customized a VM and installed and configured application software — a Minecraft server!

Task 4: Allow client traffic

Create a firewall rule

| Property            | Value                         |
| Name | minecraft-rule |
| Target Specified | target tags |
| Target tags | minecraft-server |
| Source filter | IP ranges |
| Source IP ranges | |
| Protocols and ports | Specified protocols and ports |

Verify server availability

If the above website is not working, feel free to use a different site or the Chrome extension:
Server Status Minecraft extension (Chrome)

Task 5: Schedule regular backups

Create a Cloud Storage bucket

If this command failed, you might not have created a unique bucket name. If so, choose another bucket name, update your environment variable, and try to create the bucket again.

Create a backup script

screen -r mcs -X stuff '/save-all\n/save-off\n'
/usr/bin/gsutil 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'
The script saves the current state of the server's world data and pauses the server's auto-save functionality. Next, it backs up the server's world data directory (world), placing its contents in a timestamped directory (<timestamp>-world) in the Cloud Storage bucket. After the script finishes backing up the data, it resumes auto-saving on the Minecraft server.

Test the backup script and schedule a cron job

That line instructs cron to run backups every 4 hours.
This creates about 300 backups a month in Cloud Storage. So you will want to regularly delete them to avoid charges.

Task 6: Server maintenance

Connect via SSH to the server, stop it, and shut down the VM

To start up your instance again, visit the instance page and then click Start. To start the Minecraft server again, you can establish an SSH connection with the instance, remount your persistent disk, and start your Minecraft server in a new screen terminal, just as you did previously.

Automate server maintenance with startup and shutdown scripts

mount /dev/disk/by-id/google-minecraft-disk /home/minecraft
(crontab -l ; echo "0 */4 * * * /home/minecraft/")| crontab -
cd /home/minecraft
screen -d -m -S mcs java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.11.2.jar nogui
sudo screen -r -X stuff '/stop\n'
These scripts run automatically when you start or stop your instance.
When you restart your instance, the startup script automatically mounts the Minecraft disk to the appropriate directory, reinstalls your cron job, starts your Minecraft server in a screen session, and detaches the session. When you stop the instance, the shutdown script shuts down your Minecraft server before the instance shuts down.

Task 7: Review

software developer