SpringBoot Kubernetes Tutorial Series

Goal

  1. Build REST API using SpringBoot
  2. Build UI using ReactJS
  3. Run app locally using docker-compose
  4. Deploy app on Kubernetes (kind) Cluster

What we are going to learn?

Backend

  • Build REST API using Java 17, SpringBoot, Postgresql, Maven
  • Spring Data JPA
  • Flyway DB Migration
  • Swagger Documentation
  • Integration Testing using Testcontainers
  • GitHub Actions CI/CD

Frontend

  • Build UI using ReactJS/NextJS
  • Bootstrap CSS
  • Axios for API communication

Docker and Kubernetes

  • Dockerizing the application using Buildpacks and Jib
  • Dev env setup using docker-compose
  • Kubernetes - Pods, ReplicaSet, Deployment, Service, Ingress

Deployment

  • Setting up Kubernetes (kind) Cluster
  • Deploy app on Kubernetes
  • Using Lens as K8s GUI

Approach

  1. Build REST API using SpringBoot
  2. Build UI using NextJS
  3. Deploy app on Kubernetes (kind) Cluster

Software/Tools Setup

Project Setup

Get Bookmarks API with Pagination

  1. Create JPA Entity for Bookmark
  2. Create Spring Data JPA Repository
  3. Create BookmarkService
  4. Create Get Bookmarks API endpoint
  5. Add "pagination" support

Database Migrations

  1. Flyway
  2. Liquibase

Spring Data Jpa Projections

  1. Load ONLY desired data(columns)
  2. Optimized for read-only scenarios
  3. Interface-based Projections
  4. Class-based Projections

How to create Docker Image for SpringBoot app?

  1. Dockerfile using fat-jar
  2. Multistage Dockerfile with layers
  3. SpringBoot Maven/Gradle Plugin using Buildpacks
  4. Jib Maven/Gradle Plugin

Continuous Integration

  1. Run Tests
  2. Code Quality Checks(SonarQube, CheckStyle etc)
  3. Deploy to Dev/QA Env
  4. Run E2E Tests(Selenium, Cypress etc)
  5. Deploy to Performance Test Env
  6. Run Performance Tests using Gatling/JMeter
  7. Deploy to Production
  8. Run Smoke/Sanity Tests

REST API Guidelines

                
                GET     /api/bookmarks                 <- get all
                GET     /api/bookmarks/{id}            <- get by id
                GET     /api/bookmarks?query=k&page=2  <- search
                POST    /api/bookmarks                 <- create
                PUT     /api/bookmarks/{id}            <- replace by id
                PATCH   /api/bookmarks/{id}            <- partial update by id
                DELETE  /api/bookmarks/{id}            <- delete by id
                
            

Create Bookmark API

                
                POST    /api/bookmarks

                Request Payload:
                {
                   "title": "SivaLabs Blog",
                   "url": "https://sivalabs.in"
                }

                Response StatusCode: 201
                Payload:
                {
                   "id": 1,
                   "title": "SivaLabs Blog",
                   "url": "https://sivalabs.in"
                }
                
            

Better Developer Experience

                
                    $ git clone https://github.com/sivaprasadreddy/myapp.git
                    $ cd myapp
                    $ docker-compose up -d
                    $ docker-compose logs -f
                
            

One Step further

                    
                        $ cd myapp
                        $ ./run.sh start
                        $ ./run.sh stop
                    
                

What is Docker?

Docker is a containerization technology to package and distribute applications.

What problems Docker trying to solve?

The infamous
"It works on my machine"
problem

How to build a Docker Image?

Example Dockerfile

                    
                        FROM eclipse-temurin:17-jre-focal
                        COPY target/myapp-1.0.0.jar /apps/myapp.jar
                        EXPOSE 8080
                        ENTRYPOINT ["java","-jar","/apps/myapp.jar"]
                    
                

Docker Image and Container

If Docker Image is a "class" then "container" is an instance of that class.
                    
                        # Create Docker Image
                        $ docker build -t username/myapp .

                        # Create 1st container
                        $ docker run --name app1 -p 8081:8080 -d username/myapp

                        # Create 2nd container
                        $ docker run --name app2 -p 8082:8080 -d username/myapp

                    
                

Docker Volumes

                    
                        # Creating volume and mount it to container
                        docker volume create my-vol
                        docker run -d -v my-vol:/app nginx:latest

                        # Bind a host file system path to container
                        docker run -d -v /some/host/dir/path:/app nginx:latest
                    
                

Docker Image Registry

  • DockerHub
  • Quay.io
  • Amazon ECR (Elastic Container Registry)
  • Google Container Registry
  • Azure Container Registry

Docker Compose

Run multiple Docker containers

                    
                        version: "3.9"
                        services:
                          myapp:
                            image: my_web_app:latest
                            depends_on:
                              - db
                              - cache
                          db:
                            image: postgres:latest

                          cache:
                            image: redis:latest
                    
                

What is Kubernetes?

Kubernetes is a container orchestration platform.

Kubernetes Architecture

Kubernetes Key Object Types

  • Pods
  • ReplicaSets
  • Deployments
  • Services
  • ConfigMaps
  • Secrets
  • PersistentVolumes
  • PersistentVolumeClaims
  • Ingress Controller

Kubernetes Cluster Installation

  • Cloud (AWS, GCP, Azure, Digital Ocean etc)
  • Local Development (KinD, Minikube, K3s etc)

KinD Cluster Installation

Reference: Kind Docs

Lens (Kubernetes GUI Tool) Installation

Reference: K8S Lens