'Access mysql running on localhost from minikube

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

I read this and trying to create service and Endpoints. However, when I specify 127.0.0.1 as IP, it throws error as below:

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)

my deployment is like below:

---
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

Please assist me to understand how can I connect to mysql db from minikube.

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

Any help or guide towards right direction is appreciated.



Solution 1:[1]

As the OS and minikube vm-driver wasn't mentioned, I assume it is --vm-driver=virtualbox because it's probably most common case. If you use something different you need to adjust this solution according to your configuration.

Explanation:

127.0.0.1 is a localhost(lo0) interface IP address. Nodes, Hosts and Pods have their own localhost interfaces and they are not connected to each other.

Your mysql-server is running on the Host machine and cannot be accessible using the localhost (or it's IP range) from inside a minikube cluster pod or from inside minikube vm.

Solution:

  1. 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 create additional host-only network in Virtualbox UI with the name vmnet2 and IP range 192.168.77.1/24 . You don't need to enable DHCP for that network.

  2. You have to configure mysql to listen on the interface vmnet2 or ip 192.168.77.1 which is by default used for the host machine. Check if it's accessible from the host:

mysql -h 192.168.77.1 -u root -p 
  1. To attach this network to minikube VM --host-only-cidr key should be used. Different type of vm-driver use different cli options for this purpose. Check the minikube start --help output. So, for virtualbox it will look like the following:

    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
    

    I wrote other most common cli options just for convenience.

    MinikubeVM will get the following IP address: 192.168.77.100 (at least the first time.) You can check it using minikube ssh and then ifconfig commands.

  2. Last part - we need to create a Service and Endpoint for it inside the minikube cluster:

kubectl apply -f mysql-service.yaml

Here is a content of the mysql-service.yaml file:

---
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
  1. Now we can use the mysql-service name and port 3306 inside any pod of this cluster as a destination point.

Solution 2:[2]

? According to minikube's document, you can use host.minikube.internal to access your localhost if your minikube's version is higher than v1.10. you can use minikube ssh to confirm connectivity.

Solution 3:[3]

This is not related to your question but maybe it helps.

If the real problem is that you want to connect some service to the database it is possible to run the database (not the storage, containers might be stateless) inside Minikube. In this scenario, you will be able to use labels to match your database service with other services. We would use PersistentVolume, PersistenceVolumeClaim, and Secrets. This video was really helpful for that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2
Solution 3 Rubén Morales