Running Minecraft Server in Kubernetes Cluster


My own Minecraft server 🙂

A month ago I had an idea to run a Minecraft server in my garage Kubernetes lab. I though it might interest my little Minecraft player at home with some Kubernetes and GitOps stuff but that failed miserably. But at least I knew how to host a Minecraft server in Kubernetes, with ArgoCD too.

First step, of course the server which is an java app needs to be packed into a Docker image. From the official installation guide, the essential steps are

  • Use OpenJDK 16+ as base image
  • Download the server.jar
  • Run it with java -jar server.jar nogui
  • Make sure port 25565/tcp is open

That’s it! The Dockerfile looks like:

FROM openjdk:16.0.2

WORKDIR /minecraft

RUN curl -O https://launcher.mojang.com/v1/objects/a16d67e5807f57fc4e550299cf20226194497dc2/server.jar

ADD config/* ./
RUN chown -R nobody /minecraft

USER nobody
CMD ["java", "-jar", "server.jar", "nogui"]

And the GitHub Action pipeline:

jobs:
  build:
    runs-on: [ ubuntu-20.04 ]

    steps:
      - uses: actions/checkout@v2

      - name: build number
        run: |
          echo "build_number=dev-${GITHUB_RUN_ID}" >> $GITHUB_ENV

      - name: Docker build kubecraft-server
        run: |
          docker build -t ghcr.io/raynix/kubecraft:$build_number .

      - name: Docker push
        run: |
          echo ${{ secrets.GHCR_PAT }} | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
          docker push ghcr.io/raynix/kubecraft:$build_number
      # The GitHub Action pipeline will update the Kustomize patch file so the most recent tag will be referred to. 
      - name: Update deployment version
        run: |
          sed -i -E "s|(image: ghcr.io/raynix/kubecraft:).*|\1$build_number|g" kustomize/mc.awes.one/patch.yaml

      - name: Auto commit & push changes
        run: |
          git config --global user.name '***'
          git config --global user.email '***'
          git commit -am "Automated commit"
          git push

For more information, here’s my GitHub repository containing the docker build pipeline using GitHub Actions. There is also a kustomize directory where all necessary Kustomize templates reside.

The next step is to register the Kustomize templates as an app in ArgoCD. It can be done declaratively like:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: kubecraft
  namespace: argocd
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: kubecraft
    server: https://kubernetes.default.svc
  project: default
  source:
    path: kustomize/mc.awes.one
    repoURL: https://github.com/raynix/kubecraft.git
    targetRevision: HEAD
  syncPolicy:
    automated:
      prune: true

Here’s what it looks like in ArgoCD:

Since it’s not mentioned in the office document on how to configure session sharing for multiple Minecraft server instances, I doubt the meaning to create more than one pod.

TO-DO: I haven’t included a persistent volume in my Kustomize templates, which means the server will lose the state of the game world when re-deployed.

EDIT: A PersistentVolume is a nice-to-have. A PV sample made with NFS + CSI is here.

🙂

,