arrow_back

Working with Virtual Machines

Zyskaj dostęp do ponad 700 modułów i kursów

Working with Virtual Machines

Moduł 1 godz. 30 godz. universal_currency_alt Punkty: 5 show_chart Wprowadzające
info Ten moduł może zawierać narzędzia AI, które ułatwią Ci naukę.
Zyskaj dostęp do ponad 700 modułów i kursów

Overview

In this lab, you set up a game application—a Minecraft server.

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

You use an e2-medium machine type that includes a 10-GB boot disk, 2 virtual CPU (vCPU), and 4 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 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.

Objectives

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

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

Setup and Requirements

For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left, or type the service or product name in the Search field.

Task 1. Create the VM

In this task, you create a VM that will host your Minecraft-server and install the software.

Define a VM using advanced options

  1. In the Google Cloud console, in the Navigation menu (), click Compute Engine > VM instances.
  2. Click Create instance.
  3. Specify the following and leave the remaining settings as their defaults:
Property Value (type value or select option as specified)
Name mc-server
Region
Zone
  1. Click OS and storage.

  2. Ensure that the boot disk image is shown as Debian GNU/Linux 12 (bookworm). If not, click Change and select it from the Version drop down list, and then click Select.

  3. Click Add new disk.

  4. Specify the following and leave the remaining settings as their defaults:

Property Value (type value or select option as specified)
Name minecraft-disk
Disk source type Blank Disk
Disk settings SSD Persistent Disk
Size (GB) 50
Encryption Google-managed encryption key
  1. Click Save. This creates the disk and automatically attaches it to the VM when the VM is created.

  2. Click Networking.

  3. Specify the following and leave the remaining settings as their defaults:

Property Value (type value or select option as specified)
Network tags minecraft-server
Network interfaces Click default to edit the interface
External IPv4 address Reserve Static External IP Address
Name mc-server-ip
  1. Click Reserve, and then click Done.

  2. Click Security.

  3. Specify the following and leave the remaining settings as their defaults:

Property Value (type value or select option as specified)
Identity and API access > Access Scopes Set access for each API
Storage Read Write
  1. Click Create.

Task 2. Prepare the data disk

In this task, you format and mount the minecraft 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. For mc-server, click SSH to open a terminal and connect.
  2. To create a directory that serves as the mount point for the data disk, run the following command:
sudo mkdir -p /home/minecraft
  1. 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.47.0 (5-Feb-2023) 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
  1. 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.

Click Check my progress to verify the objective.

Create the VM and prepare the data disk

Task 3. Install and run the application

In this task, you install and run the Minecraft application. 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, which helps ensure that the Minecraft server has enough room to expand its own resource usage if 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
  1. After the repositories are updated, to install the headless JRE, run the following command:
sudo apt-get install -y default-jre-headless
  1. To navigate to the directory where the persistent disk is mounted, run the following command:
cd /home/minecraft
  1. To install wget, run the following command:
sudo apt-get install wget
  1. If prompted to continue, type Y.

  2. 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. Note: The Minecraft server won't run unless you accept the terms of the End User Licensing Agreement (EULA).

Click Check my progress to verify the objective.

Install the Java Runtime Environment (JRE) and the Minecraft server
  1. To see the files that were created in the first initialization of the Minecraft server, run the following command:
sudo ls -l Note: You could edit the server.properties file to change the default behavior of the Minecraft server.
  1. To edit the EULA, run the following command:
sudo nano eula.txt
  1. Change the last line of the file from eula=false to eula=true.
  2. Press Ctrl+O, ENTER to save the file and then press Ctrl+X to exit nano.
Note: 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

If you start the Minecraft server again now, it is tied to the life of your SSH session: that is, if you close your SSH terminal, the server is also terminated. To avoid 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.

  1. To install screen, run the following command:
sudo apt-get install -y screen
  1. 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
  1. If necessary, exit the screen terminal by pressing Ctrl+A, Ctrl+D.
  2. To exit the SSH terminal, run the following command:
exit

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

Task 4. Allow client traffic

In this task, you configure a firewall rule to allow client connections to the Minecraft Server. Up to this point, the server has an external static IP address, but it cannot receive traffic because there is no firewall rule in place.

Minecraft server uses TCP port 25565 by default.

Create a firewall rule

  1. In the Google Cloud console, in the Navigation menu (), click VPC network > Firewall.
  2. Click Create firewall rule.
  3. Specify the following and leave the remaining settings as their defaults:
Property Value (type value or select option as specified)
Name minecraft-rule
Target Specified target tags
Target tags minecraft-server
Source filter IPv4 ranges
Source IPv4 ranges 0.0.0.0/0
Protocols and ports Specified protocols and ports
  1. For tcp, specify port 25565.
  2. Click Create. Users can now access your server from their Minecraft clients.

Verify server availability

  1. In the Cloud console, in the Navigation menu (), click 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.

Note: If the above website is not working, you can use a different site or the Chrome extension:

Click Check my progress to verify the objective.

Allow client traffic

Task 5. Schedule regular backups

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

Create a Cloud Storage bucket

  1. In the Google Cloud console, in 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>
  1. Verify it with echo:
echo $YOUR_BUCKET_NAME
  1. 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 Note: 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. Note: To make this environment variable permanent, you can add it to the root's .profile by running this command:
echo YOUR_BUCKET_NAME=$YOUR_BUCKET_NAME >> ~/.profile

Create a backup script

  1. In the mc-server SSH terminal, navigate to your home directory:
cd /home/minecraft
  1. To create the script, run the following command:
sudo nano /home/minecraft/backup.sh
  1. 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'
  1. Press Ctrl+O, ENTER to save the file, and press Ctrl+X to exit nano.
Note: 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) and places 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.
  1. 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
  1. After the script finishes, return to the Cloud console.
  2. To verify that the backup file was written, in the Navigation menu ( ), click Cloud Storage > Buckets.
  3. 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.
  4. In the mc-server SSH terminal, open the cron table for editing:
sudo crontab -e
  1. When you are prompted to select an editor, type the number corresponding to nano, and press ENTER.
  2. 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.
  1. Press Ctrl+O, ENTER to save the cron table, and press Ctrl+X to exit nano.
Note: This creates about 300 backups a month in Cloud Storage, so you will want to regularly delete them to avoid charges. Cloud Storage offers the Object Lifecycle Management feature to set a Time to Live (TTL) for objects, archive older versions of objects, or "downgrade" storage classes of objects to help manage costs.

Click Check my progress to verify the objective.

Schedule regular backups

Task 6. Server maintenance

In this task, you connect to the server and shut it down in order to perform server maintenance.

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'
  1. In the Google Cloud console, in the Navigation menu ( ), click Compute Engine > VM instances.
  2. Select mc-server.
  3. Click Stop.
  4. In the confirmation dialog, click Stop to confirm. You will be logged out of your SSH session.
Note: 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

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.

  2. Click Edit.

  3. For Metadata, click + Add Item and specify the following:

    Key Value
    startup-script-url https://storage.googleapis.com/cloud-training/archinfra/mcserver/startup.sh
    shutdown-script-url https://storage.googleapis.com/cloud-training/archinfra/mcserver/shutdown.sh
Note: You'll have to click Add item to add the shutdown-script-url. When you restart your instance, the startup script automatically mounts the Minecraft disk to the appropriate directory, 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. It's a best practice to store these scripts in Cloud Storage.
  1. Click Save.

Click Check my progress to verify the objective.

Server maintenance

Task 7. Review

In this lab, you created a customized virtual machine instance by installing base software (a headless JRE) and application software (a Minecraft game server). You customized the VM by attaching and preparing a high-speed SSD data disk, and you reserved a static external IP so the address would remain consistent. Then you verified availability of the gaming server online.

You set up a backup system to back up the server's data to a Cloud Storage bucket, and you tested the backup system. Then you automated backups using cron. Finally, you set up maintenance scripts using metadata for graceful startup and shutdown of the server.

End your lab

When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.

You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

The number of stars indicates the following:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

You can close the dialog box if you don't want to provide feedback.

For feedback, suggestions, or corrections, please use the Support tab.

Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Zanim zaczniesz

  1. Moduły tworzą projekt Google Cloud i zasoby na określony czas.
  2. Moduły mają ograniczenie czasowe i nie mają funkcji wstrzymywania. Jeśli zakończysz moduł, musisz go zacząć od początku.
  3. Aby rozpocząć, w lewym górnym rogu ekranu kliknij Rozpocznij moduł.

Ta treść jest obecnie niedostępna

Kiedy dostępność się zmieni, wyślemy Ci e-maila z powiadomieniem

Świetnie

Kiedy dostępność się zmieni, skontaktujemy się z Tobą e-mailem

Jeden moduł, a potem drugi

Potwierdź, aby zakończyć wszystkie istniejące moduły i rozpocząć ten

Aby uruchomić moduł, użyj przeglądania prywatnego

Uruchom ten moduł w oknie incognito lub przeglądania prywatnego. Dzięki temu unikniesz konfliktu między swoim kontem osobistym a kontem do nauki, co mogłoby spowodować naliczanie dodatkowych opłat na koncie osobistym.