2021-02-07 23:45:55 +00:00
|
|
|
variable "tls_secret_name" {}
|
2026-01-10 16:28:12 +00:00
|
|
|
variable "tier" { type = string }
|
2021-02-07 23:45:55 +00:00
|
|
|
variable "wg_0_conf" {}
|
|
|
|
|
variable "firewall_sh" {}
|
|
|
|
|
variable "wg_0_key" {}
|
|
|
|
|
|
2021-02-17 19:22:04 +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.wireguard.metadata[0].name
|
2021-02-17 19:22:04 +00:00
|
|
|
tls_secret_name = var.tls_secret_name
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 23:45:55 +00:00
|
|
|
resource "kubernetes_namespace" "wireguard" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "wireguard"
|
2026-02-21 23:38:05 +00:00
|
|
|
labels = {
|
|
|
|
|
tier = var.tier
|
|
|
|
|
}
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
resource "kubernetes_config_map" "wg_0_conf" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "wg0-conf"
|
2025-12-29 10:23:42 +00:00
|
|
|
namespace = kubernetes_namespace.wireguard.metadata[0].name
|
2021-02-07 23:45:55 +00:00
|
|
|
|
|
|
|
|
labels = {
|
|
|
|
|
app = "wireguard"
|
|
|
|
|
}
|
2021-03-17 21:02:53 +00:00
|
|
|
annotations = {
|
|
|
|
|
"reloader.stakater.com/match" = "true"
|
|
|
|
|
}
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
"setup-firewall.sh" = var.firewall_sh
|
2021-03-15 23:32:56 +00:00
|
|
|
"wg0.conf" = format("%s%s", var.wg_0_conf, file("${path.module}/extra/clients.conf"))
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_secret" "wg_0_key" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "wg0-key"
|
2025-12-29 10:23:42 +00:00
|
|
|
namespace = kubernetes_namespace.wireguard.metadata[0].name
|
2021-03-17 21:02:53 +00:00
|
|
|
|
|
|
|
|
annotations = {
|
|
|
|
|
"reloader.stakater.com/match" = "true"
|
|
|
|
|
}
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
data = {
|
|
|
|
|
"wg0.key" = var.wg_0_key
|
2021-04-02 23:14:47 +01:00
|
|
|
# If thep rivate key changes the pub key must be updated manually
|
|
|
|
|
"wg-ui-config" = format("{\"PrivateKey\": \"%s\",\"PublicKey\": \"%s\",\"Users\": {}}", var.wg_0_key, "3OeDa6Z3Z6vPVxn/WKJujYL7DoDYPPpI5W+2glUYLHU=")
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
type = "generic"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_deployment" "wireguard" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "wireguard"
|
2025-12-29 10:23:42 +00:00
|
|
|
namespace = kubernetes_namespace.wireguard.metadata[0].name
|
2021-02-07 23:45:55 +00:00
|
|
|
labels = {
|
2026-01-10 16:28:12 +00:00
|
|
|
app = "wireguard"
|
|
|
|
|
tier = var.tier
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
2021-03-17 21:02:53 +00:00
|
|
|
annotations = {
|
|
|
|
|
"reloader.stakater.com/search" = "true"
|
|
|
|
|
}
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
spec {
|
|
|
|
|
replicas = 1
|
|
|
|
|
strategy {
|
|
|
|
|
rolling_update {
|
|
|
|
|
max_surge = "2"
|
|
|
|
|
max_unavailable = "0"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
selector {
|
|
|
|
|
match_labels = {
|
|
|
|
|
app = "wireguard"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
template {
|
|
|
|
|
metadata {
|
|
|
|
|
labels = {
|
|
|
|
|
app = "wireguard"
|
|
|
|
|
}
|
|
|
|
|
annotations = {
|
|
|
|
|
"prometheus.io/scrape" = "true"
|
2026-02-11 22:40:56 +00:00
|
|
|
"prometheus.io/port" = "9586"
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spec {
|
2025-04-14 16:39:44 +00:00
|
|
|
init_container {
|
|
|
|
|
name = "sysctl-setup"
|
|
|
|
|
image = "busybox"
|
|
|
|
|
command = ["/bin/sh", "-c", "echo 1 > /proc/sys/net/ipv4/ip_forward"]
|
|
|
|
|
|
|
|
|
|
security_context {
|
|
|
|
|
privileged = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-07 23:45:55 +00:00
|
|
|
container {
|
|
|
|
|
image = "sclevine/wg:latest"
|
|
|
|
|
name = "wireguard"
|
|
|
|
|
image_pull_policy = "IfNotPresent"
|
|
|
|
|
lifecycle {
|
|
|
|
|
post_start {
|
|
|
|
|
exec {
|
|
|
|
|
command = ["wg-quick", "up", "wg0"]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pre_stop {
|
|
|
|
|
exec {
|
|
|
|
|
command = ["wg-quick", "down", "wg0"]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
command = ["tail", "-f", "/dev/null"]
|
|
|
|
|
port {
|
|
|
|
|
container_port = 51820
|
|
|
|
|
protocol = "UDP"
|
|
|
|
|
}
|
|
|
|
|
volume_mount {
|
|
|
|
|
name = "wg0-key"
|
|
|
|
|
mount_path = "/etc/wireguard/wg0.key"
|
|
|
|
|
sub_path = "wg0.key"
|
|
|
|
|
}
|
|
|
|
|
volume_mount {
|
|
|
|
|
name = "wg0-conf"
|
|
|
|
|
mount_path = "/etc/wireguard/wg0.conf"
|
|
|
|
|
sub_path = "wg0.conf"
|
|
|
|
|
}
|
|
|
|
|
volume_mount {
|
|
|
|
|
name = "wg0-conf"
|
|
|
|
|
mount_path = "/etc/wireguard/setup-firewall.sh"
|
|
|
|
|
sub_path = "setup-firewall.sh"
|
|
|
|
|
}
|
|
|
|
|
security_context {
|
|
|
|
|
capabilities {
|
|
|
|
|
add = ["NET_ADMIN", "SYS_MODULE"]
|
|
|
|
|
}
|
|
|
|
|
}
|
[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
|
|
|
resources {
|
|
|
|
|
requests = {
|
|
|
|
|
cpu = "10m"
|
|
|
|
|
memory = "16Mi"
|
|
|
|
|
}
|
|
|
|
|
limits = {
|
|
|
|
|
cpu = "100m"
|
|
|
|
|
memory = "128Mi"
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container {
|
|
|
|
|
name = "prometheus-exporter"
|
|
|
|
|
image = "mindflavor/prometheus-wireguard-exporter"
|
|
|
|
|
image_pull_policy = "IfNotPresent"
|
2022-08-30 00:02:50 +01:00
|
|
|
command = ["prometheus_wireguard_exporter", "-a", "true", "-v", "true", "-n", "/etc/wireguard/wg0.conf"]
|
2021-02-07 23:45:55 +00:00
|
|
|
volume_mount {
|
|
|
|
|
name = "wg0-conf"
|
|
|
|
|
mount_path = "/etc/wireguard/wg0.conf"
|
|
|
|
|
sub_path = "wg0.conf"
|
|
|
|
|
}
|
|
|
|
|
security_context {
|
|
|
|
|
capabilities {
|
|
|
|
|
add = ["NET_ADMIN"]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
port {
|
|
|
|
|
container_port = 9586
|
|
|
|
|
protocol = "TCP"
|
|
|
|
|
}
|
[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
|
|
|
resources {
|
|
|
|
|
requests = {
|
|
|
|
|
cpu = "10m"
|
|
|
|
|
memory = "16Mi"
|
|
|
|
|
}
|
|
|
|
|
limits = {
|
|
|
|
|
cpu = "50m"
|
|
|
|
|
memory = "64Mi"
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
volume {
|
|
|
|
|
name = "wg0-key"
|
|
|
|
|
secret {
|
|
|
|
|
secret_name = "wg0-key"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
volume {
|
|
|
|
|
name = "wg0-conf"
|
|
|
|
|
config_map {
|
|
|
|
|
name = "wg0-conf"
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-23 22:43:05 +00:00
|
|
|
dns_config {
|
|
|
|
|
option {
|
|
|
|
|
name = "ndots"
|
|
|
|
|
value = "2"
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-07 23:45:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_service" "wireguard" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "wireguard"
|
2025-12-29 10:23:42 +00:00
|
|
|
namespace = kubernetes_namespace.wireguard.metadata[0].name
|
2021-02-07 23:45:55 +00:00
|
|
|
annotations = {
|
|
|
|
|
"metallb.universe.tf/allow-shared-ip" = "shared"
|
|
|
|
|
}
|
|
|
|
|
labels = {
|
|
|
|
|
"app" = "wireguard"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spec {
|
|
|
|
|
type = "LoadBalancer"
|
|
|
|
|
external_traffic_policy = "Cluster"
|
|
|
|
|
selector = {
|
|
|
|
|
app = "wireguard"
|
|
|
|
|
}
|
|
|
|
|
port {
|
|
|
|
|
port = "51820"
|
|
|
|
|
protocol = "UDP"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_service" "wireguard_exporter" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "wireguard-exporter"
|
2025-12-29 10:23:42 +00:00
|
|
|
namespace = kubernetes_namespace.wireguard.metadata[0].name
|
2021-02-07 23:45:55 +00:00
|
|
|
labels = {
|
|
|
|
|
"app" = "wireguard-exporter"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spec {
|
|
|
|
|
selector = {
|
|
|
|
|
app = "wireguard"
|
|
|
|
|
}
|
|
|
|
|
port {
|
|
|
|
|
port = "9102"
|
|
|
|
|
target_port = "9586"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|