Search code examples
dockeropenshiftminishift

Integrating External Services: Connect to Docker MariaDB from Pod running on Minishift


On a local development environment (Ubuntu) Docker and Minishift are installed.

With Docker a container with mariadb:10.3.11 is started:

docker run -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 --name mariadb 4f2e75647d2a

Running curl from the host system has the following output:

curl 172.17.0.1:3306
curl: (56) Recv failure: Connection reset by peer
5.5.5-10.3.11-MariaDB-1:10.3.11+maria~bionic

QpatFAPM���rv{(RC:7G@H+mysql_native_password!��#08S01Got packets out of order%

The target is to connect to this MariaDB from a pod running in Minishift. I followed this tutorial: https://docs.okd.io/latest/dev_guide/integrating_external_services.html

apiVersion: v1                                                                             
kind: List                                                                                       
items:
- kind: "Service"
  apiVersion: "v1"
  metadata:
    name: "external-mysql-service"
  spec:
    ports:
      - name: "mysql"
        protocol: "TCP"
        port: 3306
        targetPort: 3306 
        nodePort: 0
  selector: {} 
- kind: "Endpoints"
  apiVersion: "v1"
  metadata:
    name: "external-mysql-service" 
  subsets: 
    - addresses:
        - ip: "172.17.0.1"
      ports:
        - port: 3306 
          name: "mysql"

However, the pod cannot connect to the MariaDB with the following error:

2019-02-19 18:00:42 [ERROR] HikariPool:567 - HikariPool-1 - Exception during pool initialization.
 java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=external-mysql-service)(port=3306)(type=master) : Connection refused (Connection refused)

If I connect to the pod via the terminal tab in the OpenShift Web Console and try

$ curl -v external-mysql-service:3306
* Rebuilt URL to: external-mysql-service:3306/
*   Trying 172.30.23.62...
* TCP_NODELAY set
* connect to 172.30.23.62 port 3306 failed: Connection refused
* Failed to connect to external-mysql-service port 3306: Connection refused
* Closing connection 0
curl: (7) Failed to connect to external-mysql-service port 3306: Connection refused

How can I establish a connection from the Minishift pod to the Docker container?

The overall goal is to use arbitrary external services.


Solution

  • The IP of the Endpoint has to be the IP of the host system from the perspective of the vm-driver used with Minishift.

    In my case the vm-driver is VirtualBox and the IP to access the host system is 10.0.2.2: https://github.com/kubernetes/minikube/issues/352#issuecomment-237615642

    Changing the IP of the endpoint worked for me:

    - kind: "Endpoints"
      apiVersion: "v1"
      metadata:
        name: "external-mysql-service" 
      subsets: 
        - addresses:
            - ip: "10.0.2.2"
          ports:
            - port: 3306 
              name: "mysql"
    

    Now the pod inside MiniShift can access the MariaDB running with docker on the host.