Deploy java/spring application + MySql DB on Kubernetes

Target Architecture

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.

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

$ docker build -t <YOUR_DOCKER_USERNAME>/kubernetes-spring-mysql-demo:1.0 .
$ docker push -t <YOUR_DOCKER_USERNAME>/kubernetes-spring-mysql-demo:1.0

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:

$ kubectl apply -f mysql-configmap.yaml

2 — Secret

Create a new file secrets.yaml and paste the following content:

$ echo -n '<YOUR_NEW_PASSWORD>' | base64
$ 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.

$ kubectl pply -f mysql-deployment.yaml
$ kubectl get all

BONUS !

You can access the MySql server by running the command:

$ kubectl exec -it deploy/demo-app-mysql -- /bin/bash
$ 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.

$ 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 https://index.docker.io/v1/ for DockerHub.
  • <your-name> is your Docker username.
  • <your-pword> is your Docker password.
  • <your-email> is your Docker email.

2 — Create the app deployment file

Create a new file and name it app-deployment.yaml with the following content:

$ kubectl apply -f app-deployment.yaml

V — Final step

Et voilà … your application is running now on http://localhost:8080/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store