简体   繁体   中英

How to generate DNS for GCP cloud sql private ip

I've been trying to set up a Kubernetes cluster with Google Cloud SQL with a private IP connection with Terraform scripts in Google cloud. These terraform scripts work perfectly fine. Then I rollup Kubernetes configuration with deployments, services, and pods that use this database private IP. The problem that Terraform scripts creates Database with a random private IP each time when it got created. So it is very inconvenient to set up the IP in Kubernetes configuration manually. Is there any way to generate private DNS for this private IP and use it in the Kubernetes configuration?

I tried to create a private DNS zone and assign a record pointing to Cloud SQL private IP. But it doesn't work for some reason.

This worked for me in Terraform. Just replace internal.example.org with whatever domain you want to use. Also make sure you have permissions to admin Cloud DNS.

resource "google_dns_managed_zone" "private_zone" {
  name        = "private-zone"
  dns_name    = "internal.*example*.org."
  description = "Internal DNS zone"

  visibility = "private"

  private_visibility_config {
    networks {
      network_url = module.vpc.network_self_link
    }
  }
}

resource "google_dns_record_set" "db_dns" {
  depends_on = [google_dns_managed_zone.private_zone]
  managed_zone = google_dns_managed_zone.private_zone.name
  name = "db.${google_dns_managed_zone.private_zone.dns_name}"
  rrdatas = [google_sql_database_instance.instance.private_ip_address]
  ttl = 300
  type = "A"
}

Is there any proper way to do it in GCP.

Updated solution that works for me

DNS zone and record set were created by a command below

gcloud beta dns --project=project-name managed-zones create private-db-zone --description= --dns-name=db.test.com. --visibility=private --networks project-network-dev

gcloud dns --project=project-name record-sets transaction start --zone=private-db-zone

gcloud dns --project=project-name record-sets transaction add 10.116.0.3 --name=private.db.test.com. --ttl=300 --type=A --zone=private-db-zone

gcloud dns --project=project-name record-sets transaction execute --zone=private-db-zone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM