[FEATURE] Server service containerization
This commit is contained in:
parent
22bf905415
commit
12d198456c
9 changed files with 165 additions and 91 deletions
|
@ -25,8 +25,8 @@ readinessProbe:
|
|||
timeoutSeconds: 1
|
||||
resources:
|
||||
requests:
|
||||
cpu: 500m
|
||||
memory: 512Mi
|
||||
cpu: 50m
|
||||
memory: 64Mi
|
||||
stdin: true
|
||||
terminationMessagePath: /dev/termination-log
|
||||
terminationMessagePolicy: File
|
||||
|
|
|
@ -1,15 +1,23 @@
|
|||
env:
|
||||
# System Values
|
||||
- name: JVM_OPTS
|
||||
- name: JVM_XX_OPTS
|
||||
- name: OVERRIDE_SERVER_PROPERTIES
|
||||
value: "false"
|
||||
- name: EULA
|
||||
value: "TRUE"
|
||||
# Updated at recreation
|
||||
- name: MEMORY
|
||||
value: 1024M
|
||||
- name: TYPE
|
||||
value: VANILLA
|
||||
- name: VERSION
|
||||
value: "latest"
|
||||
# Set at creation but not updated on recreation
|
||||
- name: DIFFICULTY
|
||||
value: easy
|
||||
- name: WHITELIST
|
||||
- name: OPS
|
||||
- name: ICON
|
||||
- name: MAX_PLAYERS
|
||||
value: "20"
|
||||
- name: MAX_WORLD_SIZE
|
||||
|
@ -52,16 +60,8 @@ env:
|
|||
- name: GENERATOR_SETTINGS
|
||||
- name: LEVEL
|
||||
value: world
|
||||
# - name: MODPACK
|
||||
# value: https://somemodpack.com
|
||||
- name: ONLINE_MODE
|
||||
value: "true"
|
||||
- name: MEMORY
|
||||
value: 1024M
|
||||
- name: JVM_OPTS
|
||||
- name: JVM_XX_OPTS
|
||||
- name: OVERRIDE_SERVER_PROPERTIES
|
||||
value: "true"
|
||||
- name: ENABLE_RCON
|
||||
value: "true"
|
||||
- name: RCON_PASSWORD
|
||||
|
@ -80,7 +80,7 @@ livenessProbe:
|
|||
periodSeconds: 5
|
||||
successThreshold: 1
|
||||
timeoutSeconds: 1
|
||||
name: changeme-name
|
||||
name: changeme-name-server
|
||||
ports:
|
||||
- containerPort: 25565
|
||||
name: minecraft
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
import k8s from "@kubernetes/client-node";
|
||||
import yaml from "js-yaml";
|
||||
import { VERB, ERR } from "../util/logging.js";
|
||||
import { getServerEntry } from "../database/queries/server-queries.js";
|
||||
import {
|
||||
getFtpContainer,
|
||||
getCoreServerContainer,
|
||||
getBackupContainer,
|
||||
} from "./server-containers.js";
|
||||
const kc = new k8s.KubeConfig();
|
||||
kc.loadFromDefault();
|
||||
|
||||
|
@ -8,6 +15,8 @@ const k8sCore = kc.makeApiClient(k8s.CoreV1Api);
|
|||
|
||||
const namespace = process.env.MCL_SERVER_NAMESPACE;
|
||||
|
||||
const loadYaml = (f) => yaml.load(fs.readFileSync(path.resolve(f), "utf8"));
|
||||
|
||||
const mineclusterManaged = (o) =>
|
||||
o.metadata &&
|
||||
o.metadata.annotations &&
|
||||
|
@ -93,13 +102,26 @@ export async function getContainers(serverName) {
|
|||
return deployment.spec.template.spec.containers;
|
||||
}
|
||||
|
||||
async function containerControl(serverName, deployment, scaleUp) {
|
||||
const { containers } = deployment.spec.template.spec;
|
||||
const depFtp = containers.find((c) => c.name.endsWith("-ftp"));
|
||||
const depServer = containers.find((c) => c.name.endsWith("-server"));
|
||||
const depBackup = containers.find((c) => c.name.endsWith("-backup"));
|
||||
const serverSpec = await getServerEntry(serverName);
|
||||
const ftpContainer = depFtp ?? getFtpContainer(serverSpec);
|
||||
const serverContainer = depServer ?? getCoreServerContainer(serverSpec);
|
||||
const backupContainer = depBackup ?? getBackupContainer(serverSpec);
|
||||
if (scaleUp) return [ftpContainer, serverContainer];
|
||||
return [ftpContainer];
|
||||
}
|
||||
|
||||
export async function toggleServer(serverName, scaleUp = false) {
|
||||
const deployment = await getDeployment(serverName);
|
||||
const { containers } = deployment.spec.template.spec;
|
||||
const ftpContainer = containers.find((c) => c.name.endsWith("-ftp"));
|
||||
|
||||
res.sendStatus(200);
|
||||
deployment.spec.template.spec.containers = containers;
|
||||
deployment.spec.template.spec.containers = await containerControl(
|
||||
serverName,
|
||||
deployment,
|
||||
scaleUp,
|
||||
);
|
||||
return k8sDeps.replaceNamespacedDeployment(
|
||||
deployment.metadata.name,
|
||||
namespace,
|
||||
|
|
67
lib/k8s/server-containers.js
Normal file
67
lib/k8s/server-containers.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import yaml from "js-yaml";
|
||||
const loadYaml = (f) => yaml.load(fs.readFileSync(path.resolve(f), "utf8"));
|
||||
|
||||
export function getFtpContainer(serverSpec) {
|
||||
const { name } = serverSpec;
|
||||
const ftpContainer = loadYaml("lib/k8s/configs/containers/ftp-server.yml");
|
||||
ftpContainer.name = `mcl-${name}-ftp`;
|
||||
const ftpPortList = [
|
||||
{ p: 20, n: "ftp-data" },
|
||||
{ p: 21, n: "ftp-commands" },
|
||||
];
|
||||
for (var p = 40000; p <= 40009; p++)
|
||||
ftpPortList.push({ p, n: `ftp-passive-${p - 40000}` });
|
||||
ftpContainer.ports = ftpPortList.map(({ p: containerPort, n: name }) => ({
|
||||
containerPort,
|
||||
name,
|
||||
protocol: "TCP",
|
||||
}));
|
||||
return ftpContainer;
|
||||
}
|
||||
|
||||
export function getCoreServerContainer(serverSpec) {
|
||||
const { name, version, serverType, memory } = serverSpec;
|
||||
const container = loadYaml("lib/k8s/configs/containers/minecraft-server.yml");
|
||||
// Container Updates
|
||||
container.name = `mcl-${name}-server`;
|
||||
container.resources.requests.memory = `${memory}Mi`;
|
||||
|
||||
const findEnv = (k) => container.env.find(({ name: n }) => n === k);
|
||||
const updateEnv = (k, v) => (findEnv(k).value = v);
|
||||
|
||||
// Enviornment variables
|
||||
updateEnv("TYPE", serverType);
|
||||
updateEnv("VERSION", version);
|
||||
updateEnv("MEMORY", `${memory}M`);
|
||||
// RCON
|
||||
const rs = `mcl-${name}-rcon-secret`;
|
||||
findEnv("RCON_PASSWORD").valueFrom.secretKeyRef.name = rs;
|
||||
return container;
|
||||
}
|
||||
|
||||
export function getServerContainer(serverSpec) {
|
||||
const { difficulty, gamemode, motd, maxPlayers, seed, ops, whitelist } =
|
||||
serverSpec;
|
||||
const container = getCoreServerContainer(serverSpec);
|
||||
|
||||
const findEnv = (k) => container.env.find(({ name: n }) => n === k);
|
||||
const updateEnv = (k, v) => (findEnv(k).value = v);
|
||||
|
||||
// Enviornment variables
|
||||
updateEnv("DIFFICULTY", difficulty);
|
||||
updateEnv("MODE", gamemode);
|
||||
updateEnv("MOTD", motd);
|
||||
updateEnv("MAX_PLAYERS", maxPlayers);
|
||||
updateEnv("SEED", seed);
|
||||
updateEnv("OPS", ops);
|
||||
updateEnv("WHITELIST", whitelist);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
export function getBackupContainer(serverSpec) {
|
||||
const container = loadYaml("lib/k8s/configs/containers/minecraft-backup.yml");
|
||||
return container;
|
||||
}
|
|
@ -47,11 +47,13 @@ export async function getInstances() {
|
|||
const serverInstances = serverDeployments.map((s) => {
|
||||
name = s.metadata.annotations["minecluster.dunemask.net/server-name"];
|
||||
metrics = null;
|
||||
started = !!s.spec.replicas;
|
||||
started = !!s.spec.template.spec.containers.find((c) =>
|
||||
c.name.includes(`mcl-${name}-server`),
|
||||
);
|
||||
const pod = podMetricsResponse.items.find(({ metadata: md }) => {
|
||||
return md.labels && md.labels.app && md.labels.app === `mcl-${name}-app`;
|
||||
});
|
||||
if (pod) {
|
||||
if (started && pod) {
|
||||
const podCpus = pod.containers.map(
|
||||
({ usage }) => parseInt(usage.cpu) / 1_000_000,
|
||||
);
|
||||
|
|
|
@ -5,6 +5,11 @@ import yaml from "js-yaml";
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import ExpressClientError from "../util/ExpressClientError.js";
|
||||
import {
|
||||
getFtpContainer,
|
||||
getServerContainer,
|
||||
getBackupContainer,
|
||||
} from "./server-containers.js";
|
||||
|
||||
const kc = new k8s.KubeConfig();
|
||||
kc.loadFromDefault();
|
||||
|
@ -39,72 +44,6 @@ function createServerVolume(serverSpec) {
|
|||
return volumeYaml;
|
||||
}
|
||||
|
||||
function getFtpContainer(serverSpec) {
|
||||
const { name } = serverSpec;
|
||||
const ftpContainer = loadYaml("lib/k8s/configs/containers/ftp-server.yml");
|
||||
ftpContainer.name = `mcl-${name}-ftp`;
|
||||
const ftpPortList = [
|
||||
{ p: 20, n: "ftp-data" },
|
||||
{ p: 21, n: "ftp-commands" },
|
||||
];
|
||||
for (var p = 40000; p <= 40009; p++)
|
||||
ftpPortList.push({ p, n: `ftp-passive-${p - 40000}` });
|
||||
ftpContainer.ports = ftpPortList.map(({ p: containerPort, n: name }) => ({
|
||||
containerPort,
|
||||
name,
|
||||
protocol: "TCP",
|
||||
}));
|
||||
return ftpContainer;
|
||||
}
|
||||
|
||||
function getServerContainer(serverSpec) {
|
||||
const {
|
||||
name,
|
||||
version,
|
||||
serverType,
|
||||
difficulty,
|
||||
gamemode,
|
||||
memory,
|
||||
motd,
|
||||
maxPlayers,
|
||||
seed,
|
||||
ops,
|
||||
whitelist,
|
||||
} = serverSpec;
|
||||
const container = loadYaml("lib/k8s/configs/containers/minecraft-server.yml");
|
||||
|
||||
// Container Updates
|
||||
container.name = `mcl-${name}-server`;
|
||||
container.resources.requests.memory = `${memory}Mi`;
|
||||
// container.resources.limits.memory = `${memory}Mi`; // TODO Allow for limits beyond initial startup
|
||||
|
||||
const findEnv = (k) => container.env.find(({ name: n }) => n === k);
|
||||
const updateEnv = (k, v) => (findEnv(k).value = v);
|
||||
|
||||
// Enviornment variables
|
||||
updateEnv("TYPE", serverType);
|
||||
updateEnv("VERSION", version);
|
||||
updateEnv("DIFFICULTY", difficulty);
|
||||
updateEnv("MODE", gamemode);
|
||||
updateEnv("MOTD", motd);
|
||||
updateEnv("MAX_PLAYERS", maxPlayers);
|
||||
updateEnv("SEED", seed);
|
||||
updateEnv("OPS", ops);
|
||||
updateEnv("WHITELIST", whitelist);
|
||||
updateEnv("MEMORY", `${memory}M`);
|
||||
|
||||
// RCON
|
||||
const rs = `mcl-${name}-rcon-secret`;
|
||||
findEnv("RCON_PASSWORD").valueFrom.secretKeyRef.name = rs;
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
function getBackupContainer(serverSpec) {
|
||||
const container = loadYaml("lib/k8s/configs/containers/minecraft-backup.yml");
|
||||
return container;
|
||||
}
|
||||
|
||||
function createServerDeploy(serverSpec) {
|
||||
const { name } = serverSpec;
|
||||
const deployYaml = loadYaml("lib/k8s/configs/server-deployment.yml");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue