Java app unable to connect to local Mysql pod running on MinikubeHow do I connect to a MySQL Database in Python?Kubernetes equivalent of env-file in DockerKubernetes local port for deployment in MinikubeHow to start a pod in command line without deployment in kubernetes?Can't connect to port-forwarded Pod running MySQLNewer versions of Minikube don't allow Pods to use their own ServicesKubernetes / minikube can't ping pods in the same cluster, but nslookup worksConnect to local database from inside minikube clusterUse local docker image with minikubeUse local docker image having mysql with minikube

Is the Joker left-handed?

Why can't we play rap on piano?

What is the word for reserving something for yourself before others do?

Could gravitational lensing be used to protect a spaceship from a laser?

AES: Why is it a good practice to use only the first 16bytes of a hash for encryption?

Can I use a neutral wire from another outlet to repair a broken neutral?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

Infinite Abelian subgroup of infinite non Abelian group example

What's the point of deactivating Num Lock on login screens?

How to model explosives?

SSH "lag" in LAN on some machines, mixed distros

Is "remove commented out code" correct English?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Can one be a co-translator of a book, if he does not know the language that the book is translated into?

Fully-Firstable Anagram Sets

Is there a hemisphere-neutral way of specifying a season?

A reference to a well-known characterization of scattered compact spaces

What does it mean to describe someone as a butt steak?

In a Spin are Both Wings Stalled?

Anagram holiday

How to show the equivalence between the regularized regression and their constraint formulas using KKT

Blender 2.8 I can't see vertices, edges or faces in edit mode

prove that the matrix A is diagonalizable

Did converts (ger tzedek) in ancient Israel own land?



Java app unable to connect to local Mysql pod running on Minikube


How do I connect to a MySQL Database in Python?Kubernetes equivalent of env-file in DockerKubernetes local port for deployment in MinikubeHow to start a pod in command line without deployment in kubernetes?Can't connect to port-forwarded Pod running MySQLNewer versions of Minikube don't allow Pods to use their own ServicesKubernetes / minikube can't ping pods in the same cluster, but nslookup worksConnect to local database from inside minikube clusterUse local docker image with minikubeUse local docker image having mysql with minikube






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have a Java Spring boot application deployed on a local Minikube cluster running on my machine.



The Java application connects to a mysql database and takes the configuration at runtime by using a DataSource Configuration. This is the DB config class:



public class DatasourceConfig 
@Bean
@Primary
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(System.getProperty("mysql_user"));
dataSource.setUsername(System.getProperty("mysql_user"));
dataSource.setPassword(System.getProperty("mysql_pass"));

return dataSource;




I pass the values for mysql_url, mysql_pass, mysql_userat runtime like this (from the Dockerfile):



ENTRYPOINT java -Dmysql_url=$mysql_url 
-Dmysql_user=$mysql_user
-Dmysql_pass=$mysql_pass
-jar myapp.jar


I created another pod/deployment for local mysql container image and exposed it via a service locally.



this is the mysql.yml file for kubernetes:



apiVersion: v1
kind: Service
metadata:
name: mysql-svc
labels:
app: mysql
spec:
ports:
- port: 3306
nodePort: 30306
targetPort: mysql-port
protocol: TCP
selector:
app: mysql
type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: mysql
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
tier: mysql
spec:
containers:
- name: mysql
image: mysql
imagePullPolicy: Never
ports:
- name: mysql-port
containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: pass
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim



So, now I have two each of pods+deployment+services running on Minikube one for my application and other for mysql container: (below is the output of kubectl get all



NAME READY STATUS RESTARTS AGE
pod/mysql-deployment-8464d4875d-jlszz 1/1 Running 0 168m
pod/myapp-deployment-6469d8554b-4sbhz 1/1 Running 0 38m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d
service/mysql-svc NodePort 10.105.186.178 <none> 3306:30306/TCP 168m
service/myapp-svc NodePort 10.96.243.6 <none> 8080:31001/TCP 41m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql-deployment 1/1 1 1 168m
deployment.apps/myapp-deployment 1/1 1 1 41m

NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-deployment-8464d4875d 1 1 1 168m
replicaset.apps/myapp-deployment-6469d8554b 1 1 1 41m



I can run the java application correctly from host machine, by doing this:



minikube service myapp-svc



and I can also connect to the Mysql from the host machine by doing this:



run minikube service mysql-svc --url which gives me: http://<mysql-ip>:30306



and then I can connect to it like this (from host machine):



mysql -h <mysql-ip> -P 30306 -u root -p .



This correctly connects me to the mysql running on the local kubernetes cluster through minikube.



I can even connect to the mysql service from inside the java container running on minikube:



first I connect into the java pod like this:



kubectl exec -it myapp-deployment-6469d8554b-4sbhz /bin/bash



and then I run mysql -h <mysql-ip> -P 30306 -u root -p inside the pod. Which also correctly connects me to the mysql running inside the other pod.



The only problem is this:



From inside the Java pod, when I check the application logs, I see this:




ERROR 1 --- [main] com.zaxxer.hikari.pool.HikariPool :
HikariPool-1 - Exception during pool initialization.



com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications
link failure



The last packet sent successfully to the server was 0 milliseconds
ago. The driver has not received any packets from the server.




and also, when ever I try to call any of the APIs which have database operations in them, I get the following error: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection



Which means that my Java code is actually unable to connect to the MySql deployed on the other pod.



I verified the the Java pod is successfully able to connect to the Mysql pod, then why is my Java code not able to connect??



I even verified this that I am able to run the docker java image locally by passing the mysql url of the pod deployed on minikube like this:



docker run -e mysql_url=jdbc:mysql://<mysql-ip>:<mysql-port>/my_db 
-e mysql_user=root
-e mysql_pass=pass
--rm -p 8081:8080 --name myApp -it me/myapp


This verifies that my java code is correctly configured to connect to mysql (even the one deployed on the pod), when I run it locally.



So, it seems to me that mostly everything is configured correctly..



  • The mysql pod itself is running correctly, which I verified by first connecting to it by using the mysql client locally.

  • Java code is able to connect to the mysql running on Minikube when I run docker image locally.

  • The Java pod on Minikube is able to connect to mysql pod which I verified by connecting to the mysql pod from within the java pod using the mysql client from bash.

  • The java code is also running (sans the db connection part).

The only thing that is not working is that the Java app deployed inside Minikube is unable to connect to the mysql deployed in the other pod.



For that, the only point of failure that I can think of is the way Kubernetes passes on the ENV variables on to the docker image while running it.. But I even verified that by running env inside the bash running on my Java pod. which displayed that all the required environment variables are set there correctly.



This is my deployment file for java:



myapp.deployment.yml



apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: me/myapp
imagePullPolicy: Never
env:
- name: mysql_url
value: jdbc:mysql://<mysql-ip>:30306/my_db
- name: mysql_user
value: root
- name: mysql_pass
value: pass
ports:
- name: java-port
containerPort: 8080


Should I also define a pod definition and provide the ENV variables there too ?



Is there something else that I need to do here as well ?



How can I fix this?



P.S.: I have already run eval $(minikube docker-env) in my current terminal window so that the local docker images are available to Minikube.










share|improve this question
























  • Try adding logging to that DatasourceConfig class, and see what's in the env variables at the time when the configuration is invoked?

    – Andreas Lorenzen
    Mar 8 at 23:47

















0















I have a Java Spring boot application deployed on a local Minikube cluster running on my machine.



The Java application connects to a mysql database and takes the configuration at runtime by using a DataSource Configuration. This is the DB config class:



public class DatasourceConfig 
@Bean
@Primary
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(System.getProperty("mysql_user"));
dataSource.setUsername(System.getProperty("mysql_user"));
dataSource.setPassword(System.getProperty("mysql_pass"));

return dataSource;




I pass the values for mysql_url, mysql_pass, mysql_userat runtime like this (from the Dockerfile):



ENTRYPOINT java -Dmysql_url=$mysql_url 
-Dmysql_user=$mysql_user
-Dmysql_pass=$mysql_pass
-jar myapp.jar


I created another pod/deployment for local mysql container image and exposed it via a service locally.



this is the mysql.yml file for kubernetes:



apiVersion: v1
kind: Service
metadata:
name: mysql-svc
labels:
app: mysql
spec:
ports:
- port: 3306
nodePort: 30306
targetPort: mysql-port
protocol: TCP
selector:
app: mysql
type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: mysql
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
tier: mysql
spec:
containers:
- name: mysql
image: mysql
imagePullPolicy: Never
ports:
- name: mysql-port
containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: pass
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim



So, now I have two each of pods+deployment+services running on Minikube one for my application and other for mysql container: (below is the output of kubectl get all



NAME READY STATUS RESTARTS AGE
pod/mysql-deployment-8464d4875d-jlszz 1/1 Running 0 168m
pod/myapp-deployment-6469d8554b-4sbhz 1/1 Running 0 38m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d
service/mysql-svc NodePort 10.105.186.178 <none> 3306:30306/TCP 168m
service/myapp-svc NodePort 10.96.243.6 <none> 8080:31001/TCP 41m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql-deployment 1/1 1 1 168m
deployment.apps/myapp-deployment 1/1 1 1 41m

NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-deployment-8464d4875d 1 1 1 168m
replicaset.apps/myapp-deployment-6469d8554b 1 1 1 41m



I can run the java application correctly from host machine, by doing this:



minikube service myapp-svc



and I can also connect to the Mysql from the host machine by doing this:



run minikube service mysql-svc --url which gives me: http://<mysql-ip>:30306



and then I can connect to it like this (from host machine):



mysql -h <mysql-ip> -P 30306 -u root -p .



This correctly connects me to the mysql running on the local kubernetes cluster through minikube.



I can even connect to the mysql service from inside the java container running on minikube:



first I connect into the java pod like this:



kubectl exec -it myapp-deployment-6469d8554b-4sbhz /bin/bash



and then I run mysql -h <mysql-ip> -P 30306 -u root -p inside the pod. Which also correctly connects me to the mysql running inside the other pod.



The only problem is this:



From inside the Java pod, when I check the application logs, I see this:




ERROR 1 --- [main] com.zaxxer.hikari.pool.HikariPool :
HikariPool-1 - Exception during pool initialization.



com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications
link failure



The last packet sent successfully to the server was 0 milliseconds
ago. The driver has not received any packets from the server.




and also, when ever I try to call any of the APIs which have database operations in them, I get the following error: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection



Which means that my Java code is actually unable to connect to the MySql deployed on the other pod.



I verified the the Java pod is successfully able to connect to the Mysql pod, then why is my Java code not able to connect??



I even verified this that I am able to run the docker java image locally by passing the mysql url of the pod deployed on minikube like this:



docker run -e mysql_url=jdbc:mysql://<mysql-ip>:<mysql-port>/my_db 
-e mysql_user=root
-e mysql_pass=pass
--rm -p 8081:8080 --name myApp -it me/myapp


This verifies that my java code is correctly configured to connect to mysql (even the one deployed on the pod), when I run it locally.



So, it seems to me that mostly everything is configured correctly..



  • The mysql pod itself is running correctly, which I verified by first connecting to it by using the mysql client locally.

  • Java code is able to connect to the mysql running on Minikube when I run docker image locally.

  • The Java pod on Minikube is able to connect to mysql pod which I verified by connecting to the mysql pod from within the java pod using the mysql client from bash.

  • The java code is also running (sans the db connection part).

The only thing that is not working is that the Java app deployed inside Minikube is unable to connect to the mysql deployed in the other pod.



For that, the only point of failure that I can think of is the way Kubernetes passes on the ENV variables on to the docker image while running it.. But I even verified that by running env inside the bash running on my Java pod. which displayed that all the required environment variables are set there correctly.



This is my deployment file for java:



myapp.deployment.yml



apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: me/myapp
imagePullPolicy: Never
env:
- name: mysql_url
value: jdbc:mysql://<mysql-ip>:30306/my_db
- name: mysql_user
value: root
- name: mysql_pass
value: pass
ports:
- name: java-port
containerPort: 8080


Should I also define a pod definition and provide the ENV variables there too ?



Is there something else that I need to do here as well ?



How can I fix this?



P.S.: I have already run eval $(minikube docker-env) in my current terminal window so that the local docker images are available to Minikube.










share|improve this question
























  • Try adding logging to that DatasourceConfig class, and see what's in the env variables at the time when the configuration is invoked?

    – Andreas Lorenzen
    Mar 8 at 23:47













0












0








0








I have a Java Spring boot application deployed on a local Minikube cluster running on my machine.



The Java application connects to a mysql database and takes the configuration at runtime by using a DataSource Configuration. This is the DB config class:



public class DatasourceConfig 
@Bean
@Primary
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(System.getProperty("mysql_user"));
dataSource.setUsername(System.getProperty("mysql_user"));
dataSource.setPassword(System.getProperty("mysql_pass"));

return dataSource;




I pass the values for mysql_url, mysql_pass, mysql_userat runtime like this (from the Dockerfile):



ENTRYPOINT java -Dmysql_url=$mysql_url 
-Dmysql_user=$mysql_user
-Dmysql_pass=$mysql_pass
-jar myapp.jar


I created another pod/deployment for local mysql container image and exposed it via a service locally.



this is the mysql.yml file for kubernetes:



apiVersion: v1
kind: Service
metadata:
name: mysql-svc
labels:
app: mysql
spec:
ports:
- port: 3306
nodePort: 30306
targetPort: mysql-port
protocol: TCP
selector:
app: mysql
type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: mysql
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
tier: mysql
spec:
containers:
- name: mysql
image: mysql
imagePullPolicy: Never
ports:
- name: mysql-port
containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: pass
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim



So, now I have two each of pods+deployment+services running on Minikube one for my application and other for mysql container: (below is the output of kubectl get all



NAME READY STATUS RESTARTS AGE
pod/mysql-deployment-8464d4875d-jlszz 1/1 Running 0 168m
pod/myapp-deployment-6469d8554b-4sbhz 1/1 Running 0 38m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d
service/mysql-svc NodePort 10.105.186.178 <none> 3306:30306/TCP 168m
service/myapp-svc NodePort 10.96.243.6 <none> 8080:31001/TCP 41m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql-deployment 1/1 1 1 168m
deployment.apps/myapp-deployment 1/1 1 1 41m

NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-deployment-8464d4875d 1 1 1 168m
replicaset.apps/myapp-deployment-6469d8554b 1 1 1 41m



I can run the java application correctly from host machine, by doing this:



minikube service myapp-svc



and I can also connect to the Mysql from the host machine by doing this:



run minikube service mysql-svc --url which gives me: http://<mysql-ip>:30306



and then I can connect to it like this (from host machine):



mysql -h <mysql-ip> -P 30306 -u root -p .



This correctly connects me to the mysql running on the local kubernetes cluster through minikube.



I can even connect to the mysql service from inside the java container running on minikube:



first I connect into the java pod like this:



kubectl exec -it myapp-deployment-6469d8554b-4sbhz /bin/bash



and then I run mysql -h <mysql-ip> -P 30306 -u root -p inside the pod. Which also correctly connects me to the mysql running inside the other pod.



The only problem is this:



From inside the Java pod, when I check the application logs, I see this:




ERROR 1 --- [main] com.zaxxer.hikari.pool.HikariPool :
HikariPool-1 - Exception during pool initialization.



com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications
link failure



The last packet sent successfully to the server was 0 milliseconds
ago. The driver has not received any packets from the server.




and also, when ever I try to call any of the APIs which have database operations in them, I get the following error: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection



Which means that my Java code is actually unable to connect to the MySql deployed on the other pod.



I verified the the Java pod is successfully able to connect to the Mysql pod, then why is my Java code not able to connect??



I even verified this that I am able to run the docker java image locally by passing the mysql url of the pod deployed on minikube like this:



docker run -e mysql_url=jdbc:mysql://<mysql-ip>:<mysql-port>/my_db 
-e mysql_user=root
-e mysql_pass=pass
--rm -p 8081:8080 --name myApp -it me/myapp


This verifies that my java code is correctly configured to connect to mysql (even the one deployed on the pod), when I run it locally.



So, it seems to me that mostly everything is configured correctly..



  • The mysql pod itself is running correctly, which I verified by first connecting to it by using the mysql client locally.

  • Java code is able to connect to the mysql running on Minikube when I run docker image locally.

  • The Java pod on Minikube is able to connect to mysql pod which I verified by connecting to the mysql pod from within the java pod using the mysql client from bash.

  • The java code is also running (sans the db connection part).

The only thing that is not working is that the Java app deployed inside Minikube is unable to connect to the mysql deployed in the other pod.



For that, the only point of failure that I can think of is the way Kubernetes passes on the ENV variables on to the docker image while running it.. But I even verified that by running env inside the bash running on my Java pod. which displayed that all the required environment variables are set there correctly.



This is my deployment file for java:



myapp.deployment.yml



apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: me/myapp
imagePullPolicy: Never
env:
- name: mysql_url
value: jdbc:mysql://<mysql-ip>:30306/my_db
- name: mysql_user
value: root
- name: mysql_pass
value: pass
ports:
- name: java-port
containerPort: 8080


Should I also define a pod definition and provide the ENV variables there too ?



Is there something else that I need to do here as well ?



How can I fix this?



P.S.: I have already run eval $(minikube docker-env) in my current terminal window so that the local docker images are available to Minikube.










share|improve this question
















I have a Java Spring boot application deployed on a local Minikube cluster running on my machine.



The Java application connects to a mysql database and takes the configuration at runtime by using a DataSource Configuration. This is the DB config class:



public class DatasourceConfig 
@Bean
@Primary
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(System.getProperty("mysql_user"));
dataSource.setUsername(System.getProperty("mysql_user"));
dataSource.setPassword(System.getProperty("mysql_pass"));

return dataSource;




I pass the values for mysql_url, mysql_pass, mysql_userat runtime like this (from the Dockerfile):



ENTRYPOINT java -Dmysql_url=$mysql_url 
-Dmysql_user=$mysql_user
-Dmysql_pass=$mysql_pass
-jar myapp.jar


I created another pod/deployment for local mysql container image and exposed it via a service locally.



this is the mysql.yml file for kubernetes:



apiVersion: v1
kind: Service
metadata:
name: mysql-svc
labels:
app: mysql
spec:
ports:
- port: 3306
nodePort: 30306
targetPort: mysql-port
protocol: TCP
selector:
app: mysql
type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: mysql
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
tier: mysql
spec:
containers:
- name: mysql
image: mysql
imagePullPolicy: Never
ports:
- name: mysql-port
containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: pass
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim



So, now I have two each of pods+deployment+services running on Minikube one for my application and other for mysql container: (below is the output of kubectl get all



NAME READY STATUS RESTARTS AGE
pod/mysql-deployment-8464d4875d-jlszz 1/1 Running 0 168m
pod/myapp-deployment-6469d8554b-4sbhz 1/1 Running 0 38m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d
service/mysql-svc NodePort 10.105.186.178 <none> 3306:30306/TCP 168m
service/myapp-svc NodePort 10.96.243.6 <none> 8080:31001/TCP 41m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql-deployment 1/1 1 1 168m
deployment.apps/myapp-deployment 1/1 1 1 41m

NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-deployment-8464d4875d 1 1 1 168m
replicaset.apps/myapp-deployment-6469d8554b 1 1 1 41m



I can run the java application correctly from host machine, by doing this:



minikube service myapp-svc



and I can also connect to the Mysql from the host machine by doing this:



run minikube service mysql-svc --url which gives me: http://<mysql-ip>:30306



and then I can connect to it like this (from host machine):



mysql -h <mysql-ip> -P 30306 -u root -p .



This correctly connects me to the mysql running on the local kubernetes cluster through minikube.



I can even connect to the mysql service from inside the java container running on minikube:



first I connect into the java pod like this:



kubectl exec -it myapp-deployment-6469d8554b-4sbhz /bin/bash



and then I run mysql -h <mysql-ip> -P 30306 -u root -p inside the pod. Which also correctly connects me to the mysql running inside the other pod.



The only problem is this:



From inside the Java pod, when I check the application logs, I see this:




ERROR 1 --- [main] com.zaxxer.hikari.pool.HikariPool :
HikariPool-1 - Exception during pool initialization.



com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications
link failure



The last packet sent successfully to the server was 0 milliseconds
ago. The driver has not received any packets from the server.




and also, when ever I try to call any of the APIs which have database operations in them, I get the following error: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection



Which means that my Java code is actually unable to connect to the MySql deployed on the other pod.



I verified the the Java pod is successfully able to connect to the Mysql pod, then why is my Java code not able to connect??



I even verified this that I am able to run the docker java image locally by passing the mysql url of the pod deployed on minikube like this:



docker run -e mysql_url=jdbc:mysql://<mysql-ip>:<mysql-port>/my_db 
-e mysql_user=root
-e mysql_pass=pass
--rm -p 8081:8080 --name myApp -it me/myapp


This verifies that my java code is correctly configured to connect to mysql (even the one deployed on the pod), when I run it locally.



So, it seems to me that mostly everything is configured correctly..



  • The mysql pod itself is running correctly, which I verified by first connecting to it by using the mysql client locally.

  • Java code is able to connect to the mysql running on Minikube when I run docker image locally.

  • The Java pod on Minikube is able to connect to mysql pod which I verified by connecting to the mysql pod from within the java pod using the mysql client from bash.

  • The java code is also running (sans the db connection part).

The only thing that is not working is that the Java app deployed inside Minikube is unable to connect to the mysql deployed in the other pod.



For that, the only point of failure that I can think of is the way Kubernetes passes on the ENV variables on to the docker image while running it.. But I even verified that by running env inside the bash running on my Java pod. which displayed that all the required environment variables are set there correctly.



This is my deployment file for java:



myapp.deployment.yml



apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: me/myapp
imagePullPolicy: Never
env:
- name: mysql_url
value: jdbc:mysql://<mysql-ip>:30306/my_db
- name: mysql_user
value: root
- name: mysql_pass
value: pass
ports:
- name: java-port
containerPort: 8080


Should I also define a pod definition and provide the ENV variables there too ?



Is there something else that I need to do here as well ?



How can I fix this?



P.S.: I have already run eval $(minikube docker-env) in my current terminal window so that the local docker images are available to Minikube.







java mysql docker kubernetes minikube






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 23:37







Sumit

















asked Mar 8 at 23:24









SumitSumit

4772924




4772924












  • Try adding logging to that DatasourceConfig class, and see what's in the env variables at the time when the configuration is invoked?

    – Andreas Lorenzen
    Mar 8 at 23:47

















  • Try adding logging to that DatasourceConfig class, and see what's in the env variables at the time when the configuration is invoked?

    – Andreas Lorenzen
    Mar 8 at 23:47
















Try adding logging to that DatasourceConfig class, and see what's in the env variables at the time when the configuration is invoked?

– Andreas Lorenzen
Mar 8 at 23:47





Try adding logging to that DatasourceConfig class, and see what's in the env variables at the time when the configuration is invoked?

– Andreas Lorenzen
Mar 8 at 23:47












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072362%2fjava-app-unable-to-connect-to-local-mysql-pod-running-on-minikube%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072362%2fjava-app-unable-to-connect-to-local-mysql-pod-running-on-minikube%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229