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
- Set up a
google cloud cluster
with 3 nodes. - Set up a
Postgres
instance ofGoogle Cloud SQL
. Add a new usergothamuser
and password isgothampass
: Running on 5432 port - Set up an
I am service
account throughgothamcity-gke@miles-789.iam.gserviceaccount.com
and saved theconfig.json
file. - Link of
Docker file
[.1] - Build the container using
gcloud builds submit --tag gcr.io/miles-789/gothamcity:0.10 .
- Link of
Deployment.yaml
[.2] - Execute deployment file using
kubectl create -f deployment.yaml
- Link of
service.yaml
[3] - Execute service file using
kubectl create -f service.yaml
- Created cloud SQL instances using
kubectl create secret generic cloudsql-instance-credentials --from-file=cred.json=/Users/gotham/Downloads/cofig.json
- 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. - Added
cloudsql-db-credentials
andcloudsql-instance-credentials
inpsql.yaml
file - Link of
psql.yaml
file [4] - Execute this file using
kubectl create -f psql.yaml
- DB Configurations on
Go
project [5]
Screenshot of kubectl commands
1 kubectl get nodes
2 kubectl get pods
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.