From 7680383837ebe2f4ca7601620e1ae400477f2584 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sun, 27 Oct 2024 14:36:37 +0000 Subject: [PATCH 01/17] Intial commit --- Dockerfile | 39 ++++++++++++++------------------ docker-compose.yml | 55 +++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 49 deletions(-) diff --git a/Dockerfile b/Dockerfile index 079acabe..da04c5b8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,37 +1,32 @@ #---------------------------------- -# Stage 1 +# Stage 1 - Build Environment #---------------------------------- -# Import docker image with maven installed -FROM maven:3.8.3-openjdk-17 as builder +# Use Maven with OpenJDK 17 to compile the application +FROM maven:3.8.3-openjdk-17 AS builder -# Add maintainer, so that new user will understand who had written this Dockerfile -MAINTAINER Madhup Pandey +# Set the working directory in the container +WORKDIR /app -# Add labels to the image to filter out if we have multiple application running -LABEL app=bankapp +# Copy all project files to the working directory +COPY . /app -# Set working directory -WORKDIR /src - -# Copy source code from local to container -COPY . /src - -# Build application and skip test cases +# Build the project and skip tests for a faster build process RUN mvn clean install -DskipTests=true #-------------------------------------- -# Stage 2 +# Stage 2 - Production Environment #-------------------------------------- -# Import small size java image -FROM openjdk:17-alpine as deployer +# Use a lightweight OpenJDK 17 image to run the application +FROM openjdk:17-jdk-alpine -# Copy build from stage 1 (builder) -COPY --from=builder /src/target/*.jar /src/target/bankapp.jar +# Copy the application JAR from the builder stage to the target location +COPY --from=builder /app/target/*.jar /app/target/bankapp.jar -# Expose application port +# Expose the application port EXPOSE 8080 -# Start the application -ENTRYPOINT ["java", "-jar", "/src/target/bankapp.jar"] +# Define the command to run the application +ENTRYPOINT ["java", "-jar", "/app/target/bankapp.jar"] + diff --git a/docker-compose.yml b/docker-compose.yml index f3977b3c..3c71e6a9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,43 +1,44 @@ version: "3.8" + services: + # MySQL database service mysql: image: mysql:latest container_name: mysql environment: - - MYSQL_ROOT_PASSWORD=Test@123 - - MYSQL_DATABASE=BankDB + MYSQL_ROOT_PASSWORD: Test@123 # Root password for MySQL + MYSQL_DATABASE: BankDB # Name of the database to create volumes: - - ./mysql-data:/var/lib/mysql + - mysql-bankapp:/var/lib/mysql # Persistent storage for MySQL data networks: - - bankapp - healthcheck: - test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] - interval: 10s - timeout: 5s - retries: 3 - start_period: 30s + - bankapp # Network for communication between services + restart: always + healthcheck: # Healthcheck to ensure MySQL is ready + test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-pTest@123"] + interval: 10s # Interval between health checks + timeout: 5s # Timeout for health check + retries: 3 # Retry count before marking as unhealthy + start_period: 30s # Initial delay before health check starts - mainapp: - build: . - container_name: Bankapp + # Spring Boot application service (BankApp) + bankapp: + image: bankapp-mini # Specify the image for the BankApp + container_name: bankapp-mini environment: - - SPRING_DATASOURCE_USERNAME=root - - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC - - SPRING_DATASOURCE_PASSWORD=Test@123 + SPRING_DATASOURCE_USERNAME: root # Database username + SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC + SPRING_DATASOURCE_PASSWORD: Test@123 # Database password ports: - - "8080:8080" + - "8080:8080" # Expose the application on port 8080 depends_on: - mysql: - condition: service_healthy + - mysql # Ensure MySQL starts before BankApp networks: - - bankapp + - bankapp # Network for communication with MySQL restart: always - healthcheck: - test: ["CMD-SHELL", "curl -f http://localhost:8080/actuator/health || exit 1"] - interval: 10s - timeout: 5s - retries: 5 - start_period: 30s + +volumes: + mysql-bankapp: # Volume for persistent MySQL data networks: - bankapp: + bankapp: # Network for services communication + From a966c85556a843743aaea93a791aa4d6a0990a88 Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Sun, 27 Oct 2024 22:22:58 +0530 Subject: [PATCH 02/17] Update README.md --- README.md | 167 ++++++++++++++++++++++++------------------------------ 1 file changed, 75 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index 3ddfdaac..6e36b85d 100644 --- a/README.md +++ b/README.md @@ -1,94 +1,77 @@ -## End-to-End Bank Application Deployment using DevSecOps on AWS EKS -- This is a multi-tier bank an application written in Java (Springboot). - -![Login diagram](images/login.png) -![Transactions diagram](images/transactions.png) - -### PRE-REQUISITES FOR THIS PROJECT: -- AWS Account -- AWS Ubuntu EC2 instance (t2.medium) -- Install Docker -- Install docker compose -# -### DEPLOYMENT: -| Deployments | Paths | -| -------- | ------- | -| Deployment using Docker and Networking | Click me | -| Deployment using Docker Compose | Click me | -| Deployment using Jenkins on EKS | Click me | -| Deployment using Argocd on EKS| Click me | - -# -### STEPS TO IMPLEMENT THE PROJECT -- **

Deployment using Docker

** - - Clone the repository - ```bash - git clone -b DevOps https://github.com/DevMadhup/Springboot-BankApp.git - ``` - # - - Install docker, docker compose and provide neccessary permission - ```bash - sudo apt update -y - - sudo apt install docker.io docker-compose-v2 -y - - sudo usermod -aG docker $USER && newgrp docker - ``` - # - - Move to the cloned repository - ```bash - cd Springboot-BankApp - ``` - # - - Build the Dockerfile - ```bash - docker build -t madhupdevops/springboot-bankapp . - ``` -> [!Important] -> Make sure to change docker build command with your DockerHub username. - # - - Create a docker network - ```bash - docker network create bankapp - ``` - # - - Run MYSQL container - ```bash - docker run -itd --name mysql -e MYSQL_ROOT_PASSWORD=Test@123 -e MYSQL_DATABASE=BankDB --network=bankapp mysql - ``` - # - - Run Application container - ```bash - docker run -itd --name BankApp -e SPRING_DATASOURCE_USERNAME="root" -e SPRING_DATASOURCE_URL="jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC" -e SPRING_DATASOURCE_PASSWORD="Test@123" --network=bankapp -p 8080:8080 madhupdevops/springboot-bankapp - ``` - # - - Verify deployment - ```bash - docker ps - ``` - # - - Open port 8080 of your AWS instance and access your application - ```bash - http://:8080 - ``` - ### Congratulations, you have deployed the application using Docker - # -- **

Deployment using Docker compose

** -- Install docker compose -```bash -sudo apt update -sudo apt install docker-compose-v2 -y -``` -# -- Run docker-compose file present in the root directory of a project -```bash -docker compose up -d +Here's a sample README file for your Dockerized Banking Application project. You can customize it further as needed: + +```markdown +# Dockerized Banking Application + +## Overview + +This project demonstrates a Dockerized Banking Application built using Spring Boot and MySQL. It showcases containerization techniques using Docker and Docker Compose to manage services efficiently. + +## Table of Contents + +- [Pre-requisites](#pre-requisites) +- [Key Features](#key-features) +- [Deployment Steps](#deployment-steps) +- [Accessing the Application](#accessing-the-application) +- [Detailed Guide](#detailed-guide) +- [Implementation Notes](#implementation-notes) +- [Acknowledgments](#acknowledgments) + +## Pre-requisites + +Before you begin, ensure you have the following: + +- An **AWS account** +- A **t2.medium EC2 instance** for better performance +- **Docker** and **Docker Compose** installed on your EC2 instance + +## Key Features + +- **Containerization:** Multi-stage Docker builds for improved performance +- **Service Management:** Simplified deployment using Docker Compose +- **Persistent Storage:** MySQL database configured with persistent storage + +## Deployment Steps + +1. **Clone the Repository** + ```bash + git clone + cd + ``` + +2. **Create a Multi-Stage Dockerfile** + - A sample Dockerfile is included for building the Spring Boot application. + +3. **Docker Compose Setup** + - A `docker-compose.yml` file is provided to manage the MySQL and Spring Boot application services. + +4. **Run Docker Compose** + ```bash + docker-compose up -d + ``` + +5. **Stop and Remove Containers (if needed)** + ```bash + docker-compose down + ``` + +## Accessing the Application + +Once the application is running, you can access it via the public IP of your EC2 instance on port 8080: + ``` -# -- Access it on port 8080 -```bash - http://:8080 +http://:8080 ``` -> [!Important] -> If you face issues with exiting docker container while running docker compose, run ``` docker compose down``` and then ``` docker compose up -d ```. -# + +## Detailed Guide + +For a comprehensive walkthrough of the setup and deployment, refer to my blog post: [Deploying Multi-Tier Application with Docker](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker) + +## Implementation Notes + +For detailed implementation notes with images, visit my Notion page: [Notion Notes](https://www.notion.so/Docker-bankapp-project-12c7311ab980801a929ad23bf654b64d) + +## Acknowledgments + +Special thanks to **Shubham Londhe** for his incredible support throughout this journey! + From 13fb32f543a79fd6a041c896079d94221a91674e Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Sun, 27 Oct 2024 22:23:44 +0530 Subject: [PATCH 03/17] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 6e36b85d..099a5063 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,3 @@ -Here's a sample README file for your Dockerized Banking Application project. You can customize it further as needed: - -```markdown # Dockerized Banking Application ## Overview From 56a16ac84df946f269f463b20b9f1170b1ad08fa Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Sun, 27 Oct 2024 22:29:53 +0530 Subject: [PATCH 04/17] Update README.md --- README.md | 63 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 099a5063..c6e3dd8a 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ + # Dockerized Banking Application ## Overview -This project demonstrates a Dockerized Banking Application built using Spring Boot and MySQL. It showcases containerization techniques using Docker and Docker Compose to manage services efficiently. +This project showcases a Dockerized Banking Application built using Spring Boot and MySQL. The application is containerized using Docker and Docker Compose, enabling efficient service management and deployment. ## Table of Contents - [Pre-requisites](#pre-requisites) - [Key Features](#key-features) -- [Deployment Steps](#deployment-steps) +- [Setup and Deployment](#setup-and-deployment) - [Accessing the Application](#accessing-the-application) -- [Detailed Guide](#detailed-guide) -- [Implementation Notes](#implementation-notes) +- [Git Workflow](#git-workflow) - [Acknowledgments](#acknowledgments) ## Pre-requisites @@ -19,37 +19,54 @@ This project demonstrates a Dockerized Banking Application built using Spring Bo Before you begin, ensure you have the following: - An **AWS account** -- A **t2.medium EC2 instance** for better performance -- **Docker** and **Docker Compose** installed on your EC2 instance +- A **t2.medium EC2 instance** for deployment +- **Docker** and **Docker Compose** installed ## Key Features -- **Containerization:** Multi-stage Docker builds for improved performance -- **Service Management:** Simplified deployment using Docker Compose -- **Persistent Storage:** MySQL database configured with persistent storage +- **Containerization:** Utilizes multi-stage Docker builds for performance optimization. +- **Service Management:** Manages services with Docker Compose. +- **Persistent Storage:** MySQL database with data persistence using Docker volumes. -## Deployment Steps +## Setup and Deployment 1. **Clone the Repository** ```bash - git clone - cd + git clone https://github.com/Amitabh-DevOps/Springboot-BankApp.git + cd Springboot-BankApp ``` -2. **Create a Multi-Stage Dockerfile** - - A sample Dockerfile is included for building the Spring Boot application. +2. **Create Docker Volume and Network** + ```bash + docker volume create mysql-bankapp + docker network create bankapp + ``` + +3. **Run MySQL Container** + ```bash + docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=Test@123 -e MYSQL_DATABASE=BankDB --network=bankapp mysql:latest + ``` -3. **Docker Compose Setup** - - A `docker-compose.yml` file is provided to manage the MySQL and Spring Boot application services. +4. **Build the Spring Boot Application** + ```bash + docker build -t bankapp-mini . + ``` -4. **Run Docker Compose** +5. **Run the Spring Boot Application Container** ```bash - docker-compose up -d + docker run -d --name bankapp-mini \ + -e SPRING_DATASOURCE_USERNAME="root" \ + -e SPRING_DATASOURCE_URL="jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC" \ + -e SPRING_DATASOURCE_PASSWORD="Test@123" \ + --network=bankapp \ + -p 8080:8080 bankapp-mini:latest ``` -5. **Stop and Remove Containers (if needed)** +6. **Using Docker Compose (if applicable)** + - Ensure you have a properly configured `docker-compose.yml`. + - Run: ```bash - docker-compose down + docker compose up ``` ## Accessing the Application @@ -60,6 +77,8 @@ Once the application is running, you can access it via the public IP of your EC2 http://:8080 ``` +--- + ## Detailed Guide For a comprehensive walkthrough of the setup and deployment, refer to my blog post: [Deploying Multi-Tier Application with Docker](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker) @@ -68,7 +87,9 @@ For a comprehensive walkthrough of the setup and deployment, refer to my blog po For detailed implementation notes with images, visit my Notion page: [Notion Notes](https://www.notion.so/Docker-bankapp-project-12c7311ab980801a929ad23bf654b64d) + ## Acknowledgments -Special thanks to **Shubham Londhe** for his incredible support throughout this journey! +Special thanks to **Shubham Londhe** for his guidance and support throughout this project! +--- From 2604c357b88c77e68b45a186aa2b1e190de7e017 Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 17:02:46 +0530 Subject: [PATCH 05/17] Create default.conf --- nginx/default.conf | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 nginx/default.conf diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100644 index 00000000..040cab8f --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,17 @@ +upstream springboot_app { + server springboot_app:8000; +} + +server { + listen 80; + + server_name localhost; + + location / { + proxy_pass http://springboot_app:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +} From 01ea9666d17906c6b83b482b4c3f31df0cffb670 Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 17:03:05 +0530 Subject: [PATCH 06/17] Create Dockerfile --- nginx/Dockerfile | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 nginx/Dockerfile diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 00000000..6f333588 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,3 @@ +FROM nginx:1.23.3-alpine + +COPY ./default.conf /etc/nginx/conf.d/default.conf From bd95c5b7ba11f37417f98e9160740ffeb107464b Mon Sep 17 00:00:00 2001 From: Amitabh-DevOps Date: Thu, 14 Nov 2024 17:22:48 +0530 Subject: [PATCH 07/17] Updated --- Dockerfile | 7 +++---- docker-compose.yml | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index da04c5b8..a2ceb988 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,9 +24,8 @@ FROM openjdk:17-jdk-alpine # Copy the application JAR from the builder stage to the target location COPY --from=builder /app/target/*.jar /app/target/bankapp.jar -# Expose the application port -EXPOSE 8080 +# Expose the application port (updated to 8000) +EXPOSE 8000 # Define the command to run the application -ENTRYPOINT ["java", "-jar", "/app/target/bankapp.jar"] - +ENTRYPOINT ["java", "-jar", "/app/target/bankapp.jar"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 3c71e6a9..4954497f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,28 +17,39 @@ services: test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-pTest@123"] interval: 10s # Interval between health checks timeout: 5s # Timeout for health check - retries: 3 # Retry count before marking as unhealthy - start_period: 30s # Initial delay before health check starts + retries: 3 # Retry count before health check starts # Spring Boot application service (BankApp) bankapp: - image: bankapp-mini # Specify the image for the BankApp + image: bankapp-mini # Specify the image for the BankApp container_name: bankapp-mini environment: SPRING_DATASOURCE_USERNAME: root # Database username SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC SPRING_DATASOURCE_PASSWORD: Test@123 # Database password - ports: - - "8080:8080" # Expose the application on port 8080 + expose: + - "8000" # Expose the application internally on port 8000 depends_on: - mysql # Ensure MySQL starts before BankApp networks: - - bankapp # Network for communication with MySQL + - bankapp # Network for communication with MySQL and Nginx restart: always + # Nginx reverse proxy service + nginx: + image: nginx:1.23.3-alpine + container_name: nginx + ports: + - "80:80" # Expose Nginx on port 80 + volumes: + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf # Nginx configuration + depends_on: + - bankapp # Ensure BankApp starts before Nginx + networks: + - bankapp # Network for communication with BankApp + volumes: mysql-bankapp: # Volume for persistent MySQL data networks: bankapp: # Network for services communication - From 755e98a46c5a26066564c73931166736ed66cae8 Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 18:05:18 +0530 Subject: [PATCH 08/17] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 4954497f..f3ac71ba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,7 +40,7 @@ services: image: nginx:1.23.3-alpine container_name: nginx ports: - - "80:80" # Expose Nginx on port 80 + - "8080:80" # Expose Nginx on port 80 volumes: - ./nginx/default.conf:/etc/nginx/conf.d/default.conf # Nginx configuration depends_on: From e1e4e822d2db06bde0b484610da412ebcd8979d1 Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 18:25:07 +0530 Subject: [PATCH 09/17] Update docker-compose.yml --- docker-compose.yml | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index f3ac71ba..67eaa1ea 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,20 +20,21 @@ services: retries: 3 # Retry count before health check starts # Spring Boot application service (BankApp) - bankapp: - image: bankapp-mini # Specify the image for the BankApp - container_name: bankapp-mini - environment: - SPRING_DATASOURCE_USERNAME: root # Database username - SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC - SPRING_DATASOURCE_PASSWORD: Test@123 # Database password - expose: - - "8000" # Expose the application internally on port 8000 - depends_on: - - mysql # Ensure MySQL starts before BankApp - networks: - - bankapp # Network for communication with MySQL and Nginx - restart: always +bankapp: + image: bankapp-mini + container_name: bankapp-mini + environment: + SPRING_DATASOURCE_USERNAME: root + SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC + SPRING_DATASOURCE_PASSWORD: Test@123 + ports: + - "8000:8000" # Expose port 8000 externally + depends_on: + - mysql + networks: + - bankapp + restart: always + # Nginx reverse proxy service nginx: From ddca7c414f61e62c2020d9b02bc47426b78b8874 Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 18:30:52 +0530 Subject: [PATCH 10/17] Update docker-compose.yml --- docker-compose.yml | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 67eaa1ea..3201da3d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,21 +20,20 @@ services: retries: 3 # Retry count before health check starts # Spring Boot application service (BankApp) -bankapp: - image: bankapp-mini - container_name: bankapp-mini - environment: - SPRING_DATASOURCE_USERNAME: root - SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC - SPRING_DATASOURCE_PASSWORD: Test@123 - ports: - - "8000:8000" # Expose port 8000 externally - depends_on: - - mysql - networks: - - bankapp - restart: always - + bankapp: + image: bankapp-mini # Specify the image for the BankApp + container_name: bankapp-mini + environment: + SPRING_DATASOURCE_USERNAME: root # Database username + SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC + SPRING_DATASOURCE_PASSWORD: Test@123 # Database password + ports: + - "8000:8000" # Expose the application externally on port 8000 + depends_on: + - mysql # Ensure MySQL starts before BankApp + networks: + - bankapp # Network for communication with MySQL and Nginx + restart: always # Nginx reverse proxy service nginx: From 37a9ece37cfaf3794283004caa87c57277e4f8c1 Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 18:43:03 +0530 Subject: [PATCH 11/17] Update default.conf --- nginx/default.conf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nginx/default.conf b/nginx/default.conf index 040cab8f..781ad3a8 100644 --- a/nginx/default.conf +++ b/nginx/default.conf @@ -1,5 +1,5 @@ -upstream springboot_app { - server springboot_app:8000; +upstream bankapp { + server bankapp:8000; # Use the correct service name from docker-compose.yml } server { @@ -8,7 +8,7 @@ server { server_name localhost; location / { - proxy_pass http://springboot_app:8000; + proxy_pass http://bankapp:8000; # Use the service name 'bankapp' here as well proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; From 462d4180dad616c33787f132dd931077b10f8112 Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 18:43:52 +0530 Subject: [PATCH 12/17] Update docker-compose.yml --- docker-compose.yml | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3201da3d..f751f013 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,55 +1,52 @@ version: "3.8" services: - # MySQL database service mysql: image: mysql:latest container_name: mysql environment: - MYSQL_ROOT_PASSWORD: Test@123 # Root password for MySQL - MYSQL_DATABASE: BankDB # Name of the database to create + MYSQL_ROOT_PASSWORD: Test@123 + MYSQL_DATABASE: BankDB volumes: - - mysql-bankapp:/var/lib/mysql # Persistent storage for MySQL data + - mysql-bankapp:/var/lib/mysql networks: - - bankapp # Network for communication between services + - bankapp restart: always - healthcheck: # Healthcheck to ensure MySQL is ready + healthcheck: test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-pTest@123"] - interval: 10s # Interval between health checks - timeout: 5s # Timeout for health check - retries: 3 # Retry count before health check starts + interval: 10s + timeout: 5s + retries: 3 - # Spring Boot application service (BankApp) bankapp: - image: bankapp-mini # Specify the image for the BankApp + image: bankapp-mini container_name: bankapp-mini environment: - SPRING_DATASOURCE_USERNAME: root # Database username + SPRING_DATASOURCE_USERNAME: root SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC - SPRING_DATASOURCE_PASSWORD: Test@123 # Database password + SPRING_DATASOURCE_PASSWORD: Test@123 ports: - - "8000:8000" # Expose the application externally on port 8000 + - "8000:8000" depends_on: - - mysql # Ensure MySQL starts before BankApp + - mysql networks: - - bankapp # Network for communication with MySQL and Nginx + - bankapp restart: always - # Nginx reverse proxy service nginx: image: nginx:1.23.3-alpine container_name: nginx ports: - - "8080:80" # Expose Nginx on port 80 + - "8080:80" volumes: - - ./nginx/default.conf:/etc/nginx/conf.d/default.conf # Nginx configuration + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf depends_on: - - bankapp # Ensure BankApp starts before Nginx + - bankapp networks: - - bankapp # Network for communication with BankApp + - bankapp volumes: - mysql-bankapp: # Volume for persistent MySQL data + mysql-bankapp: networks: - bankapp: # Network for services communication + bankapp: From fbfcf01f4e818a5e4def32af8516846b0adae212 Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 18:59:10 +0530 Subject: [PATCH 13/17] Update application.properties --- src/main/resources/application.properties | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 08663a63..e3f21bec 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,5 @@ spring.application.name=bankapp + # MySQL Database configuration spring.datasource.url=jdbc:mysql://localhost:3306/bankappdb?useSSL=false&serverTimezone=UTC spring.datasource.username=root @@ -9,3 +10,6 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect spring.jpa.show-sql=true + +# Set server port to 8000 +server.port=8000 From ddcb499558adf4b4280c12a34e3ac5d72659d47f Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 19:04:36 +0530 Subject: [PATCH 14/17] Create Jenkinsfile --- Jenkinsfile | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 00000000..2d7d7897 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,32 @@ +@Library('Shared')_ + +pipeline{ + agent {label 'dev-server'} + + stages{ + stage("Code"){ + steps{ + clone("https://github.com/Amitabh-DevOps/banking-app-project.git","dev") + echo "Code clonning done." + } + } + stage("Build"){ + steps{ + dockerbuild("bankapp-mini","latest") + echo "Code build bhi hogaya." + } + } + stage("Push to DockerHub"){ + steps{ + dockerpush("dockerHub","bankapp-mini","latest") + echo "Push to dockerHub is also done." + } + } + stage("Deplying"){ + steps{ + deploy() + echo "Deployment bhi done." + } + } + } +} From a01d1ccca6f9e7e78f82b6ccd93062b8db9a730c Mon Sep 17 00:00:00 2001 From: Amitabh soni Date: Thu, 14 Nov 2024 19:17:36 +0530 Subject: [PATCH 15/17] Update docker-compose.yml From b1f70dea8721053ace690a44beb9d4a1a6d7dcd4 Mon Sep 17 00:00:00 2001 From: Amitabh-DevOps Date: Thu, 14 Nov 2024 21:37:59 +0530 Subject: [PATCH 16/17] Added README.md --- README.md | 400 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 338 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index c6e3dd8a..34d22844 100644 --- a/README.md +++ b/README.md @@ -1,95 +1,371 @@ +In this Project, I will walk you through the process of setting up a **CI/CD pipeline** for a **Spring Boot banking application**. This pipeline will automate the build, deployment, and integration processes using **Jenkins**, **Docker**, and **GitHub**. You will learn how to leverage a **multi-node Jenkins architecture**, **shared libraries**, and **webhooks** for automatic deployments. -# Dockerized Banking Application +**Repository for this Project**: Used this Repo + Shared library repo : https://github.com/Amitabh-DevOps/Jenkins-shared-libraries -## Overview +**Check Blog** : https://amitabhdevops.hashnode.dev/spring-boot-bank-jenkins -This project showcases a Dockerized Banking Application built using Spring Boot and MySQL. The application is containerized using Docker and Docker Compose, enabling efficient service management and deployment. +> Use the `dev` branch for the code related to this project. -## Table of Contents +--- + +## **Project Overview** -- [Pre-requisites](#pre-requisites) -- [Key Features](#key-features) -- [Setup and Deployment](#setup-and-deployment) -- [Accessing the Application](#accessing-the-application) -- [Git Workflow](#git-workflow) -- [Acknowledgments](#acknowledgments) +This project involves creating a complete CI/CD pipeline that automates the deployment of a Spring Boot-based banking application. Here are the steps we will follow: -## Pre-requisites +1. **Create AWS EC2 Instances** to host Jenkins and Docker. + +2. **Set up Jenkins** to automate the CI/CD pipeline. + +3. **Containerize the Spring Boot application** using Docker. + +4. **Integrate GitHub** for automatic deployment triggered by code changes. + +5. **Use a multi-node Jenkins setup** to deploy the application on a development server. + +6. Create a **Jenkinsfile** for automated builds and deployment. + +7. **Set up webhooks** in GitHub to trigger Jenkins builds on code changes. + -Before you begin, ensure you have the following: +--- -- An **AWS account** -- A **t2.medium EC2 instance** for deployment -- **Docker** and **Docker Compose** installed +## Steps to Implement the Project -## Key Features +To set up a **Jenkins Master-Agent** architecture on AWS, we will create two EC2 instances. The Jenkins Master instance will manage Jenkins, while the Jenkins Agent instance, with higher resources, will host and deploy the Spring bootapplication. -- **Containerization:** Utilizes multi-stage Docker builds for performance optimization. -- **Service Management:** Manages services with Docker Compose. -- **Persistent Storage:** MySQL database with data persistence using Docker volumes. +### Step 1: Create Two AWS EC2 Instances -## Setup and Deployment +We'll start by setting up two separate instances: one for the **Jenkins Master** and one for the **Jenkins Agent**. -1. **Clone the Repository** - ```bash - git clone https://github.com/Amitabh-DevOps/Springboot-BankApp.git - cd Springboot-BankApp - ``` +1. **Log in to AWS**: + Go to the [AWS Console](https://aws.amazon.com/console/) and log in. + +2. **Launch an EC2 Instance (Jenkins Master)**: + + * Go to the **EC2 Dashboard** and click on **Launch Instance**. + + * Select the **Ubuntu 24.04 LTS** AMI. + + * Choose **t2.micro** for the Jenkins Master instance, eligible for the free tier. + + * Configure **Security Group**: + + * **SSH (port 22)** for remote access. + + * **HTTP (port 80)** to access Jenkins through the browser. + + * Click **Review and Launch**. + +3. **Launch an EC2 Instance (Jenkins Agent)**: + + * Repeat the above steps, but select **t2.medium** for the Jenkins Agent instance. + + * Use the **same key pair** as used for the Jenkins Master. + -2. **Create Docker Volume and Network** - ```bash - docker volume create mysql-bankapp - docker network create bankapp - ``` +> Note: For simplicity and consistency, it’s best to use the same key pair for both instances. This enables both instances to be accessed with a single private key file (e.g., `.pem`), which is useful for managing both servers in a DevOps environment. -3. **Run MySQL Container** - ```bash - docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=Test@123 -e MYSQL_DATABASE=BankDB --network=bankapp mysql:latest - ``` +### Step 2: Connect to Each EC2 Instance -4. **Build the Spring Boot Application** - ```bash - docker build -t bankapp-mini . - ``` +SSH into both instances using: -5. **Run the Spring Boot Application Container** - ```bash - docker run -d --name bankapp-mini \ - -e SPRING_DATASOURCE_USERNAME="root" \ - -e SPRING_DATASOURCE_URL="jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC" \ - -e SPRING_DATASOURCE_PASSWORD="Test@123" \ - --network=bankapp \ - -p 8080:8080 bankapp-mini:latest - ``` +```bash +ssh -i .pem ubuntu@ +``` -6. **Using Docker Compose (if applicable)** - - Ensure you have a properly configured `docker-compose.yml`. - - Run: - ```bash - docker compose up - ``` +### Step 3: Update Each EC2 Instance + +Ensure both instances are up-to-date by running: + +```bash +sudo apt update && sudo apt upgrade -y +``` -## Accessing the Application +### Step 4: Install Java on Both Instances -Once the application is running, you can access it via the public IP of your EC2 instance on port 8080: +Jenkins requires Java, so install OpenJDK 17 on each instance: +```bash +sudo apt install openjdk-17-jre -y +java -version ``` -http://:8080 + +### Step 5: Install Jenkins (Only on Jenkins Master) + +1. **Install dependencies**: + + ```bash + sudo apt-get install -y ca-certificates curl gnupg + ``` + +2. **Add the Jenkins repository key**: + + ```bash + curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null + ``` + +3. **Add Jenkins to APT sources**: + + ```bash + echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null + ``` + +4. **Install Jenkins**: + + ```bash + sudo apt-get update + sudo apt-get install jenkins -y + ``` + +5. **Enable Jenkins to start on boot**: + + ```bash + sudo systemctl enable jenkins + sudo systemctl start jenkins + ``` + +6. **Verify Jenkins status**: + + ```bash + sudo systemctl status jenkins + ``` + + +### Step 6: Install Docker on Both Instances + +1. **Install Docker**: + + ```bash + sudo apt install docker.io -y + ``` + +2. **Add Jenkins to the Docker group** (on both instances): + + ```bash + sudo usermod -aG docker $USER + ``` + +3. **Refresh Docker group membership**: + + ```bash + newgrp docker + ``` + + +### Step 7: Install Docker Compose on Both Instances + +```bash +sudo apt install docker-compose-v2 -y +docker --version +docker-compose --version +``` + +### Step 8: Configure Jenkins Security Groups for Web Access + +Edit the security group of your Jenkins Master instance: + +1. Go to the **EC2 Dashboard**, select **Security Groups**, and choose the security group associated with your EC2 instance. + +2. Click on **Edit Inbound Rules** and add a rule for **Custom TCP Rule** with **port 8080**. + +3. Access Jenkins in a web browser using `http://:8080`. + + +### Step 9: Retrieve Jenkins Admin Password + +Retrieve the initial admin password: + +```bash +sudo cat /var/lib/jenkins/secrets/initialAdminPassword +``` + +Use this password to complete the initial setup in Jenkins by following the on-screen instructions. + +--- + +## Creating a Development Server from Jenkins Agent Instance to Deploy Spring boot bank App + +To add a new node in Jenkins: + +1. **Log in to Jenkins**. + +2. Go to **Manage Jenkins > Manage Nodes and Clouds**. + +3. Click **New Node**: + + * **Node name**: Enter a name for the Jenkins Agent (e.g., `dev-server`). + + * Choose **Permanent Agent** and click **OK**. + +4. **Configure Node Settings**: + + * **Remote root directory**: `/home/ubuntu/bankapp` + + * **Labels**: Add `dev-server` + + * **Usage**: Choose **Only build jobs with label expressions matching this node**. + +5. Under **Launch method**, select **Launch agents via SSH**: + + * **Host**: Enter the private IP of your Jenkins Agent instance. + + * **Credentials**: Add credentials by selecting **SSH Username with private key**. + + * Use **ubuntu** for the username. + + * Add the private key associated with the key pair used for the Jenkins Agent EC2 instance. + + * Click **Save** and connect to the Jenkins Agent. + + +--- + +### Step 10: Set Up Docker Hub Credentials in Jenkins + +1. Go to **Manage Jenkins > Security > Credentials > System > Global credentials (unrestricted)** and click **Add Credentials**. + +2. Set **Kind** to **Username with password**. + +3. Add your Docker Hub username and password and save , for password generate Personal Access Token on DockerHub. + + +--- + +### Step 11: Create a Jenkins Pipeline Job + +1. **Create a New Job**: + + * From the Jenkins dashboard, click on **New Item**. + + * Enter a name (e.g., `Springboot bank CI/CD`), select **Pipeline**, and click **OK**. + +2. **Configure GitHub Integration**: + + * In the **General** section, check the **GitHub project** option. + + * Provide the URL of your GitHub repository. + +3. **Pipeline**: + + * Under **Pipeline**, select **Pipeline script from SCM**. + + * Set **SCM** to **Git** and provide the Git repository URL. + + * Choose the `dev` branch and set **Script Path** to `Jenkinsfile`. + + +--- + +### Step 12: Create the Jenkinsfile on GitHub + +In the GitHub repository, create a `Jenkinsfile` containing the pipeline script: + +```yaml +@Library('Shared')_ + +pipeline{ + agent {label 'dev-server'} + + stages{ + stage("Code"){ + steps{ + clone("https://github.com/Amitabh-DevOps/banking-app-project.git","dev") + echo "Code clonning done." + } + } + stage("Build"){ + steps{ + dockerbuild("bankapp-mini","latest") + echo "Code build bhi hogaya." + } + } + stage("Push to DockerHub"){ + steps{ + dockerpush("dockerHub","bankapp-mini","latest") + echo "Push to dockerHub is also done." + } + } + stage("Deplying"){ + steps{ + deploy() + echo "Deployment bhi done." + } + } + } +} ``` +This script pulls the code from GitHub, builds and pushes a Docker image to Docker Hub, and deploys it on the Jenkins Agent instance. + +* This script includes multiple stages: cloning the code from GitHub, building the Docker image, pushing it to Docker Hub, and deploying the container. + +* This script allows to used shared library repo which is stored on my github profile + +* **used shared library repo** : [https://github.com/Amitabh-DevOps/Jenkins-shared-libraries](https://github.com/Amitabh-DevOps/Jenkins-shared-libraries) + +* **Commit the Changes**: + + * Save and commit the `Jenkinsfile` to your GitHub repository. + + --- -## Detailed Guide +### Step 13. Set Up Webhooks for Automatic Deployment + +To trigger the Jenkins pipeline automatically on code changes, set up webhooks in your GitHub repository. -For a comprehensive walkthrough of the setup and deployment, refer to my blog post: [Deploying Multi-Tier Application with Docker](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker) +1. **Go to GitHub Repository Settings**: + + * Navigate to your GitHub repository, and click on **Settings**. + +2. **Set Up Webhooks**: + + * In the left sidebar, click on **Webhooks** and then **Add webhook**. + + * Enter the **Payload URL**: + + + ```http + http://:8080/github-webhook/ + ``` + + * Set **Content type** to default one and enable **Just the push event**. + + * Click on **Add webhook** and wait for it to show a green tick, indicating successful setup. + + -## Implementation Notes +--- + +### Step 14. Build the Project in Jenkins -For detailed implementation notes with images, visit my Notion page: [Notion Notes](https://www.notion.so/Docker-bankapp-project-12c7311ab980801a929ad23bf654b64d) +1. **Trigger the First Build**: + + * Go back to the Jenkins dashboard and click on the **Build Now** button for your pipeline job. + + * This action will initiate the pipeline and deploy your Spring boot application. + +2. **Access the Application**: + + * To allow incoming traffic to your application, go to your EC2 security group and add an inbound rule for **port 8000**. + + * After the build completes successfully, visit your deployed Spring boot application at: + + + ```http + http://:8000 + ``` + +--- -## Acknowledgments +### Step 15. Automatic Deployment -Special thanks to **Shubham Londhe** for his guidance and support throughout this project! +From this point on, any changes you make and push to the GitHub repository will automatically trigger Jenkins to run the pipeline, rebuild the Docker image, and redeploy the application. This completes the CI/CD setup for your Springboot bank Application. --- + +## Conclusion + +By following these steps, you have successfully set up a **CI/CD pipeline** to automate the deployment of your Springboot bank Application using **Jenkins**, **GitHub**, and **Docker, shared libraries, multinode agent etc**. This setup not only simplifies the deployment process but also enhances productivity by ensuring that every code change is automatically tested and deployed. + From b8ad79c9006f2b6a087ab0df98d69efcbf221e7f Mon Sep 17 00:00:00 2001 From: Amitabh-DevOps Date: Thu, 14 Nov 2024 21:49:15 +0530 Subject: [PATCH 17/17] Added README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 34d22844..d8bd99ce 100644 --- a/README.md +++ b/README.md @@ -368,4 +368,3 @@ From this point on, any changes you make and push to the GitHub repository will ## Conclusion By following these steps, you have successfully set up a **CI/CD pipeline** to automate the deployment of your Springboot bank Application using **Jenkins**, **GitHub**, and **Docker, shared libraries, multinode agent etc**. This setup not only simplifies the deployment process but also enhances productivity by ensuring that every code change is automatically tested and deployed. -