commit 860a20f1ac7a5aca25ed143ff691eb557c675d9f Author: RaviAnand Mohabir Date: Wed Jan 18 11:51:06 2023 +0100 feat: add Redis Terraform module diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..d9bba07 --- /dev/null +++ b/main.tf @@ -0,0 +1,86 @@ +terraform { + required_providers { + kubernetes = { + source = "hashicorp/kubernetes" + version = "2.13.1" + } + } +} + +locals { + app = "redis" + match_labels = { + "app.kubernetes.io/name" = "redis" + "app.kubernetes.io/instance" = "redis" + } + labels = merge(local.match_labels, var.labels) +} + +resource "kubernetes_stateful_set" "redis" { + metadata { + name = var.stateful_set_name + namespace = var.namespace + labels = local.labels + } + + spec { + selector { + match_labels = local.match_labels + } + service_name = local.app + template { + metadata { + labels = local.match_labels + } + spec { + container { + image = var.image_registry == "" ? "${var.image_repository}:${var.image_tag}" : "${var.image_registry}/${var.image_repository}:${var.image_tag}" + name = var.container_name + command = ["redis-server"] + env { + name = "master" + value = "true" + } + port { + name = "redis" + container_port = 6379 + } + volume_mount { + name = "data" + mount_path = "/data" + } + } + } + } + volume_claim_template { + metadata { + name = "data" + } + spec { + access_modes = ["ReadWriteOnce"] + resources { + requests = { + storage = var.storage_size + } + } + storage_class_name = var.storage_class_name + } + } + } +} + +resource "kubernetes_service" "redis" { + metadata { + name = var.service_name + namespace = var.namespace + } + spec { + port { + name = "redis" + port = 6379 + target_port = "redis" + } + type = "ClusterIP" + selector = local.match_labels + } +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..97473cb --- /dev/null +++ b/outputs.tf @@ -0,0 +1,4 @@ +output "redis_namespace_host" { + description = "Hostname of the Redis service in the namespace" + value = "${kubernetes_service.redis.metadata.0.name}" +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..93c5793 --- /dev/null +++ b/variables.tf @@ -0,0 +1,65 @@ +variable "namespace" { + description = "Namespace to deploy Redis to" + type = string + default = "default" +} + +variable "labels" { + description = "Labels to add to the Redis deployment" + type = map(any) + default = {} +} + +variable "stateful_set_name" { + description = "Name of StatefulSet" + type = string + default = "redis" +} + +variable "storage_size" { + description = "Storage size for StatefulSet PVC" + type = string + default = "5Gi" +} + +variable "storage_class_name" { + description = "Storage class to use for StatefulSet PVCs" + type = string + default = "" +} + +variable "image_registry" { + description = "Image registry, e.g. gcr.io, docker.io" + type = string + default = "" +} + +variable "image_repository" { + description = "Image to start for this pod" + type = string + default = "redis" +} + +variable "image_tag" { + description = "Image tag to use" + type = string + default = "7.0.4" +} + +variable "container_name" { + description = "Name of the Redis container" + type = string + default = "redis" +} + +variable "service_name" { + description = "Name of service to deploy" + type = string + default = "redis" +} + +variable "service_type" { + description = "Type of service to deploy" + type = string + default = "ClusterIP" +}