Trouble connecting to postgres from outside Kubernetes cluster Trouble connecting to postgres from outside Kubernetes cluster kubernetes kubernetes

Trouble connecting to postgres from outside Kubernetes cluster


I just deployed postgres and exposed its service through NodePort and following is my pod and service.

[root@master postgres]# kubectl get podsNAME                        READY     STATUS    RESTARTS   AGEpostgres-7ff9df5765-2mpsl   1/1       Running   0          1m[root@master postgres]# kubectl get svc postgresNAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGEpostgres     NodePort    10.100.199.212   <none>        5432:31768/TCP   20s

And this is how connected to postgres though the nodeport:

[root@master postgres]# kubectl exec -it postgres-7ff9df5765-2mpsl --  psql -h 10.6.35.83 -U postgresadmin --password -p 31768 postgresdbPassword for user postgresadmin: psql (10.4 (Debian 10.4-2.pgdg90+1))Type "help" for help.postgresdb=# 

In above, 10.6.35.83 is my node/host IP (not pod IP or clusterIP) and port is the NodePort defined in service. The issue is you're not using the right IP to connect to the postgresql.