Skip to content

Commit

Permalink
MINOR: Support PodMonitor as alternative to ServiceMonitor
Browse files Browse the repository at this point in the history
Signed-off-by: Philipp Hossner <[email protected]>
  • Loading branch information
phihos committed Dec 20, 2024
1 parent 6bb553b commit b5208fd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
16 changes: 16 additions & 0 deletions kubernetes-ingress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ helm install my-ingress haproxytech/kubernetes-ingress \
--set controller.serviceMonitor.enabled=true
```

### Installing the PodMonitor

As an alternative to a `ServiceMonitor` you can use a `PodMonitor`, which targets the pods directly instead of using a service.
If you're using the [Prometheus Operator](https://github.com/prometheus-operator/prometheus-operator), you can automatically install the `PodMonitor` definition in order to automate the scraping options according to your needs.

```console
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts

helm install prometheus prometheus-community/kube-prometheus-stack \
--set prometheus.prometheusSpec.podMonitorSelectorNilUsesHelmValues=false \
--set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false

helm install my-ingress haproxytech/kubernetes-ingress \
--set controller.podMonitor.enabled=true
```

### Installing with Kubernetes Event-driven Autoscaling (KEDA)

[KEDA](https://keda.sh/docs/2.3/concepts/scaling-deployments/) is an improved scaling solution built on top of HPA which allows autoscaling criteria based on information from any event source including Prometheus metrics collected from HAProxy native Prometheus Exporter.
Expand Down
6 changes: 6 additions & 0 deletions kubernetes-ingress/ci/deployment-podmonitor-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
controller:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
7 changes: 7 additions & 0 deletions kubernetes-ingress/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ Create a default fully qualified ServiceMonitor name.
{{- default (include "kubernetes-ingress.fullname" .) .Values.controller.serviceMonitor.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{/*
Create a default fully qualified PodMonitor name.
*/}}
{{- define "kubernetes-ingress.podMonitorName" -}}
{{- default (include "kubernetes-ingress.fullname" .) .Values.controller.podMonitor.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{/*
Create a FQDN for the Service metrics.
*/}}
Expand Down
42 changes: 42 additions & 0 deletions kubernetes-ingress/templates/controller-podmonitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{/*
Copyright 2024 HAProxy Technologies LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}

{{- if and (.Capabilities.APIVersions.Has "monitoring.coreos.com/v1") .Values.controller.podMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: {{ include "kubernetes-ingress.podMonitorName" . }}
namespace: {{ include "kubernetes-ingress.namespace" . }}
labels:
app.kubernetes.io/name: {{ include "kubernetes-ingress.name" . }}
helm.sh/chart: {{ include "kubernetes-ingress.chart" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion }}
{{- if .Values.controller.podMonitor.extraLabels }}
{{ toYaml .Values.controller.podMonitor.extraLabels | nindent 4 }}
{{- end }}
spec:
podMetricsEndpoints:
{{ .Values.controller.podMonitor.endpoints | toYaml | nindent 4 }}
namespaceSelector:
matchNames:
- {{ include "kubernetes-ingress.namespace" . }}
selector:
matchLabels:
app.kubernetes.io/name: {{ include "kubernetes-ingress.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
23 changes: 23 additions & 0 deletions kubernetes-ingress/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ controller:
## --set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false
serviceMonitor:
## Toggle the ServiceMonitor true if you have Prometheus Operator installed and configured
## Should not be enabled when controller.podMonitor.enabled is true
enabled: false

## Specify the labels to add to the ServiceMonitors to be selected for target discovery
Expand All @@ -576,6 +577,28 @@ controller:
scheme: http
interval: 30s

## PodMonitor
## ref: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/user-guides/getting-started.md
## Note: requires Prometheus Operator to be able to work, for example:
## helm install prometheus prometheus-community/kube-prometheus-stack \
## --set prometheus.prometheusSpec.podMonitorSelectorNilUsesHelmValues=false \
## --set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false
podMonitor:
## Toggle the PodMonitor true if you have Prometheus Operator installed and configured
## Should not be enabled when controller.serviceMonitor.enabled is true
enabled: false

## Specify the labels to add to the PodMonitors to be selected for target discovery
extraLabels: {}

## Specify the endpoints
## ref: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/design.md#podMonitor
endpoints:
- port: stat
path: /metrics
scheme: http
interval: 30s

## Controller sync mode with Kubernetes
## Note: requires Enterprise Kubernetes Ingress Controller
## Possible values: 'default' or 'fetch'
Expand Down

0 comments on commit b5208fd

Please sign in to comment.