This is example of using a Spring Boot application with Kubernetes, Prometheus, and Grafana.
Prometheus is an open source monitoring and alerting system. It's primarliy used for collecting and storing metrics. It also provides a graphical interface and a query language (PromQL) for querying metrics.
Prometheus uses a pull model for collecting metrics.
In this example, Spotify's Dockerfile Maven Plugin is used for creating the Docker image. Spotify reccomends you to use this plugin instead of Docker Maven plugin.
Here is the change to this project's pom.xml
.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>basaki/spring-k8s-example</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
To build the docker image, execute mvn clean install
command from the parent directory:
$ mvn clean install
...
[INFO] ---> Using cache
[INFO] ---> f5d60c9391eb
[INFO] Step 8/8 : ENTRYPOINT ["java", "-Dapp.port=${app.port}", "-jar","/app.jar"]
[INFO]
[INFO] ---> Using cache
[INFO] ---> 3e84ee5ba1cd
[INFO] [Warning] One or more build-args [JAR_FILE] were not consumed
[INFO] Successfully built 3e84ee5ba1cd
[INFO] Successfully tagged basaki/spring-k8s-example:1.0.0
[INFO]
[INFO] Detected build of image with id 3e84ee5ba1cd
[INFO] Successfully built basaki/spring-k8s-example:1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 52.519 s
[INFO] Finished at: 2018-10-21T19:55:36-07:00
[INFO] Final Memory: 67M/525M
[INFO] ------------------------------------------------------------------------
If the build is successful, it should create a new Docker image namedm
basaki/spring-k8s-example:1.0.0
.
You can list all the docker images by typing docker images
in any terminal,
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
basaki/spring-k8s-example 1.0.0 3e84ee5ba1cd 10 minutes ago 532MB
<none> <none> 963c418de905 14 minutes ago 532MB
<none> <none> 5eeb5eb716b4 29 minutes ago 532MB
...
If you just want to run the newly created Docker image without Kubernetes, please go here.
kubectl create -f src/docker/namespace.yml
namespace/example created
kubectl create -f src/docker/config.yml
configmap/spring-prom-config created
docker login
Authenticating with existing credentials...
Login Succeeded
kubectl get deployments -n example
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
spring-prom 1 1 1 0 15m
MPORC02MT22Q:spring-k8s-prometheus indra.basak$ kubectl delete deployment spring-prom -n example
deployment.extensions "spring-prom" deleted
http://localhost:30090/graph
http://spring-prom.example.svc:8082/metrics
java_lang_GarbageCollector_LastGcInfo_memoryUsageAfterGc_max{instance="10.1.0.247:8082",job="svc-disc-endpoint",key="Code Cache",name="MarkSweepCompact",namespace="example",pod="spring-prom-9f5f48c74-vs77x",service="spring-prom"}
java_lang_GarbageCollector_LastGcInfo_memoryUsageAfterGc_max{instance="spring-prom.example.svc:8083",job="svc-disc-service",key="Compressed Class Space",name="Copy",namespace="example",service="spring-prom"}
ps auxw | grep kube-proxy
indra.basak 67603 0.0 0.0 4268056 808 s019 S+ 11:15PM 0:00.00 grep kube-proxy