Building Cloud-Native Applications with Quarkus and Kubernetes
In today’s fast-paced digital landscape, the demand for cloud-native applications is at an all-time high. As organizations strive to enhance scalability, flexibility, and deployment speed, frameworks like Quarkus have emerged as essential tools for building microservices. Coupled with Kubernetes, a powerful orchestration platform, developers can deploy, manage, and scale applications seamlessly. In this article, we will walk through the process of deploying a Quarkus microservice on Kubernetes, covering containerization with Docker or Podman and essential scaling considerations.
What is Quarkus?
Check my last articles on Quarkus topic:
Quarkus: The Supersonic, Subatomic Revolution in Java
A Beginner’s Guide to Quarkus: Build Your First Java Microservice/API
What is Kubernetes?
Kubernetes is an open-source platform that automates the deployment, scaling, and management of containerized applications. With Kubernetes, developers can focus on writing code while the platform handles the orchestration of their applications, ensuring high availability and efficient resource management.
Setting Up Your Environment
Before we begin, ensure you have the following prerequisites installed:
- Java JDK 11 or higher: Required for building Quarkus applications.
- Maven: The build tool used for managing Quarkus projects.
- Docker or Podman: For containerization of your Quarkus application.
- Kubernetes CLI (kubectl): To interact with your Kubernetes cluster.
- Kubernetes Cluster: You can use a local cluster like Minikube or a managed service like Google Kubernetes Engine (GKE) or Amazon EKS.
Step 1: Creating a Quarkus Microservice
- Generate a Quarkus Project: Use the Quarkus CLI or the online project generator to create a new project. For instance, you can use the command line:
mvn io.quarkus:quarkus-maven-plugin:2.11.1:create \
-DprojectGroupId=com.example \
-DprojectArtifactId=hello-quarkus \
-DclassName="com.example.GreetingResource" \
-Dpath="/hello"
This command creates a new Quarkus project with a REST endpoint at /hello
.
2. Implement Your Service: Navigate to src/main/java/com/example/GreetingResource.java
and modify the class to look like this:
package com.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello, Quarkus!";
}
}
3. Run Your Application Locally: To test your microservice locally, navigate to the project directory and run:
./mvnw compile quarkus:dev
You can access the service at http://localhost:8080/hello
Step 2: Containerizing the Quarkus Application
Now that your Quarkus microservice is up and running locally, it’s time to containerize it.
- Create a Dockerfile: In the root of your project, create a file named
Dockerfile.
FROM registry.access.redhat.com/ubi8/openjdk-11
COPY target/*-runner /deployments/application
ENTRYPOINT ["/deployments/application", "-Dquarkus.http.host=0.0.0.0"]
2. Build the Container Image: First, package your application using Maven:
./mvnw package
Then, build the Docker image:
docker build -f Dockerfile -t hello-quarkus .
If you are using Podman, the command is the same.
3. Verify the Image: You can list your Docker images to verify that the image has been created successfully:
docker images
Step 3: Deploying to Kubernetes
- Create a Kubernetes Deployment: Now that you have your Docker image, you need to create a Kubernetes deployment. Create a file named
hello-quarkus-deployment.yaml
with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-quarkus
spec:
replicas: 2
selector:
matchLabels:
app: hello-quarkus
template:
metadata:
labels:
app: hello-quarkus
spec:
containers:
- name: hello-quarkus
image: hello-quarkus:latest
ports:
- containerPort: 8080
2. Create a Kubernetes Service: To expose your application, create a service definition in the same file or a new file named hello-quarkus-service.yaml
:
apiVersion: v1
kind: Service
metadata:
name: hello-quarkus
spec:
selector:
app: hello-quarkus
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
3. Apply the Configuration: Use kubectl
to deploy your application to Kubernetes:
kubectl apply -f hello-quarkus-deployment.yaml
kubectl apply -f hello-quarkus-service.yaml
4. Check the Deployment: You can check the status of your deployment by running:
kubectl get deployments
kubectl get pods
kubectl get services
Step 4: Accessing the Service
To access your Quarkus service running on Kubernetes, you’ll need to determine the external IP assigned to your service. If you’re using Minikube, you can access it using:
minikube service hello-quarkus --url
This command provides you with the URL to access your application. If you’re using a cloud provider, it might take a few moments for the external IP to be allocated.
Scaling Considerations
One of the major benefits of deploying applications on Kubernetes is the ability to scale your services easily. Quarkus applications are lightweight, making them perfect candidates for horizontal scaling.
Scaling the Deployment
You can scale your Quarkus microservice deployment by updating the replicas
field in your deployment YAML or by using the following command:
kubectl scale deployment hello-quarkus --replicas=5
Auto-scaling
Kubernetes also supports Horizontal Pod Autoscaling (HPA), which can automatically adjust the number of replicas based on CPU utilization or other select metrics. Here’s a basic example of creating an HPA:
kubectl autoscale deployment hello-quarkus --cpu-percent=50 --min=1 --max=10
This command will automatically scale your deployment between 1 and 10 replicas based on CPU usage.
Conclusion
Building cloud-native applications with Quarkus and deploying them on Kubernetes is a powerful approach to modern software development. With its lightweight nature and Kubernetes-native features, Quarkus allows developers to build scalable and efficient microservices.
In this article, we explored how to create a simple Quarkus microservice, containerize it with Docker, and deploy it on a Kubernetes cluster. By understanding the deployment process and scaling considerations, you can leverage the power of Quarkus and Kubernetes to build robust applications that can thrive in a cloud-native environment.
Next Steps
- Experiment with different Quarkus extensions to enhance your application.
- Explore more Kubernetes features like persistent storage, secrets management, and CI/CD integrations.
- Consider integrating monitoring tools like Prometheus and Grafana to observe your application’s performance in real-time.
With Quarkus and Kubernetes, you are well-equipped to tackle the challenges of building and deploying modern cloud-native applications.
Happy coding!