<p>I'm currently setting up a postgres instance on my Kubernetes cluster hosted on OVH public cloud. The problem is that I can't access it. I know that I have to make <code>psql -h host -U user --password -p 30904 db</code> to connect, but the problem is that i don't know what to put instead of <strong>host</strong>. Localhost ? The ip of the master node ? Another ip ?</p>
<p>Thank you for your time.</p>
<p>postgres-deploy.yaml :</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  type:
    NodePort
  ports:
  - port: 5432
  selector:
    app: postgres
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - image: postgres:13.1
        name: postgres
        env:
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: ident
                  key: POSTGRES_DB
          
              name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: ident
                  key: POSTGRES_USER

              name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ident
                  key: POSTGRES_PASSWORD
        ports:
        - containerPort: 5432
          name: postgres
        volumeMounts:
        - name: postgres-persistent-storage
          mountPath: /var/lib/postgresql/data2
      volumes:
      - name: postgres-persistent-storage
        persistentVolumeClaim:
          claimName: postgres-pv-claim
</code></pre>

<p>I have deployed your <code>YAMLs</code> and I was able to connect.</p>
<p>In <code>--h</code> you have to provide IP of <code>node</code> or <code>host machine</code> where <code>PostgreSQL</code> pod was deployed.</p>
<h3>Test Case</h3>
<p>I have tested this on my GKE cluster.</p>
<p>Based on your <code>YAMLs</code> and <a href="https://severalnines.com/database-blog/using-kubernetes-deploy-postgresql" rel="nofollow noreferrer">this</a> tutorial. Mainly for <code>ConfigMap</code> and <code>PV/PVC</code> configuration.</p>
<pre><code>  POSTGRES_DB: postgresdb
  POSTGRES_USER: postgresadmin
  POSTGRES_PASSWORD: admin123
</code></pre>
<h3>Connection to PostgreSQL Database</h3>
<ul>
<li><strong>1 - From SQL Pod</strong></li>
</ul>
<pre><code>    $ kubectl get po
    NAME                        READY   STATUS    RESTARTS   AGE
    postgres-5586dc9864-pwpsn   1/1     Running   0          37m
</code></pre>
<p>You have to <code>kubectl exec</code> to <code>PostgreSQL</code> pod.</p>
<pre><code>$ kubectl exec -ti postgres-5586dc9864-pwpsn -- bin/bash
root@postgres-5586dc9864-pwpsn:/#
</code></pre>
<p>Use <code>psql</code> command to connect <code>database</code>.</p>
<pre><code>root@postgres-5586dc9864-pwpsn:/# psql -p 5432 -U postgresadmin -d postgresdb
psql (13.1 (Debian 13.1-1.pgdg100+1))
Type &quot;help&quot; for help.

postgresdb=#
</code></pre>
<ul>
<li><strong>2 - From cluster</strong></li>
</ul>
<p>Service Details:</p>
<pre><code>$ kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.28.0.1    &lt;none&gt;        443/TCP          60m
postgres     NodePort    10.28.14.2   &lt;none&gt;        5432:31431/TCP   45m
</code></pre>
<p>Pod Details:</p>
<pre><code>$ kubectl get po -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP          NODE                                       NOMINATED NODE   READINESS GATES
postgres-5586dc9864-pwpsn   1/1     Running   0          41m   10.24.1.6   gke-cluster-1-default-pool-8baf2b67-jjjh   &lt;none&gt;           &lt;none&gt;
</code></pre>
<p>Node details, where <code>PostgreSQL</code> pod was deployed.</p>
<pre><code>$ kubectl get node -o wide | grep gke-cluster-1-default-pool-8baf2b67-jjjh
gke-cluster-1-default-pool-8baf2b67-jjjh   Ready    &lt;none&gt;   56m   v1.16.15-gke.4300   10.154.15.222   35.197.210.241   Container-Optimized OS from Google   4.19.112+        docker://19.3.1
</code></pre>
<p><code>NodeIP</code> address of <code>node</code> where <code>PostgreSQL</code> pod was deployed is <code>10.154.15.222</code>.</p>
<p>Command:</p>
<pre><code>$ kubectl exec -ti &lt;podname&gt; -- psql -h &lt;Internal IP address of hosting node&gt; -U postgresadmin --password -p &lt;nodeport number from service&gt; &lt;database name&gt;
</code></pre>
<p>Output:</p>
<pre><code>$ kubectl exec -ti postgres-5586dc9864-pwpsn -- psql -h 10.154.15.222 -U postgresadmin --password -p 31431 postgresdb
Password:
psql (13.1 (Debian 13.1-1.pgdg100+1))
Type &quot;help&quot; for help.

postgresdb=#
</code></pre>
