Deploying Spring Boot Applications on Kubernetes with Spring Boot Operator
This article explains how to package a Spring Boot application into a Docker image using Jib, install the Spring Boot Operator, and manage the application lifecycle on Kubernetes—including deployment, scaling, graceful shutdown, node affinity, and custom operator configuration—through detailed code examples and YAML manifests.
Using Spring Boot Operator to deploy Spring Boot applications to Kubernetes.
Introduction
Deploying Spring Boot apps on Kubernetes can be cumbersome; the Spring Boot Operator simplifies the process by extending the Kubernetes API with Custom Resource Definitions (CRDs).
Packaging Docker Image
Before deployment, the Spring Boot app must be packaged into a Docker image. The article introduces Google’s open‑source Jib plugin for Maven/Gradle, noting that Jib‑built images run the Java process with pid = 1, which the operator resolves by enabling the ShareProcessNamespace flag.
Example command to build the image with Jib:
mvn com.google.cloud.tools:jib-maven-plugin:build \
-Djib.to.auth.username=${{ secrets.MY_USERNAME }} \
-Djib.to.auth.password=${{ secrets.MY_PASSWORD }} \
-Djib.container.jvmFlags=--add-opens,java.base/sun.nio.ch=ALL-UNNAMED \
-Djib.from.image=freemanliu/oprenjre:11.0.5 \
-Dimage=registry.cn-shanghai.aliyuncs.com/qingmuio/operator-demo/operator-demo:v1.0.0Operator Quick Start
After building the image, install the operator in a Kubernetes cluster (a three‑node cluster is assumed). The quick‑install command is:
kubectl apply -f https://raw.githubusercontent.com/goudai/spring-boot-operator/master/manifests/deployment.yamlThe console output shows the creation of namespaces, CRDs, RBAC resources, services, and the operator deployment.
Verify the installation with:
kubectl get po -n spring-boot-operator-systemDeploying the Demo Application
Create a CRD yaml (Demo.yaml) that defines a SpringBootApplication resource with name and version. Apply it:
kubectl apply -f Demo.yamlThe operator creates the corresponding pods and service. The article demonstrates checking pod status, verifying the ShareProcessNamespace setting, and accessing the service via curl.
Scaling and Cleanup
To scale replicas, add a replicas field to Demo.yaml and re‑apply. Deleting the application is done with:
kubectl delete -f Demo.yamlDeploying a Private Image
Create a Docker‑registry secret, then reference it in the CRD’s imagePullSecrets field. Provide a full example yaml with image, replicas, resources, health check paths, and node affinity.
Graceful Shutdown
Enable the Spring Boot shutdown endpoint in application.yml and expose it via a custom GET controller.
@RestController
public class ShutdownController {
@Autowired
private ShutdownEndpoint shutdownEndpoint;
@GetMapping("/spring/shutdown")
public Map
shutdown(HttpServletRequest request) {
return shutdownEndpoint.shutdown();
}
}Node Affinity Example
Label nodes with zones (cn‑i, cn‑h, cn‑g) and configure the CRD’s nodeAffinity to distribute pods across zones.
Custom Operator Installation
Modify the operator Deployment (spring‑boot‑operator‑controller‑manager) to set environment variables such as IMAGE_REPOSITORY, resource requests, health check paths, replica count, and node‑affinity parameters.
Appendix
Provides a table of environment variables used by the operator.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.