Search code examples
kubernetesterraformkubernetes-ingress

Is there a way to inject loadBalancerIP to kubernetes ingress from a terraform created static IP resource (GCP)?


We use Terraform to create all of our infrastructure resources then we use Helm to deploy apps in our cluster.

We're looking for a way to streamline the creation of infra and apps, so currently this is what we do:

  • Terraform creates kubernetes cluster, VPC network etc and a couple of static public IP addresses
  • We have to wait for the dynamic creation of these static IPs by Terraform to complete
  • We find out what the public IP is that's been created, and manually add that to our loadBalancerIP: spec on our ingress controller helm chart

If at all possible, I'd like to store the generated public IP somewhere via terraform (config map would be nice), and then reference that in the ingress service loadBalancerIP: spec, so the end to end process is sorted.

I know configmaps are for pods and I don't think they can be used for kubernetes service objects - does anyone have any thoughts/ideas on how I could achieve this?


Solution

  • I suggest creating a static public IP in GCP using terraform by specifying the name you want like this:

    module "address" {
      source       = "terraform-google-modules/address/google"
      version      = "3.0.0"
      project_id   = "your-project-id"
      region       = "your-region"
      address_type = "EXTERNAL"
      names = [ "the-name-you-want" ]
      global = true
    }
    

    You can then refer to this static public IP name in the Kubernetes ingress resource by specifying the annotations kubernetes.io/ingress.global-static-ip-name: "the-name-you-want" like this:

    resource "kubernetes_ingress_v1" "example" {
      wait_for_load_balancer = true
      metadata {
        name = "example"
        namespace = "default"
        annotations = {
          "kubernetes.io/ingress.global-static-ip-name" = "the-name-you-want"
        }
      }
      spec {
    ....
    

    This will create ingress resource 'example' in GKE and attach static public IP named 'the-name-you-want' to it.