简体   繁体   中英

How to connect postgresql from app using helm and kubernetes?

I am really struggling regarding how my application which is deployed in --dev namespace can connect to postgreSQL database which I deployed independently using helm with --database namespace . What I did so far is as below.

Database and myapp deployed different namespace. I just copy the name PGHOST,PGPASSWORD from some examples but I am not sure where should I use this name and is that has to be same somewhere in postgreSQL?

Should I take care anything else to connect database or is there anything that is not best practice? Should I add a namespace to jdbc url?

Locally we connect to database using below parameters but what should be the way after we deploy our application via helm? We are using sequelize as a client library

const connectionString = postgres://${global.config.database_username}:${global.config.database_password}@${global.config.database_host}:${global.config.database_port}/${global.config.database_name};

postgres values

## Specify PGDATABASE
##
DBName: db

After I deployed postgres;

  # of replicas: 3
  service name:  my-postgres-postgresql-helm
  service port:  64000
  database name: db
  database user: admin
  jdbc url:      jdbc:postgresql://my-postgres-postgresql-helm:port

deployment.yaml

        - name: PGHOST
          valueFrom:
            configMapKeyRef:
              name: {{ .Release.Name }}-configmap
              key: jdbc-url
        - name: PGDATABASE
          value: {{ .Values.postgres.database name | quote }}
        - name: PGPASSWORD
          value: "64000"
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "my-mp.name" . }}
              key: POSTGRES_PASSWORD

configmaps.yaml

apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
    labels:
        app.kubernetes.io/name: {{ include "my-mp.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        helm.sh/chart: {{ include "my-mp.chart" . }}
data:
  jdbc-url: jdbc:postgresql://my-postgres-postgresql-helm..

values.yaml

postgres:
  service name:  my-postgres-postgresql-helm
  service port:  64000
  database name: db
  database user: admin

Is this a typo in your question about the jdbc url jdbc url: jdbc:postgresql://my-postgre ? You have mentioned that the service name is my-postgres-postgresql-helm and hence the jdbc url should be something like: jdbc:postgresql://my-postgres-postgresql-helm.database . Note the .database appended to the service name! Since your application pod is running in a different namespace, you should append the namespace name at the end of the service name. Had they been in the same namespace, you wouldn't need it.

Now, if that doesn't fix it, to debug the issues, this is what I would do if I were you:

  1. Check if there any NetworkPolicies which add restrictions on the namespace level; that is allowing traffic only between specific namespaces or even pods, which may prevent the traffic from your application pod reaching your postgres pod.
  2. Make sure your Service for postgres pod is proper. That is, describing the service should list the Pod's IP as Endpoints. If not check the Service's label selector and make sure it uses the same labels as the postgres pod.
  3. Exec into your pod and check if your application pod is able to reach the service through nslookup using the service name, that is my-postgres-postgresql-helm.database .

If all these tests are positive and working, then most probably it is some other configuration issue. Let me know if this fixes your issue and GL.

If I understand correctly, you have the database and the app in different namespaces and the point of namespaces is to isolate. If you really need to access it, you can use the DNS autogenerated entry servicename.namespace.svc.cluster.local

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM