Demo presents how to deploy a Spring Boot application in the local Kubernetes cluster.
- Java 17 (e.g. SAP Machine)
- Docker
- Kubectl
- k3d
- Launch Docker deamon
- Create a local registry
k3d registry create registry.localhost --port 5000
- Build the Counter service and push it to the local registry
.\counter-service-java\gradlew dockerBuildImage -p counter-service-java
docker tag com.github.agabrys/counter-service:latest localhost:5000/com.github.agabrys/counter-service:latest
docker push localhost:5000/com.github.agabrys/counter-service:latest
- Build the Kubernetes controller and push it to the local registry
.\kubernetes-controller-java\gradlew dockerBuildImage -p kubernetes-controller-java
docker tag com.github.agabrys/kubernetes-controller:latest localhost:5000/com.github.agabrys/kubernetes-controller:latest
docker push localhost:5000/com.github.agabrys/kubernetes-controller:latest
- Create a local Kubernetes cluster with access to the local registry
k3d cluster create -p "9080:80@loadbalancer" k8s-learn --registry-use k3d-registry.localhost:5000
- Configure Kubernetes configuration
kubectl apply -f ./counter-service.yaml
The Counter service is configured to returns a number of deployments with thekubectl apply -f ./kubernetes-controller.yaml
app=nginx
label
The cluster needs a few seconds (30-60) to provision all pods, so please wait a moment before you start going through the demo steps.
Use the following steps to verify that the integration between the Kubernetes controller and the Counter service works properly.
- Open the Counter service endpoints:
http://localhost:9080/counter/value
. It should return0
- Add the
nginx1
deployment (withapp=nginx
)The Counter service should returnkubectl apply -f ./test-1.yaml
1
- Add the
none-nginx
deployment (withoutapp=nginx
)The Counter service should returnkubectl apply -f ./test-2.yaml
1
- Add the
nginx2
andnginx3
deployments (withapp=nginx
)The Counter service should returnkubectl apply -f ./test-3.yaml
3
- Remove the
nginx2
deployementThe Counter service should returnkubectl delete deployment nginx2
2
- Remove the
none-nginx
deploymentThe Counter service should returnkubectl delete deployment none-nginx
2
The project has been created in two days and almost all used technologies were new to me. I see a lot of fields to improve, but I didn't have enough time to work on it. Some examples:
- create a K8s namespace and use it instead of
default
- use
fieldSelector
to gather less data whendeployments
are requested - add tests to the K8s controller written by using kubernetes-client/java
- use more precise resource limits
The git commits are not descriptive. The main goal was to learn K8s, so I didn't focus on them too much (especially that nobody will read them 😉). Some examples of git messages which I usually write: