<p>I setup a series of VM <code>192.168.2.(100,105,101,104)</code> where kubernetes master is on <code>100</code> and two workers on <code>101,104</code>. Also setup the postgres on <code>192.168.2.105</code>, followed <a href="https://stackoverflow.com/questions/43354167/minikube-expose-mysql-running-on-localhost-as-service/43477742#43477742">this tutorial</a> but it is still unreachable from within. Tried it in minikube inside a test VM where minikube and postgres were installed in the same VM, worked just fine.</p>
<p>Changed the postgers config file from <code>localhost</code> to <code>*</code>, changed listen at pg_hba.conf to <code>0.0.0.0/0</code></p>
<p>Installed postgesql-12 and postgresql-client-12 in the VM <code>192.168.2.105:5432</code>, now i added headless service to kubernetes which is as follows</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
    name: my-service
spec:
    ports:
        - protocol: TCP
          port: 5432
          targetPort: 5432
------
apiVersion: v1
kind: Endpoints
metadata:
    name: my-service
subsets:
    - addresses:
        - ip: 192.168.2.105
      ports:
        - port: 5432
</code></pre>
<p>in my deployment I am defining this to access database</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: keycloak
  labels:
    app: keycloak
spec:
  ports:
  - name: http
    port: 8080
    targetPort: 8080
  selector:
    app: keycloak
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: keycloak
  namespace: default
  labels:
    app: keycloak
spec:
  replicas: 1
  selector:
    matchLabels:
      app: keycloak
  template:
    metadata:
      labels:
        app: keycloak
    spec:
      containers:
      - name: keycloak
        image: quay.io/keycloak/keycloak:11.0.0
        env:
        - name: KEYCLOAK_USER
          value: &quot;admin&quot;
        - name: KEYCLOAK_PASSWORD
          value: &quot;admin&quot;
        - name: PROXY_ADDRESS_FORWARDING
          value: &quot;true&quot;
        - name: DB_ADDR
          value: 'my-service:5432'
        - name: DB_DATABASE
          value: postgres
        - name: DB_PASSWORD
          value: admin
        - name: DB_SCHEMA
          value: public
        - name: DB_USER
          value: postgres
        - name: DB_VENDOR
          value: POSTGRES
        ports:
        - name: http
          containerPort: 8080
        - name: https
          containerPort: 8443
        readinessProbe:
          httpGet:
            path: /auth/realms/master
            port: 8080
</code></pre>
<p>Also the VMs are bridged, not on NAT.</p>
<p>What i am doing wrong here ?</p>

Mainak Das
<p>The first thing we have to do is create the headless service with custom endpoint. The <strong>IP</strong> in my solution is only specific for my machine.</p>
<p>Endpoint with service:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
---
apiVersion: v1
kind: Endpoints
metadata:
  name: postgres-service
subsets:
  - addresses:
      - ip: 192.168.2.105
    ports:
      - port: 5432
</code></pre>
<p>As for my particular specs, I haven't defined any ingress or loadbalancer so i'll change the selector type from <strong>LoadBalancer</strong> to <strong>NodePort</strong> in the service after its deployed.</p>
<p>Now i deployed the keycloak with the the mentioned .yaml file</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: keycloak
  labels:
    app: keycloak
spec:
  ports:
    - name: http
      port: 8080
      targetPort: 8080
    - name: https
      port: 8443
      targetPort: 8443
  selector:
    app: keycloak
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: keycloak
  namespace: default
  labels:
    app: keycloak
spec:
  replicas: 1
  selector:
    matchLabels:
      app: keycloak
  template:
    metadata:
      labels:
        app: keycloak
    spec:
      containers:
        - name: keycloak
          image: quay.io/keycloak/keycloak:11.0.0
          env:
            - name: KEYCLOAK_USER
              value: &quot;admin&quot; # TODO give username for master realm
            - name: KEYCLOAK_PASSWORD
              value: &quot;admin&quot; # TODO give password for master realm
            - name: PROXY_ADDRESS_FORWARDING
              value: &quot;true&quot;
            - name: DB_ADDR
              value: # &lt;Node-IP&gt;:&lt;LoadBalancer-Port/ NodePort&gt;
            - name: DB_DATABASE
              value: &quot;keycloak&quot; # Database to use
            - name: DB_PASSWORD
              value: &quot;admin&quot; # Database password
            - name: DB_SCHEMA
              value: public
            - name: DB_USER
              value: &quot;postgres&quot; # Database user
            - name: DB_VENDOR
              value: POSTGRES
          ports:
            - name: http
              containerPort: 8080
            - name: https
              containerPort: 8443
          readinessProbe:
            httpGet:
              path: /auth/realms/master
              port: 8080
</code></pre>
<p>After mentioning all the possible values, it connects successfully to the postgres server that is hosted on another server away from kubernetes master and workers node !</p>
