- Setting up the dev, and production envs for a springboot stack (springboot, and mysql).
- Setting up the dev env using docker, docker volumes, and docker-compose.
- Setting up the prod env using k8s, and persisting the data using persistent volumes on k8s (tested in minikube).
- Source repo for the springboot app
- Deploy using docker and docker-compose
- Deploy in Kuberentes
- Errors and troubleshooting
- Improvements
- Resources
- LICENSE
-
Create
.env
file with the below content for storing the db credentialsMYSQL_DB_HOST=db #mysqldb service name MYSQL_DB_PORT=3306 #default port for mysql MYSQL_DB_USERNAME= #your username MYSQL_DB_PASSWORD= #your password MYSQL_DB_DNAME=springbootdb #your default database name
-
run the below command for starting both the database and the spring-boot service
docker-compose up --build .
-
Tag your docker image and push it to dockerhub
docker build -t image_name your_username/image_name:version
docker push your_username/image_name:version
-
Create
mysql-secret.yaml
for storing your db credentialsTo put any values in a Secret file in kuberntes it needs to be base64 encoded
echo -n your_lovely_name | base64 echo -n your_strong_complicated_password | base64
mysql-secret.yaml
fileapiVersion: v1 kind: Secret metadata: name: mysql-secret data: MYSQL_DB_USERNAME: #your_lovely encode username MYSQL_DB_PASSWORD: #your_strong and complicated pass
Connect to mysql pod
exec -it pod/mysql-depl-57969854d7-fqc8h -- /bin/bash mysql -u root -p # enter your pw in the prompt
-
Run the files:
kubectl apply -f mysql-configmap.yml && kubectl apply -f mysql-secrets.yml
kubectl apply -f mysql-pvm-pvmc.yml
kubectl apply -f mysql-deployment.yml
kubectl apply -f mysql-service.yml
kubectl apply -f springboot-depl.yml
kubectl apply -f mysql-service.yml
kubectl apply -f springboot-depl.yml
kubectl apply -f springboot-service.yml
-
Get the port to accesss your springboot app
minikube service --url springboot-srv
-
Commands for debugging:
check logs of a deployment
kubectl logs -f pod/springboot-depl-
- Debendancies in the
pom.xml
file specfically- Updating the springboot version to
2.1.6.RELEASE
- Adding the
maven-surefire-plugin
- Updating the springboot version to
- If your mysql pod doesn't start prompting this error
mysql: [ERROR] unknown option '--"'
- This means you have problem with the encoding you shall use an encoding website instead of the echo command
- Try to enable hot reload for the local dev environment
- Search for a way that enables you to hot reload in springboot (e.g flask server in python)
- Mount the source dir to the docker image
- putting an ingress intop of the service
- The option
-DskipTests
for skipping the unit tests from the build process Resource - Enable cashing for mvn
- How to create secrets for kuberntes
- ConfigMaps from k8s official docs
- Why data in secrets is base64 encoded
- Creating a mysql volume and volume claim
- Deploying a fullstack mysql+springboot+react using k8s
- How to use mysql with github actions