Hosting Minecraft Server + Grafana
Tags: minecraft, grafana
Published: 2023-06-14 12:11:47

As of yesterday, Minecraft 1.20 has been released, which has rekindled the builder spirit within me and my friend. So, why not host a Minecraft server for us? Without further ado, I have listed three possible options for a Minecraft server.

### Requirements

- Low cost
- At least a single decent core CPU
- 6GB or more (expecting up to 5 players online at most, ~4GB + 2 GB as buffer/for other resources)

### Options

1. Minecraft realm
    1. 👍 Easy
    2. 👎 Very limited configuration
    3. 👎 Not the cheapest option
2. Hosting on my desktop
    1. 👍 Free
    2. 👎 Need my desktop to be online 24/7 (as my players will be spanning over several time zones)
3. Hosting on a VPS
    1. 👍 Sounds fun
    2. 👍 Cheaper than realm (in theory)
    3. 👎 Takes some time to set up

In the end, I went with option 3 and looked for a good VPS provider. I ended up choosing Oracle, mostly due to its generous free tier (up to 24GB RAM on an ARM server 🤯), which is going to be crucial for a Minecraft server.

[Oracle Hong Kong SAR, PRC | Cloud Applications and Cloud Platform](https://www.oracle.com/hk/)

# Creating a VPS instance on Oracle Cloud
![](https://storage.googleapis.com/zenn-user-upload/6bbb485d3994-20230613.png)

The UI is quite straightforward, feeling like a mix of Azure and AWS. Nothing fancy, but I like how it estimates the monthly cost, giving me an idea of how much it's going to cost me every week. In the end, I picked a medium-sized free instance (2 CPUs + 12GB RAM), which is way better than what I expected, with only 300 yen monthly cost!!!

# Setting up the server

### Dependencies

To create a basic Minecraft server, we just need the following:

```
- Java
- Screen / tmux / zellij
- vi / vim / nvim
```

### Setting up Minecraft server

I chose the [fabric](https://fabricmc.net/use/server/) server this time because:

- It is closer to vanilla Minecraft
- There are many mods for server enhancement or even new content for future gameplay
1. Create a folder at `$HOME`

```bash
mkdir ~/mc-server
```

1. Download the server JAR

```bash
curl -OJ <https://meta.fabricmc.net/v2/versions/loader/1.20.1/0.14.21/0.11.2/server/jar>

```

1. Create a `start.sh`

```bash
echo 'java -Xmx10G -jar fabric-server-mc.1.20.1-loader.0.14.21-launcher.0.11.2.jar nogui' > start.sh
```

1. Give permission to `sh` and then start

```bash
chmod +x start.sh
./start.sh
```

1. After accepting the EULA, the server should be started! 🎉

### Opening ports for public traffic

At this point, although the server is now started, no one can access it yet because I haven't opened the port for public traffic. We need to tune the network setting at Oracle dashboard and Ubuntu firewall.

1. Adding ingress rules at Oracle dashboard

![](https://storage.googleapis.com/zenn-user-upload/78549a6eede9-20230613.png)

1. Opening ports at `ufw`

```bash
sudo ufw allow 25565
sudo ufw enable
```

### Server is up!

Now you should see your server online! Yay

![](https://storage.googleapis.com/zenn-user-upload/7f61fd0827d2-20230613.png)

## Setting up Grafana

What if you want to know your server more? Or show some cool graphs to your friends? Then Grafana is here for you. Grafana also provides a generous [free tier](https://grafana.com/products/cloud/?plcmt=footer) for us, so it costs nothing to make some shiny charts for our server ;)

> 💡 Grafana and Prometheus are integral tools for metric visualization and monitoring. Prometheus, an open-source monitoring system with a robust query language, collects time-series data identified by metric name and key/value pairs. It efficiently scrapes and stores metrics from services, providing powerful queries and alerts. Grafana, an open-source visualization tool, integrates with Prometheus to present these complex metrics in easy-to-understand, customizable dashboards. Together, they provide real-time, data-driven insights into system performance, enabling informed decision-making for improvements, troubleshooting, and ensuring system reliability. Their tandem operation facilitates comprehensive monitoring and data exploration in a user-friendly manner.
*- GPT-4 “Grafana & Prometheus in 100 words”*

### Connecting server to Grafana cloud

After opening a new Grafana cloud account, you will get a friendly guide guiding you to various features that Grafana can do. Despite Grafana being a very powerful tool, we are going to use it for two tasks:

1. Showing general server metrics
2. Showing Minecraft metrics

### Scraping server condition via `node_scraper`

Following the guide at `https://[your_domain].grafana.net/connections/add-new-connection/linux-node`, you are essentially setting up a `node_scraper` for your server. It will collect various system data and then upload it to Grafana cloud for visualization.

![](https://storage.googleapis.com/zenn-user-upload/1ee33986696e-20230613.png)

If you followed the steps correctly, you will have a nice-looking dashboard like this:

!https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d3b5934b-c2ba-4f79-92ba-23a43dbfa73d/Untitled.png

### Minecraft Metrics on Grafana

Now we take one step further to show Minecraft-specific data on Grafana as well. We are going to use a fabric mod, [Unified Metrics](https://github.com/Cubxity/UnifiedMetrics), to export all the sweets data to Grafana.

![](https://storage.googleapis.com/zenn-user-upload/47a39d02d0f3-20230613.png)

1. Install Unified Metrics at your server's `mods` folder

```
wget <https://github.com/Cubxity/UnifiedMetrics/releases/download/v0.3.8/unifiedmetrics-platform-fabric-0.3.8.jar>
```

1. Configure `/etc/grafana-agent.yml`, insert the following to the correct place

```yaml
metrics:
  configs:
  - name: integrations
    remote_write:
    - basic_auth:
        password: <PW>
        username: <USERNAME>
      url: <URL>
    scrape_configs:
      # Add here any snippet that belongs to the `metrics.configs.scrape_configs` section.
      # For a correct indentation, paste snippets copied from Grafana Cloud at the beginning of the line.
      - job_name: minecraft_scrape
        static_configs:
        # Put your server's IP here
        - targets: [ "localhost:9100" ]
        # Feel free to add any labels here
          labels:
            node: <>
```

1. Reload `grafana-agent`

```
sudo systemctl restart grafana-agent.service
```

1. Import dashboard
    
    We need a dashboard to display our collected data. The provided dashboard by Unified Metrics' author is a good starting point: https://grafana.com/grafana/dashboards/14756-unifiedmetrics-0-3-x-prometheus/
    

If everything is set up correctly, you will see data flowing in, and charts start updating in a few minutes. Now enjoy Minecraft and all the fancy visualizations!