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) =>