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. 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.
Let’s try it again — this time to run a free Minecraft server on Kubernetes.
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 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.
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.
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.
The Minecraft wiki lists a bunch of tips for setting up your own server. The game developers behind Minecraft offer a free Java version of server software. The open source community has developed a C++ version 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
- Step 1 — Add a cluster to the Raspberry Pi
- Step 2 — Fork a public template
- Step 3 — Deploy on Kubernetes
- Step 4 — Connect to the Minecraft server
Let’s set up a server today!
Sign up for a free KubeSail account — to deploy and manage the apps on our cluster.
Decide where to run your Kubernetes cluster. I’m using a $35 Raspberry Pi 4. But you can also use a spare machine if it has enough input/output (I/O) performance to run Minecraft.
- Flash an operating system, like Ubuntu, onto an SD card
- Enable Secure Shell (SSH) 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 — a lightweight Kubernetes recommended for IOT apps, so your terminal commands will be
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)
The Raspberry Pi is now a single-node Kubernetes cluster!
You can manage this new cluster in the KubeSail dashboard. 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, like one called games.
Now we have a Kubernetes context consisting of a cluster, a namespace, and a user — everything that we need to set up our deployment.
KubeSail has a bunch of Kubernetes templates to browse YAML examples and customize your own deployments.
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 .
minecraft-raspberry-pi template has two kinds of Kubernetes resources.
- Deployment — to describe how an application is deployed
- Persistent Volume Claim — a request for a particular type of storage
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.
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: to specify the number of pods to run
image: the base Docker image for the container is
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: there’s a single volume, or data store, that points to our second resource which is a
volumeMounts: uses subpaths to specify where to mount the volumes into the container according to how the base image is structured
ports: exposes the port by setting the
containerPort— these two ports should be the same value, the default Minecraft port 25565
resources: 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
PersistentVolumeClaim document tab in the YAML editor. This claim asks the cluster for a particular type of storage, to be provisioned as a
name: points back to the volume specified in the
resources: this is a request for resources for the volume, or the data stored, and 2Gi should be enough for the Minecraft world.
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 ⛏ 💎
We’re ready to deploy our app on Kubernetes!
In the dropdown menu next to the Launch Template button, select theKubernetes context from Step 1.
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!
To connect to the Minecraft server, 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.
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.
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, so friends can input a URL instead of an IP address.
Players need to run the same version of Minecraft as the server. As of the writing of this article, the latest version of cuberite 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, also on Raspberry Pi using 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 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.
Try it yourself, and let me know what you think!