Home > Enterprise >  Get redis host url from terraform helm_release resource
Get redis host url from terraform helm_release resource

Time:12-16

I'm using Terraform helm_release resource to install bitnami/redis instance in my K8s cluster.

The code looks like this:

resource "helm_release" "redis-chart" {
  name = "redis-${var.env}"

  repository = "https://charts.bitnami.com/bitnami"
  chart      = "redis"
  namespace  = "redis"
  create_namespace = true

  set {
    name  = "auth.enabled"
    value = "false"
  }

  set {
    name  = "master.containerPort"
    value = "6379"
  }

  set {
    name  = "replica.replicaCount"
    value = "2"
  }
}

It completes successfully and in a separate directory I have my app terraform configuration. In the app configuration, I want to get get the redis host from the above helm_release.

I'm doing it like this:

data "kubernetes_service" "redis-master" {
  metadata {
    name      = "redis-${var.env}-master"
    namespace = "redis"
  }
}

And then in the kubernetes_secret resource I'm passing the data to my app deployment:

resource "kubernetes_secret" "questo-server-secrets" {
  metadata {
    name      = "questo-server-secrets-${var.env}"
    namespace = kubernetes_namespace.app-namespace.metadata.0.name
  }

  data = {
    REDIS_HOST            = data.kubernetes_service.redis-master.metadata.0.name
    REDIS_PORT            = data.kubernetes_service.redis-master.spec.0.port.0.port
  }
}

But unfortunately, when I run the app deployment I'm getting the following logs:

[ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND redis-dev-master at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) [ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND

Which suggests that redis-dev-master is not a correct host for the redis instance.

How do I get the redis host from helm_release or any underlying services the release creates?

I've tried debugging and pinging a specific pod from my app deployment.

For reference, these are my redis resources:

NAME                       READY   STATUS    RESTARTS   AGE
pod/redis-dev-master-0     1/1     Running   0          34m
pod/redis-dev-replicas-0   1/1     Running   1          34m
pod/redis-dev-replicas-1   1/1     Running   0          32m

NAME                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/redis-dev-headless   ClusterIP   None             <none>        6379/TCP   34m
service/redis-dev-master     ClusterIP   172.20.215.60    <none>        6379/TCP   34m
service/redis-dev-replicas   ClusterIP   172.20.117.134   <none>        6379/TCP   34m

NAME                                  READY   AGE
statefulset.apps/redis-dev-master     1/1     34m
statefulset.apps/redis-dev-replicas   2/2     34m

CodePudding user response:

Maybe you were just using the wrong attribute to get that information. Checking the documentation at the Terraform Registry Website we can use the cluster_ip attribute as described at the spec documentation description. So you should end up with something like:

data.kubernetes_service.redis-master.spec.cluster_ip

And finally ending up with the following:

resource "kubernetes_secret" "questo-server-secrets" {
  metadata {
    name      = "questo-server-secrets-${var.env}"
    namespace = kubernetes_namespace.app-namespace.metadata.0.name
  }

  data = {
     REDIS_HOST            = data.kubernetes_service.redis-master.spec.cluster_ip
     REDIS_PORT            = data.kubernetes_service.redis-master.spec.port
  }
}
  • Related