25

I have minikube version v0.17.1 running on my machine. I want to simulate the environment I will have in AWS, where my MySQL instance will be outside of my Kubernetes cluster.

Basically, how can I expose my local MySQL instance running on my machine to the Kubernetes cluster running via minikube?

2
  • Maybe this answer will help you? Commented Apr 11, 2017 at 21:23
  • 3
    Not really, or at least I'm not sure. Most posts, including this one, seem to talk about how you can access a service running in Kubernetes from your localhost, while I want the exact opposite: expose a port on my localhost and have it available in my Kubernetes cluster. Commented Apr 11, 2017 at 23:05

6 Answers 6

15

Kubernetes allows you to create a service without selector, and cluster will not create related endpoint for this service, this feature is usually used to proxy a legacy component or an outside component.

  1. Create a service without selector

    apiVersion: v1
    kind: Service
    metadata:
        name: my-service
    spec:
        ports:
            - protocol: TCP
              port: 1443
              targetPort: <YOUR_MYSQL_PORT>
    
  2. Create a relative Endpoint object

    apiVersion: v1
    kind: Endpoints
    metadata:
        name: my-service
    subsets:
        - addresses:
            - ip: <YOUR_MYSQL_ADDR>
          ports:
            - port: <YOUR_MYSQL_PORT>
    
  3. Get service IP

    $ kubectl get svc my-service
    NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
    my-service   <SERVICE_IP>   <none>        1443/TCP    18m
    
  4. Access your MYSQL from service <SERVICE_IP>:1443 or my-service:1443

Sign up to request clarification or add additional context in comments.

6 Comments

For inter-pod reference, the service name "my-service" could be used in place of configuration of other pods. Just need to make sure you find the IP "<YOUR_MASQL_ADDR>" in the same network section.
It worked, You may use my-service:1443 instead of <SERVICE_IP>:1443
but what do you put for YOUR_MYSQL_ADDR when it is running on localhost?
@Calicoder That should be the ip address of the machine running the mysql database. For mysql i had to adjust the database's bind-address
It is not working even when we add mysql local host address. I have been trying for 4 days.
|
5

As of minikube 1.10, there is a special hostname host.minikube.internal that resolves to the host running the minikube VM or container. You can then configure this hostname in your pod's environment variables or the ConfigMap that defines the relevant settings.

1 Comment

Unfortunate for OP, but this should be the accepted answer for those with minikube 1.10+
3

Option 1 - use a headless service without selectors

Because this service has no selector, the corresponding Endpoints object will not be created. You can manually map the service to your own specific endpoints (See doc).

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  ports:
  - port: 80
    targetPort: 8080
---
kind: Endpoints
apiVersion: v1
metadata:
  name: my-service
subsets:
- addresses:
  - ip: 10.0.2.2
  ports:
  - port: 8080

Option 2 - use ExternalName service

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  type: ExternalName
  externalName: minikube.host

The only caveat is that it needs to be able to resolve minikube.host. Simply add this line to the etc/hosts file should do it.

10.0.2.2        minikube.host

ExternalName doesn't support port mapping at the moment.


Another note: The IP 10.0.2.2 is known to work with Virtual Box only (see SO). For xhyve, try replacing that with 192.168.99.1 (see GitHub issue and issue). A demo GitHub.

Comments

0
  • Follow instructions in https://minikube.sigs.k8s.io/docs/handbook/host-access/

  • You need then obtain your pc IP ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2} --> This one is for mac, you can find similar command to linx, windows

  • minikube ssh and copy printed IP and pasted in \etc\hosts as it says and it should work

  • Note use host.minikube.internal as mysql host and exposed MySQL port.

Comments

0

If you are using minikube, try host.minikube.internal, for example,

data:
  CACHE_LOCATION: redis://host.minikube.internal:6379/0
  DJANGO_ITEK_DB_URL: postgres://xxx:[email protected]/db

Comments

-1

Just a reminder, if on Windows, open your firewall.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.