Connecting to an external mysql database

I am trying to connect my spring-boot api inside a kubernetes pod to an external database (a separate machine but within the local network), however when running, I get SQLNonTransientConnectionException, UknownHostException: mysql-svc, the api cannot connect to the database but whenever I ping the database address from inside the pod using kubectl exec -it, I can ping it successfully, anybody who has experienced the same error as mine?

here is my application.properties connection uri and kubernetes objects:

spring.datasource.url=jdbc:mysql://mysql-svc/test?autoReconnect=true

---
apiVersion: v1
kind: Service
metadata:
 name: mysql-svc
spec:
 type: ClusterIP
 ports:
 - port: 3306
   targetPort: 3306
---
apiVersion: v1
kind: Endpoints
metadata:
 name: mysql-svc
subsets:
 - addresses:
   - ip: 10.0.123.28
   ports:
   - port: 3306
     name: mysql-svc

Cluster information:
Kubernetes version: 1.16
Cloud being used: VMs on local network
Host OS: CentOS 7
CNI and version: Flannel latest
CRI and version: Docker 19.03.2

Hi,

Are you ping’ing the IP address, or the mysql-mvc name?

It seems to be a problem to dns-resolve the name mysql-mvc. Perhaps because your pod is not in the same namespace as your service?

Philippe

yes the ip address, I was able to ping the database server via ip address, I haven’t tried pinging with the mysql-svc name…

why would you need to create a service for a mysql server that’s outside of the kubernetes cluster? just provide the ip or the endpoint, i don’t think you do a service as that setups an ip inside the k cluster right?

If the database is external to the cluster, then m the service type cluster IP won’t help. That will try to match pods inside the cluster (there are none, as it is an external db).

You can use the IP to try to connect and make sure this works. Once this works, we can follow up with improvements. But let’s get started with the basic :slight_smile: