Running Minecraft server in K8s

Running Minecraft server in K8s

In this article, I will explain how to run a Minecraft server in Kubernetes with auto-scaling features.

Why on Kubernetes?

We can easily scale our Minecraft server as per our requirements. We can set a threshold for memory and CPU utilization and based on that it can autoscale itself.

Requirement -

Running K8s or Minikube environment.
Minecraft docker image.

Steps -

First, create a namespace using the following command -

kubectl create ns minecraft

Volume -

First, we need to create PersistentVolumeClaim to store minecraft-server data -

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mine-chest
spec:
  resources:
    requests:
      storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce

Create volume using following command -

kubectl apply -f pvc.yaml -n minecraft

You can see the status using the following command -

kubectl get pvc -n minecraft

Now we need to create a deployment which will mount this volume and create pod.

Deployment -

apiVersion: v1
kind: Deployment
metadata:
  name: minecarft-server
  namespace: minecraft
  labels:
    app: minecraft
    release: minecraft
spec:
  replicas: 1
  selector:
    matchLabels:
      app: minecarft
      release: minecarft
  template:
    metadata:
      labels:
        app: minecraft
        release: minecraft
    spec:
      containers:
        - name: minecarft
          image: asabhi6776/minecraft:latest
          imagePullPolicy: IfNotPresent
          env:
            - name: test
              value: test
          resources:
            limits:
              memory: "4Gi"
              cpu: "1000m"
            requests:
              memory: "2Gi"
              cpu: "500m"
          ports:
            - containerPort: 25565
              name: http
              protocol: TCP
          volumeMounts:
            - mountPath: /minecraft
              name: data
      nodeSelector:
        memory: high
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: "mine-chest"

Apply the above deployment file and it will create a pod named minecraft-server-*

kubectl apply -f deployment.yaml -n minecraft

You can check using the following command -

kubectl get pods -n minecraft

Once the pod is up then proceed with the next step -

Service -

Create a service for the minecraft-server which will expose our Minecraft server on nodeport.

apiVersion: v1
kind: Service
metadata:
  name: minecarft
spec:
  selector:
    app: minecarft
    release: minecraft
  ports:
  - port: 25565
    targetPort: 25565
    protocol: TCP
    name: TCP
    nodePort: 32566
  type: NodePort

Create a service using the following command -

kubectl apply -f service.yaml -n minecraft

Once everything is up and running use your node IP address and nodeport to connect with your Minecraft game client.

nodeIP:32566

Now your server setup is done you can access it in your game client.

This is just Part 1, in next parts we will discuss about AutoScalling CI/CD and Backup and restore.

If you like this article, please leave a comment and reaction.

Thanks

Did you find this article valuable?

Support Abhishek Singh by becoming a sponsor. Any amount is appreciated!