kubernetes unable to connect postgres from nodejs

10/25/2019

I have nodejs application & postgres as database, both are deployed through kubernetes (with kubeadm & docker as container). i created clusterip service for postgres which i'm not able to access from nodejs.

I logged into nodejs pod and pinged clusterip of postgres, it is not connecting but i'm able to connect host ip (using node service) where postgres pod is deployed.

following is my postgres configuration

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: kubia
    tier: database
data:
  POSTGRES_DB: postgresdb
  POSTGRES_USER: postgresadmin
  POSTGRES_PASSWORD: admin123
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  labels:
    type: local
    app: kubia
    tier: database
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pv-claim
  labels:
    app: kubia
    tier: database
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kubia
      tier: database
  template:
    metadata:
      labels:
        app: kubia
        tier: database
    spec:
      containers:
        - name: postgres
          image: postgres:10.4
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      nodeSelector:
        nodetype: database
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim

---
apiVersion: v1
kind: Service
metadata:
  name: postgres-node
  labels:
    app: kubia
    tier: database
spec:
  type: NodePort
  ports:
   - port: 5432
     nodePort: 30010
  selector:
   app: kubia
   tier: database

---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: kubia
    tier: database
  namespace: default  
spec:
  type: ClusterIP
  ports:
   - port: 5432     
  selector:
   app: kubia
   tier: database

following are pod and services

NAME                                   READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE   READINESS GATES
pod/kubia-container-797dcf95c6-lgzkr   1/1     Running   0          3s      172.16.3.23   kube-worker-3   <none>           <none>
pod/postgres-75b85f8f5f-h9vrq          1/1     Running   0          2m31s   172.16.3.21   kube-worker-3   <none>           <none>

NAME                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE     SELECTOR
service/kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP          10d     <none>
service/kubia-svc       NodePort    10.98.129.62    <none>        8080:30008/TCP   3s      app=kubia,tier=ui
service/postgres        ClusterIP   10.111.153.26   <none>        5432/TCP         2m31s   app=kubia,tier=database
service/postgres-node   NodePort    10.107.59.255   <none>        5432:30010/TCP   2m31s   app=kubia,tier=database

I used POSTGRES_SERVICE_HOST & POSTGRES_SERVICE_PORT in nodejs application which contains cluster ip (10.111.153.26) and port (5432), with this application is not able to connect (postgres://postgresadmin:admin123@10.111.153.26:5432/postgresdb).

but application able to connect postgres with host machine ip (10.30.111.108) and node port like postgres://postgresadmin:admin123@10.30.111.108:30010/postgresdb

with reference to other post i even mentioned service name in connection like postgres://postgresadmin:admin123@postgres/postgresdb but it is also not working.

kubectl get ep

gives following output

NAME            ENDPOINTS          AGE
kubernetes      x.x.x.x:6443       11d
kubia-svc       172.16.3.25:8080   171m
postgres        172.16.3.24:5432   171m
postgres-node   172.16.3.24:5432   171m

I tried connecting postgres from postgres pod but it fails to connect

psql -h postgres -p 5432 -U postgresadmin -d postgresdb
psql: could not translate host name "postgres" to address: Temporary failure in name resolution

I could not hardcode postgres host as it could change at anytime, instead i need to connect through clusterip. What is missing in this?.

code is available here.

-- Prasad
kubernetes

1 Answer

10/25/2019

I just deployed postgres from above yaml and dont see any problem. i tried clusterIP as well as service name and can connect postgres using both. it works. see below

master $ kubectl get svc,po
NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP          111m
service/postgres        ClusterIP   10.97.192.16   <none>        5432/TCP         11m
service/postgres-node   NodePort    10.103.72.80   <none>        5432:30010/TCP   11m

NAME                            READY   STATUS    RESTARTS   AGE
pod/postgres-848d4d8db8-n764v   1/1     Running   0          10m
master $ kubectl exec -it postgres-848d4d8db8-n764v sh


using service name
----------------

# psql -h postgres -p 5432 -U postgresadmin -d postgresdb
Password for user postgresadmin:
psql (10.4 (Debian 10.4-2.pgdg90+1))
Type "help" for help.

postgresdb=# \list
                                 List of databases
    Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
------------+----------+----------+------------+------------+-----------------------
 postgres   | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 postgresdb | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
            |          |          |            |            | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
            |          |          |            |            | postgres=CTc/postgres
(4 rows)

postgresdb=#\q

using clusterIP
----------------
postgresdb=# \q
#
#
# psql -h 10.97.192.16 -p 5432 -U postgresadmin -d postgresdb
Password for user postgresadmin:
psql (10.4 (Debian 10.4-2.pgdg90+1))
Type "help" for help.

postgresdb=# \list
                                 List of databases
    Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
------------+----------+----------+------------+------------+-----------------------
 postgres   | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 postgresdb | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
            |          |          |            |            | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
            |          |          |            |            | postgres=CTc/postgres
(4 rows)

postgresdb=#

Suggest you to update the connection details in nodejs to use either service name or clusterIP. both nodejs and postgres are run in same namespace. it should work

-- P Ekambaram
Source: StackOverflow