Terraform: Deploy spring boot application with MySql DB on Kubernetes
Hello everyone, in this tutorial we will learn how to deploy our previous created application on Kubernetes using Terraform this time.
Enjoy !
Prerequisites
- Terraform: you can check the install Terrafom guide
- Docker: by using Docker Desktop for example
- Kubernetes: you can enable Kubernetes single-node cluster when starting Docker Desktop.
I — Ctreate your application
You can create a new simple spring boot application using your IDE or by using start.spring.io to generate a new one.
Or you can just clone the repo: https://github.com/benstitou/kubernetes-spring-mysql-demo/tree/terraform
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 the application
In the 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>/k8s-terraform-spring-mysql .
After building the image, run the next command to push it to your docker hub:
$ docker push -t <YOUR_DOCKER_USERNAME>/k8s-terraform-spring-mysql
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 config-map.tf and paste the following content:
2 — Secret
Create a new file secrets.tf and paste the following content:
You can use ‘base64encode()’ to create base 64 passwords for mysql database or using a vault service in production workflow.
3 — Mysql deployment and service
We can use one file to create the deployment and the service resources for mysql database.
Create a new file mysql.tf and paste the following content :
This file contain :
- The deployment configurations
- The service configurations
IV — Application deployment
1 — Create Docker Hub connection secret
To pull the application image, you need to connect to the Docker Hub.
Use the following resource to create a new docker connection secret :
This resource uses three variables, the registry_server, registry_username and the registry_password.
Create a new file variables.tf and add the required variables :
where:
- registry_server: is the Docker Hub registry.
- registry_username: is your Docker Hub username.
- registry_password: is your Docker Hub password.
2 — Create the app deployment and service resources
Create a new file and name it application.tf with the following content:
V — Create the main Terraform file
The last step, is to create the file main.tf, this file contains the Kubernetes provider declaration and the resource to create the common Kubernetes namespace.
Make sure that the file is in the same directory with the previous created files
The main.tf file, is the entry point for Terraform.
Paste the following content to main.tf file :
VI — Deploy your application
In the main terraform directory, run the following command to initialize your working directory containing the Terraform onfigurations :
Next, deploy the application by running the command:
$ terraform init
When it’s done, run the plan command to refresh and preview all changes that Terraform plans to make to your infrastructure :
$ terraform plan -var 'registry_password=<YOUR_DOCKER_HUB_PASSWORD>'
Next, apply the changes by running the command :
$ terraform apply -var 'registry_password=<YOUR_DOCKER_HUB_PASSWORD>' -auto-approve
Make sure that all the containers are running and the app service is exposed as a LoadBalancer :
VII — Final step
Your application is running now on http://localhost:8080/
Go to your web browser and test your application.
When you finish, you can delete all the created resources by using the command :
$ terraform destroy
Et voilà … ! Thank you !
- The GitHub repository : https://github.com/benstitou/kubernetes-spring-mysql-demo/tree/terraform
- Deploy java/spring application + MySql DB on Kubernetes : https://benstitou-anas.medium.com/deploy-java-spring-application-with-mysql-db-on-kubernetes-1e456271c6a1