Unable to connect NodeJs application with Postgres running in Minikube

1/2/2020

I am getting started with helm to carry out deployments to Kubernetes and i am stuck while connecting Nodejs application with postgres DB. I am using helm to carry out the deployment to K8.

Below is my YAML file for application

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "service-chart.fullname" . }}-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: {{ template "service-chart.fullname" . }}-converstionrate
  template:
    metadata:
      labels:
        app: {{ template "service-chart.fullname" . }}-converstionrate
    spec:
      containers:
        - name: {{ template "service-chart.fullname" . }}-converstionrate
          image: <application_image>
          env:
            - name: DB_URL
              value: postgres://{{ template "postgres.fullname" . }}.default.svc.cluster.local:5432/{{ .Values.DbName }}

---

kind: Service
apiVersion: v1
metadata:
  name: {{ template "service-chart.fullname" . }}-service
spec:
  type: NodePort
  selector:
    app: {{ template "service-chart.fullname" . }}-converstionrate
  ports:
    - port: 8080
      targetPort: 3000

Below is my requirement file where i am using the postgres dependency

dependencies:
  - name: postgresql
    version: "8.1.2"
    repository: "https://kubernetes-charts.storage.googleapis.com/"

Below is application code where i try to connect to DB:-

if (config.use_env_variable) {
  // sequelize = new Sequelize(process.env[config.use_env_variable], config);
  sequelize = new Sequelize(
    process.env.POSTGRES_HOST,
    process.env.POSTGRES_USER,
    process.env.POSTGRES_PASSWORD,
    pocess.env.POSTGRES_DIALECT
  );
} else {
  sequelize = new Sequelize(
    config.database,
    config.username,
    config.password,
    config
  );
}

What i am not able to understand is how to connect to the DB as with the above i am not able to.? Can anyone please help me out here.?

I am newbie with helm hence not able to figure it out. I have looked into lot of blogs but some how it is not clear on how it needs to be done. As the DB is running in one POD and Node app in another so how do i wire it up together.? How to set the env variables of DB in yaml to be consumed.?

FYI --- I am using minikube to deploy as of now.

The application code is available:- at https://github.com/Vishesh30/Node-express-Postgress-helm

Thanks, Vishesh.

-- Vishesh
kubernetes
kubernetes-helm
postgresql

1 Answer

1/3/2020

As a mentioned by @David Maze, you need to fix the variable name from DB_URL to POSTGRES_HOST, but there are some other things I could see.

I've tried to reproduce you scenario and the following works for me:

You need to fix the service dns from you YAML file:

postgres://{{ template "postgres.fullname" . }}.default.svc.cluster.local:5432/{{ .Values.DbName }}

to

postgres://{{ template "postgresql.fullname" . }}-postgresql.default.svc.cluster.local:5432/{{ .Values.DbName }}

After that you need to pass to your application the host, database username and password of the database, you can do it overriding the postgresql default variables (because it's a subchart from you aplication), as describe in Helm documentaion and inject to you container using environment variables.

There's a lot of variables, see here.

Add this values to you values.yaml in order to override postgresql defaults:

postgresql:
  postgresqlUsername: dbuser 
  postgresqlPassword: secret # just for example

You can create a secret file to store the password:

service-chart/templates/secrets.yaml

apiVersion: v1
kind: Secret
metadata:
  name: dbconnection
type: Opaque
stringData:
  POSTGRES_PASSWORD: {{ .Values.postgresql.postgresqlPassword }

If you don't want keep the password in file, maybe you can try some automation process to >rewrite you file before the deployment. You can read more about secrets here.

Now in you deployment.yaml add the variables, the POSTGRES_USERNAME will be get from values.yaml file, and the value to POSTGRES_PASSWORD:

          env:
            - name: POSTGRES_HOST 
              value: postgres://{{ template "postgresql.fullname" . }}-postgresql.default.svc.cluster.local:5432/{{ .Values.DbName }}
            - name: POSTGRES_USERNAME
              value: {{ .Values.postgresql.postgresqlUsername }}
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: dbconnection
                  key: POSTGRES_PASSWORD

After the deployment you can check the container's environment variables.

I really hope it helps you!

-- KoopaKiller
Source: StackOverflow