Blog-Desktop_100x385

Lets build a Minecraft Server with Chef


There are many ways to use Chef and not all of them have to be related to your day job. If you’re a fan of Minecraft you can use Chef to create a Minecraft server on Digital Ocean. The initial setup is only done the first time, then you’ll be able to do just the create command when you want a new Minecraft Server.

You can do the following steps with something like Rackspace or AWS you just need to switch out the knife-digital_ocean commands with either knife-rackspace or knife-ec2. There are different configurations you need to do with either knife plugins, and slightly different commands to create servers, so please read the documentation.

  1. Download the ChefDK, and install it.
  2. Install the Digital Ocean knife plugin chef exec gem install knife-digital_ocean
  3. Sign up for hosted Chef, and log in to your org you create.
  4. Pull down the “starter kit,” which is located at https://manage.chef.io/organizations//getting_started.
  5. Configure the knife.rb for Digital Ocean you’ll find it in the “starter kit” in the .chef/ directory.
  6. Git clone the minecraft-basic into the chef-repo/cookbooks/ directory.
  7. In your main chef-repo/ directory run the following steps:
    1. Run chef exec knife status to verify you can talk to your hosted Chef instance.
    2. Run chef exec knife digital_ocean sshkey list to verify you can talk to Digital Ocean, and figure out your SSHKEYNUMBER for a following step.
    3. Run chef exec knife cookbook upload minecraft-basic to upload minecraft-basic cookbook to your hosted Chef instance.
    4. Run chef exec knife cookbook list to verify you successfully uploaded the cookbook. You should see the cookbook name and version number output.
  8. You should be ready to run something like the following: chef exec knife digital_ocean droplet create --server-name minecraft --image ubuntu-14-04-x64 --location sfo1 --size 4gb --ssh-keys --bootstrap --run-list "recipe[minecraft-basic]" You may want to tweak this for your usage, I picked 4 gig because it seems java seems to play nice with this size. A 4 gig box will run you 40 bucks a month, or $0.06 cents an hour, while a 2 gig instance will cost you $0.03 per hour or 20 bucks a month. Don’t forget to blow the machine away, if you aren’t planing on running the Digital Ocean server 24×7. I take no responsibility for your forgetfulness ;). If you would like to use CentOS 7 you can change the --image ubuntu-14-04-x64 to --image centos-7-0-x64. I haven’t tested this build with anything other then CentOS or Ubuntu, but I’d be interested if you got it working on other distributions.
  9. I haven’t automated the saving or exporting the worlds that is created with this cookbook yet. But here is a link to a place that tells you how to do it by hand, in case you need a refresher.
  10. When you are done, you can blow it all away with these commands:

[shell]
~$ SERVER=`chef exec knife digital_ocean droplet list | grep minecraft | awk -F ‘ ‘ {‘print $1’}`
~$ chef exec knife digital_ocean droplet destroy -S $SERVER # This destroys the machine on Digital Ocean
~$ chef exec knife node delete minecraft -y && chef exec knife client delete minecraft -y # This deletes it from Hosted Chef
[/shell]

If you’ve completed these steps, you won’t need to login to the Host Chef instance unless you want to check the cookbook. You should be able to just spin up your Minecraft server with just chef exec knife digital_ocean droplet create --server-name minecraft --image ubuntu-14-04-x64 --location sfo1 --size 4gb --ssh-keys --bootstrap --run-list "recipe[minecraft-basic]". And use the above commands to blow everything away.

I’ll post another article when I figure out a good/chefie/automated way to backup, export, and share your worlds you create.

Posted in:

JJ Asghar

JJ works with Strategic Technical Alliances at Chef Software making integrations work with Chef, Habitat, and InSpec. He works on everything from Azure, VMware, OpenStack, and Cisco with everything in between. He also heads up the Chef Partner Cookbook Program to make sure customers of Chef and vendors get the highest quality certified cookbooks. He grew up and currently lives in Austin, Texas.