Google kubernetes engine 从私有GKE集群连接到公共云SQL
我正在尝试使用云SQL代理从GKE集群连接到一个具有公共IP的云SQL实例。我使用以下命令创建了集群:Google kubernetes engine 从私有GKE集群连接到公共云SQL,google-kubernetes-engine,google-cloud-sql,cloud-sql-proxy,gke-networking,Google Kubernetes Engine,Google Cloud Sql,Cloud Sql Proxy,Gke Networking,我正在尝试使用云SQL代理从GKE集群连接到一个具有公共IP的云SQL实例。我使用以下命令创建了集群: gcloud services enable compute.googleapis.com gcloud services enable container.googleapis.com gcloud container clusters create my-cluster \ --disk-size=10GB \ --machine-type=e2-small \ --node-
gcloud services enable compute.googleapis.com
gcloud services enable container.googleapis.com
gcloud container clusters create my-cluster \
--disk-size=10GB \
--machine-type=e2-small \
--node-locations=us-central1-b,us-central1-c,us-central1-f \
--num-nodes=1 \
--preemptible \
--release-channel=regular \
--workload-pool=my-production.svc.id.goog \
--zone=us-central1-f \
--no-enable-master-authorized-networks \
--enable-ip-alias \
--enable-private-nodes \
--master-ipv4-cidr 172.16.0.32/28
gcloud sql instances create my-db \
--database-version=POSTGRES_12 \
--region=us-central1 \
--storage-auto-increase \
--storage-size=10 \
--storage-type=SSD \
--tier=db-f1-micro
gcloud iam service-accounts create my-service-account
gcloud iam service-accounts add-iam-policy-binding \
--role=roles/iam.workloadIdentityUser \
--member="serviceAccount:my-production.svc.id.goog[default/my-service-account]" \
my-service-account@my-production.iam.gserviceaccount.com
gcloud projects add-iam-policy-binding my-production \
--member serviceAccount:"my-service-account@my-production.iam.gserviceaccount.com" \
--role "roles/cloudsql.client"
我使用以下命令创建了我的云SQL:
gcloud services enable compute.googleapis.com
gcloud services enable container.googleapis.com
gcloud container clusters create my-cluster \
--disk-size=10GB \
--machine-type=e2-small \
--node-locations=us-central1-b,us-central1-c,us-central1-f \
--num-nodes=1 \
--preemptible \
--release-channel=regular \
--workload-pool=my-production.svc.id.goog \
--zone=us-central1-f \
--no-enable-master-authorized-networks \
--enable-ip-alias \
--enable-private-nodes \
--master-ipv4-cidr 172.16.0.32/28
gcloud sql instances create my-db \
--database-version=POSTGRES_12 \
--region=us-central1 \
--storage-auto-increase \
--storage-size=10 \
--storage-type=SSD \
--tier=db-f1-micro
gcloud iam service-accounts create my-service-account
gcloud iam service-accounts add-iam-policy-binding \
--role=roles/iam.workloadIdentityUser \
--member="serviceAccount:my-production.svc.id.goog[default/my-service-account]" \
my-service-account@my-production.iam.gserviceaccount.com
gcloud projects add-iam-policy-binding my-production \
--member serviceAccount:"my-service-account@my-production.iam.gserviceaccount.com" \
--role "roles/cloudsql.client"
我还使用以下命令设置了一个服务帐户:
gcloud services enable compute.googleapis.com
gcloud services enable container.googleapis.com
gcloud container clusters create my-cluster \
--disk-size=10GB \
--machine-type=e2-small \
--node-locations=us-central1-b,us-central1-c,us-central1-f \
--num-nodes=1 \
--preemptible \
--release-channel=regular \
--workload-pool=my-production.svc.id.goog \
--zone=us-central1-f \
--no-enable-master-authorized-networks \
--enable-ip-alias \
--enable-private-nodes \
--master-ipv4-cidr 172.16.0.32/28
gcloud sql instances create my-db \
--database-version=POSTGRES_12 \
--region=us-central1 \
--storage-auto-increase \
--storage-size=10 \
--storage-type=SSD \
--tier=db-f1-micro
gcloud iam service-accounts create my-service-account
gcloud iam service-accounts add-iam-policy-binding \
--role=roles/iam.workloadIdentityUser \
--member="serviceAccount:my-production.svc.id.goog[default/my-service-account]" \
my-service-account@my-production.iam.gserviceaccount.com
gcloud projects add-iam-policy-binding my-production \
--member serviceAccount:"my-service-account@my-production.iam.gserviceaccount.com" \
--role "roles/cloudsql.client"
pod中云sql代理的sidecar容器设置如下:
- name: cloud-sql-proxy
image: gcr.io/cloudsql-docker/gce-proxy:1.20.2
command:
- "/cloud_sql_proxy"
- "-instances=my-production:us-central1:my-db=tcp:5432"
- "-term_timeout=20s"
尽管如此,当我的应用程序尝试连接到云SQL实例时,我可以在云SQL代理日志中看到以下错误:
2021/03/19 21:30:02 couldn't connect to "my-production:us-central1:my-db": dial tcp MY_DB_PUBLIC_IP:3307: connect: connection timed out
我检查过了,pod可以访问互联网(我可以访问),所以它应该能够连接到云SQL的公共IP。我可以在笔记本电脑上毫无问题地使用云sql代理,并连接到那里的实例。我错过了什么?我还能查什么
我找到了,但我启用了SQL管理API。只讨论没有互联网接入的GKE集群。看起来你做的每件事都是正确的。您是否有防火墙可能阻止3307上的云sql实例的出站流量?结论是:
- 不要使用检查节点是否可以访问GCP基础设施上的Internet。这似乎被视为GCP上的内部流量。在专用GKE群集上尝试获取失败
- 要从私有GKE集群连接到云SQL,请为云SQL使用私有IP(请记住将
添加到云SQL代理)-IP\u address\u types=private
gcloud compute firewall rules list
向我展示的内容:我尝试创建一个没有--启用私有节点(和--master-ipv4-cidr
)的集群,然后一切正常(连接到同一个云SQL实例)。但是,我希望使用--启用私有节点
,这样我就不用为节点的外部IP付费。我不需要它们,因为我的POD是web服务器,我使用Google Cloud Load Balancer将流量路由到它们。很高兴你能找到答案!看起来你实际上没有外部访问权限,但可能仍然无法访问o解析google.com的主机?看看,它说“如果你想为某些私有节点提供出站互联网访问,你可以使用云NAT或管理你自己的NAT网关。”否则,我建议您只使用私有IP和云SQL,因为您已经在使用私有集群。您是否能够获取非google.com
资源?从我上面链接的页面上看,如果您使用--启用私有节点
,您的节点将不会有外部访问。这是可能的google。com
域可能仍然可以通过专有网络访问。