Deploy java/spring application + MySql DB on Kubernetes
Hello everyone, in this tutorial we will learn how to deploy a simple spring boot application using a MySql database on Kubernetes.
I — Create your application
You can start by creating a new simple spring boot application using your IDE or using start.spring.io to generate a new one.
You can also check my previous article to create a simple spring boot application with a database conection.
Thanks to JPA you can connect to a different types of databases just by changing your connection configurations.
After creating your application, make sure that you have the following configuration in your application.yml file :
We will use Kubernetes to replace values for: DB_SERVER, DB_USERNAME, DB_NAME and DB_PASSWORD
II — Dockerize your application
The second step is to dockerize the application so that you can use it as a container and can be orchestrated using kubernetes
Note that we can deploy only containerized applications on kubernetes !
In root directory, create a new file and name it Dockerfile (without extension)
Following is the content of the Dockerfile, this is a simple example of a Dockerfile for a java application (in the next articles we will learn how to create a Dockerfile using the best practices)
The next step is to build the image and push it to docker hub.
To do this you can run the following command to build the image:
$ docker build -t <YOUR_DOCKER_USERNAME>/kubernetes-spring-mysql-demo:1.0 .
After building the image, run the next command to push it to your docker hub:
$ docker push -t <YOUR_DOCKER_USERNAME>/kubernetes-spring-mysql-demo:1.0
You can also check my application repository on GitHub:
III — Deploy MySql on Kubernetes
To deploy MySql we need 3 kubernetes objects:
- ConfigMap: used by MySql container and the app container to share common configurations
- Secret: to save sensitive data like passwords for databases …
- Deployment: to checkout the MySql image and run it as a Pod/Container
- PersistentVolumeClaim: to manage storage
- Service: to expose the MySql container inside Kubernetes
1 — ConfigMap
Create a new file mysql-configmap.yaml and paste the following content:
Run the next command to deploy your configmap:
$ kubectl apply -f mysql-configmap.yaml
2 — Secret
Create a new file secrets.yaml and paste the following content:
To generate new secret values you can use the next command on a bash CLI:
$ echo -n '<YOUR_NEW_PASSWORD>' | base64
and copy paste the new value to the secrets.yaml file.
Run the next command to deploy your secrets:
$ kubectl apply -f secrets.yaml
3 — Mysql deployment file
One of the best practices is to create a single file with all the kubernetes objects needed to deploy an application.
So you can create a new file mysql-deployment.yaml and paste the following content:
Deploy mysql by runinng the next command:
$ kubectl pply -f mysql-deployment.yaml
To check the status of your Kubernetes cluster, you can run the next command and make sure that the mysql Pod is running as expected:
$ kubectl get all
You can access the MySql server by running the command:
$ kubectl exec -it deploy/demo-app-mysql -- /bin/bash
And connect to the database inside the pod by using the next command:
$ mysql --user=$MYSQL_USER --password=$MYSQL_PASSWORD
IV— Deploy the application
1 — Create Docker Hub connection secret
To pull the application image, you need to connect to the Docker Hub.
Create a secret by running the command:
$ kubectl create secret docker-registry regcred --docker-server=<your-registry-server> --docker-username=<your-name> --docker-password=<your-pword> --docker-email=<your-email>
<your-registry-server>is your Private Docker Registry FQDN. Use
<your-name>is your Docker username.
<your-pword>is your Docker password.
<your-email>is your Docker email.
You have successfully set your Docker credentials in the cluster as a Secret called
Check the Kubernets official documentation for more information.
2 — Create the app deployment file
Create a new file and name it app-deployment.yaml with the following content:
Next, deploy the application by running the command:
$ kubectl apply -f app-deployment.yaml
Make sure that all the containers are running and the app service is exposed as a LoadBalancer:
V — Final step
Et voilà … your application is running now on http://localhost:8080/
Stay tuned to learn how to deploy a java application on Kubernetes using an advanced mode for production environment.
Thank you !