Setting up a Minecraft Server on MicroK8s Kubernetes

Share with:


Minecraft is a popular multi-player game that allows players to explore, create, and survive in a blocky, pixelated world. There are multiple ways for players to play together in the same world, and each have their own benefits. This post will focus on setting up a private server on your local network. Benefits include having more control over who can access the server, and not needing to pay a monthly cloud service fee. If you’re interested in hosting a Minecraft server on a Microk8s stack, this guide will walk you through the process step by step.

What is Microk8s? Microk8s is a lightweight Kubernetes distribution designed for development and testing. It’s ideal for running containerized applications in a private environment. If you want to learn how to setup a MicroK8s cluster so that you can run apps on your own private network, see my post here.

Step 1: Install Required Microk8s Add-Ons

The first step is to enable some addons that are required for running a Minecraft server. You can enable the following addons:

microk8s enable dns storage ingress

Step 2: Create a Persistent Volume

A Minecraft server needs a persistent storage to save the game data, player profiles, and other configuration files. In Kubernetes, you can create a Persistent Volume (PV) and a Persistent Volume Claim (PVC) to manage the storage. A PV is a physical storage resource, such as a disk or a network share, while a PVC is a request for a specific amount of storage.

You can create a PV by creating a YAML file called pv.yaml with the following content:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: minecraft-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /mnt/data/minecraft

This YAML file defines a PV with a capacity of 10GB and an access mode of ReadWriteOnce, which means that only one node can access the volume at a time. The PV uses a hostPath as the storage backend, which means that the data is stored on the local filesystem of the node. You can customize the path to match your server configuration, then create the PV with the following terminal command:

microk8s.kubectl create -f pv.yaml

After creating the PV, you can create a PVC by creating a YAML file called pvc.yaml with the following content:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: minecraft-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

This YAML file defines a PVC that requests a 10GB storage from the PV. You can customize the size to match your requirements. Create the PVC with the following:

microk8s.kubectl create -f pvc.yaml

Step 3: Create a Secret to hold Minecraft properties

Next we will create a secret to hold the Minecraft properties, so that we can adjust the default settings before the server boots. Grab a copy of the Minecraft properties file if you already have one, or use the following as a template.

#Properties Config file
motd=Message of the day
sub-motd=Sub-Message of the day
server-port=19132
server-ip=0.0.0.0
view-distance=30
white-list=off
achievements=on
announce-player-achievements=on
spawn-protection=16
max-players=10
allow-flight=on
spawn-animals=on
spawn-mobs=on
gamemode=1
force-gamemode=off
hardcore=off
pvp=on
difficulty=1
generator-settings=
level-name=world
level-seed=
level-type=DEFAULT
allow-nether=on
enable-query=on
enable-rcon=on
rcon.password=SetYourOwnPasswordHere
auto-save=on
force-resources=off
xbox-auth=off

Create a file called server.properties, adjust the settings as needed. Next convert the properties file to a Base64 encoded string with the following terminal command.

base64 -w 0 server.properties

Copy the encoded output and paste into the following YAML definition, then save to a file called secret-props.yaml.

apiVersion: v1
kind: Secret
metadata:
  name: minecraft-props
type: Opaque
data:
  server.properties: BASE64_SERVER_PROPS

Create the secret with:

microk8s.kubectl create -f secret-props.yaml

Step 4: Create a Service

Next we can create the service so that we can allow access to the Minecraft server from outside the Kubernetes cluster. Create a YAML file called service.yaml with the following content.

apiVersion: v1
kind: Service
metadata:
  name: minecraft
spec:
  type: NodePort
  selector:
    app: minecraft
  ports:
    - protocol: UDP
      port: 19132
      targetPort: 19132

Step 5: Deploy the Minecraft Server

Now that the storage is ready, we can deploy the Minecraft server as a Kubernetes Deployment. Create a YAML file called deployent.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: minecraft-deployment
spec:
  selector:
    matchLabels:
      app: minecraft
  replicas: 1
  template:
    metadata:
      labels:
        app: minecraft
    spec:
      containers:
        - name: minecraft
          image: matt404/nukkit:arm64-v108
          ports:
            - containerPort: 19132
              protocol: UDP
          volumeMounts:
            - mountPath: "/var/opt/nukkit/server.properties"
              subPath: "server.properties"
              name: minecraft-props
            - mountPath: /data
              name: minecraft-persistent-storage
      volumes:
        - name: minecraft-props
          secret:
            secretName: minecraft-props
            defaultMode: 0777
        - name: minecraft-persistent-storage
          persistentVolumeClaim:
            claimName: minecraft-pvc

Create the deployment with:

microk8s.kubectl create -f deployment.yaml

Step 6: Access the Minecraft Server

To access the Minecraft server we need to find the Node and NodePort for the server.

To find the Node, run the following:

microk8s.kubectl get pod -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name | grep minecraft

To find the NodePort, run the following:

microk8s.kubectl get svc -o=custom-columns=PORT:.spec.ports[*].nodePort,NAME:.metadata.name | grep minecraft

Enter the Node as the Server Address and the NodePort as the Port when adding an external server within the Minecraft game.

Have Fun!

Share with:


Leave a Reply

Your email address will not be published. Required fields are marked *

*


The reCAPTCHA verification period has expired. Please reload the page.