<p>I am running some services in minikube and trying to connect to mysql running on localhost(127.0.0.1) on 3306 port. </p>

<p>I read <a href="https://stackoverflow.com/a/43477742/9958058">this</a> and trying to create <code>service</code> and <code>Endpoints</code>. However, when I specify <code>127.0.0.1</code> as IP, it throws error as below:</p>

<p><code>The Endpoints "mysql-service" is invalid: subsets[0].addresses[0].ip: Invalid value: "127.0.0.1": may not be in the loopback range (127.0.0.0/8)</code></p>

<p>my deployment is like below:</p>

<pre><code>---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  ports:
  - protocol: TCP
    port: 1443
    targetPort: mysql

---
apiVersion: v1
kind: Endpoints
metadata:
  name: mysql-service
subsets:
  - addresses:
    - ip: 127.0.0.1
    ports:
    - name: mysql
      port: 3306
</code></pre>

<p>Please assist me to understand how can I connect to <code>mysql</code> db from <code>minikube</code>.</p>

<p>I have also tried replacing <code>127.0.0.1</code> with public IP of my computer(Don't know why though) and connection was timed out.</p>

<p>Any help or guide towards right direction is appreciated.</p>

Nirav
<p>As the OS and minikube <code>vm-driver</code> wasn't mentioned, I assume it is <code>--vm-driver=virtualbox</code> because it's probably most common case. If you use something different you need to adjust this solution according to your configuration.</p>
<h3>Explanation:</h3>
<p><code>127.0.0.1</code> is a <code>localhost</code>(<code>lo0</code>) interface IP address. Nodes, Hosts and Pods have their own localhost interfaces and they are not connected to each other.</p>
<p>Your <code>mysql-server</code> is running on the Host machine and cannot be accessible using the <code>localhost</code> (or it's IP range) from inside a minikube cluster pod or from inside minikube vm.</p>
<h3>Solution:</h3>
<ol>
<li><p>You should have a network between minikube VM and the host. Default NAT network in Virtualbox is not good for that, so it's better to create another host-only network. Let's <a href="https://www.techrepublic.com/article/how-to-create-virtualbox-networks-with-the-host-network-manager/" rel="noreferrer">create</a> additional host-only network in Virtualbox UI with the name <code>vmnet2</code> and IP range <code>192.168.77.1/24</code> . You don't need to enable DHCP for that network.</p>
</li>
<li><p>You have to configure mysql to listen on the interface vmnet2 or ip <code>192.168.77.1</code> which is by default used for the host machine. Check if it's accessible from the host:</p>
</li>
</ol>
<pre><code>mysql -h 192.168.77.1 -u root -p 
</code></pre>
<ol start="3">
<li><p>To attach this network to minikube VM --host-only-cidr key should be used. Different type of <code>vm-driver</code> use different cli options for this purpose. Check the <code>minikube start --help</code> output. So, for <code>virtualbox</code> it will look like the following:</p>
<pre><code>minikube start --cpus 2 \
               --memory 2048 \
               --disk-size 20g \
               --vm-driver virtualbox \
               --network-plugin flannel \
               --kubernetes-version v1.12.2 \
               --host-only-cidr 192.168.77.1/24
</code></pre>
<p>I wrote other most common cli options just for convenience.</p>
<p>MinikubeVM will get the following IP address: <code>192.168.77.100</code> (<a href="https://stackoverflow.com/questions/53871053/how-to-completely-purge-minikube-config-or-reset-ip-back-to-192-168-99-100">at least the first time.</a>)
You can check it using <code>minikube ssh</code> and then <code>ifconfig</code> commands.</p>
</li>
<li><p>Last part - we need to create a Service and Endpoint for it inside the <code>minikube</code> cluster:</p>
</li>
</ol>
<pre><code>kubectl apply -f mysql-service.yaml
</code></pre>
<p>Here is a content of the <code>mysql-service.yaml</code> file:</p>
<pre><code>---
apiVersion: v1
kind: Service
metadata:
   name: mysql-service
spec:
   type: ClusterIP
   ports:
   - protocol: TCP
     port: 3306
     targetPort: 3306
---
apiVersion: v1
kind: Endpoints
metadata:
  name: mysql-service
subsets:
  - addresses:
      - ip: 192.168.77.1
    ports:
      - port: 3306
</code></pre>
<ol start="5">
<li>Now we can use the <code>mysql-service</code> name and port <code>3306</code> inside any pod of this cluster as a destination point.</li>
</ol>
