Connect to local database from inside minikube cluster

I'm using ubuntu with Minikube and my database runs outside of minikube inside a docker container and can be accessed from localhost @ 172.17.0.2. My Kubernetes service for my external mysql container reads as follows:

kind: Service
apiVersion: v1
metadata:
  name: mysql-db-svc
  namespace: external
spec: 
  type: ExternalName
  externalName: 10.0.2.2

Then inside my .env for a project my DB_HOST is defined as

mysql-db-svc.external.svc

... the name of the service "mysql-db-svc" followed by its namespace "external" with "svc"

Hope that makes sense.


If I'm not mistaken, you should also create an Endpoint for this service as it's external.

In your case, the Endpoints definition should be as follows:

kind: "Endpoints"
apiVersion: "v1"
metadata:
  name: mysql-db-svc
  namespace: external
subsets: 
- addresses:
  - ip: "10.10.1.1"
  ports:
    port: 3306

You can read about the external sources on Kubernetes Defining a service docs.