From bd56ee24c0316b23063cb8cea143359f6808b2b1 Mon Sep 17 00:00:00 2001 From: Tuan-Dat Tran Date: Mon, 22 Jul 2024 19:47:23 +0200 Subject: [PATCH] Added traefik/sonarr and ingress Signed-off-by: Tuan-Dat Tran --- sonarr.tf | 254 ++++++++++++++++++++++++++++++++++++++++++++++++++ traefik.tf | 143 ++++++++++++++++++++++------ uptimekuma.tf | 26 +++--- 3 files changed, 379 insertions(+), 44 deletions(-) create mode 100644 sonarr.tf diff --git a/sonarr.tf b/sonarr.tf new file mode 100644 index 0000000..1914cce --- /dev/null +++ b/sonarr.tf @@ -0,0 +1,254 @@ +resource "kubernetes_deployment" "sonarr" { + metadata { + name = "sonarr-deployment" + labels = { + name = "sonarr.k3s.seyshiro.de" + } + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + replicas = 1 + + selector { + match_labels = { + name = "sonarr.k3s.seyshiro.de" + } + } + + template { + metadata { + labels = { + name = "sonarr.k3s.seyshiro.de" + } + } + + spec { + container { + image = "lscr.io/linuxserver/sonarr:latest" + name = "sonarr-pod" + + port { + container_port = 8989 + } + + resources { + limits = { + cpu = "0.5" + memory = "512Mi" + } + requests = { + cpu = "250m" + memory = "50Mi" + } + } + volume_mount { + name = kubernetes_persistent_volume.sonarr_config.metadata.0.name + mount_path = "/config" + } + volume_mount { + name = kubernetes_persistent_volume.sonarr_tv.metadata.0.name + mount_path = "/tv" + } + volume_mount { + name = kubernetes_persistent_volume.sonarr_downloads.metadata.0.name + mount_path = "/downloads" + } + } + volume { + name = kubernetes_persistent_volume.sonarr_config.metadata.0.name + persistent_volume_claim { + claim_name = kubernetes_persistent_volume.sonarr_config.metadata.0.name + } + } + volume { + name = kubernetes_persistent_volume.sonarr_tv.metadata.0.name + persistent_volume_claim { + claim_name = kubernetes_persistent_volume.sonarr_tv.metadata.0.name + } + } + volume { + name = kubernetes_persistent_volume.sonarr_downloads.metadata.0.name + persistent_volume_claim { + claim_name = kubernetes_persistent_volume.sonarr_downloads.metadata.0.name + } + } + } + } + } +} + +resource "kubernetes_service" "sonarr" { + metadata { + name = "sonarr-service" + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + selector = { + name = kubernetes_deployment.sonarr.spec.0.template.0.metadata.0.labels.name + } + port { + port = 8989 + target_port = 8989 + } + type = "NodePort" + + } +} + +resource "kubernetes_persistent_volume_claim" "sonarr_config" { + metadata { + name = "sonarr-config" + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + access_modes = ["ReadWriteMany"] + resources { + requests = { + storage = "2Gi" + } + } + + volume_name = kubernetes_persistent_volume.sonarr_config.metadata.0.name + storage_class_name = "local-path" + } + + wait_until_bound = true +} + +resource "kubernetes_persistent_volume" "sonarr_config" { + metadata { + name = "sonarr-config" + } + + spec { + access_modes = ["ReadWriteMany"] + capacity = { + storage = "2Gi" + } + node_affinity { + + required { + node_selector_term { + match_fields { + key = "metadata.name" + operator = "In" + values = ["sonarr"] + } + } + } + } + persistent_volume_source { + local { + path = "/home/tudattr/terraform/sonarr/config/" + } + } + storage_class_name = "local-path" + } +} + +resource "kubernetes_persistent_volume_claim" "sonarr_tv" { + metadata { + name = "sonarr-tv" + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + access_modes = ["ReadWriteMany"] + resources { + requests = { + storage = "2Gi" + } + } + + volume_name = kubernetes_persistent_volume.sonarr_tv.metadata.0.name + storage_class_name = "local-path" + } + + wait_until_bound = true +} + +resource "kubernetes_persistent_volume" "sonarr_tv" { + metadata { + name = "sonarr-tv" + } + + spec { + access_modes = ["ReadWriteMany"] + capacity = { + storage = "2Gi" + } + node_affinity { + + required { + node_selector_term { + match_fields { + key = "metadata.name" + operator = "In" + values = ["sonarr"] + } + } + } + } + persistent_volume_source { + local { + path = "/home/tudattr/terraform/sonarr/tv/" + } + } + storage_class_name = "local-path" + } +} + +resource "kubernetes_persistent_volume_claim" "sonarr_downloads" { + metadata { + name = "sonarr-downloads" + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + access_modes = ["ReadWriteMany"] + resources { + requests = { + storage = "2Gi" + } + } + + volume_name = kubernetes_persistent_volume.sonarr_downloads.metadata.0.name + storage_class_name = "local-path" + } + + wait_until_bound = true +} + +resource "kubernetes_persistent_volume" "sonarr_downloads" { + metadata { + name = "sonarr-downloads" + } + + spec { + access_modes = ["ReadWriteMany"] + capacity = { + storage = "2Gi" + } + node_affinity { + + required { + node_selector_term { + match_fields { + key = "metadata.name" + operator = "In" + values = ["sonarr"] + } + } + } + } + persistent_volume_source { + local { + path = "/home/tudattr/terraform/sonarr/downloads/" + } + } + storage_class_name = "local-path" + } +} diff --git a/traefik.tf b/traefik.tf index 89a0345..ccb4f7b 100644 --- a/traefik.tf +++ b/traefik.tf @@ -1,36 +1,125 @@ -resource "kubernetes_deployment" "traefik" { +resource "kubernetes_ingress_v1" "ingress" { + wait_for_load_balancer = true metadata { - name = "traefik" - labels = { - name = "traefik.k3s.seyshiro.de" - } + name = "ingress" namespace = kubernetes_namespace.testing.metadata.0.name } spec { - replicas = 1 + default_backend { + service { + name = kubernetes_service.ingress.metadata.0.name + port { + number = kubernetes_service.ingress.spec.0.port.0.port + } + } + } + rule { + host = "tooltoulto.seyshiro.de" + http { + path { + path = "/" + backend { + service { + name = kubernetes_service.ingress.metadata.0.name + port { + number = kubernetes_service.ingress.spec.0.port.0.port + } + } + } + } + } + } + + rule { + host = "tooltoulto.seyshiro.de" + http { + path { + path = "/status" + backend { + service { + name = kubernetes_service.status.metadata.0.name + port { + number = kubernetes_service.status.spec.0.port.0.port + } + } + } + } + } + } + + rule { + http { + path { + path = "/sonarr" + backend { + service { + name = kubernetes_service.sonarr.metadata.0.name + port { + number = kubernetes_service.sonarr.spec.0.port.0.port + } + } + } + } + + } + } + + tls { + secret_name = "tls-secret" + } + } +} + +resource "kubernetes_service" "ingress" { + metadata { + name = "ingress-service" + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + selector = { + name = kubernetes_deployment.ingress.spec.0.template.0.metadata.0.labels.name + } + port { + port = 8080 + target_port = 80 + protocol = "TCP" + } + type = "NodePort" + } +} + +resource "kubernetes_deployment" "ingress" { + metadata { + name = "ingress-deployment" + namespace = kubernetes_namespace.testing.metadata.0.name + labels = { + name = "status.k3s.seyshiro.de" + } + } + + spec { + replicas = 1 selector { match_labels = { - name = "traefik.k3s.seyshiro.de" + name = "nginx.k3s.seyshiro.de" } } template { metadata { labels = { - name = "traefik.k3s.seyshiro.de" + name = "nginx.k3s.seyshiro.de" } } + spec { container { - image = "traefik:v3.0" - name = "traefik" - - port { - container_port = 80 - } + image = "nginx:1.7.9" + name = "ingress-pod" port { container_port = 8080 @@ -52,21 +141,13 @@ resource "kubernetes_deployment" "traefik" { } } -resource "kubernetes_service" "traefik" { - metadata { - name = "traefik" - namespace = kubernetes_namespace.testing.metadata.0.name - } - - spec { - selector = { - name = kubernetes_deployment.traefik.spec.0.template.0.metadata.0.labels.name - } - port { - port = 80 - target_port = 80 - } - type = "NodePort" - - } +# Display load balancer hostname (typically present in AWS) +output "load_balancer_hostname" { + value = kubernetes_ingress_v1.ingress.status.0.load_balancer.0.ingress.0.hostname } + +# Display load balancer IP (typically present in GCP, or using Nginx ingress controller) +output "load_balancer_ip" { + value = kubernetes_ingress_v1.ingress.status.0.load_balancer.0.ingress.0.ip +} + diff --git a/uptimekuma.tf b/uptimekuma.tf index 4981cbb..794f7ce 100644 --- a/uptimekuma.tf +++ b/uptimekuma.tf @@ -1,6 +1,6 @@ resource "kubernetes_deployment" "status" { metadata { - name = "status" + name = "status-deployment" labels = { name = "status.k3s.seyshiro.de" } @@ -26,7 +26,7 @@ resource "kubernetes_deployment" "status" { spec { container { image = "louislam/uptime-kuma:1.23.6" - name = "status" + name = "status-pod" port { container_port = 3001 @@ -43,12 +43,12 @@ resource "kubernetes_deployment" "status" { } } volume_mount { - name = "volv" + name = "storage" mount_path = "/app/data" } } volume { - name = "volv" + name = "storage" persistent_volume_claim { claim_name = kubernetes_persistent_volume.status.metadata.0.name } @@ -60,7 +60,7 @@ resource "kubernetes_deployment" "status" { resource "kubernetes_service" "status" { metadata { - name = "status" + name = "status-service" namespace = kubernetes_namespace.testing.metadata.0.name } @@ -79,7 +79,7 @@ resource "kubernetes_service" "status" { resource "kubernetes_persistent_volume_claim" "status" { metadata { - name = "status" + name = "status-config" namespace = kubernetes_namespace.testing.metadata.0.name } @@ -91,8 +91,8 @@ resource "kubernetes_persistent_volume_claim" "status" { } } - volume_name = kubernetes_persistent_volume.status.metadata.0.name - storage_class_name = "local-path" + volume_name = kubernetes_persistent_volume.status.metadata.0.name + storage_class_name = "local-path" } wait_until_bound = true @@ -100,7 +100,7 @@ resource "kubernetes_persistent_volume_claim" "status" { resource "kubernetes_persistent_volume" "status" { metadata { - name = "status" + name = "status-config" } spec { @@ -112,17 +112,17 @@ resource "kubernetes_persistent_volume" "status" { required { node_selector_term { - match_fields{ - key = "metadata.name" + match_fields { + key = "metadata.name" operator = "In" - values = ["status"] + values = ["status"] } } } } persistent_volume_source { local { - path = "/home/tuan/terraform/status/" + path = "/home/tudattr/terraform/status/" } } storage_class_name = "local-path"