Expose service (postgres) to outside of kubernetes cluster

Im trying to expose Postgres to outside world and for this reason I tried NodePort service.

Postgres DB service test-db:

kubectl -n test get svc test-db
NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)             AGE
test-db          ClusterIP   10.100.108.0   <none>        5432/TCP,9187/TCP   43h

NodePort service created for this case: kubectl apply -f postgresql-external.yaml

apiVersion: v1
kind: Service
metadata:
  name: postgresql-external
  namespace: test
  labels:
    version: 1.0.0
spec:
  selector:
   svc: test-db
  ports:
    - name: postgresql-external
      port: 5432
      targetPort: 5432
      nodePort: 30030
  type: NodePort

It seems that the NodePort service is created:

kubectl -n test get svc postgresql-external
NAME                  TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
postgresql-external   NodePort   10.99.173.146   <none>        5432:30030/TCP   17s

Now, as what I understand, I should use kubernetes NODE IP address (where postgres pod is deployed) to reach Postgres DB but it does NOT work:

psql -h 10.0.10.168 -p 30030 -d testdb -U postgres
psql: error: connection to server at "10.0.10.168", port 30030 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?

What am I doing wrong? Can anybody help please?

1 Like