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:
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 range192.168.77.1/24
. You don't need to enable DHCP for that network.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
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 theminikube start --help
output. So, forvirtualbox
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 usingminikube ssh
and thenifconfig
commands.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: v1kind: Servicemetadata: name: mysql-servicespec: type: ClusterIP ports: - protocol: TCP port: 3306 targetPort: 3306---apiVersion: v1kind: Endpointsmetadata: name: mysql-servicesubsets: - addresses: - ip: 192.168.77.1 ports: - port: 3306
- Now we can use the
mysql-service
name and port3306
inside any pod of this cluster as a destination point.
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.