Enterprise Java

Monitor applications using Prometheus Operator on Kubernetes

You can make the Prometheus configuration aware of the Kubernetes environment your applications are running in. I’ve described how to do that manually, in a previous blog post. Prometheus Operator is an extension to Kubernetes that manages Prometheus monitoring instances in a more automated and effective way.

Prometheus Operator allows you to define and manage monitoring instances as Kubernetes resources. If you know how to manage Kubernetes, there’s a low threshold to get started and effectively define the monitoring of your applications.

In order to enable our Kubernetes for Prometheus operators, we setup the resource and RBAC definitions that you can find here. This enhances our cluster with more Kubernetes resources types, such as ServiceMonitor, or Prometheus. Similarly, you can use the Prometheus Operator helm chart.

We define the operators of our config-example application, similar to the previous post:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: config-example
  labels:
    team: example
spec:
  selector:
    matchLabels:
      app: config-example
  endpoints:
  - basicAuth:
      password:
        name: basic-auth
        key: password
      username:
        name: basic-auth
        key: username
    port: https
    scheme: https
    path: '/metrics/'
    tlsConfig:
      insecureSkipVerify: true
01
02
03
04
05
06
07
08
09
10
11
12
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
spec:
  serviceAccountName: prometheus
  serviceMonitorSelector:
    matchLabels:
      team: example
  resources:
    requests:
      memory: 400Mi
01
02
03
04
05
06
07
08
09
10
apiVersion: v1
kind: Service
metadata:
  name: prometheus
spec:
  ports:
  - port: 9090
    name: http
  selector:
    prometheus: prometheus
1
2
3
4
5
6
7
apiVersion: v1
kind: Secret
metadata:
  name: basic-auth
data:
  password: YWRtaW5hZG1pbg==
  username: YWRtaW4=

This sets up a prometheus instance, that will scrape applications that are deployed with the app: config-example label using the provided configuration to access it. It also creates a prometheus service to access the monitoring instances.

You can find a full description of the Prometheus Operator API in the documentation.

After we applied all resources, we can see the running monitoring instances in our cluster:

1
gt; kubectl get pods NAME READY STATUS RESTARTS AGE config-example-7db586bb95-jdmsx 1/1 Running 0 12m config-example-7db586bb95-z4ln8 1/1 Running 0 12m [...] prometheus-prometheus-0 3/3 Running 0 14m

This enables us to simply monitor all application instances without manually configuring the Prometheus instances.

Have a look at the full example on GitHub (deployment/ directory).

Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java:

Success! Now check your email to confirm your subscription.

© Sebastian Daschner, CC BY-NC-SA 4.0

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Monitor applications using Prometheus Operator on Kubernetes

Opinions expressed by Java Code Geeks contributors are their own.

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button