How to make connection with Google Cloud SQL using Google Container Engine?

9/22/2017

i am using Node JS and deployed it using Kubernetes in Google Container Engine. but I cant make a connection to MySQL.

this is my node JS connection

var pool =  mysql.createPool({
 connectionLimit : 100, 
 user : process.env.DB_USER,
 password : process.env.DB_PASSWORD,
 database : process.env.DB_NAME,
 multipleStatements : true, 
 socketPath : '/cloudsql/' + process.env.INSTANCE_CONNECTION_NAME
})

I was using this for my Google App Engine and its work. now i need to move to GKE and it sent an error that said mySQL is not defined.

this is my app-frontend.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: app-frontend
labels:
 app: app
spec:
 replicas: 1
 template:
  metadata:
   labels:
    app: app
    tier: frontend
spec:
  containers:
  - name: app
    image: gcr.io/app-12345/app:1.0 
    env :
      - name : DB_HOST
        value : 127.0.0.1:3306
      - name : DB_USER 
        valueFrom:
            secretKeyRef:
              name: cloudsql-db-credentials
              key: username
      - name : DB_PASSWORD
        valueFrom:
            secretKeyRef:
              name: cloudsql-db-credentials
              key: password

    ports:
    - name: http-server
      containerPort: 8080
    imagePullPolicy: Always

  - image: gcr.io/cloudsql-docker/gce-proxy:1.09
    name: cloudsql-proxy
    imagePullPolicy: Always
    command: 
    - /cloud_sql_proxy 
    - --dir=/cloudsql 
    - --instances=mulung=tcp:3306
    - --credential_file=/secrets/cloudsql/credentials.json
    volumeMounts:
    - name: cloudsql-instance-credentials
      mountPath: /secrets/cloudsql
      readOnly: true
    - name: ssl-certs
      mountPath: /etc/ssl/certs
    - name: cloudsql
      mountPath: /cloudsql
      # [END proxy_container]
    ports:
    - name: portdb
      containerPort: 3306

    # [START volumes]
  volumes:
    - name: cloudsql-instance-credentials
      secret:
        secretName: cloudsql-instance-credentials
    - name: ssl-certs
      hostPath:
        path: /etc/ssl/certs
    - name: cloudsql
      emptyDir:

what should i do to fix it? thank you guys.

-- Ridhogillang
google-cloud-platform
google-kubernetes-engine
kubernetes
mysql
node.js

1 Answer

9/22/2017

Can you check the command part

    command: ["/cloud_sql_proxy", "--dir=/cloudsql",
        "-instances=CLOUD_SQL_INSTANCE_NAME",
        "-credential_file=/secrets/cloudsql/credentials.json"]

Here is the working deployment.yaml from one of my backend application

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: <appname>
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: <appname>
    spec:
      containers:
      - image: gcr.io/<some_name>/cloudsql-docker/gce-proxy:1.05
        name: cloudsql-proxy
        command: ["/cloud_sql_proxy", "--dir=/cloudsql",
            "-instances=CLOUD_SQL_INSTANCE_NAME",
            "-credential_file=/secrets/cloudsql/credentials.json"]
        volumeMounts:
          - name: cloudsql-oauth-credentials
            mountPath: /secrets/cloudsql
            readOnly: true
          - name: ssl-certs
            mountPath: /etc/ssl/certs
          - name: cloudsql
            mountPath: /cloudsql
      - name: <appname>
        image: IMAGE_NAME
        ports:
        - containerPort: 8888
        readinessProbe:
          httpGet:
            path: /<appname>/health
            port: 8888
          initialDelaySeconds: 30
          periodSeconds: 30
          timeoutSeconds: 30
          successThreshold: 1
          failureThreshold: 5
        env:
            - name: PROJECT_NAME
              value: <some_name>
            - name: PROJECT_ZONE
              value: <some_name>
            - name: INSTANCE_NAME
              value: <some_name>
            - name: INSTANCE_PORT
              value: <some_name>
            - name: CONTEXT_PATH
              value: <appname>
        volumeMounts:
          - name: application-config
            mountPath: /opt/config-mount
      volumes:
        - name: cloudsql-oauth-credentials
          secret:
            secretName: cloudsql-oauth-credentials
        - name: ssl-certs
          hostPath:
            path: /etc/ssl/certs
        - name: cloudsql
          emptyDir:
        - name: application-config
          secret:
            secretName: <appname>-ENV_NAME-config
-- Abhay
Source: StackOverflow