Home

Kenny's Blog

06 Mar 2021

Minecraft with Hashicorp Nomad

I recently got my local nomad cluster setup and figured I’d give it a shot to try and setup a minecraft server. Here’s what the job description looks like:

job "minecraft" {
  datacenters = ["atx01"]
  type        = "service"

  group "minecraft" {
    volume "minecraft" {
      type   = "host"
      source = "minecraft"
    }

    task "minecraft" {
      driver = "exec"

      config {
        command = "/bin/sh"
        args    = ["-c", "cd /var/volume && echo 'eula=true' > eula.txt && exec java -Xms1024M -Xmx2048M -jar /local/server.jar --nogui --port 25560"]
      }

      artifact {
        source = "https://launcher.mojang.com/v1/objects/1b557e7b033b583cd9f66746b7a9ab1ec1673ced/server.jar"
      }

      resources {
        cpu    = 2000
        memory = 1492
      }

      volume_mount {
        volume      = "minecraft"
        destination = "/var/volume"
      }
    }
  }
}

One thing you also have to do is create a host volume on the node(s) you want the server to run on. This involves adding some configuration to the client configuration for your nomad node.

host_volume "minecraft" {
    path      = "/opt/minecraft"
    read_only = false
}

Further reading:

  • https://learn.hashicorp.com/tutorials/nomad/stateful-workloads-host-volumes?in=nomad/stateful-workloads
  • https://gaming.stackexchange.com/questions/123194/is-there-a-way-to-get-the-latest-server-jar-through-a-url-that-doesnt-change

Appendix

One thing that’s a pain is you have to manually add host volumes as additional lines in your nomad client configuration. There isn’t a programatic way to declare host volumes, or even to specify one volume and then mount sub directories in your job manifests. This isn’t a huge problem for me right now since I use salt to manage the configuration, but it would be nice if you could do it mostly in nomad. Here’s an open issue that hopefully gets closed one day:

  • https://github.com/hashicorp/nomad/issues/6536

I’m not too sure if k8s handles this problem a bit more nicely.