How can I monitor CPU and memory usage of Kubernetes pods from a Spring Boot service?
Asked by
quietly curious
I'm new to the DevOps field and looking for some guidance on setting up a proof of concept. I need to monitor the active pods for two Spring Boot services (let's call them S1 and S2) in my Kubernetes cluster and retrieve metrics like CPU and memory usage for each pod. The goal is to create a third Spring Boot microservice (S3) that can fetch this data. Is it possible to run this S3 service locally on my machine to make debugging easier, even though the other services are in the cluster? Eventually, I plan to deploy S3 in the same Kubernetes namespace. This data is crucial as it will inform various follow-up tasks based on the metrics collected. Right now, I'm running Kubernetes locally using Docker and kubeadm.
Answers
For a more integrated solution, consider using the Java client library to interact with the Kubernetes API. Don't forget to install the metrics-server to get accurate data! Hereโs a link to a [Java example](https://github.com/kubernetes-client/java/blob/master/examples/examples-release-latest/src/main/java/io/kubernetes/client/examples/MetricsExample.java) that might help you get started.
Reply to this answer
You can use `kubectl top pods` to check the CPU and memory usage directly. To see only the active pods, run `kubectl get pods` with the appropriate filters or use the replica set values. Running some scripts locally to fetch this data is a solid first step!
Reply to this answer
Have you thought about using Grafana and Prometheus for your monitoring? They can provide valuable insights and are pretty established in the community.
Reply to this answer
Add your answer
Answers are reviewed before publication.
quietly curious: Thanks for the info! Can I still fetch this data if my Spring Boot service (S3) isnโt running inside the cluster, but on my local machine?