From c41fc4ef81704f2e358e26067b9e181fc4a50427 Mon Sep 17 00:00:00 2001 From: TuDatTr Date: Tue, 21 Nov 2023 14:55:39 +0100 Subject: [PATCH] Initial commit Signed-off-by: TuDatTr --- .gitignore | 34 ++++++++++++ .terraform.lock.hcl | 22 ++++++++ README.org | 2 + namespaces.tf | 5 ++ provider.tf | 12 ++++ traefik.tf | 72 ++++++++++++++++++++++++ uptimekuma.tf | 130 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 277 insertions(+) create mode 100644 .gitignore create mode 100644 .terraform.lock.hcl create mode 100644 README.org create mode 100644 namespaces.tf create mode 100644 provider.tf create mode 100644 traefik.tf create mode 100644 uptimekuma.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b8a46e --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log +crash.*.log + +# Exclude all .tfvars files, which are likely to contain sensitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +*.tfvars +*.tfvars.json + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..234067b --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,22 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/kubernetes" { + version = "2.23.0" + constraints = "2.23.0" + hashes = [ + "h1:xyFc77aYkPoU4Xt1i5t0B1IaS8TbTtp9aCSuQKDayII=", + "zh:10488a12525ed674359585f83e3ee5e74818b5c98e033798351678b21b2f7d89", + "zh:1102ba5ca1a595f880e67102bbf999cc8b60203272a078a5b1e896d173f3f34b", + "zh:1347cf958ed3f3f80b3c7b3e23ddda3d6c6573a81847a8ee92b7df231c238bf6", + "zh:2cb18e9f5156bc1b1ee6bc580a709f7c2737d142722948f4a6c3c8efe757fa8d", + "zh:5506aa6f28dcca2a265ccf8e34478b5ec2cb43b867fe6d93b0158f01590fdadd", + "zh:6217a20686b631b1dcb448ee4bc795747ebc61b56fbe97a1ad51f375ebb0d996", + "zh:8accf916c00579c22806cb771e8909b349ffb7eb29d9c5468d0a3f3166c7a84a", + "zh:9379b0b54a0fa030b19c7b9356708ec8489e194c3b5e978df2d31368563308e5", + "zh:aa99c580890691036c2931841e88e7ee80d59ae52289c8c2c28ea0ac23e31520", + "zh:c57376d169875990ac68664d227fb69cd0037b92d0eba6921d757c3fd1879080", + "zh:e6068e3f94f6943b5586557b73f109debe19d1a75ca9273a681d22d1ce066579", + "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", + ] +} diff --git a/README.org b/README.org new file mode 100644 index 0000000..711dc12 --- /dev/null +++ b/README.org @@ -0,0 +1,2 @@ +* Terraform Homelab +This repo contains the terraform configuration for my homelab to deploy various services. diff --git a/namespaces.tf b/namespaces.tf new file mode 100644 index 0000000..d418703 --- /dev/null +++ b/namespaces.tf @@ -0,0 +1,5 @@ +resource "kubernetes_namespace" "testing" { + metadata { + name = "homelab-testing" + } +} diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..1548860 --- /dev/null +++ b/provider.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + kubernetes = { + source = "hashicorp/kubernetes" + version = ">= 2.0" + } + } +} + +provider "kubernetes" { + config_path = "~/.kube/config" +} diff --git a/traefik.tf b/traefik.tf new file mode 100644 index 0000000..89a0345 --- /dev/null +++ b/traefik.tf @@ -0,0 +1,72 @@ +resource "kubernetes_deployment" "traefik" { + metadata { + name = "traefik" + labels = { + name = "traefik.k3s.seyshiro.de" + } + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + replicas = 1 + + selector { + match_labels = { + name = "traefik.k3s.seyshiro.de" + } + } + + template { + metadata { + labels = { + name = "traefik.k3s.seyshiro.de" + } + } + + spec { + container { + image = "traefik:v3.0" + name = "traefik" + + port { + container_port = 80 + } + + port { + container_port = 8080 + } + + resources { + limits = { + cpu = "0.5" + memory = "512Mi" + } + requests = { + cpu = "250m" + memory = "50Mi" + } + } + } + } + } + } +} + +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" + + } +} diff --git a/uptimekuma.tf b/uptimekuma.tf new file mode 100644 index 0000000..4981cbb --- /dev/null +++ b/uptimekuma.tf @@ -0,0 +1,130 @@ +resource "kubernetes_deployment" "status" { + metadata { + name = "status" + labels = { + name = "status.k3s.seyshiro.de" + } + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + replicas = 1 + + selector { + match_labels = { + name = "status.k3s.seyshiro.de" + } + } + + template { + metadata { + labels = { + name = "status.k3s.seyshiro.de" + } + } + + spec { + container { + image = "louislam/uptime-kuma:1.23.6" + name = "status" + + port { + container_port = 3001 + } + + resources { + limits = { + cpu = "0.5" + memory = "512Mi" + } + requests = { + cpu = "250m" + memory = "50Mi" + } + } + volume_mount { + name = "volv" + mount_path = "/app/data" + } + } + volume { + name = "volv" + persistent_volume_claim { + claim_name = kubernetes_persistent_volume.status.metadata.0.name + } + } + } + } + } +} + +resource "kubernetes_service" "status" { + metadata { + name = "status" + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + selector = { + name = kubernetes_deployment.status.spec.0.template.0.metadata.0.labels.name + } + port { + port = 3001 + target_port = 3001 + } + type = "NodePort" + + } +} + +resource "kubernetes_persistent_volume_claim" "status" { + metadata { + name = "status" + namespace = kubernetes_namespace.testing.metadata.0.name + } + + spec { + access_modes = ["ReadWriteMany"] + resources { + requests = { + storage = "2Gi" + } + } + + volume_name = kubernetes_persistent_volume.status.metadata.0.name + storage_class_name = "local-path" + } + + wait_until_bound = true +} + +resource "kubernetes_persistent_volume" "status" { + metadata { + name = "status" + } + + spec { + access_modes = ["ReadWriteMany"] + capacity = { + storage = "2Gi" + } + node_affinity { + + required { + node_selector_term { + match_fields{ + key = "metadata.name" + operator = "In" + values = ["status"] + } + } + } + } + persistent_volume_source { + local { + path = "/home/tuan/terraform/status/" + } + } + storage_class_name = "local-path" + } +}