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

<p>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.</p>

<p>following is my postgres configuration</p>

<pre><code>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
</code></pre>

<p>following are pod and services </p>

<pre><code>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   &lt;none&gt;           &lt;none&gt;
pod/postgres-75b85f8f5f-h9vrq          1/1     Running   0          2m31s   172.16.3.21   kube-worker-3   &lt;none&gt;           &lt;none&gt;

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

<p>I used POSTGRES_SERVICE_HOST &amp; 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).</p>

<p>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</p>

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

<pre><code>kubectl get ep
</code></pre>

<p>gives following output</p>

<pre><code>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
</code></pre>

<p>I tried connecting postgres from postgres pod but it fails to connect  </p>

<pre class="lang-sql prettyprint-override"><code>psql -h postgres -p 5432 -U postgresadmin -d postgresdb
psql: could not translate host name "postgres" to address: Temporary failure in name resolution
</code></pre>

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

<p>code is available <a href="https://github.com/vmuthuprasad/privplat/tree/master/kubia" rel="nofollow noreferrer">here</a>.</p>

Prasad
<p>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</p>

<pre><code>master $ kubectl get svc,po
NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/kubernetes      ClusterIP   10.96.0.1      &lt;none&gt;        443/TCP          111m
service/postgres        ClusterIP   10.97.192.16   &lt;none&gt;        5432/TCP         11m
service/postgres-node   NodePort    10.103.72.80   &lt;none&gt;        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=#
</code></pre>

<p>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>
