5

I am trying to set up a connection between Google Cloud Postgres and Google Kubernetes Engine using a CloudSQL proxy. If I check out the logs of both workloads:

I am getting:

2021-02-02T11:42:09.748706102Z2021/02/02 11:42:09 dial tcp 127.0.0.1:5432: connect: connection refused

I am writing down all the steps that I performed with all the real file keywords or file names.

Steps

  1. Set up a google cloud cluster with 3 nodes.
  2. Set up a Postgres instance of Google Cloud SQL. Add a new user gothamuser and password is gothampass: Running on 5432 port
  3. Set up an I am service account through gothamcity-gke@miles-789.iam.gserviceaccount.com and saved the config.json file.
  4. Link of Docker file [.1]
  5. Build the container using gcloud builds submit --tag gcr.io/miles-789/gothamcity:0.10 .
  6. Link of Deployment.yaml [.2]
  7. Execute deployment file using kubectl create -f deployment.yaml
  8. Link of service.yaml[3]
  9. Execute service file using kubectl create -f service.yaml
  10. Created cloud SQL instances using kubectl create secret generic cloudsql-instance-credentials --from-file=cred.json=/Users/gotham/Downloads/cofig.json
  11. Created cloudsql-db credentials using kubectl create secret generic cloudsql-db-credentials --from-literal=username=gothamuser --from-literal=password=gothampass. Didn't mention the database name because I have to connect multiple databases.
  12. Added cloudsql-db-credentials and cloudsql-instance-credentials in psql.yaml file
  13. Link of psql.yaml file [4]
  14. Execute this file using kubectl create -f psql.yaml
  15. DB Configurations on Go project [5]

Screenshot of kubectl commands

1 kubectl get nodes

enter image description here

2 kubectl get pods

enter image description here

Mentioned Files

[1.] Docker

FROM golang:alpine AS build-env
ENV GOPATH /go
WORKDIR /go/src
COPY . /go/src/gothamcity

RUN cd /go/src/gothamcity && go build .

FROM alpine
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk*

WORKDIR /app
COPY --from=build-env /go/src/gothamcity/gothamcity /app
COPY .env /app

EXPOSE 5432
EXPOSE 8080

ENTRYPOINT [ "./gothamcity" ]

[2.] Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gothamcity-backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gothamcity
  template:
    metadata:
      labels:
        app: gothamcity
    spec:
      containers:
      - name: gothamcity-app
        image: gcr.io/miles-789/gothamcity:0.10
        ports:
        - containerPort: 8080
        env:
          - name: PORT
            value: "8080"

[3] Service.yaml

apiVersion: v1
kind: Service
metadata:
  name: gothamcity
spec:
  type: LoadBalancer
  selector:
    app: gothamcity
  ports:
  - port: 80
    targetPort: 8080

[4] psql.yaml

 apiVersion: apps/v1
kind: Deployment
metadata:
  name: gothamcity-psql-proxy
spec:
  selector:
    matchLabels:
      app: gothamcity
  template:
    metadata:
      labels:
        app: gothamcity
    spec:
      containers:
      - image: gcr.io/miles-789/gothamcity:0.10
        name: gothamcity-app
        env:
        - namee: DB_HOST
          value: 127.0.0.1
        - name: DB_USER
          valueFrom:
            secretKeyRef:
              name: cloudsql-db-credentials
              key: username
        - name: DB_PASS
          valueFrom:
            secretKeyRef:
              name: cloudsql-db-credentials
              key: password
        - name: DB_NAME
          valueFrom:
            secretKeyRef:
              name: cloudsql-db-credentials
              key: database
      - name: cloud-sql-proxy
        image: gcr.io/cloudsql-docker/gce-proxy:1.17
        command:
          - "/cloud_sql_proxy"
          - "-instances=miles-789:europe-east1:gotham-miles-v1=tcp:5432"
          - "-credential_file=/secrets/config.json"
        securityContext:
          runAsNonRoot: true
        volumeMounts:
        - name: cloudsql-instance-credentials
          mountPath: /secrets/
          readOnly: true
      volumes:
      - name: cloudsql-instance-credentials
        secret:
          secretName: cloudsql-instance-credentials

[5] DB Configurations

package dataservices

import "os"

const (
    dbhost = "DBHOST"
    dbport = "DBPORT"
    dbuser = "DBUSER"
    dbpass = "DBPASS"
    dbname = "DBNAME"
    env    = "ENV"
)

func dbConfig(database string) map[string]string {
    var host = os.Getenv("DB_HOST")
    var user = os.Getenv("DB_USER")
    var password = os.Getenv("DB_PASS")
    var name = database
    var port = "5432"
    conf := make(map[string]string)
    conf[dbhost] = host
    conf[dbport] = port
    conf[dbuser] = user
    conf[dbpass] = password
    conf[dbname] = name
    return conf
}

I can't understand What am i doing wrong here. Is it a typo or I am missing some kind of configuration here.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Amit Pal
  • 10,604
  • 26
  • 80
  • 160
  • According to [this document](https://cloud.google.com/sql/docs/postgres/connect-kubernetes-engine#proxy-with-service-account-key), If connecting using private IP, you must use a VPC-native GKE cluster, in the same VPC as your Cloud SQL instance. Do you verify if the port 5432 open in gothamcity-psql-proxy? If you have an outbound firewall policy, make sure it allows connections to [port 3307](https://cloud.google.com/sql/docs/postgres/sql-proxy#troubleshooting) on the target machine. – William Feb 02 '21 at 21:07
  • Did you ever figure out what was wrong? I have a very similar deployment and suspect it is because the DBHost is 127.0.0.1 but we are deploying multiple pods. – War Gravy May 16 '22 at 18:46

1 Answers1

0

Is this a private cluster? If so you need to have SQL DB on same VPC (or have VPC peering) for your app to reach SQL DB

Or you need to setup Cloud NAT on your VPC for cluster to SQL DB to talk back

this can be useful

https://groups.google.com/g/google-cloud-sql-discuss/c/6lA0r6zRfeI/m/hr8mRt9AGgAJ

RandomQuests
  • 635
  • 4
  • 16
  • Thanks for your answer: I did check the cluster-info and found out: Networking Private cluster is Disabled VPC-native traffic routing is Enabled is that correct? – Amit Pal Feb 02 '21 at 18:07