2023-11-04 01:07:41 +00:00
|
|
|
variable "tls_secret_name" {}
|
2026-01-10 16:28:12 +00:00
|
|
|
variable "tier" { type = string }
|
[ci skip] Infrastructure hardening: security, monitoring, reliability, maintainability
Phase 1 - Critical Security:
- Netbox: move hardcoded DB/superuser passwords to variables
- MeshCentral: disable public registration, add Authentik auth
- Traefik: disable insecure API dashboard (api.insecure=false)
- Traefik: configure forwarded headers with Cloudflare trusted IPs
Phase 2 - Security Hardening:
- Add security headers middleware (HSTS, X-Frame-Options, nosniff, etc.)
- Add Kyverno pod security policies in audit mode (privileged, host
namespaces, SYS_ADMIN, trusted registries)
- Tighten rate limiting (avg=10, burst=50)
- Add Authentik protection to grampsweb
Phase 3 - Monitoring & Alerting:
- Add critical service alerts (PostgreSQL, MySQL, Redis, Headscale,
Authentik, Loki)
- Increase Loki retention from 7 to 30 days (720h)
- Add predictive PV filling alert (predict_linear)
- Re-enable Hackmd and Privatebin down alerts
Phase 4 - Reliability:
- Add resource requests/limits to Redis, DBaaS, Technitium, Headscale,
Vaultwarden, Uptime Kuma
- Increase Alloy DaemonSet memory to 512Mi/1Gi
Phase 6 - Maintainability:
- Extract duplicated tiers locals to terragrunt.hcl generate block
(removed from 67 stacks)
- Replace hardcoded NFS IP 10.0.10.15 with var.nfs_server (114
instances across 63 files)
- Replace hardcoded Redis/PostgreSQL/MySQL/Ollama/mail host references
with variables across ~35 stacks
- Migrate xray raw ingress resources to ingress_factory modules
2026-02-23 22:05:28 +00:00
|
|
|
variable "nfs_server" { type = string }
|
2023-11-04 01:07:41 +00:00
|
|
|
|
|
|
|
|
resource "kubernetes_namespace" "redis" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "redis"
|
2026-02-21 23:38:05 +00:00
|
|
|
labels = {
|
|
|
|
|
tier = var.tier
|
|
|
|
|
}
|
2023-11-04 01:07:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module "tls_secret" {
|
2026-02-22 14:38:14 +00:00
|
|
|
source = "../../../../modules/kubernetes/setup_tls_secret"
|
2025-12-29 10:23:42 +00:00
|
|
|
namespace = kubernetes_namespace.redis.metadata[0].name
|
2023-11-04 01:07:41 +00:00
|
|
|
tls_secret_name = var.tls_secret_name
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-28 19:59:58 +00:00
|
|
|
# Redis with Sentinel HA via Bitnami Helm chart
|
|
|
|
|
# Architecture: 1 master + 2 replicas + 3 sentinels
|
|
|
|
|
# Sentinel automatically promotes a replica if master fails
|
|
|
|
|
# The K8s Service always points at the current master
|
|
|
|
|
resource "helm_release" "redis" {
|
|
|
|
|
namespace = kubernetes_namespace.redis.metadata[0].name
|
|
|
|
|
create_namespace = false
|
|
|
|
|
name = "redis"
|
|
|
|
|
atomic = true
|
|
|
|
|
timeout = 600
|
2026-02-28 19:44:08 +00:00
|
|
|
|
2026-02-28 19:59:58 +00:00
|
|
|
repository = "oci://10.0.20.10:5000/bitnamicharts"
|
|
|
|
|
chart = "redis"
|
|
|
|
|
version = "25.3.2"
|
2026-02-28 19:44:08 +00:00
|
|
|
|
2026-02-28 19:59:58 +00:00
|
|
|
values = [yamlencode({
|
|
|
|
|
architecture = "replication"
|
|
|
|
|
|
|
|
|
|
auth = {
|
|
|
|
|
enabled = false
|
2025-11-10 22:40:28 +00:00
|
|
|
}
|
2026-02-28 19:59:58 +00:00
|
|
|
|
|
|
|
|
sentinel = {
|
2026-02-28 21:59:08 +00:00
|
|
|
enabled = true
|
|
|
|
|
quorum = 2
|
|
|
|
|
masterSet = "mymaster"
|
|
|
|
|
automateCluster = true
|
2026-02-28 19:59:58 +00:00
|
|
|
|
|
|
|
|
resources = {
|
|
|
|
|
requests = {
|
|
|
|
|
cpu = "50m"
|
|
|
|
|
memory = "64Mi"
|
|
|
|
|
}
|
|
|
|
|
limits = {
|
|
|
|
|
cpu = "200m"
|
|
|
|
|
memory = "128Mi"
|
|
|
|
|
}
|
2023-11-04 01:07:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-28 19:59:58 +00:00
|
|
|
|
|
|
|
|
master = {
|
|
|
|
|
persistence = {
|
|
|
|
|
enabled = true
|
2026-03-06 20:50:55 +00:00
|
|
|
storageClass = "iscsi-truenas"
|
2026-02-28 19:59:58 +00:00
|
|
|
size = "2Gi"
|
2023-11-04 01:07:41 +00:00
|
|
|
}
|
2026-02-28 19:44:08 +00:00
|
|
|
|
2026-02-28 19:59:58 +00:00
|
|
|
resources = {
|
|
|
|
|
requests = {
|
2026-02-28 21:59:08 +00:00
|
|
|
cpu = "100m"
|
|
|
|
|
memory = "64Mi"
|
2026-02-28 19:59:58 +00:00
|
|
|
}
|
|
|
|
|
limits = {
|
2026-02-28 21:59:08 +00:00
|
|
|
cpu = "500m"
|
|
|
|
|
memory = "256Mi"
|
2026-02-28 19:59:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-04 01:07:41 +00:00
|
|
|
|
2026-02-28 19:59:58 +00:00
|
|
|
replica = {
|
|
|
|
|
replicaCount = 2
|
[ci skip] Infrastructure hardening: security, monitoring, reliability, maintainability
Phase 1 - Critical Security:
- Netbox: move hardcoded DB/superuser passwords to variables
- MeshCentral: disable public registration, add Authentik auth
- Traefik: disable insecure API dashboard (api.insecure=false)
- Traefik: configure forwarded headers with Cloudflare trusted IPs
Phase 2 - Security Hardening:
- Add security headers middleware (HSTS, X-Frame-Options, nosniff, etc.)
- Add Kyverno pod security policies in audit mode (privileged, host
namespaces, SYS_ADMIN, trusted registries)
- Tighten rate limiting (avg=10, burst=50)
- Add Authentik protection to grampsweb
Phase 3 - Monitoring & Alerting:
- Add critical service alerts (PostgreSQL, MySQL, Redis, Headscale,
Authentik, Loki)
- Increase Loki retention from 7 to 30 days (720h)
- Add predictive PV filling alert (predict_linear)
- Re-enable Hackmd and Privatebin down alerts
Phase 4 - Reliability:
- Add resource requests/limits to Redis, DBaaS, Technitium, Headscale,
Vaultwarden, Uptime Kuma
- Increase Alloy DaemonSet memory to 512Mi/1Gi
Phase 6 - Maintainability:
- Extract duplicated tiers locals to terragrunt.hcl generate block
(removed from 67 stacks)
- Replace hardcoded NFS IP 10.0.10.15 with var.nfs_server (114
instances across 63 files)
- Replace hardcoded Redis/PostgreSQL/MySQL/Ollama/mail host references
with variables across ~35 stacks
- Migrate xray raw ingress resources to ingress_factory modules
2026-02-23 22:05:28 +00:00
|
|
|
|
2026-02-28 19:59:58 +00:00
|
|
|
persistence = {
|
|
|
|
|
enabled = true
|
2026-03-06 20:50:55 +00:00
|
|
|
storageClass = "iscsi-truenas"
|
2026-02-28 19:59:58 +00:00
|
|
|
size = "2Gi"
|
|
|
|
|
}
|
2026-02-28 19:44:08 +00:00
|
|
|
|
2026-02-28 19:59:58 +00:00
|
|
|
resources = {
|
|
|
|
|
requests = {
|
2026-02-28 21:59:08 +00:00
|
|
|
cpu = "50m"
|
|
|
|
|
memory = "64Mi"
|
2023-11-04 01:07:41 +00:00
|
|
|
}
|
2026-02-28 19:59:58 +00:00
|
|
|
limits = {
|
|
|
|
|
cpu = "500m"
|
2026-02-28 21:59:08 +00:00
|
|
|
memory = "256Mi"
|
2026-02-23 22:43:05 +00:00
|
|
|
}
|
2023-11-04 01:07:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-28 19:44:08 +00:00
|
|
|
|
2026-02-28 19:59:58 +00:00
|
|
|
# Metrics for Prometheus
|
|
|
|
|
metrics = {
|
|
|
|
|
enabled = false
|
2023-11-04 01:07:41 +00:00
|
|
|
}
|
2026-02-28 19:59:58 +00:00
|
|
|
|
|
|
|
|
# Use the existing service name so clients don't need changes
|
|
|
|
|
# Sentinel-enabled Bitnami chart creates a headless service
|
|
|
|
|
# and a regular service pointing at the master
|
|
|
|
|
nameOverride = "redis"
|
|
|
|
|
})]
|
2023-11-04 01:07:41 +00:00
|
|
|
}
|
2026-02-28 19:44:08 +00:00
|
|
|
|
2026-03-01 17:13:25 +00:00
|
|
|
# Override the Helm-managed service to pin to master pod
|
|
|
|
|
# Sentinel clients can use the headless service for discovery,
|
|
|
|
|
# but simple redis:// clients (paperless-ngx, etc.) need to hit the master
|
|
|
|
|
resource "kubernetes_service" "redis" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "redis"
|
|
|
|
|
namespace = kubernetes_namespace.redis.metadata[0].name
|
|
|
|
|
}
|
|
|
|
|
spec {
|
|
|
|
|
selector = {
|
[ci skip] right-size all pod resources based on VPA + live metrics audit
Full cluster resource audit: cross-referenced Goldilocks VPA recommendations,
live kubectl top metrics, and Terraform definitions for 100+ containers.
Critical fixes:
- dashy: CPU throttled at 98% (490m/500m) → 2 CPU limit
- stirling-pdf: CPU throttled at 99.7% (299m/300m) → 2 CPU limit
- traefik auth-proxy/bot-block-proxy: mem limit 32Mi → 128Mi
Added explicit resources to ~40 containers that had none:
- audiobookshelf, changedetection, cyberchef, dawarich, diun, echo,
excalidraw, freshrss, hackmd, isponsorblocktv, linkwarden, n8n,
navidrome, ntfy, owntracks, privatebin, send, shadowsocks, tandoor,
tor-proxy, wealthfolio, networking-toolbox, rybbit, mailserver,
cloudflared, pgadmin, phpmyadmin, crowdsec-web, xray, wireguard,
k8s-portal, tuya-bridge, ollama-ui, whisper, piper, immich-server,
immich-postgresql, osrm-foot
GPU containers: added CPU/mem alongside GPU limits:
- ollama: removed CPU/mem limits (models vary in size), keep GPU only
- frigate: req 500m/2Gi, lim 4/8Gi + GPU
- immich-ml: req 100m/1Gi, lim 2/4Gi + GPU
Right-sized ~25 over-provisioned containers:
- kms-web-page: 500m/512Mi → 50m/64Mi (was using 0m/10Mi)
- onlyoffice: CPU 8 → 2 (VPA upper 45m)
- realestate-crawler-api: CPU 2000m → 250m
- blog/travel-blog/webhook-handler: 500m → 100m
- coturn/health/plotting-book: reduced to match actual usage
Conservative methodology: limits = max(VPA upper * 2, live usage * 2)
2026-03-01 19:18:50 +00:00
|
|
|
"app.kubernetes.io/component" = "node"
|
|
|
|
|
"app.kubernetes.io/instance" = "redis"
|
|
|
|
|
"app.kubernetes.io/name" = "redis"
|
|
|
|
|
"statefulset.kubernetes.io/pod-name" = "redis-node-0"
|
2026-03-01 17:13:25 +00:00
|
|
|
}
|
|
|
|
|
port {
|
|
|
|
|
name = "tcp-redis"
|
|
|
|
|
port = 6379
|
|
|
|
|
target_port = 6379
|
|
|
|
|
}
|
|
|
|
|
port {
|
|
|
|
|
name = "tcp-sentinel"
|
|
|
|
|
port = 26379
|
|
|
|
|
target_port = 26379
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
depends_on = [helm_release.redis]
|
|
|
|
|
}
|
|
|
|
|
|
[ci skip] complete NFS CSI migration: complex stacks + platform modules
Migrate remaining multi-volume stacks and all platform modules from
inline NFS volumes to CSI-backed PV/PVC with nfs-truenas StorageClass
(soft,timeo=30,retrans=3 mount options).
Complex stacks: openclaw (4 vols), immich (8 vols), frigate (2 vols),
nextcloud (2 vols + old PV replaced), rybbit (1 vol)
Remaining stacks: affine, ebook2audiobook, f1-stream, osm_routing,
real-estate-crawler
Platform modules: monitoring (prometheus, loki, alertmanager PVs
converted from native NFS to CSI), redis, dbaas, technitium,
headscale, vaultwarden, uptime-kuma, mailserver, infra-maintenance
2026-03-02 01:24:07 +00:00
|
|
|
module "nfs_backup" {
|
|
|
|
|
source = "../../../../modules/kubernetes/nfs_volume"
|
|
|
|
|
name = "redis-backup"
|
|
|
|
|
namespace = kubernetes_namespace.redis.metadata[0].name
|
|
|
|
|
nfs_server = var.nfs_server
|
|
|
|
|
nfs_path = "/mnt/main/redis-backup"
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-28 19:59:58 +00:00
|
|
|
# Hourly backup: copy RDB snapshot from master to NFS
|
2026-02-28 19:44:08 +00:00
|
|
|
resource "kubernetes_cron_job_v1" "redis-backup" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "redis-backup"
|
|
|
|
|
namespace = kubernetes_namespace.redis.metadata[0].name
|
|
|
|
|
}
|
|
|
|
|
spec {
|
|
|
|
|
concurrency_policy = "Replace"
|
|
|
|
|
failed_jobs_history_limit = 3
|
|
|
|
|
schedule = "0 * * * *"
|
|
|
|
|
starting_deadline_seconds = 10
|
|
|
|
|
successful_jobs_history_limit = 3
|
|
|
|
|
job_template {
|
|
|
|
|
metadata {}
|
|
|
|
|
spec {
|
|
|
|
|
backoff_limit = 2
|
|
|
|
|
ttl_seconds_after_finished = 60
|
|
|
|
|
template {
|
|
|
|
|
metadata {}
|
|
|
|
|
spec {
|
|
|
|
|
container {
|
|
|
|
|
name = "redis-backup"
|
|
|
|
|
image = "redis:7-alpine"
|
|
|
|
|
command = ["/bin/sh", "-c", <<-EOT
|
|
|
|
|
set -eux
|
2026-02-28 19:59:58 +00:00
|
|
|
# Trigger a fresh RDB save on the master
|
2026-02-28 19:44:08 +00:00
|
|
|
redis-cli -h redis.redis BGSAVE
|
|
|
|
|
sleep 5
|
2026-02-28 19:59:58 +00:00
|
|
|
# Copy the RDB via redis-cli --rdb
|
2026-02-28 19:44:08 +00:00
|
|
|
redis-cli -h redis.redis --rdb /backup/dump.rdb
|
|
|
|
|
echo "Backup complete: $(ls -lh /backup/dump.rdb)"
|
|
|
|
|
EOT
|
|
|
|
|
]
|
|
|
|
|
volume_mount {
|
|
|
|
|
name = "backup"
|
|
|
|
|
mount_path = "/backup"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
volume {
|
|
|
|
|
name = "backup"
|
[ci skip] complete NFS CSI migration: complex stacks + platform modules
Migrate remaining multi-volume stacks and all platform modules from
inline NFS volumes to CSI-backed PV/PVC with nfs-truenas StorageClass
(soft,timeo=30,retrans=3 mount options).
Complex stacks: openclaw (4 vols), immich (8 vols), frigate (2 vols),
nextcloud (2 vols + old PV replaced), rybbit (1 vol)
Remaining stacks: affine, ebook2audiobook, f1-stream, osm_routing,
real-estate-crawler
Platform modules: monitoring (prometheus, loki, alertmanager PVs
converted from native NFS to CSI), redis, dbaas, technitium,
headscale, vaultwarden, uptime-kuma, mailserver, infra-maintenance
2026-03-02 01:24:07 +00:00
|
|
|
persistent_volume_claim {
|
|
|
|
|
claim_name = module.nfs_backup.claim_name
|
2026-02-28 19:44:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-04 01:07:41 +00:00
|
|
|
}
|