Skip to content

Deploying a springboot and mysql stack, using docker, docker-compose for dev env, using kuberntes for production env, the whole deployment tested in minikube

License

Notifications You must be signed in to change notification settings

amshrbo/deploying_springboot_stack

Repository files navigation

Spring Boot MySQL Example

  • 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

TOC

  1. Deploy using docker and docker-compose
  2. Deploy in Kuberentes
  3. Errors and troubleshooting
  4. Improvements
  5. Resources
  6. LICENSE

Deploy locally using Docker

  1. Create .env file with the below content for storing the db credentials

    MYSQL_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
    
  2. run the below command for starting both the database and the spring-boot service

    docker-compose up --build .
    
  3. 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
    

Deploy on k8s tested in minikube

  1. Create mysql-secret.yaml for storing your db credentials

    To 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 file

    apiVersion: 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
    
  2. 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
    
  3. Get the port to accesss your springboot app

    minikube service --url springboot-srv
    
  4. Commands for debugging:

    check logs of a deployment

    kubectl logs -f pod/springboot-depl-
    

Errors and troubleshooting

  1. Debendancies in the pom.xml file specfically
    • Updating the springboot version to 2.1.6.RELEASE
    • Adding the maven-surefire-plugin
  2. 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

Improvements

  • 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

Resources


About

Deploying a springboot and mysql stack, using docker, docker-compose for dev env, using kuberntes for production env, the whole deployment tested in minikube

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published