Skip to content

Commit b9ba642

Browse files
authored
[postgres] feat: add metrics exporter (CloudPirates-io#285)
1 parent c80c98a commit b9ba642

File tree

6 files changed

+218
-1
lines changed

6 files changed

+218
-1
lines changed

charts/postgres/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v2
22
name: postgres
33
description: The World's Most Advanced Open Source Relational Database
44
type: application
5-
version: 0.7.3
5+
version: 0.8.0
66
appVersion: "18.0"
77
keywords:
88
- postgres

charts/postgres/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,30 @@ extraObjects:
288288
289289
All objects in `extraObjects` will be rendered and deployed with the release. You can use any valid Kubernetes manifest, and reference Helm values or built-in objects as needed (just remember to quote template expressions).
290290

291+
### Metrics Configuration
292+
293+
| Parameter | Description | Default |
294+
| ------------------------------------------ | ------------------------------------------------------------------------------- | --------------------------------------- |
295+
| `metrics.enabled` | Start a sidecar prometheus exporter to expose PostgreSQL metrics | `false` |
296+
| `metrics.image.registry` | PostgreSQL exporter image registry | `quay.io` |
297+
| `metrics.image.repository` | PostgreSQL exporter image repository | `prometheuscommunity/postgres-exporter` |
298+
| `metrics.image.tag` | PostgreSQL exporter image tag | `v0.18.1` |
299+
| `metrics.image.pullPolicy` | PostgreSQL exporter image pull policy | `Always` |
300+
| `metrics.resources` | Resource limits and requests for metrics container | `{}` |
301+
| `metrics.service.annotations` | Additional custom annotations for Metrics service | `{}` |
302+
| `metrics.service.labels` | Additional custom labels for Metrics service | `{}` |
303+
| `metrics.service.port` | Metrics service port | `9187` |
304+
| `metrics.serviceMonitor.enabled` | Create ServiceMonitor resource(s) for scraping metrics using PrometheusOperator | `false` |
305+
| `metrics.serviceMonitor.namespace` | The namespace in which the ServiceMonitor will be created | `""` |
306+
| `metrics.serviceMonitor.interval` | The interval at which metrics should be scraped | `30s` |
307+
| `metrics.serviceMonitor.scrapeTimeout` | The timeout after which the scrape is ended | `10s` |
308+
| `metrics.serviceMonitor.selector` | Additional labels for ServiceMonitor resource | `{}` |
309+
| `metrics.serviceMonitor.annotations` | ServiceMonitor annotations | `{}` |
310+
| `metrics.serviceMonitor.honorLabels` | honorLabels chooses the metric's labels on collisions with target labels | `false` |
311+
| `metrics.serviceMonitor.relabelings` | ServiceMonitor relabel configs to apply to samples before scraping | `[]` |
312+
| `metrics.serviceMonitor.metricRelabelings` | ServiceMonitor metricRelabelings configs to apply to samples before ingestion | `[]` |
313+
| `metrics.serviceMonitor.namespaceSelector` | ServiceMonitor namespace selector | `{}` |
314+
291315
## Examples
292316

293317
### Basic Deployment
@@ -439,6 +463,33 @@ data:
439463
# Add your custom configuration here
440464
```
441465

466+
### Monitoring with Prometheus
467+
468+
Enable metrics collection with Prometheus:
469+
470+
```yaml
471+
# values-monitoring.yaml
472+
metrics:
473+
enabled: true
474+
serviceMonitor:
475+
enabled: true
476+
```
477+
478+
Deploy with monitoring enabled:
479+
480+
```bash
481+
helm install my-postgres ./charts/postgres -f values-monitoring.yaml
482+
```
483+
484+
The PostgreSQL exporter will expose metrics on port 9187, and if you have Prometheus Operator installed, the ServiceMonitor will automatically configure Prometheus to scrape the metrics.
485+
486+
You can access metrics directly via port-forward:
487+
488+
```bash
489+
kubectl port-forward service/my-postgres-metrics 9187:9187
490+
curl http://localhost:9187/metrics
491+
```
492+
442493
## Access PostgreSQL
443494

444495
### Via kubectl port-forward
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{- if .Values.metrics.enabled }}
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
name: {{ include "postgres.fullname" . }}-metrics
6+
namespace: {{ .Release.Namespace }}
7+
labels:
8+
{{- include "postgres.labels" . | nindent 4 }}
9+
app.kubernetes.io/component: metrics
10+
{{- with .Values.metrics.service.labels }}
11+
{{- toYaml . | nindent 4 }}
12+
{{- end }}
13+
{{- with .Values.metrics.service.annotations }}
14+
annotations:
15+
{{- toYaml . | nindent 4 }}
16+
{{- end }}
17+
spec:
18+
type: ClusterIP
19+
ports:
20+
- name: metrics
21+
port: {{ .Values.metrics.service.port }}
22+
protocol: TCP
23+
targetPort: metrics
24+
selector:
25+
{{- include "postgres.selectorLabels" . | nindent 4 }}
26+
{{- end }}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }}
2+
apiVersion: monitoring.coreos.com/v1
3+
kind: ServiceMonitor
4+
metadata:
5+
name: {{ include "postgres.fullname" . }}-metrics
6+
{{- if .Values.metrics.serviceMonitor.namespace }}
7+
namespace: {{ .Values.metrics.serviceMonitor.namespace }}
8+
{{- else }}
9+
namespace: {{ .Release.Namespace }}
10+
{{- end }}
11+
labels:
12+
{{- include "postgres.labels" . | nindent 4 }}
13+
app.kubernetes.io/component: metrics
14+
{{- with .Values.metrics.serviceMonitor.selector }}
15+
{{- toYaml . | nindent 4 }}
16+
{{- end }}
17+
{{- with .Values.metrics.serviceMonitor.annotations }}
18+
annotations:
19+
{{- toYaml . | nindent 4 }}
20+
{{- end }}
21+
spec:
22+
jobLabel: {{ include "postgres.fullname" . }}
23+
selector:
24+
matchLabels:
25+
{{- include "postgres.selectorLabels" . | nindent 6 }}
26+
app.kubernetes.io/component: metrics
27+
endpoints:
28+
- port: metrics
29+
{{- with .Values.metrics.serviceMonitor.interval }}
30+
interval: {{ . }}
31+
{{- end }}
32+
{{- with .Values.metrics.serviceMonitor.scrapeTimeout }}
33+
scrapeTimeout: {{ . }}
34+
{{- end }}
35+
path: /metrics
36+
{{- with .Values.metrics.serviceMonitor.honorLabels }}
37+
honorLabels: {{ . }}
38+
{{- end }}
39+
{{- with .Values.metrics.serviceMonitor.relabelings }}
40+
relabelings:
41+
{{- toYaml . | nindent 8 }}
42+
{{- end }}
43+
{{- with .Values.metrics.serviceMonitor.metricRelabelings }}
44+
metricRelabelings:
45+
{{- toYaml . | nindent 8 }}
46+
{{- end }}
47+
{{- with .Values.metrics.serviceMonitor.namespaceSelector }}
48+
namespaceSelector:
49+
{{- toYaml . | nindent 4 }}
50+
{{- end }}
51+
{{- end }}

charts/postgres/templates/statefulset.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,49 @@ spec:
197197
mountPath: /docker-entrypoint-initdb.d/init-custom-user.sh
198198
subPath: init-custom-user.sh
199199
{{- end }}
200+
{{- if .Values.metrics.enabled }}
201+
- name: metrics
202+
image: "{{ .Values.metrics.image.registry }}/{{ .Values.metrics.image.repository }}:{{ .Values.metrics.image.tag }}"
203+
imagePullPolicy: {{ .Values.metrics.image.pullPolicy }}
204+
securityContext:
205+
allowPrivilegeEscalation: false
206+
capabilities:
207+
drop:
208+
- ALL
209+
privileged: false
210+
readOnlyRootFilesystem: true
211+
runAsNonRoot: true
212+
runAsUser: 65534
213+
runAsGroup: 65534
214+
env:
215+
- name: DATA_SOURCE_NAME
216+
value: "postgresql://{{ include "postgres.username" . }}:$(POSTGRES_PASSWORD)@localhost:{{ .Values.service.targetPort }}/{{ include "postgres.database" . }}?sslmode=disable"
217+
- name: POSTGRES_PASSWORD
218+
valueFrom:
219+
secretKeyRef:
220+
name: {{ include "postgres.secretName" . }}
221+
key: {{ .Values.auth.secretKeys.passwordKey }}
222+
ports:
223+
- name: metrics
224+
containerPort: 9187
225+
protocol: TCP
226+
startupProbe:
227+
httpGet:
228+
path: /
229+
port: metrics
230+
readinessProbe:
231+
httpGet:
232+
path: /
233+
port: metrics
234+
livenessProbe:
235+
httpGet:
236+
path: /
237+
port: metrics
238+
{{- with .Values.metrics.resources }}
239+
resources:
240+
{{- toYaml . | nindent 12 }}
241+
{{- end }}
242+
{{- end }}
200243
volumes:
201244
{{- if not .Values.persistence.enabled }}
202245
- name: data

charts/postgres/values.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,49 @@ initContainers: []
285285

286286
## @param extraObjects Array of extra objects to deploy with the release
287287
extraObjects: []
288+
289+
## @section Metrics configuration
290+
metrics:
291+
## @param metrics.enabled Start a sidecar prometheus exporter to expose PostgreSQL metrics
292+
enabled: false
293+
image:
294+
## @param metrics.image.registry PostgreSQL exporter image registry
295+
registry: quay.io
296+
## @param metrics.image.repository PostgreSQL exporter image repository
297+
repository: prometheuscommunity/postgres-exporter
298+
## @param metrics.image.tag PostgreSQL exporter image tag
299+
tag: "v0.18.1@sha256:fb96c4413985d4b23ab02b19022b3d70a86c8e0a62f41ab15ebb6f4673781a5d"
300+
## @param metrics.image.pullPolicy PostgreSQL exporter image pull policy
301+
pullPolicy: Always
302+
## @param metrics.resources Resource limits and requests for metrics container
303+
resources: {}
304+
## Metrics service configuration
305+
service:
306+
## @param metrics.service.annotations Additional custom annotations for Metrics service
307+
annotations: {}
308+
## @param metrics.service.labels Additional custom labels for Metrics service
309+
labels: {}
310+
## @param metrics.service.port Metrics service port
311+
port: 9187
312+
## Prometheus Operator ServiceMonitor configuration
313+
serviceMonitor:
314+
## @param metrics.serviceMonitor.enabled Create ServiceMonitor resource(s) for scraping metrics using PrometheusOperator
315+
enabled: false
316+
## @param metrics.serviceMonitor.namespace The namespace in which the ServiceMonitor will be created
317+
namespace: ""
318+
## @param metrics.serviceMonitor.interval The interval at which metrics should be scraped
319+
interval: 30s
320+
## @param metrics.serviceMonitor.scrapeTimeout The timeout after which the scrape is ended
321+
scrapeTimeout: 10s
322+
## @param metrics.serviceMonitor.selector Additional labels for ServiceMonitor resource
323+
selector: {}
324+
## @param metrics.serviceMonitor.annotations ServiceMonitor annotations
325+
annotations: {}
326+
## @param metrics.serviceMonitor.honorLabels honorLabels chooses the metric's labels on collisions with target labels
327+
honorLabels: false
328+
## @param metrics.serviceMonitor.relabelings ServiceMonitor relabel configs to apply to samples before scraping
329+
relabelings: []
330+
## @param metrics.serviceMonitor.metricRelabelings ServiceMonitor metricRelabelings configs to apply to samples before ingestion
331+
metricRelabelings: []
332+
## @param metrics.serviceMonitor.namespaceSelector ServiceMonitor namespace selector
333+
namespaceSelector: {}

0 commit comments

Comments
 (0)