This is a sample app that shows how to use a Kubernetes config map as a property source in a Spring Boot app.
Upon startup, the app prints out the value of the foo
configuration property.
|
The critical portion for this to work is settings spring.config.import=kubernetes: which must be escaped when specified in application.yml as spring.config.import: "kubernetes:"
|
kubectl apply -f configmap.yaml kubectl apply -f clusterrole.yaml kubectl apply -f deployment.yaml
Make the http 8080
port available over localhost
w/ the following command:
kubectl port-forward svc/demo 8080:8080
In another terminal, invoke the actuator health endpoint by executing:
curl localhost:8080/actuator/health
which results in:
{"status":"UP","groups":["liveness","readiness"]}
View the app logs w/ k9s
or via kubectl
such as:
kc get pods kc logs pods/<enter-pod-name-from-previous-command>
You should see something like the following (notice the value of the foo
var):
2023-12-24T04:06:40.338Z INFO 1 --- [demo] [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' 2023-12-24T04:06:40.727Z INFO 1 --- [demo] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '' 2023-12-24T04:06:40.853Z INFO 1 --- [demo] [ main] com.example.demo.DemoApplication : Started DemoApplication in 27.859 seconds (process running for 30.721) ****** FOO = bar 2023-12-24T04:06:46.353Z INFO 1 --- [demo] [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2023-12-24T04:06:46.357Z INFO 1 --- [demo] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2023-12-24T04:06:46.364Z INFO 1 --- [demo] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms
kubectl delete -f deployment.yaml kubectl delete -f clusterrole.yaml kubectl delete -f configmap.yaml
Make the jdwp 5005
port available over localhost
w/ the following command:
kubectl port-forward svc/demo 5005:5005
Then attach a remote debugger to port localhost:5005
๐ก
|
If you want to debug the startup of the app you will need to edit deployment.yaml and set suspend=y in the JAVA_OPTS env var and then re-apply the resources.
|