Access mysql running on localhost from minikube

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.

Leave a Comment