Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit e13128c

Browse files
committed
Add monitoring docs and small fixes
1 parent a444ba2 commit e13128c

File tree

9 files changed

+108
-6
lines changed

9 files changed

+108
-6
lines changed

docs/monitoring.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
This documentation is intended to guide you how to enable custom monitoring on operator installed Kafka cluster
2+
3+
#### Using Helm for Prometheus:
4+
5+
By default operator installs Kafka Pods with the following annotations, also it opens port 9020 in all brokers to enable scraping.
6+
7+
```
8+
"prometheus.io/scrape": "true"
9+
"prometheus.io/port": "9020"
10+
```
11+
12+
Prometheus must be configured to recognize these annotations. The following example contains the required config.
13+
14+
```
15+
# Example scrape config for pods
16+
#
17+
# The relabeling allows the actual pod scrape endpoint to be configured via the
18+
# following annotations:
19+
#
20+
# * `prometheus.io/scrape`: Only scrape pods that have a value of `true`
21+
# * `prometheus.io/path`: If the metrics path is not `/metrics` override this.
22+
# * `prometheus.io/port`: Scrape the pod on the indicated port instead of the default of `9102`.
23+
- job_name: 'kubernetes-pods'
24+
25+
kubernetes_sd_configs:
26+
- role: pod
27+
28+
relabel_configs:
29+
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
30+
action: keep
31+
regex: true
32+
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
33+
action: replace
34+
target_label: __metrics_path__
35+
regex: (.+)
36+
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
37+
action: replace
38+
regex: ([^:]+)(?::\d+)?;(\d+)
39+
replacement: $1:$2
40+
target_label: __address__
41+
```
42+
43+
Using the provided [CR](https://github.com/banzaicloud/kafka-operator/blob/master/config/samples/banzaicloud_v1alpha1_kafkacluster.yaml), the operator installs the official [jmx exporter](https://github.com/prometheus/jmx_exporter) for Prometheus.
44+
45+
To change this behavior please modify the following lines in the end of the CR.
46+
47+
```
48+
monitoringConfig:
49+
jmxImage describes the used prometheus jmx exporter agent container
50+
jmxImage: "banzaicloud/jmx-javaagent:0.12.0"
51+
pathToJar describes the path to the jar file in the given image
52+
pathToJar: "/opt/jmx_exporter/jmx_prometheus_javaagent-0.12.0.jar"
53+
kafkaJMXExporterConfig describes jmx exporter config for Kafka
54+
kafkaJMXExporterConfig: |
55+
lowercaseOutputName: true
56+
```
57+
58+
#### Using the ServiceMonitors:
59+
60+
To use ServiceMonitors, we recommend to use kafka with unique service/broker instead of headless service.
61+
62+
Configure the CR in a following way:
63+
64+
```
65+
# Specify if the cluster should use headlessService for Kafka or individual services
66+
# using service/broker may come in handy in case of service mesh
67+
headlessServiceEnabled: false
68+
```
69+
70+
Disabling Headless service means the operator will set up Kafka with unique services per broker.
71+
72+
Once you have a cluster up and running create as many ServiceMonitors as brokers.
73+
74+
```
75+
apiVersion: monitoring.coreos.com/v1
76+
kind: ServiceMonitor
77+
metadata:
78+
name: kafka-0
79+
spec:
80+
selector:
81+
matchLabels:
82+
app: kafka
83+
brokerId: "0"
84+
kafka_cr: kafka
85+
endpoints:
86+
- port: metrics
87+
interval: 10s
88+
```

pkg/resources/cruisecontrol/cruisecontrol.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const (
3434
pemFilesVolume = "pem-files"
3535
jmxVolumePath = "/opt/jmx-exporter/"
3636
jmxVolumeName = "jmx-jar-data"
37+
metricsPort = 9020
3738
)
3839

3940
var labelSelector = map[string]string{

pkg/resources/cruisecontrol/deployment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (r *Reconciler) deployment(log logr.Logger) runtime.Object {
5656
Template: corev1.PodTemplateSpec{
5757
ObjectMeta: metav1.ObjectMeta{
5858
Labels: labelSelector,
59-
Annotations: util.MonitoringAnnotations(),
59+
Annotations: util.MonitoringAnnotations(metricsPort),
6060
},
6161
Spec: corev1.PodSpec{
6262
TerminationGracePeriodSeconds: util.Int64Pointer(30),

pkg/resources/cruisecontrol/service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ func (r *Reconciler) service(log logr.Logger) runtime.Object {
3333
TargetPort: intstr.FromInt(8090),
3434
Protocol: corev1.ProtocolTCP,
3535
},
36+
{
37+
Name: "metrics",
38+
Port: metricsPort,
39+
TargetPort: intstr.FromInt(metricsPort),
40+
Protocol: corev1.ProtocolTCP,
41+
},
3642
},
3743
},
3844
}

pkg/resources/kafka/kafka.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const (
5252
pemFilesVolume = "pem-files"
5353
jmxVolumePath = "/opt/jmx-exporter/"
5454
jmxVolumeName = "jmx-jar-data"
55+
metricsPort = 9020
5556

5657
//jaasConfig = "jaas-config"
5758
//scramSecret = "scram-secret"

pkg/resources/kafka/pod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (r *Reconciler) pod(broker banzaicloudv1alpha1.BrokerConfig, pvcs []corev1.
6868
}
6969

7070
pod := &corev1.Pod{
71-
ObjectMeta: templates.ObjectMetaWithGeneratedNameAndAnnotations(r.KafkaCluster.Name, util.MergeLabels(labelsForKafka(r.KafkaCluster.Name), map[string]string{"brokerId": fmt.Sprintf("%d", broker.Id)}), util.MonitoringAnnotations(), r.KafkaCluster),
71+
ObjectMeta: templates.ObjectMetaWithGeneratedNameAndAnnotations(r.KafkaCluster.Name, util.MergeLabels(labelsForKafka(r.KafkaCluster.Name), map[string]string{"brokerId": fmt.Sprintf("%d", broker.Id)}), util.MonitoringAnnotations(metricsPort), r.KafkaCluster),
7272
Spec: corev1.PodSpec{
7373
InitContainers: append(initContainers, []corev1.Container{
7474
{

pkg/resources/kafka/service.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ func (r *Reconciler) service(broker banzaicloudv1alpha1.BrokerConfig, log logr.L
4040
Protocol: corev1.ProtocolTCP,
4141
})
4242
}
43+
usedPorts = append(usedPorts, corev1.ServicePort{
44+
Name: "metrics",
45+
Port: metricsPort,
46+
TargetPort: intstr.FromInt(metricsPort),
47+
Protocol: corev1.ProtocolTCP,
48+
})
4349

4450
return &corev1.Service{
45-
ObjectMeta: templates.ObjectMeta(fmt.Sprintf("%s-%d", r.KafkaCluster.Name, broker.Id), labelsForKafka(r.KafkaCluster.Name), r.KafkaCluster),
51+
ObjectMeta: templates.ObjectMeta(fmt.Sprintf("%s-%d", r.KafkaCluster.Name, broker.Id), util.MergeLabels(labelsForKafka(r.KafkaCluster.Name), map[string]string{"brokerId": fmt.Sprintf("%d", broker.Id)}), r.KafkaCluster),
4652
Spec: corev1.ServiceSpec{
4753
Type: corev1.ServiceTypeClusterIP,
4854
SessionAffinity: corev1.ServiceAffinityNone,

pkg/resources/kafkamonitoring/configmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
func (r *Reconciler) configMap() runtime.Object {
2727
configMap := &corev1.ConfigMap{
2828
ObjectMeta: templates.ObjectMeta(fmt.Sprintf(BrokerJmxTemplate, r.KafkaCluster.Name), labelsForJmx(r.KafkaCluster.Name), r.KafkaCluster),
29-
Data: map[string]string{"config.yaml": r.KafkaCluster.Spec.MonitoringConfig.KafkaJMXExporterConfig},
29+
Data: map[string]string{"config.yaml": r.KafkaCluster.Spec.MonitoringConfig.GetKafkaJMXExporterConfig()},
3030
}
3131
return configMap
3232
}

pkg/util/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func MergeLabels(l map[string]string, l2 map[string]string) map[string]string {
6060
}
6161

6262
// MonitoringAnnotations returns specific prometheus annotations
63-
func MonitoringAnnotations() map[string]string {
63+
func MonitoringAnnotations(port int) map[string]string {
6464
return map[string]string{
6565
"prometheus.io/scrape": "true",
66-
"prometheus.io/port": "9020",
66+
"prometheus.io/port": string(port),
6767
}
6868
}
6969

0 commit comments

Comments
 (0)