Example of a K8s operator using Spring Boot v3 and the Official Kubernetes Java client.
-
2 projects with https://start.spring.io.
-
default-spring-boot-app: normal web app used as example of Spring Boot app (with actuator, web)
-
spring-deployment-operator: the actual focus of this repo, the K8s native operator.
-
-
1
scripts
directory with utilities. -
1
manifests
directory for Kubernetes configuration.
The example crd found in spring-operator-demo/manifests/crds/spring-deployment-crd.yaml defines a new K8s resource to manage Spring Boot deployments.
To generate the Model classes to be used in our operator run ./scripts/generate_model.sh
.
Create KinD cluster.
kind create cluster --config kind-config.yaml
Run build and deploy script
./scripts/build_and_deploy.sh
Cannot be run as app until permissions are correctly set. Locally api calls work, but not the controller in k8s
So long you have .kube/config
, the kubernetes client will connect.
kind create cluster --config kind-config.yaml
Start OperatorApplication
from IntelliJ
./spring-deployment-operator/gradlew -p spring-deployment-operator bootBuildImage kind load docker-image spring-deployment-operator:0.0.1-SNAPSHOT
After running build_and_deploy.sh
you should see the operator pod running in spring-deploy-operator
with:
kubectl get pods -n spring-deploy-operator
Apply the example provided.
kubectl apply -f manifests/examples/spring-deployment.yaml
Then, list the pods again to see the new deployment and 2 pods associated with it.
> kubectl get deploy -n spring-deploy-operator NAME READY UP-TO-DATE AVAILABLE AGE hello-madrid 2/2 2 2 3m3s spring-deployment-operator 1/1 1 1 43m
> kubectl get pods -n spring-deploy-operator NAME READY STATUS RESTARTS AGE hello-madrid-6d4f4dc5c6-fwd7l 1/1 Running 0 2m16s hello-madrid-6d4f4dc5c6-v4sck 1/1 Running 0 2m16s spring-deployment-operator-6b6cb9655b-m8zjl 1/1 Running 0 42m
As a bonus, since we installed metrics server, we can monitor the resources to see the difference between the normal deployments and a native one.
Note
|
Numbers may vary. |
> kubectl top pods NAME CPU(cores) MEMORY(bytes) hello-madrid-6d4f4dc5c6-fwd7l 9m 158Mi hello-madrid-6d4f4dc5c6-v4sck 6m 155Mi spring-deployment-operator-6b6cb9655b-m8zjl 1m 44Mi
- Extend the Kubernetes API with CustomResourceDefinitions
-
https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/