Connect openshift pod to external mysql database

Viewed 52

I am trying to set up a generic pod on OpenShift 4 that can connect to a mysql server running on a separate VM outside the OpenShift cluster (testing using local openshift crc). However when creating the deployment, I'm unable to connect to the mysql server from inside the pod (for testing purposes).

The following is the deployment that I use:

kind: "Service"
apiVersion: "v1"
metadata:
  name: "mysql"
spec:
  ports:
    - name: "mysql"
      protocol: "TCP"
      port: 3306
      targetPort: 3306 
      nodePort: 0
selector: {}
---
kind: "Endpoints"
apiVersion: "v1"
metadata:
  name: "mysql" 
subsets: 
  - addresses:
      - ip: "***ip of host with mysql database on it***" 
    ports:
      - port: 3306 
        name: "mysql"
---
apiVersion: v1
kind: DeploymentConfig
metadata:
  name: "deployment"
spec: 
  template:
    metadata:
      labels:
        name: "mysql"
    spec:
      containers:
        - name: "test-mysql"
          image: "***image repo with docker image that has mysql package installed***"
          ports:
            - containerPort: 3306
              protocol: "TCP"
          env:
            - name: "MYSQL_USER"
              value: "user" 
            - name: "MYSQL_PASSWORD"
              value: "******" 
            - name: "MYSQL_DATABASE"
              value: "mysql_db"  
            - name: "MYSQL_HOST"
              value: "***ip of host with mysql database on it***" 
            - name: "MYSQL_PORT"
              value: "3306"  

I'm just using a generic image for testing purposes that has standard packages installed (net-tools, openjdk, etc.)

I'm testing by going into the deployed pod via the command:

$ oc rsh {{ deployed pod name }}

however when I try to run the following command, I cannot connect to the server running mysql-server

$ mysql --host **hostname** --port 3306 -u user -p

I get this error:

ERROR 2003 (HY000): Can't connect to MySQL server on '**hostname**:3306' (111)

I've also tried to create a route from the service and point to that as a "fqdn" alternative but still no luck.

If I try to ping the host (when inside the pod), I cannot reach it either. But I can reach the host from outside the pod, and from inside the pod, I can ping sites like google.com or github.com

For reference, the image being used is essentially the following dockerfile

FROM ubi:8.0

RUN dnf install -y python3 \
                   wget \
                   java-1.8.0-openjdk \ 
                   https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm \
                   postgresql-devel

WORKDIR /tmp


RUN wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm && \
    rpm -ivh mysql-community-release-el7-5.noarch.rpm && \
    dnf update -y && \
    dnf install mysql -y && \
    wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.48.tar.gz && \
    tar zxvf mysql-connector-java-5.1.48.tar.gz && \
    mkdir -p /usr/share/java/ && \
    cp mysql-connector-java-5.1.48/mysql-connector-java-5.1.48-bin.jar /usr/share/java/mysql-connector-java.jar

RUN dnf install -y tcping \
                   iputils \
                   net-tools 

I imagine there is something I am fundamentally misunderstanding about connecting to an external database from inside OpenShift, and/or my deployment configs need some adjustment somewhere. Any help would be greatly appreciated.

1 Answers

As mentioned in the conversation for the post, it looks to be a firewall issue. I've tested again with the same config, but instead of an external mysql db, I've tested via deploying mysql in openshift as well and the pods can connect. Since I don't have control of the firewall in the organisation, and the config didn't change between the two deployments, I'll mark this as solved as there isn't much more I can do to test it

Related