BreakingExpress

Build a Kubernetes Minecraft server with Ansible’s Helm modules

One of the perfect outcomes of Ansible’s move towards content collections is it spreads the 1000’s of modules in Ansible‘s “core” repository into many extra impartial repositories. This means motion on points and modules that had lengthy been delayed (typically because of the sheer volume of issues and pull requests within the repo) can progress extra quickly.

Obviously, not all modules will get the identical love and appreciation as others—that is the best way open supply works: extra standard issues flourish, as others might languish a bit—however one shiny instance of the constructive affect has been the Kubernetes assortment’s potential to include some long-awaited Helm modules.

Thanks particularly to the work of LucasBoisserie, three new Helm modules had been merged into the Kubernetes assortment:

  • helm
  • helm_info
  • helm_repository

Ansible has lengthy had a helm module, but it surely was pretty damaged for a very long time, solely labored with older variations of Helm, and was slated for deprecation in Ansible 2.14. That model of the module will nonetheless work the identical within the common neighborhood distribution of Ansible, because it’s now been moved to the community.general assortment.

But if you wish to use these new modules to automate your Helm deployments utilizing the Kubernetes container orchestration system, you are able to do it with the community.kubernetes assortment.

What is Helm?

Helm says it’s “the best way to find, share, and use software built for Kubernetes.”

There are presently dozens of how to deploy software program into Kubernetes and OpenShift clusters (you possibly can even do it utilizing Ansible natively with the k8s module), however Helm is commonly the simplest onramp to Kubernetes deployments, particularly whenever you’re beginning out in your Kubernetes journey.

The approach Helm works is that individuals keep “charts,” that are templates describing “how to deploy application XYZ” into Kubernetes. Charts can have “values” that override the default settings for a deployment’s chart.

There are 1000’s of charts on Helm Hub you should utilize to put in standard software program. If your software program isn’t included, you possibly can construct and host your personal Helm chart repositories.

What is Minecraft?

For a sure era (or their dad and mom), this query does not want a solution: Minecraft is the best-selling video game of all time, and it appeals to an especially extensive viewers as a result of there are such a lot of alternative ways you possibly can play it.

I keep in mind spending an hour right here or there throughout my post-college years tending to a farm that I inbuilt my little digital Minecraft world. Minecraft can now run on virtually any computing system with a display screen, and networked play has turn into very talked-about. To assist this, the Minecraft staff maintains a Minecraft server utility you possibly can run to play networked video games with your mates.

Where does Ansible slot in?

I like to think about Ansible because the “glue” that holds automation collectively. I beforehand wrote about how Ansible is useful in a cloud-native environment, so I will not rehash why I take advantage of Ansible to handle my Kubernetes infrastructure.

In this text, I am going to present you how one can write a brief Ansible playbook to handle the setup of Helm’s Minecraft chart in a cluster. In a real-world infrastructure, this playbook can be one small a part of a set of performs that:

  • Build or configure a Kubernetes cluster
  • Deploy monitoring instruments into the cluster
  • Deploy purposes into the cluster

Before you possibly can write the playbook, you need to set up Ansible’s official Kubernetes collection. You can do that both by requiring it in a necessities.yml file (which may very well be utilized by Ansible Tower to put in the gathering mechanically) or by manually putting in it:

ansible-galaxy assortment set up neighborhood.kubernetes

Once you will have the gathering, it is time to write the playbook. To make it straightforward so that you can view the code or obtain the file, I’ve posted my minecraft.yml playbook as a Gist on GitHub.

The playbook makes use of most of the Kubernetes assortment’s modules:

  1. The k8s module creates a namespace, minecraft.
  2. The helm_repository module provides the itzg Helm repository, which accommodates the Minecraft Helm chart.
  3. The helm module deploys the chart and creates the Minecraft server occasion.
  4. The k8s_info module retrieves the NodePort the place Minecraft is working so that you could hook up with it from Minecraft.

The playbook assumes you will have a working Kubernetes or OpenShift cluster and a kubeconfig file that factors to that cluster already. If not, create a Minikube cluster in your workstation:

  1. Make positive you will have Minikube put in.
  2. Run minikube begin, and look forward to the cluster to be created.

Make positive you will have Ansible and Helm put in, then run the playbook:

ansible-playbook minecraft.yml

After a couple of minutes, the Minecraft server will generate a spawn space and be prepared for connections! The playbook ought to present the Minecraft NodePort on the finish of its output (e.g., Minecraft NodePort: 32393).

Get the IP tackle of your Minikube cluster with minikube ip, add the NodePort to it (in my case, 192.168.64.19:32393), then open up Minecraft and hook up with it:

  1. Click Multiplayer.
  2. Click Direct Connection.
  3. Enter the server tackle (the Minikube IP and Minecraft NodePort).
  4. Click Join Server.

And voila! You ought to be capable to mess around within the little digital Minecraft world that is working in your very personal Kubernetes cluster.

View the server logs with:

kubectl logs -f -n minecraft -l app=minecraft-minecraft;

In the logs, you possibly can see that I used to be profitable to find some ways to die inside my little Minecraft world!

Take a step past

There are dozens of how to deploy purposes like a Minecraft server right into a Kubernetes cluster. Luckily for us, Ansible already helps most of these choices via its Kubernetes assortment! And if you wish to take a step past easy deployments and chart updates, you should utilize Ansible to construct a Kubernetes operator with the Operator SDK—the truth is, somebody already made a community operator constructed with Ansible that runs a Minecraft server!

I used to be impressed to write down this after utilizing Ansible to handle a seven-node Kubernetes cluster constructed with Raspberry Pis. You can study extra about that within the Turing Pi Cluster GitHub repository.


If you need to study extra about Ansible, make sure that to register for AnsibleFest, a digital expertise on October 13-14.

Exit mobile version