Search code examples
kubernetesterraformkubernetes-ingressterraform-provider-gcp

How to assign a static ip address to an Kubernetes ingress using Terraform?


I've been using a kubernetes ingress config file to assign a static external ip address created by GCP. The ingress and the deployment are managed by GKE.

ingress.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.global-static-ip-name: test-address
spec:
  backend:
    serviceName: test-service
    servicePort: 80

With this yaml file, the static ip address created already is successfully attached to the ingress.

On External IP Address on VPC Network menu, the ip is in use by forwarding rule.

Name External Address Region Type Version In use by

test-address 12.34.56.78 asia-northeast2 Static IPv4 Forwarding rule k8s2-ab-blablablabla

However, Recently I tried to test Terraform to deploy the infrastructure to GCP and I made a Terraform config file exactly the same with above ingress.yaml.

ingress.tf

resource "kubernetes_ingress" "test_ingress" {
  metadata {
    name = "test-ingress"
    annotations = {
      "kubernetes.io/ingress.global-static-ip-name" = "test-address"
    }
  }
  spec {
    backend {
      service_name = test-service
      service_port = "80"
    }
  }
}

After I apply this config to GCP, the ingress was created successfully but the ip address does not attach to the ingress.

In Ingress detail in GCP, an error occurred with the message

Error syncing to GCP: error running load balancer syncing routine: loadbalancer blablablablabla does not exist: the given static IP name test-address doesn't translate to an existing static IP.

And on External IP Address on VPC Network menu, the IP address row at In use by shows None.

What is the problem here? Did I miss something with Terraform?


Solution

  • As @MattBrowne said in the comments, needs to be global IP and not regional. This also fixed for me.