使用cloudsqlproxy从GKE集群连接到Google Cloud MySQL实例

时间:2018-11-25 00:46:00

标签: google-cloud-platform google-cloud-sql google-kubernetes-engine cloud-sql-proxy

我在GCP中有两个项目。 project1project2

我在project1中设置了mysql实例。

我还在cloudsqlproxy mypod mypod cloudsqlproxy`的GKE群集中设置了project2. I want to access mysql instance from(pod)和through

我有以下cloudmysqlproxy的代码

apiVersion: v1
kind: Service
metadata:
  name: cloudsqlproxy-service-mainnet
  namespace: dev
spec:
  ports:
  - port: 3306
    targetPort: port-mainnet
  selector:
    app: cloudsqlproxy

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloudsqlproxy
  namespace: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cloudsqlproxy
  template:
    metadata:
      labels:
        app: cloudsqlproxy
    spec:
      containers:
       # Make sure to specify image tag in production
       # Check out the newest version in release page
       # https://github.com/GoogleCloudPlatform/cloudsql-proxy/releases
      - name: cloudsqlproxy
        image: b.gcr.io/cloudsql-docker/gce-proxy:latest
       # 'Always' if imageTag is 'latest', else set to 'IfNotPresent'
        imagePullPolicy: Always
        name: cloudsqlproxy
        command:
        - /cloud_sql_proxy
        - -dir=/cloudsql
        - -instances=project1:asia-east1:development=tcp:0.0.0.0:3306
        - -credential_file=/credentials/credentials.json
        ports:
        - name: port-mainnet
          containerPort: 3306
        volumeMounts:
        - mountPath: /cloudsql
          name: cloudsql
        - mountPath: /credentials
          name: cloud-sql-client-account-token
      volumes:
      - name: cloudsql
        emptyDir:
      - name: cloud-sql-client-account-token
        secret:
          secretName: cloud-sql-client-account-token

我以以下方式设置了cloud-sql-client-account-token

kubectl create secret cloud-sql-client-account-token --from-file=credentials.json=$HOME/credentials.json

我从credentials.json中的服务帐户下载project1文件的位置。

当我尝试从我的pod访问mysql实例时,出现以下错误:

mysql --host=cloudsqlproxy-service-mainnet.dev.svc.cluster.local --port=3306
ERROR 1045 (28000): Access denied for user 'root'@'cloudsqlproxy~35.187.201.86' (using password: NO)

还有cloud-proxy日志,我得到以下信息:

2018/11/25 00:35:31 Instance project1:asia-east1:development closed connection

是否需要在与pod相同的项目(project2)中启动mysql实例?我想念什么?

编辑

我可以通过这样设置来访问本地计算机上的代理

/cloud_sql_proxy -instances=project1:asia-east1:development=tcp:3306

,然后使用mysql客户端连接到代理。

0 个答案:

没有答案