Run a Minecraft server for free on Kubernetes
Tags: docker, kubernetes, minecraft, raspberrypi
Published: 2020-08-01 15:52:26

---
title: Run a Minecraft server for free on Kubernetes
published: true
date: 2020-07-29 00:21:19 UTC
tags: docker,kubernetes,minecraft,raspberrypi
canonical_url: https://codeburst.io/run-a-minecraft-server-for-free-on-kubernetes-ac82a892969e
cover_image: https://cdn-images-1.medium.com/max/1024/1*E8CGJR4PzBHArLnrae_ihA.jpeg
---

#### Simplify deployment for multiplayer Minecraft on a self-hosted Raspberry Pi server using Kubernetes

Just like Minecraft is more fun when a friend shows you how to make torches and avoid creepers, Kubernetes is also less scary with someone to guide the way.

The Minecraft wiki has lots of pointers for [setting up your own server](https://minecraft.gamepedia.com/Tutorials/Setting_up_a_server). But it doesn’t say how to do it with Kubernetes. I recently learned how easy it is [to deploy an app to a local Raspberry Pi with Kubernetes](https://medium.com/@joycelin.codes/ddclient-c9a6ac1d8f81?source=friends_link&sk=71085ae2ef2612b40cda0130496d1410).

Let’s try it again — this time to run a free Minecraft server on Kubernetes.

![](https://cdn-images-1.medium.com/max/293/1*MCJH-LUAyZ-VtD7wUA09GA.jpeg)<figcaption>Running a Minecraft server on Kubernetes? Piece of cake!</figcaption>

### Why Kubernetes instead of just Docker?

At a busy airport, air traffic controllers manage the flow of aircraft into and out of the airport airspace.

- **Aircraft** transport passengers and cargo from one city to another. The pilots don’t know much about how the airport is managed. They don’t worry about the other aircraft because they rely on the air traffic controllers to keep them safe and moving.
- [**Air traffic controllers**](https://www.bls.gov/ooh/transportation-and-material-moving/air-traffic-controllers.htm#tab-2) guide pilots during takeoff and landing and monitor aircraft as they travel through the skies. They’re not opinionated about the aircraft, and don’t mind any loose chickens running through the aisles 🐔

At a less busy airport, you might not need a controller to manage the safety and efficiency of the aircraft. But having one makes operations run smoothly.

![](https://cdn-images-1.medium.com/max/1024/1*eZukzpdVUcYS5WBWv6goWA.jpeg)

There’s a similar relationship between Docker and Kubernetes. Docker containers package software. Kubernetes is the orchestration system coordinating the behavior of those containers.

There’s a separation of concerns — so your containers don’t care **_where_** they run, and the orchestration system doesn’t care **_what_** is running inside those containers. If you want a refresher, I’ve previously written an intro to [the basics](https://dev.to/loopdelicious/deploying-a-scalable-web-application-with-docker-and-kubernetes-4op9).

![](https://cdn-images-1.medium.com/max/1024/1*CDFtmCxHhDE-JQIU19nB2w.png)<figcaption>separation of concerns between Docker and Kubernetes</figcaption>

Kubernetes has a reputation of being “hard”. So why go through the trouble of using it on top of Docker for a simple game server? Even if you run a single app with a single data store, as we’ll do today, Kubernetes can simplify deployment.

The friendly, but independent, relationship between Docker and Kubernetes gives us the flexibility to run our app the way we want. For example, I plan to deploy a lightweight C++ version of Minecraft. Later on, I can update my app to the default Java Minecraft server by swapping out the image of the deployment.

Let’s run Minecraft for free by deploying with Kubernetes on a self-hosted Raspberry Pi server.

### A recipe for deploying Minecraft on Kubernetes with KubeSail

The Minecraft wiki lists a bunch of tips for [setting up your own server](https://minecraft.gamepedia.com/Tutorials/Setting_up_a_server). The game developers behind Minecraft offer [a free Java version of server software](https://minecraft.gamepedia.com/Server). The open source community has developed a [C++ version](https://cuberite.org/) that’s more lightweight so we can run it on a Raspberry Pi.

Let’s roll our own Minecraft server using Kubernetes.

- Step 0 — [Pre-requisites](#4b39)
- Step 1 — [Add a cluster to the Raspberry Pi](#616a)
- Step 2 — [Fork a public template](#de71)
- Step 3 — [Deploy on Kubernetes](#6086)
- Step 4 — [Connect to the Minecraft server](#8faf)

Let’s set up a server today!

![](https://cdn-images-1.medium.com/max/1024/1*q7xz8oLUEkY4vj25R-fMxw.png)<figcaption>run a Minecraft server on a single-node cluster on Raspberry Pi</figcaption>

### Step 0: pre-requisites

#### KubeSail

Sign up for a [free KubeSail account](https://kubesail.com/) — to deploy and manage the apps on our cluster.

#### Kubernetes

Decide where to run your Kubernetes cluster. I’m using a $35 [Raspberry Pi 4.](https://www.raspberrypi.org/products/raspberry-pi-4-model-b/) But you can also use a spare machine if it has enough input/output (I/O) performance to run Minecraft.

### Step 1: add a cluster to the Raspberry Pi

I already have a [Raspberry Pi 4](https://www.raspberrypi.org/products/raspberry-pi-4-model-b/) to run a Kubernetes cluster. [This very good tutorial](https://kubesail.com/blog/microk8s-raspberry-pi) shows us how to set up a cluster on a new Raspberry Pi.

- Flash an operating system, like [Ubuntu](https://ubuntu.com/desktop), onto an SD card
- Enable [Secure Shell (SSH)](https://www.ssh.com/ssh/server) to operate a headless Raspberry Pi without a separate keyboard and monitor
- Connect the Raspberry Pi directly to the ethernet to reduce latency for gaming — or enable Wi-Fi on the SD card
- Move the SD card over to the Raspberry Pi
- Log in to the Raspberry Pi using SSH
- Install [MicroK8s](https://microk8s.io/) — a lightweight Kubernetes recommended for IOT apps, so your terminal commands will be `microk8s.kubectl` instead of `kubectl`

To connect the Raspberry Pi to KubeSail, apply the configuration file to your personal namespace from the Raspberry Pi’s command line. This might take a couple minutes to spin up.

```
$ sudo microk8s.kubectl apply -f [https://byoc.kubesail.com/<your-kubesail-username>.yaml](https://byoc.kubesail.com/loopdelicious.yaml)
```

![](https://cdn-images-1.medium.com/max/1024/1*mzaOM21YHWil1o-CZcac8A.gif)

![](https://cdn-images-1.medium.com/max/800/1*RqmkX7LMMt5vhTIazx1WhA.gif)<figcaption>1) add a cluster on the Raspberry Pi using `microk8s.kubectl` instead of `kubectl`, and 2) give it a name</figcaption>

The Raspberry Pi is now a single-node Kubernetes cluster!

You can manage this new cluster in the [KubeSail dashboard](https://kubesail.com/clusters). Find the new cluster, give it a name, and then select **Namespaces** from the sub-menu. This is where you can add a new [Kubernetes namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/), like one called games.

![](https://cdn-images-1.medium.com/max/1024/0*9rJSq5IO0C43pP5g.png)<figcaption>Add a new namespace called `games`</figcaption>

Now we have a [Kubernetes context](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#context) consisting of a cluster, a namespace, and a user — everything that we need to set up our deployment.

### Step 2: fork a public template

KubeSail has [a bunch of Kubernetes templates](https://kubesail.com/templates) to browse YAML examples and customize your own deployments.

Under **Templates** , I made this one [for Minecraft on Raspberry Pi](https://kubesail.com/template/loopDelicious/minecraft-raspberry-pi). If you’re not using a Raspberry Pi, use [this template](https://kubesail.com/template/loopDelicious/minecraft) instead.

> [**_minecraft-raspberry-pi_**](https://kubesail.com/template/loopDelicious/minecraft-raspberry-pi)_: Minecraft server with a Persistent Volume Claim to store your world data, on Raspberry Pi 🍓. This template also runs on any board with an_ **_arm64 CPU_** _._

The `minecraft-raspberry-pi` template has two kinds of Kubernetes resources.

- [Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) — to describe how an application is deployed
- [Persistent Volume Claim](https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims) — a request for a particular type of storage

[![](https://cdn-images-1.medium.com/max/1024/1*K9Gh5mtdurBce8EEyCf24A.png)](https://kubesail.com/template/loopDelicious/minecraft-raspberry-pi)<figcaption>fork the minecraft-raspberry-pi template</figcaption>

**Fork** the template. You can rename it, and also toggle the new template’s setting to **Private** if you don’t want the template to be listed publicly as one of your own.

#### The YAML explained

This section explains some of the underlying YAML for these resources if you want to make any adjustments. Feel free to skip ahead to the next step.

In your new KubeSail template, click the **Edit Yaml** button to open the YAML editor on the right side of the page. Find the `Deployment` document tab in the YAML editor, and notice some details.

- [**`replicas`**](https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/#replicas): to specify the number of pods to run
- [**`image`**](https://kubernetes.io/docs/concepts/containers/images/): the base Docker image for the container is [`joycelin79/cuberite`](https://hub.docker.com/r/joycelin79/cuberite), a C++ version of Minecraft (instead of the default Java) that is more lightweight for the Raspberry Pi or any machine with an arm64 CPU
- [**`volumes`**](https://kubernetes.io/docs/concepts/storage/volumes/): there’s a single volume, or data store, that points to our second resource which is a `PersistentVolumeClaim`
- [**`volumeMounts`**](https://kubernetes.io/docs/concepts/storage/volumes/#background): uses [subpaths](https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath) to specify where to mount the volumes into the container according to how the base image is structured
- [**`ports`**](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#containerport-v1-core): exposes the port by setting the `hostPort` and `containerPort` — these two ports should be the same value, the default Minecraft port 25565
- [**`resources`**](https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/): allocates your cluster’s memory to run your game smoothly, you may need to increase this amount, but most game servers should run below 1 CPU and 2Gi of memory

![](https://cdn-images-1.medium.com/max/1024/1*KdWitJ_KSBLfwMegpDHLzg.png)<figcaption>Review the deployment details</figcaption>

Find the `PersistentVolumeClaim` document tab in the YAML editor. This claim asks the cluster for a particular type of storage, to be provisioned as a `PersistentVolume`.

- **`name`** : points back to the volume specified in the `Deployment` document
- [**`resources`**](https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/): this is a request for resources for the volume, or the data stored, and 2Gi should be enough for the Minecraft world.

![](https://cdn-images-1.medium.com/max/1024/1*IyeZqsrT2QdQa3lZBBEOUA.png)<figcaption>Review PersistentVolumeClaim</figcaption>

The `PersistentVolume` in this deployment allows us to save the state of the Minecraft world, so that we can quit our game and keep all of our precious minerals when the server reboots  **⛏** 💎

### Step 3: deploy on Kubernetes

We’re ready to deploy our app on Kubernetes!

In the dropdown menu next to the **Launch Template** button, select the[Kubernetes context](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#context) from Step 1.

![](https://cdn-images-1.medium.com/max/1024/1*sEfLtpmw_9wd2bJvbKFRFw.png)<figcaption>Select a Kubernetes context to deploy the app</figcaption>

Hit **Launch Template** , and KubeSail will deploy Minecraft. In this example, I deploy to the `games` namespace on my `Raspberry Pi` cluster.

Our Minecraft server is alive!

### Step 4: connect to the Minecraft server

To [connect to the Minecraft server](https://help.minecraft.net/hc/en-us/articles/360035131391-How-do-I-play-multiplayer-), start Minecraft on the machine where you want to play. Select **Multiplayer** from the main menu. Click the **Add Server** button, and enter the IP or web address of the Raspberry Pi server. If you’re on the same local network, you should see the server suggested in the local list.

![](https://cdn-images-1.medium.com/max/1024/1*4EvyrRtckxyvn7xT6pUQig.png)<figcaption>scanning for games on the local network</figcaption>

If you want to allow access to the Minecraft server via the internet, set up port forwarding to point port `25565` to the IP of your Raspberry Pi. If you need a refresher, here is a tutorial [to forward ports on your router](https://www.howtogeek.com/66214/how-to-forward-ports-on-your-router/).

Provide this public IP to friends you invite to multiplayer Minecraft. Or you can set up an A record with your DNS provider that points to your home network [like we did previously](https://medium.com/@joycelin.codes/ddclient-c9a6ac1d8f81?source=friends_link&sk=71085ae2ef2612b40cda0130496d1410), so friends can input a URL instead of an IP address.

![](https://cdn-images-1.medium.com/max/1024/1*x-A1L4gN_UD_1M9helxC2w.png)

Players need to run the same version of Minecraft as the server. As of the writing of this article, the latest version of [cuberite](https://cuberite.org/) is 1.12, so players should set their Minecraft version to 1.12.

So that’s it!

We set up a Minecraft server on Raspberry Pi using Kubernetes with a few terminal commands and clicks. Hosting a game server from your living room is free and easy. With the time I saved setting up a Minecraft server, I now have more time to spend on mining precious minerals  **⛏**

> _🤓_ **_Fun Fact:_** _If my IP address changes while I’m away from home, I might have trouble accessing the game server or any other self-hosted services. Check out this other tutorial I wrote for_ [_setting up a dynamic DNS service_](https://medium.com/@joycelin.codes/ddclient-c9a6ac1d8f81?source=friends_link&sk=71085ae2ef2612b40cda0130496d1410)_, also on Raspberry Pi using Kubernetes!_

### Final thoughts on deploying with Kubernetes

When people talk about Kubernetes, they picture huge companies with tons of traffic hitting their services. My little game server certainly doesn’t need Kubernetes for any advanced capabilities. But Kubernetes can also make deployment easier. I’ve done the “hard part” by configuring the Minecraft server to run on Kubernetes. And now if I want to run Kubernetes on a different cluster, I switch the context in KubeSail to deploy my changes with a single click.

Most people also picture Kubernetes running on a managed cloud provider, which can get expensive. But now we know [how to run a cluster for free on a local machine](https://medium.com/@joycelin.codes/ddclient-c9a6ac1d8f81?source=friends_link&sk=71085ae2ef2612b40cda0130496d1410) like a Raspberry Pi.

There’s a bunch of reasons why people use Kubernetes. For this game server, I’m using Kubernetes to **simplify deployment** and because it’s **free** to run on a self-hosted server.

![](https://cdn-images-1.medium.com/max/1024/1*j_dAtHXwOnI8CUtVEmNrpA.jpeg)

Try it yourself, and let me know what you think!

* * *