Quantcast
Channel: the newtown nerd » amazon ec2
Viewing all articles
Browse latest Browse all 2

Upgrading your Minecraft EC2 cloud server

0
0

Firstly, let me thank everyone who read my first two articles on setting up a free Minecraft server in the cloud with Amazon EC2. The posts have proved very popular and are currently accounting for about 80% of all traffic to this blog. So due to popular demand here is the third article in the series.

Since I last posted on this topic, Minecraft has gone “gold” and dropped the beta tag. This happened during “Minecon” in November. My son was one of the first in the world to download it since after the announcement and while we were watching the delegates rushing to the door over the streaming video, we already had it downloading in another window.  By the end of the day we had upgraded our cloud server and enjoying all the Minecraft 1.0 goodness.

Many readers have complained that the free Micro instance is laggy and only capable of supporting a handful of players at a time. This is true – but it is free! If you want more, you need to start paying. Upgrading to an EC2 “Small” instance server is the next step. This post will guide you through the upgrade process while minimising the cost.

First, let me make this perfectly clear, if you create a Small instance server on EC2, you will pay for it as it is not part of the “free tier”. It’s pretty cheap but not free. Ours is 9.5c per hour or US$2.28/day. That’s about US$70/month for a 24/7 server. That’s expensive but we are not going to build a 24/7 server, we will shutdown the server when its not in use – i.e. during school hours and for most of the night. If we limit little Johnnie or Jessie to 2 hours per day between homework and bedtime that works out to be only $5.77 / month. Add a few more hours on the weekend and we are probably up around $10/month – still cheaper than cable TV.

Check the pricing and make sure you understand what you are committing to. Don’t come running to me when you get your first $70 bill because you forgot to stop your instance.

We setup a 1.7GB small instance Minecraft server on EC2 with CraftBukkit 1.1 (a modified version of Minecraft) and it works very well.

1. Back up your server now

Unfortunately, it’s not possible to just “upgrade” an existing instance – you have to start afresh. A good approach is to build a completely new server from scratch with all the latest plugins and patches and once you are happy with it shutdown the old server and just leave it there for a while – just in case. If you forgot something, or just want to go back to your old world, you can. Amazon only charge for storage space for keeping stopped instances of servers and storage space is very cheap so you can afford to leave it there as a backup.

If you want to be really careful, now may be a good time to copy all the files from the old server into a folder on your local PC. The easiest way to do this is to use WinSCP (Windows) or Cyberduck (MAC). We won’t be using it for this exercise but it’s a good thing to do anyway.

2. Creating a small instance server

Essentially, you just need to follow the steps in part 1 of this series but instead of choosing a Micro Linux instance, choose a Small Linux instance. Follow the rest of the instructions and make sure you download the latest version of the Minecraft server code from here.

Once you have your server up and running, make sure it works OK by connecting to it from a client and checking out your new world as explained in part 2.

3. Tick tick, time is money. Scheduling automatic shutdowns.

We are running a Small Instance server so now you pay for every minute it is running. Move fast!

No, really stay calm. 9.5c per hour is not going to break anyone’s credit card balance – unless of course you forget to turn the server off! Remember shutting down an EC2 instance is different to just shutting down the Minecraft server that is running on it. Amazon will still charge you for a running server instance even if your Minecraft server is not running on it. The server instance must be stopped.

So the first thing we are going to setup is a job that automatically stops the instance at set times of the day – even if we forget to. Linux has a tool for this – called cron. Cron simply runs commands at a set time of day, day of week, day of month etc. We will use Cron to shutdown the server instance at a set time each evening. Say Johnny’s bedtime.

The command we need to issue is shutdown. The shutdown command does the obvious and shuts down the machine – this is the same as stopping an instance in EC2 speak. Pretty simple. The only fly in the ointment is that shutdown is a “protected” command. Only super users (administrators) can issue it. Even though you create this server and you started it, by default, you connect to it as a lowly “user” called ec2-user. This is for your own good. Being a super user on Linux is dangerous and should be avoided. Having said that, for our exercise we do not have much choice, so to become superuser enter the command

$ sudo su

Not much will happen except the prompt will change to a #. This is the standard way Linux uses to warn you are super user (and to be careful). The next thing we need to do is create a crontab. A crontab is just a text file with some entries to tell cron what jobs it has to do and when to run them. Enter the following command:

# nano crontab

This will open a text editor called Nano, ready for you to type in your commands. The format of the crontab is simple but important and must be followed exactly. Each line indicates one job and contains a set of fields. There must be at least one space between each field. The template format is shown below:

# Minute   Hour   Day of Month       Month          Day of Week        Command
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)

My table has two entries (one for school days) and one for the evenings:

55 21 * * SUN-THU /sbin/shutdown -P +5
55 10 * * * /sbin/shutdown -P +5

Cron reads this as “at 2155 hours on Sunday to Thursday call shutdown command and give users 5 minutes warning before shutting down”.

Be careful with the times – they are in UTC – London time. That’s because the EC2 servers always use UTC. You need to make your own adjustment for your local timezone. For me in Sydney, 2155 UTC equates to 9am (+1 day)  local time – when most kids would be at school. Thats why it says SUN-THU – allow for timezone adjustment and that means MON-FRI 9am. So at 9am each weekday, the server always shuts down. The other entry takes care of the local evening time – bed time.  1055 UTC equates to 11pm local Summer time or 10pm Winter time – either way – well after bedtime.

Type in your crontab and press Ctrl-O to write out the new crontab file. When you are returned to the prompt, type the following command to load the new crontab:

# crontab crontab

To confirm this worked, type this to see the active crontab:

# crontab -l

Now stop being super user by entering

# exit

The prompt should return to the $ sign.

If you can, make sure you are logged in the first time the job runs so you can see the 5 minute warning and watch the server shutting down using the EC2 console.

4. Starting an EC2 server from the desktop

The problem with shutting down a server every night is there is no simple way to automatically start it up again the next day. You could try having a cron job but cron does not run if the server is not running (of course).Worse, young kids have a habit of getting up really early on Saturday mornings… and the last thing you need is your Saturday sleep-in interrupted with pleadings to start the EC2 server.

My solution allows the child to start the server on demand by clicking an icon on their desktop. Some parents may decide this is a little too trusting given the costs involved in running the server but remember we are only talking 9.5c per hour and I don’t know about you, but my peaceful Saturday mornings are worth that.

Create a folder on your desktop called EC2.

Download the EC2 API command line tools from here. Extract them from the zip file and save the folder named ec2-api-tools-1.x.x.x in the EC2 folder.

Next we need our AWS public keys and certificates. Follow the instructions here and save the files in the EC2 folder you created.

Next we are going to create a simple script to start our EC2 server and associate it to the Elastic IP address.

On Mac:

Open text edit. Cut and paste the following lines:

#!/bin/bash
export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
export EC2_HOME=$HOME/Desktop/EC2/ec2-api-tools-1.x.x.x
export PATH=$PATH:$EC2_HOME/bin
export EC2_PRIVATE_KEY=$EC2_HOME/pk-<your pk file>.pem
export EC2_CERT=$EC2_HOME/cert-<your cert file>.pem
export EC2_URL=https://<your EC2 host location server>
echo Starting instance...
ec2-start-instances <your instance>
echo Associating address...
ec2-associate-address <your servers IP address> -i <your instance>
echo Minecraft EC2 server started OK...

Save the file in the EC2 folder and call it startmcserver.sh. Run terminal, issue the following commands to make it executable:

$ cd EC2
$ chmod +x startmcserver.sh

To execute the script type

$ ./startmcserver.sh
On Windows:

Open Notepad. Cut and past the following lines:

@echo off
set JAVA_HOME="C:\Program Files\Java\jre6"
set EC2=%HOME%\Desktop\EC2
set EC2_HOME=%EC2%\ec2-api-tools-1.x.x.x
set PATH=%PATH%;%EC2_HOME%\bin
set EC2_PRIVATE_KEY=%EC2%\pk-<your pk name>.pem
set EC2_CERT=%EC2%\cert-<your cert name>.pem
set EC2_URL=https://<your ec2 host location>
echo Starting instance...
call ec2-start-instances <your instance>
echo Associating address...
call ec2-associate-address <your server's ip address> -i <your instance name>
echo Minecraft EC2 server started OK...
pause

Save the file in the EC2 folder as startmcserver.bat. Create a shortcut to it from your desktop. Double click it to execute.

Now your child can start the EC2 Minecraft server on demand and the server will keep running until the next time the cron job is invoked to shut it down. Worst case, the server is running for a few more hours than necessary; best case it isn’t started and there is no expense.

Send me a comment about how you go.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images