<p>I have installed Postgresql and microk8s on Ubuntu 18.<br>
One of my microservice which is inside microk8s single node cluster needs to access postgresql installed on same VM.<br>
Some articles suggesting that I should create service.yml and endpoint.yml like this.</p>

<pre><code>apiVersion: v1
metadata:
 name: postgresql
spec:
 type: ClusterIP
 ports:
 - port: 5432
   targetPort: 5432

---

kind: Endpoints
apiVersion: v1
metadata:
 name: postgresql
subsets:
 - addresses:
     - ip: ?????
   ports:
     - port: 5432
</code></pre>

<p>Now, I am not getting what should I put in <strong>subsets.addresses.ip</strong> field ?</p>

<p>First you need to configure your <strong>Postgresql</strong> to listen not only on your vm's <code>localhost</code>. Let's assume you have a network interface with IP address <code>10.1.2.3</code>, which is configured on your node, on which <strong>Postgresql</strong> instance is installed.</p>

<p>Add the following entry in your <code>/etc/postgresql/10/main/postgresql.conf</code>:</p>

<pre><code>listen_addresses = 'localhost,10.1.2.3'
</code></pre>

<p>and restart your postgres service:</p>

<pre><code>sudo systemctl restart postgresql
</code></pre>

<p>You can check if it listens on the desired address by running:</p>

<pre><code>sudo ss -ntlp | grep postgres
</code></pre>

<p>From your <code>Pods</code> deployed within your <strong>Microk8s cluster</strong> you should be able to reach IP addresses of your node e.g. you should be able to ping mentioned <code>10.1.2.3</code> from your <code>Pods</code>.</p>

<p>As it doesn't require any loadbalancing you can reach to your <strong>Postgresql</strong> directly from your <code>Pods</code> without a need of configuring additional <code>Service</code>, that exposes it to your cluster.</p>

<p>If you don't want to refer to your <strong>Postgresql</strong> instance in your application using it's IP address, you can edit your <code>Deployment</code> (which manages the set of <code>Pods</code> that connect to your postgres db) to modify the default content of <code>/etc/hosts</code> file used by your <code>Pods</code>.</p>

<p>Edit your app Deployment by running:</p>

<pre><code>microk8s.kubectl edit deployment your-app
</code></pre>

<p>and add the following section under <code>Pod</code> template <code>spec</code>:</p>

<pre><code>  hostAliases: # it should be on the same indentation level as "containers:"
  - hostnames:
    - postgres
    - postgresql
    ip: 10.1.2.3
</code></pre>

<p>After saving it, all your <code>Pods</code> managed by this <code>Deployment</code> will be recreated according to the new specification. When you exec into your <code>Pod</code> by running:</p>

<pre><code>microk8s.kubectl exec -ti pod-name -- /bin/bash
</code></pre>

<p>you should see additional section in your /etc/hosts file:</p>

<pre><code># Entries added by HostAliases.
10.1.2.3    postgres    postgresql
</code></pre>

<p>Since now you can refer to your <strong>Postgres</strong> instance in your app by names <code>postgres:5432</code> or <code>postgresql:5432</code> and it will be resolved to your VM's IP address.</p>

<p>I hope it helps.</p>

<h1>UPDATE:</h1>

<p>I almost forgot that some time ago I've posted an answer on a very similar topic. You can find it <a href="https://stackoverflow.com/a/60096013/11714114">here</a>. It describes the usage of a <strong>Service without selector</strong>, which is basically what you mentioned in your question. And yes, it also can be used for configuring access to your <strong>Postgresql</strong> instance running on the same host. As this kind of <code>Service</code> doesn't have selectors by its definition, <strong>no endpoint</strong> is automatically created by <strong>kubernetes</strong> and you need to create one by yourself. Once you have the IP address of your <strong>Postgres</strong> instance (in our example it is <code>10.1.2.3</code>) you can use it in your <strong>endpoint</strong> definition.</p>

<p>Once you configure everything on the side of <strong>kubernetes</strong> you may still encounter an issue with <strong>Postgres</strong>. In your <code>Pod</code> that is trying to connect to the <strong>Postgres</strong> instance you may see the following error message:</p>

<pre><code>org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host 10.1.7.151
</code></pre>

<p>It basically means that your <a href="https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html" rel="nofollow noreferrer">pg_hba.conf</a> file lacks the required entry that would allow your <code>Pod</code> to access your <strong>Postgresql</strong> database. Authentication is host-based, so in other words only hosts with certain IPs or with IPs within certain IP range are allowed to authenticate.</p>

<blockquote>
  <p>Client authentication is controlled by a configuration file, which
  traditionally is named pg_hba.conf and is stored in the database
  cluster's data directory. (HBA stands for host-based authentication.)</p>
</blockquote>

<p>So now you probably wonder which network you should allow in your <code>pg_hba.conf</code>. To handle cluster networking <strong>Microk8s</strong> uses <a href="https://github.com/coreos/flannel#flannel" rel="nofollow noreferrer">flannel</a>. Take a look at the content of your <code>/var/snap/microk8s/common/run/flannel/subnet.env</code> file. Mine looks as follows:</p>

<pre><code>FLANNEL_NETWORK=10.1.0.0/16
FLANNEL_SUBNET=10.1.53.1/24
FLANNEL_MTU=1410
FLANNEL_IPMASQ=false
</code></pre>

<p>Adding to your <code>pg_hba.conf</code> only <strong>flannel subnet</strong> should be enough to ensure that all your <code>Pods</code> can connect to <strong>Posgresql</strong>.</p>
