Java app unable to connect to local Mysql pod running on Minikube Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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
Relating to the President and obstruction, were Mueller's conclusions preordained?
Sally's older brother
I got rid of Mac OSX and replaced it with linux but now I can't change it back to OSX or windows
What does Turing mean by this statement?
What order were files/directories output in dir?
How often does castling occur in grandmaster games?
Can two people see the same photon?
Why weren't discrete x86 CPUs ever used in game hardware?
What would you call this weird metallic apparatus that allows you to lift people?
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
Simple HTTP Server
What is the role of と after a noun when it doesn't appear to count or list anything?
What initially awakened the Balrog?
Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?
Most effective melee weapons for arboreal combat? (pre-gunpowder technology)
After Sam didn't return home in the end, were he and Al still friends?
malloc in main() or malloc in another function: allocating memory for a struct and its members
How to change the tick of the color bar legend to black
Does the Black Tentacles spell do damage twice at the start of turn to an already restrained creature?
I can't produce songs
How to write capital alpha?
Does silver oxide react with hydrogen sulfide?
Delete free apps from library
i2c bus hangs in master RPi access to MSP430G uC ~1 in 1000 accesses
Java app unable to connect to local Mysql pod running on Minikube
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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;
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_user
at 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
add a comment |
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_user
at 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
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
add a comment |
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_user
at 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
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_user
at 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
java mysql docker kubernetes minikube
edited Mar 8 at 23:37
Sumit
asked Mar 8 at 23:24
SumitSumit
4872924
4872924
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
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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