From ff2b565677adc7f3b07ac6e9903de09d6ea0a1a0 Mon Sep 17 00:00:00 2001 From: kasun Date: Sat, 21 Nov 2026 15:49:07 +0100 Subject: [PATCH] bug(k8s-infra): prevent Longhorn disks filling up after node outages Orphaned replica data and a large snapshot left no room for a second Prometheus replica, so the volume faulted when its only replica failed. Clean up orphans and system snapshots, trim volumes daily, and lower the free-space reserve to 10% because longhorn-extra is a dedicated disk. Align the UI default with the StorageClass at 3 replicas so volumes keep two copies during a node reboot. --- 03-k8s-infra/index.ts | 48 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/03-k8s-infra/index.ts b/03-k8s-infra/index.ts index 9be80b1..c59195f 100644 --- a/03-k8s-infra/index.ts +++ b/03-k8s-infra/index.ts @@ -138,15 +138,61 @@ const longhorn = new k8s.helm.v3.Release( version: "1.12.0", values: { defaultSettings: { - defaultReplicaCount: 2, + defaultReplicaCount: 3, nodeDownPodDeletionPolicy: "delete-both-statefulset-and-deployment-pod", createDefaultDiskLabeledNodes: true, + orphanResourceAutoDeletion: "replica-data;instance", + // Longhorn's recommendation for dedicated disks (longhorn-extra). + // Docs: https://longhorn.io/docs/1.12.0/best-practices/ + storageMinimalAvailablePercentage: 10, }, }, }, opts(), ); +// Purges system snapshots, which can otherwise grow to a full volume's size +// on every replica. Docs: +// https://longhorn.io/docs/1.12.0/snapshots-and-backups/scheduling-backups-and-snapshots/ +new k8s.apiextensions.CustomResource( + "longhorn-snapshot-cleanup", + { + apiVersion: "longhorn.io/v1beta2", + kind: "RecurringJob", + metadata: { name: "snapshot-cleanup", namespace: "longhorn-system" }, + spec: { + name: "snapshot-cleanup", + task: "snapshot-cleanup", + cron: "30 3 * * *", + groups: ["default"], + retain: 0, + concurrency: 1, + }, + }, + opts({ dependsOn: [longhorn] }), +); + +// Returns blocks deleted inside a volume (e.g. expired Prometheus data) to the +// disk. Runs after snapshot-cleanup, since trim can't reclaim valid snapshots. +// Docs: https://longhorn.io/docs/1.12.0/nodes-and-volumes/volumes/trim-filesystem/ +new k8s.apiextensions.CustomResource( + "longhorn-filesystem-trim", + { + apiVersion: "longhorn.io/v1beta2", + kind: "RecurringJob", + metadata: { name: "filesystem-trim", namespace: "longhorn-system" }, + spec: { + name: "filesystem-trim", + task: "filesystem-trim", + cron: "30 4 * * *", + groups: ["default"], + retain: 0, + concurrency: 1, + }, + }, + opts({ dependsOn: [longhorn] }), +); + // Patches Longhorn instances to use the mountend Longhorn storage on the worker nodes. ["k3s-worker-1", "k3s-worker-2", "k3s-worker-3"].forEach( (nodeName) => -- 2.54.1