diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..8f0d036 --- /dev/null +++ b/main.tf @@ -0,0 +1,64 @@ +terraform { + required_providers { + kubernetes = { + source = "hashicorp/kubernetes" + version = ">= 2.0" + } + } +} + +resource "kubernetes_deployment" "redis" { + metadata { + name = var.redis_cache_name + namespace = var.namespace + labels = { + app = "redis" + } + } + + spec { + replicas = 1 + + selector { + match_labels = { + app = "redis" + } + } + + template { + metadata { + labels = { + app = "redis" + } + } + + spec { + container { + name = "redis" + image = "redis:latest" + port { + container_port = 6379 + } + } + } + } + } +} + +resource "kubernetes_service" "redis" { + metadata { + name = var.redis_cache_name + namespace = var.namespace + } + + spec { + selector = { + app = "redis" + } + + port { + port = 6379 # Service port + target_port = 6379 # Target port of the Redis deployment + } + } +} diff --git a/output.tf b/output.tf new file mode 100644 index 0000000..ee9d3c7 --- /dev/null +++ b/output.tf @@ -0,0 +1,11 @@ +output "host" { + value = "${kubernetes_service.redis.metadata[0].name}.${kubernetes_service.redis.metadata[0].namespace}.svc.cluster.local" +} + +output "port" { + value = 6379 +} + +output "connectionString" { + value = "redis://${kubernetes_service.redis.metadata[0].name}.${kubernetes_service.redis.metadata[0].namespace}.svc.cluster.local:6379" +} \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..776c015 --- /dev/null +++ b/variables.tf @@ -0,0 +1,9 @@ +variable "redis_cache_name" { + type = string +} + +variable "namespace" { + description = "Namespace for deploying the Redis container" + type = string + default = "default" +} \ No newline at end of file