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.
- Installation instructions: Minikube Start
Docker: For building and running container images.
- Installation instructions: Docker Installation
kubectl: Command-line tool for interacting with Kubernetes.
- Installation instructions: kubectl Installation
Java Development Environment: Ensure you have Java and Maven (or Gradle) installed.
Java: Install Java
Maven: Install Maven
3. Setting Up the Kubernetes Cluster
Start Minikube:
minikube start
Verify Minikube is Running:
kubectl get nodes
4. Building and Containerizing Microservices
4.1 Spring Boot Microservice
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>
Add REST Controller:
@RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello from Spring Boot!"; } }
Create Dockerfile:
FROM openjdk:11-jre-slim COPY target/spring-boot-app.jar /app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
Build and Containerize the Application:
mvn clean package docker build -t spring-boot-app .
Push the Docker Image:
docker tag spring-boot-app <registry>/spring-boot-app docker push <registry>/spring-boot-app
4.2 Quarkus Microservice
Create a Quarkus Application:
Add the following dependency to
pom.xml
:<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> </dependency>
Define Quarkus Resource:
@Path("/hello") public class HelloResource { @GET public String hello() { return "Hello from Quarkus!"; } }
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"]
Build and Containerize Quarkus Application:
mvn clean package docker build -t quarkus-app .
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
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
Apply the Configuration:
kubectl apply -f spring-boot-deployment.yaml
5.2 Deploying Quarkus Microservice
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
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
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
Service Discovery: Services can be accessed internally using DNS, e.g.,
spring-boot-service.default.svc.cluster.local
.
8. Additional Considerations
Logging and Monitoring: Integrate tools like Prometheus and Grafana for monitoring, or use the ELK stack for logging.
Security: Use Role-Based Access Control (RBAC) and Network Policies to secure communication between services.
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!