Multiple K8S containers connecting to Google Cloud SQL through a proxy server

I would like to link my Kubernetes cluster with Google Cloud SQL.

I have at least 10 different deployed modules that are currently connecting to MySQL [docker image deployed prior to k8s] using the JDBC URL + username / password.

Can I use one instance of Google Cloud SQL Proxy and connect all modules through this proxy to the Cloud SQL database? Ideally, I would like to replace mysql running in a proxy container.

I would prefer not to run proxies inside each deployment. The only samples I found seem to indicate that a proxy should be advertised in every deployment.

+4
source share
1 answer

I have found a solution.

Deploy the proxy using yml below and set the deployment as a service. Most importantly, the proxy should listen on 0.0.0.0 instead of the default 127.0.0.1. All secrets as per Google Cloud sql documentation

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  template:
    metadata:
      name: mysql
      labels:
        name: mysql
    spec:
      containers:
         - image: b.gcr.io/cloudsql-docker/gce-proxy:1.05
           name: cloudsql-proxy
           command: ["/cloud_sql_proxy", "--dir=/cloudsql",
                     "-instances=MYSQL:ZONE:DATABASE_INSTANCE=tcp:0.0.0.0:3306",
                     "-credential_file=/secrets/cloudsql/credentials.json"]
           volumeMounts:
             - name: cloudsql-oauth-credentials
               mountPath: /secrets/cloudsql
               readOnly: true
             - name: ssl-certs
               mountPath: /etc/ssl/certs
           ports:
             - containerPort: 3306
               name: mysql
      volumes:
        - name: cloudsql-oauth-credentials
          secret:
            secretName: cloudsql-oauth-credentials
        - name: ssl-certs
          hostPath:
            path: /etc/ssl/certs

The solution is a bit more expensive than having a proxy server in the same deployment as the client software, since there is an additional TCP connection.

However, there are many advantages:

  • Much simpler and does not require modifying existing K8S deployment files.
  • Allows you to switch the implementation to the MySQL Docker container or using the Google Cloud SQL proxy server without any changes to the client configuration.
+9
source

Source: https://habr.com/ru/post/1663886/


All Articles