This project demonstrates deploying Spring Boot native applications as serverless functions on Kubernetes, leveraging Spring Boot native capabilities and Knative for efficiency, scalability, and resilience.
- Compilation of HelloWorldSpringBoot to Native Executable
- Installation of Knative Using Quickstart
- Setup of Knative Cluster with setup-knative.sh
To compile the HelloWorldSpringBoot application to a native executable using Spring Native, follow these steps:
-
Ensure you have GraalVM installed:
sudo apt-get install graalvm-ce-java17
-
Ensure you have the necessary dependencies in your
pom.xmlfile:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
-
Add the GraalVM native plugin to your
pom.xmlfile:<plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <version>0.11.2</version> <executions> <execution> <goals> <goal>compile-no-fork</goal> </goals> </execution> </executions> <configuration> <buildArgs> <!-- Optional: Add GraalVM native-image flags --> <buildArg>--no-fallback</buildArg> <buildArg>--enable-url-protocols=http,https</buildArg> <buildArg>--report-unsupported-elements-at-runtime</buildArg> </buildArgs> </configuration> </plugin>
-
Build the native executable:
mvn -Pnative native:compile
-
Run the native executable:
./target/hello-world-spring-boot
To install Knative using the quickstart method, follow the official Knative documentation:
Knative Quickstart Installation Guide
To set up the Knative cluster using the setup-knative.sh script, follow these steps:
-
Ensure you have the necessary permissions and tools installed:
kubectlkind(Kubernetes IN Docker)
-
Run the setup script:
./setup-knative.sh
This script will configure your Kubernetes cluster with Knative and deploy the necessary components.
To perform load testing on the deployed application, follow these steps:
-
Install wrk:
sudo apt-get install wrk
-
Run the load test:
wrk -t4 -c400 -d10s --latency -H "Host: hello-native-local.default.127.0.0.1.sslip.io" http://localhost:30641/helloThis command runs a load test using
wrkwith the following parameters:-t4: Use 4 threads.-c400: Use 400 concurrent connections.-d10s: Run the test for 10 seconds.--latency: Display latency statistics.-H "Host: hello-native-local.default.127.0.0.1.sslip.io": Set theHostheader to simulate a request to the specified host.http://localhost:30641/hello: The URL to test.
-
Observe the number of pods:
kubectl get pods -n default --no-headers | wc -lThis command retrieves the number of pods running in the
defaultnamespace and counts them.