Scaling a Minecraft server with Kubernetes

How Minebox runs Minecraft servers on Kubernetes: a replicated control plane, an in-pod helper for probes and drains, and rolling updates without disconnects.

contents

Minebox is a persistent Minecraft RPG. It runs as a fleet of single-threaded Minecraft servers on Kubernetes, behind proxies that move players between them. Why we split it into a network in the first place is in From one Minecraft server to a network.

Kubernetes restarts crashed pods, rolls out new images and keeps replica counts. It doesn't know when a Minecraft server has finished loading its world, whether it has room for another player, or how to move a player to another server without disconnecting them. This post covers how we handle that: the control plane, the in-pod helper, rolling updates, and running the control plane as two replicas.

A mistake anywhere in this lifecycle can lose player data, for example a pod killed mid-save. This post is about orchestration, not data safety.

Why not Agones

Agones is the standard answer for game servers on Kubernetes. It allocates a server per match and gives the player an address. For session-based games it's a good default.

Minebox works differently. Players connect to a proxy, and the proxy moves them between running servers: spawn, islands, PvP, raids. We run a standing fleet of typed servers (SPAWN, ISLAND_BAMBOO, PVP, RAID, …) instead of allocating one per session. We needed a registry and a scheduler with Minecraft-specific rules:

  • readiness based on game state, not on an open port
  • typed servers
  • drains that wait for player saves
  • health based on TPS

Fitting that into Agones would have meant working against its model. So we wrote server_manager, a Go service that handles discovery, health checks and scaling for our fleet.

Server lifecycle

A server boots, registers, sends heartbeats, drains and unregisters. Everything runs over gRPC inside the cluster.

        game server pod                         proxies
  ┌─────────────────────────────┐        ┌────────────────────┐
  │ boot → load world → RUNNING │        │ routes players     │
  └──────┬───────────────▲──────┘        │ to READY servers   │
         │               │               └─────────▲──────────┘
         │ 1. Register   │ 2. reverse channel      │
         │ 4. Heartbeat  │    (query, drain,       │ 3. pushes every
         │    (state,    │     transfer)           │    state change
         │    players,   │                         │
         │    TPS)       │                         │
  ┌──────▼───────────────┴─────────────────────────┴──────────┐
  │            server_manager · 2 replicas · Redis            │
  │   registry: SPAWN ×3 · ISLAND ×2 · PVP ×1 …               │
  │   reserves slots · tracks last-seen · alerts on silence   │
  └───────────────────────────────────────────────────────────┘

Registration. After loading its world, a server registers with server_manager with its type, address and state. server_manager adds it to the registry and opens a reverse gRPC channel into the server. It uses that channel to query the server, drain it and transfer its players. State changes are pushed to every proxy, so proxies never poll.

Heartbeats. Each server checks in every few seconds with its state, player count and TPS. When the scheduler sends a player to a server, it reserves the slot immediately instead of waiting for the next heartbeat. Without that, 40 players joining at once would all be sent to the same server. The next heartbeat replaces the reservation with the real count.

Health. server_manager checks the registry every 3 seconds:

  • silent for about 30 seconds: warning and alert
  • silent for 60 seconds: removed from routing
  • STARTING servers get a minute before they can be declared dead, because worlds take time to load
  • DRAINING servers don't trigger the warning, because they're expected to go quiet

Removing a server on the first missed heartbeat would take healthy servers out during a GC pause.

convoy

Kubernetes probes and lifecycle hooks know nothing about worlds, players or saves. Each game server pod includes convoy, a small binary that the probes and hooks call:

readinessProbe:
  exec: { command: ["/opt/server/convoy", "--state=RUNNING"] }
  initialDelaySeconds: 60
  periodSeconds: 5
livenessProbe:
  exec: { command: ["/opt/server/convoy", "--check"] }
  initialDelaySeconds: 90
  periodSeconds: 30
  failureThreshold: 5

convoy --state=RUNNING asks the local game server for its state and exits 0 only when it's RUNNING: the world is loaded and the server can take players. The pod only receives traffic after that. The usual Minecraft health check, pinging port 25565, only tells you the port is open.

The drain runs as the preStop hook:

lifecycle:
  preStop:
    exec:
      command: ["/opt/server/convoy", "--terminate", "--copy-crashes"]
terminationGracePeriodSeconds: 300

Before Kubernetes stops the pod, convoy --terminate copies crash dumps to a crash-dumps PVC so they survive the pod, then calls Drain on the server. The 300-second grace period gives the drain time to move players and save.

Rolling updates

We roll out new images, config, worlds and plugins without disconnecting players:

  1. Kubernetes starts a new pod. It loads its world, registers and becomes ready.
  2. Kubernetes marks an old pod for termination. convoy --terminate runs and the server switches to DRAINING. server_manager tells the proxies to stop sending new players to it, and the load balancer stops selecting it.
  3. server_manager transfers the old pod's players to the new pod. Players stay connected to the proxy the whole time; only the server behind it changes.
  4. convoy waits for the old pod to reach 0 players, waits 15 seconds for saves, and exits. If players remain, it keeps waiting, up to the 300-second grace period.
  5. The old pod terminates.
strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 10%
    maxUnavailable: 25%

As a Deployment rolling update, new pods are created before old ones are removed, and a rollout never takes more than 25% of a server type offline. Each Deployment has a restart-group label, so we can roll a whole class of servers, such as all islands, as one wave without dropping below the capacity the fleet needs.

The drain is the slow part. A pod with 20 players takes as long as those 20 players take to move and save. We accept that, because rushing it risks player data.

Two replicas

server_manager runs as two replicas. The registry lives in Redis, and each replica keeps an in-memory copy for routing, synced through pub/sub. A replica loads the registry before it starts serving, so restarting or deploying server_manager doesn't affect players. One replica holds a leader lock in Redis and runs the fleet-wide jobs, like health checks, so each server is checked once.

Questions, or a different approach? I'm @imevlad on X.