Minikube expose MySQL running on localhost as service

后端 未结 3 674
囚心锁ツ
囚心锁ツ 2020-11-29 06:23

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 cluste

相关标签:
3条回答
  • 2020-11-29 07:06

    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

    0 讨论(0)
  • 2020-11-29 07:08

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

    0 讨论(0)
  • 2020-11-29 07:22

    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.

    0 讨论(0)
提交回复
热议问题