I got a web server in spring boot tries to connect to mysql database, both of which is running in minikube local machine. The application and database configuration should be alright, because it is working in local machine with localhost url.
The pod is able to reach the database pod with curl in shell, but the jdbc is not able to find database.
the jdbc error message:
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
# curl -v $DB_HOST
* Trying 10.109.201.118:3306...
* Connected to mysql-svc (10.109.201.118) port 3306 (#0)
> GET / HTTP/1.1
> Host: mysql-svc:3306
> User-Agent: curl/7.81.0
> Accept: */*
>
* Received HTTP/0.9 when not allowed
* Closing connection 0
curl: (1) Received HTTP/0.9 when not allowed
application.yaml
---
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${DB_HOST}/${DB_NAME}?useSSL=false
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
hikari:
initialization-fail-timeout: 0
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: update
db-deploy.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume # name of PV essential for identifying the storage data
labels:
app: mysql
type: local
spec:
storageClassName: manual
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce #This specifies the mode of the claim that we are trying to create.
hostPath:
path: "/var/lib/mysql"
---
# Define a 'Persistent Volume Claim'(PVC) for Mysql Storage, dynamically provisioned by cluster
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim # name of PVC essential for identifying the storage data
labels:
app: mysql
tier: database
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce #This specifies the mode of the claim that we are trying to create.
resources:
requests:
storage: 5Gi #This will tell kubernetes about the amount of space we are trying to claim.
---
# Configure 'Deployment' of mysql server
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deploy
labels:
app: mysql
tier: database
spec:
selector: # mysql Pod Should contain same labels
matchLabels:
app: mysql
tier: database
strategy:
type: Recreate
template:
metadata:
labels: # Must match 'Service' and 'Deployment' selectors
app: mysql
tier: database
spec:
containers:
- image: mysql:8.4 # image from docker-hub
# args:
# - "--ignore-db-dir=lost+found" # Workaround for https://github.com/docker-library/mysql/issues/186
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts: # Mounting volume obtained from Persistent Volume Claim
- name: mysql-persistent-storage
mountPath: /var/lib/mysql #This is the path in the container on which the mounting will take place.
volumes:
- name: mysql-persistent-storage # Obtaining 'volume' from PVC
persistentVolumeClaim:
claimName: mysql-pv-claim
---
# Define a 'Service' To Expose mysql to Other Services
apiVersion: v1
kind: Service
metadata:
name: mysql-svc # DNS name
labels: # mysql Pod Should contain same labels
app: mysql
tier: database
spec:
ports:
- protocol: TCP
port: 3306
targetPort: 3306
selector:
app: mysql
tier: database
clusterIP: None # We Use DNS, Thus ClusterIP is not relevant
# type: NodePort
# type: ExternalName
# externalName: my.database.host
app-deploy.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: onlineshopping-deploy
labels:
app: onlineshopping
spec:
selector:
matchLabels:
app: onlineshopping
replicas: 3
template:
metadata:
labels:
app: onlineshopping
spec:
containers:
- name: onlineshopping-container
image: tank73/onlineshopping:h1.0 # amazon/onlineshopping:1.0
ports:
- containerPort: 9000
env: # Setting Enviornmental Variables
- name: DB_HOST # Setting Database host address from configMap
value : mysql-svc:3306
# value : my.database.host:3306
- name: DB_NAME # Setting Database name from configMap
value : onlineshopping
- name: DB_USERNAME # Setting Database username from Secret
value : root
- name: DB_PASSWORD # Setting Database password from Secret
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
---
apiVersion: v1 # Kubernetes API version
kind: Service # Kubernetes resource kind we are creating
metadata: # Metadata of the resource kind we are creating
name: onlineshopping-svc
spec:
selector:
app: onlineshopping
ports:
- protocol: TCP
port: 9000 # The port that the service is running on in the cluster
targetPort: 9000 # The port exposed by the service
type: LoadBalancer # type of the service.
This is the link of my repository, you are welcome to take a look at my code and provide any suggestions.
https://github.com/Alex-Lin5/Amazon-Spring-Application
6