<p>I have this terraform config for creating a single mongodb replica and a service but I can't connect to mongo using the cli and the cluster domain name.</p>
<pre><code>locals {
  labels = {
    &quot;app&quot; = &quot;mongo&quot;
  }
  volume_config_name = &quot;mongo-config&quot;
}

module &quot;mongo&quot; {
  source  = &quot;terraform-iaac/stateful-set/kubernetes&quot;
  version = &quot;1.4.2&quot;
  # insert the 3 required variables here
  image     = &quot;mongo:4.4&quot;
  name      = &quot;mongodb&quot;
  namespace = kubernetes_namespace.cmprimg.metadata[0].name

  custom_labels = local.labels

  volume_host_path = [
    {
      volume_name  = &quot;data&quot;
      path_on_node = &quot;/data/db&quot;
    },
  ]
  volume_mount = [
    {
      mount_path  = &quot;/data/db&quot;
      volume_name = &quot;data&quot;
    },
    {
      mount_path  = &quot;/etc/mongod.conf.orig&quot;
      volume_name = &quot;mongodb-conf&quot;
      sub_path    = &quot;configfile&quot; // Key from configmap
    }
  ]

  volume_config_map = [{
    mode        = &quot;0777&quot;
    volume_name = &quot;mongodb-conf&quot;
    name        = &quot;mongodb-confmap&quot;
  }]

  # volume_claim = [
  #   {
  #     name                   = &quot;mongo&quot;
  #     namespace              = kubernetes_namespace.cmprimg.metadata[0].name
  #     access_modes           = [&quot;ReadWriteOnce&quot;]
  #     requests_storage       = &quot;4Gi&quot;
  #     persistent_volume_name = &quot;mongo&quot;
  #     storage_class_name     = &quot;linode-block-storage-retain&quot;
  #   }
  # ]
  env = {
    &quot;MONGO_INITDB_ROOT_USERNAME&quot; = var.username,
    &quot;MONGO_INITDB_ROOT_PASSWORD&quot; = var.password,
  }
  command = [
    &quot;mongod&quot;,
    &quot;--bind_ip&quot;,
    &quot;0.0.0.0&quot;,
  ]

  internal_port = [
    {
      name          = &quot;mongo&quot;
      internal_port = 27017
    }
  ]

  resources = {
    request_cpu    = &quot;100m&quot;
    request_memory = &quot;800Mi&quot;
    limit_cpu      = &quot;120m&quot;
    limit_memory   = &quot;900Mi&quot;
  }

  replicas = 1
}

module &quot;mongo_service&quot; {
  source  = &quot;terraform-iaac/service/kubernetes&quot;
  version = &quot;1.0.4&quot;
  # insert the 3 required variables here

  app_name      = module.mongo.name
  app_namespace = kubernetes_namespace.cmprimg.metadata[0].name
  port_mapping = [
    {
      name          = &quot;mongo&quot;
      internal_port = 27107
      external_port = 27017
    }
  ]

  custom_labels = local.labels
}

resource &quot;kubernetes_persistent_volume_claim&quot; &quot;example&quot; {
  metadata {
    name      = &quot;mongo&quot;
    namespace = kubernetes_namespace.cmprimg.metadata[0].name
    labels    = local.labels
  }

  spec {
    access_modes = [&quot;ReadWriteOnce&quot;]
    resources {
      requests = {
        storage = &quot;20Gi&quot;
      }
    }
    storage_class_name = &quot;linode-block-storage-retain&quot;
  }
}

resource &quot;kubernetes_config_map&quot; &quot;mongodb_conf&quot; {
  metadata {
    name      = &quot;mongodb-confmap&quot;
    namespace = kubernetes_namespace.cmprimg.metadata[0].name
    labels    = local.labels
  }

  data = {
    &quot;configfile&quot; = yamlencode({
      storage : {
        dbPath : &quot;/data/db&quot;,
      },
      net : {
        port : 27017,
        bindIp : &quot;0.0.0.0&quot;,
      }
    })
  }
}

</code></pre>
<p>I can exec into the mongodb pod and use mongo cli to connect using localhost, but when I'm in the same pod and use mongocli to connect using the domain name <code>mongodb.default.svc.cluster.local:27017</code> I get connection refused. I can see in the logs that mongodb binds to <code>0.0.0.0</code> but can't connect through external ports. Did I misconfigure the service or do something else wrong?</p>

Dan
<p>Take a closer look at this section:</p>
<pre><code>port_mapping = [
  {
    name          = &quot;mongo&quot;
    internal_port = 27107
    external_port = 27017
  }
]
</code></pre>
<ol>
<li>You use „internal” and „external” port numbers inconsistently across file (the internal here is 27<strong>10</strong>7)</li>
<li>Are you sure the syntax of this block is correct? In the reaserch I made through the Internet the portMappings section has usually different syntax (e.g. <a href="https://github.com/hashicorp/terraform-provider-aws/issues/21861" rel="nofollow noreferrer">https://github.com/hashicorp/terraform-provider-aws/issues/21861</a>)</li>
</ol>
<p>.</p>
<pre><code>portMappings = [
  {
      containerPort = var.container_port
      hostPort = var.container_port
      protocol = &quot;tcp&quot;
    }
  ]
</code></pre>
