Deploying Spring Boot and Quarkus Microservices on Kubernetes

Deploying Spring Boot and Quarkus Microservices on Kubernetes

1. Introduction

This document provides a step-by-step guide for deploying Spring Boot and Quarkus microservices on a Kubernetes cluster. It aims to demonstrate how to build cloud-native applications using these frameworks and manage them effectively using Kubernetes.

2. Prerequisites

Before you start, ensure you have the following tools and components installed:

  • Minikube: A local Kubernetes cluster.

  • Docker: For building and running container images.

  • kubectl: Command-line tool for interacting with Kubernetes.

  • Java Development Environment: Ensure you have Java and Maven (or Gradle) installed.

3. Setting Up the Kubernetes Cluster

  1. Start Minikube:

     minikube start
    
  2. Verify Minikube is Running:

     kubectl get nodes
    

4. Building and Containerizing Microservices

4.1 Spring Boot Microservice

  1. Create a Spring Boot Application:

    • Use Spring Initializr to set up a basic application with the following dependency:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      
  2. Add REST Controller:

     @RestController
     public class HelloController {
         @GetMapping("/hello")
         public String hello() {
             return "Hello from Spring Boot!";
         }
     }
    
  3. Create Dockerfile:

     FROM openjdk:11-jre-slim
     COPY target/spring-boot-app.jar /app.jar
     ENTRYPOINT ["java", "-jar", "/app.jar"]
    
  4. Build and Containerize the Application:

     mvn clean package
     docker build -t spring-boot-app .
    
  5. Push the Docker Image:

     docker tag spring-boot-app <registry>/spring-boot-app
     docker push <registry>/spring-boot-app
    

4.2 Quarkus Microservice

  1. Create a Quarkus Application:

    • Add the following dependency to pom.xml:

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy</artifactId>
        </dependency>
      
  2. Define Quarkus Resource:

     @Path("/hello")
     public class HelloResource {
         @GET
         public String hello() {
             return "Hello from Quarkus!";
         }
     }
    
  3. Create Dockerfile:

     FROM registry.access.redhat.com/ubi8/openjdk-11
     COPY target/quarkus-app/lib/ /deployments/lib/
     COPY target/quarkus-app/*.jar /deployments/
     COPY target/quarkus-app/app/ /deployments/app/
     COPY target/quarkus-app/quarkus/ /deployments/quarkus/
     ENTRYPOINT ["java", "-jar", "/deployments/quarkus-run.jar"]
    
  4. Build and Containerize Quarkus Application:

     mvn clean package
     docker build -t quarkus-app .
    
  5. Push the Docker Image:

     docker tag quarkus-app <registry>/quarkus-app
     docker push <registry>/quarkus-app
    

5. Deploying to Kubernetes

5.1 Deploying Spring Boot Microservice

  1. Create spring-boot-deployment.yaml:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: spring-boot-app
     spec:
       replicas: 2
       selector:
         matchLabels:
           app: spring-boot-app
       template:
         metadata:
           labels:
             app: spring-boot-app
         spec:
           containers:
           - name: spring-boot-app
             image: <registry>/spring-boot-app
             ports:
             - containerPort: 8080
     ---
     apiVersion: v1
     kind: Service
     metadata:
       name: spring-boot-service
     spec:
       selector:
         app: spring-boot-app
       ports:
       - protocol: TCP
         port: 80
         targetPort: 8080
       type: LoadBalancer
    
  2. Apply the Configuration:

     kubectl apply -f spring-boot-deployment.yaml
    

5.2 Deploying Quarkus Microservice

  1. Create quarkus-deployment.yaml:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: quarkus-app
     spec:
       replicas: 2
       selector:
         matchLabels:
           app: quarkus-app
       template:
         metadata:
           labels:
             app: quarkus-app
         spec:
           containers:
           - name: quarkus-app
             image: <registry>/quarkus-app
             ports:
             - containerPort: 8080
     ---
     apiVersion: v1
     kind: Service
     metadata:
       name: quarkus-service
     spec:
       selector:
         app: quarkus-app
       ports:
       - protocol: TCP
         port: 80
         targetPort: 8080
       type: LoadBalancer
    
  2. Apply the Configuration:

     kubectl apply -f quarkus-deployment.yaml
    

6. Accessing the Microservices

To access the deployed microservices, use the following commands:

minikube service spring-boot-service --url
minikube service quarkus-service --url

7. Managing and Scaling Microservices

  1. Horizontal Pod Autoscaling (HPA):

    To enable HPA for your deployments:

     kubectl autoscale deployment spring-boot-app --cpu-percent=50 --min=2 --max=10
     kubectl autoscale deployment quarkus-app --cpu-percent=50 --min=2 --max=10
    
  2. Service Discovery: Services can be accessed internally using DNS, e.g., spring-boot-service.default.svc.cluster.local.

8. Additional Considerations

  1. Logging and Monitoring: Integrate tools like Prometheus and Grafana for monitoring, or use the ELK stack for logging.

  2. Security: Use Role-Based Access Control (RBAC) and Network Policies to secure communication between services.

  3. Traffic Management: Consider implementing Istio for advanced traffic management capabilities.

9. Conclusion

This guide provides a comprehensive overview of deploying Spring Boot and Quarkus microservices on Kubernetes using Minikube. With this setup, you can build scalable and resilient applications while taking advantage of cloud-native architectures.


This documentation can be used as a reference for developers looking to implement microservices using Spring Boot and Quarkus on Kubernetes. Let me know if you need further modifications or additional details!