Why do this?
- I have a 5 year old son
- we like to play games and build stuff with blocks
- I'm interested in kubernetes
- I saw this post on minecraft in docker
- I wanted to get to know minecraft and kubernetes better
Note: After I got this working, but before I posted this, I came across Julia Ferraioli's series of blog posts about minecraft, docker and kubernetes. These posts are good and you may want to check them out too.
One of the advantages of Docker is sharing. In the public docker hub, there were a few different minecraft builds available. I choose to reuse Geoff Bourne's (@itzg) dockerfile listed here. Its a great build that lets you specificy many server configuration options via environment variables so you don't have to worry about putting together a minecraft configuration file. With my container already created for me, getting minecraft up in kubernetes was easy.
Game on Kubernetes
First, you will need kubernetes cluster. Doesn't matter where your cluster is as the bueaty of kubernetes is this will work regardless of where your cluster is. Since I work at Centurylink Cloud, I used the scripts I created here to create my kubernetes cluster on CL cloud.
Kubernetes has the concept of pods. A pod in k8 is a logical grouping of one or more containers, zero or more storage volumes and an IP address. We will launch the above minecraft docker container in a pod in kubernetes. And, since minecraft isn't a clustered server, we only need to run one pod per minecraft server.
Here is the yaml file I used to create this pod.
(Also located here: https://github.com/ckleban/minecraft-k8)
- name: minecraft-server
- name: OPS
- name: EULA
- containerPort: 25565
You will want to at least change the username in the OPS ENV variable to be your minecraft username. This let's the server know who has admin privileges on the server. You can add more settings, like game type, minecraft version, etc as you wish. To see a more detailed list of what options are available, go check out here. Now, go create the minecraft server:
> kubectl create -f pod.yml
#See it up and running:
NAME READY STATUS RESTARTS AGE
minecraft-dfhse 1/1 Running 0 2h
#If you want this server to be reachable on the internet, you probably need to do some more things. If you are on a public cloud, like Centurylink, amazon or google, this is easy to do. Simply run:
> kubectl expose pod minecraft-server --port=25565 --type=LoadBalancer
You will be given a public IP address, which you can see by running:
> kubectl get services
That is it. You and your friends can now play minecraft, inside docker, on kubernetes.
A few things to note:
- you may need to wait a few minutes while you wait for the external IP address to be published.
- you may also need to open up a firewall rule to this IP address and port.
- this isn't a durable configuration. If the pod dies, it won't come back online and the save games and world will be gone for ever. These problems are easily solved in kubernetes. We just need to add a replication controller and persistent storage. Perhaps, I'll work on that next and post it sometime.