Skip to content

Instantly share code, notes, and snippets.

@edco
Created March 14, 2018 02:28
Show Gist options
  • Save edco/7d39aef3fe57caeb4cd9752c2b46c1d2 to your computer and use it in GitHub Desktop.
Save edco/7d39aef3fe57caeb4cd9752c2b46c1d2 to your computer and use it in GitHub Desktop.
Create a Minecraft server on Google Cloud Platform in moments. Running this script amounts to accepting the Minecraft EULA. Based on https://cloud.google.com/solutions/gaming/minecraft-server but without the dedicated storage and backups.
resources:
- type: compute.v1.address
name: mcs-ip
properties:
region: australia-southeast1
- type: compute.v1.instance
name: mc-server
properties:
zone: australia-southeast1-c
machineType: zones/australia-southeast1-c/machineTypes/n1-standard-1
disks:
- type: PERSISTENT
deviceName: mc-server
boot: true
autoDelete: true
initializeParams:
sourceImage: projects/ubuntu-os-cloud/global/images/family/ubuntu-1604-lts
diskType: zones/australia-southeast1-c/diskTypes/pd-ssd
networkInterfaces:
- network: global/networks/default
accessConfigs:
- name: External NAT
type: ONE_TO_ONE_NAT
natIP: $(ref.mcs-ip.address)
tags:
items:
- minecraft-server
scheduling:
preemptible: true
metadata:
items:
- key: startup-script
value: |
if [ ! -d /home/minecraft ] ; then
apt-get update
apt-get dist-upgrade -y
apt-get install -y default-jre-headless screen
echo alias mcc=\"sudo su -s /bin/sh minecraft -c \'script -q -c \\\"screen -r\\\" /dev/null\'\" >> /etc/bash.bashrc
echo Welcome to Ed\'s quick\'n\'dodgy Minecraft server! > /etc/motd
echo Use the \'mcc\' command to connect to the console, and \'Ctrl+A d\' to disconnect again. >> /etc/motd
echo >> /etc/motd
adduser --system minecraft
cd /home/minecraft
sudo -u minecraft -H sh -c "
curl -sSO https://s3.amazonaws.com/Minecraft.Download/versions/1.12.2/minecraft_server.1.12.2.jar
java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.12.2.jar nogui
sed -i 's/eula=false/eula=true/' eula.txt
sed -i 's/white-list=false/white-list=true/' server.properties
cat << 'EOF' >> whitelist.json
[
{
\"uuid\": \"069a79f4-44e9-4726-a5be-fca90e38aaf5\",
\"name\": \"Notch\"
}
]
EOF"
fi
cd /home/minecraft
sudo -u minecraft -H screen -d -m -S mcs java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.12.2.jar nogui
- key: shutdown-script
value: |
#!/bin/bash
sudo -u minecraft -H screen -r -X stuff '/stop\n'
- type: compute.v1.firewall
name: minecraft-rule
properties:
allowed:
- IPProtocol: tcp
ports:
- '25565'
direction: INGRESS
network: global/networks/default
sourceRanges:
- 0.0.0.0/0
targetTags:
- minecraft-server
@edco
Copy link
Author

edco commented Mar 14, 2018

  • Edit the contents of the "whitelist.json" section to give your user(s) access to the server, rather than Notch.
  • The "scheduling" section makes your server preemptible, which means GCP may shut it down at any moment, and will always shut it down after 24 hours. It makes the instance much cheaper, and ensures that you don't leave it running for days racking up charges when it's not being used. Remove it if you'd rather pay more to ensure that your server can be up 24/7.
  • Change all instances of the strings "australia-southeast1" and "australia-southeast1-c" to the region and zone of your choice
  • If a newer version is available, update instances of the string "minecraft_server.1.12.2" to refer to the newer version
  • To use the script:
    • Sign up for a GCP account
    • Create a project
    • Open the "Cloud Shell" (one of the icons in the top bar)
    • Open the "Code Editor" from the menu bar of that shell
    • Using the code editor, create the mc-server.yaml file and paste in the contents
    • Edit as required, then save
    • Run the following commands at the cloud shell command-line:
      gcloud services enable compute.googleapis.com
      gcloud services enable deploymentmanager.googleapis.com
      gcloud deployment-manager deployments create minecraft --config=mc-server.yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment