---
title: Minecraft Server (Java Edition)をKubernetesにインストールするメモ
tags: ["Minecraft", "Java", "Kubernetes"]
categories: ["Game", "Minecraft"]
date: 2025-01-03T08:08:50Z
updated: 2025-01-03T17:28:55Z
---

Minecraft Serverは機能豊富な[helm chart](https://github.com/itzg/minecraft-server-charts/tree/master)が用意されているので、Kubernetes上にインストールすると運用が楽です。

本記事では検証環境としてKind (Kubernetes in Docker)上にMinecraft Serverをインストールするメモを記します。

### Kind Clusterの作成

```bash
brew install kind
kind create cluster
```

```bash
$ kubectl get node -owide
NAME                 STATUS   ROLES           AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                         KERNEL-VERSION                        CONTAINER-RUNTIME
kind-control-plane   Ready    control-plane   39s   v1.32.0   192.168.107.2   <none>        Debian GNU/Linux 12 (bookworm)   6.12.5-orbstack-00287-gf8da5d508983   containerd://1.7.24
```

### MetalLBのインストール

筆者はDocker Runtimeとして[OrbStack](https://orbstack.dev/)を使用しています。OrbStackであればtype=LoadBalancerのServiceに対して、Macのホストからルーティング可能になります。
kind上でtype=LoadBalancerのServiceでExternal IPが払い出されるようにMetal LBをインストールします。

```bash
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.14.9/config/manifests/metallb-native.yaml
kubectl wait --namespace metallb-system \
             --for=condition=ready pod \
             --selector=app=metallb \
             --timeout=90s
```

次のコマンドでDocker NetworkのIPv4のSubnetを取得します。

```bash
SUBNET=$(docker network inspect -f '{{(index (index .IPAM.Config 1) "Subnet")}}' kind)
```

確認します。

```bash
$ echo $SUBNET
192.168.107.0/24
```

> [!TIP]
> 取得できない場合は`SUBNET=$(docker network inspect -f '{{(index (index .IPAM.Config 0) "Subnet")}}' kind)`を試してください。

MetalLBが払い出すIPのレンジを設定します。この例では`192.168.107.200-192.168.107.250`を対象にします。

```yaml
cat <<EOF > metallb.yaml
---
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: example
  namespace: metallb-system
spec:
  addresses:
  - $(echo $SUBNET | cut -d. -f1-3).200-$(echo $SUBNET | cut -d. -f1-3).250
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: empty
  namespace: metallb-system
---
EOF
kubectl apply -f metallb.yaml
```

### Minecraft Serverのインストール

事前準備ができたのでいよいよMinecraft Serverをインストールします。

https://github.com/itzg/minecraft-server-charts/tree/master/charts/minecraft のhelm chartを使用します。

ServerのIPは以下のIPで固定します。

```yaml
MINECRAFT_IP=$(echo $SUBNET | cut -d. -f1-3).222
```

```bash
$ echo $MINECRAFT_IP 
192.168.107.222
```

`helm-values.yaml`を作成します。ここではVanilla Serverをインストールしますが、`type`に`"VANILLA"`, `"FORGE"`, `"PAPER"`などが選択可能です。選択可能なtypeは[こちら](https://docker-minecraft-server.readthedocs.io/en/latest/types-and-platforms/)を参照してください。

```yaml
cat <<EOF > helm-values.yaml
---
minecraftServer:
  eula: "TRUE"
  type: "VANILLA"
  version: "1.21.4"
  difficulty: easy
  gameMode: creative
  overrideServerProperties: true
  serviceType: LoadBalancer
  loadBalancerIP: $MINECRAFT_IP
  externalTrafficPolicy: Local
  memory: 4096M
  rcon:
    enabled: true
resources:
  requests:
    memory: 3.5Gi
persistence:
  dataDir:
    enabled: true
    Size: 8Gi
---
EOF
```

> [!TIP] 
> 筆者はOracle Cloud Infrastructure Container Engine for Kubernetes（OKE）上で運用しています。
> OKE上の`helm-values.yaml`は[こちら](https://github.com/making/k8s-gitops/blob/main/lemon/app/minecraft/helm-values.yaml)を参照してください。

helmでMinecraft Serverをインストールします。

```bash
helm upgrade --install \
  -n minecraft minecraft itzg/minecraft \
  -f helm-values.yaml \
  --create-namespace \
  --wait
```

しばらくするとPodがReadyになり、LoadBalancerも指定したExternal IPで作成されます。

```bash
$ kubectl get pod,svc -n minecraft 
NAME                                       READY   STATUS    RESTARTS   AGE
pod/minecraft-minecraft-58746c5689-lcwxw   1/1     Running   0          66s

NAME                               TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)           AGE
service/minecraft-minecraft        LoadBalancer   10.96.5.89      192.168.107.222   25565:32239/TCP   66s
service/minecraft-minecraft-rcon   ClusterIP      10.96.218.111   <none>            25575/TCP         66s
```

helmコマンドでもstatusを確認できます。

```bash
$ helm list -A
NAME     	NAMESPACE	REVISION	UPDATED                            	STATUS  	CHART           	APP VERSION
minecraft	minecraft	1       	2025-01-03 16:47:39.39652 +0900 JST	deployed	minecraft-4.23.3	SeeValues  
```

このServerに対してクライアントから接続します。

<img width="966" src="https://github.com/user-attachments/assets/fb168f78-b545-487e-bdd2-3b34815777e3" />

無事接続できました。

<img width="966" src="https://github.com/user-attachments/assets/84adb9cd-8e64-493d-bd36-df116fda214d" />

アンインストールは次のコマンドで行えます。kind上であれば、試行錯誤で試せますね。

```bash
helm uninstall -n minecraft minecraft --wait
```
