New to KubeDB? Please start here.
Monitoring PostgreSQL Using Prometheus operator
Prometheus operator provides simple and Kubernetes native way to deploy and configure Prometheus server. This tutorial will show you how to use Prometheus operator to monitor PostgreSQL database deployed with KubeDB.
Before You Begin
At first, you need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using kind.
To learn how Prometheus monitoring works with KubeDB in general, please visit here.
To keep Prometheus resources isolated, we are going to use a separate namespace called
monitoringto deploy respective monitoring resources. We are going to deploy database indemonamespace.$ kubectl create ns monitoring namespace/monitoring created $ kubectl create ns demo namespace/demo createdWe need a Prometheus operator instance running. If you don’t already have a running instance, deploy one following the docs from here.
If you already don’t have a Prometheus server running, deploy one following tutorial from here.
Note: YAML files used in this tutorial are stored in docs/examples/postgres folder in GitHub repository kubedb/docs.
Configuration
Step 1 (
kube-prometheus-stack) is required to follow this tutorial. Step 2 (Panopticon) is not needed for the Prometheus Operator / ServiceMonitor scraping documented on this page — install it only if you also plan to use the KubeDB Grafana dashboards’ status panels, or phase-based Alerting. If you have already completed the step(s) you need in another guide, skip ahead.
Step 1: Deploy kube-prometheus-stack
kube-prometheus-stack installs Prometheus, Prometheus Operator, Alertmanager, and Grafana together. This is the recommended way to get the full monitoring stack on Kubernetes. This is where Grafana itself gets installed for this tutorial track — the Grafana Dashboard guide’s “Access Grafana” step assumes it’s already running from here; you do not install Grafana again there.
Add the prometheus-community Helm repo and install:
$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo update
$ helm upgrade --install prometheus prometheus-community/kube-prometheus-stack \
--namespace monitoring \
--set grafana.image.tag=7.5.5
Wait for all pods to be ready:
$ kubectl get pods -n monitoring
NAME READY STATUS RESTARTS AGE
alertmanager-prometheus-kube-prometheus-alertmanager-0 2/2 Running 0 2m
prometheus-grafana-xxxx 3/3 Running 0 2m
prometheus-kube-prometheus-operator-xxxx 1/1 Running 0 2m
prometheus-kube-prometheus-prometheus-0 2/2 Running 0 2m
prometheus-kube-state-metrics-xxxx 1/1 Running 0 2m
Find the serviceMonitorSelector label that Prometheus uses to pick up ServiceMonitor objects. You will need this label when enabling monitoring on the PostgreSQL instance.
$ kubectl get prometheus -n monitoring -o jsonpath='{.items[0].spec.serviceMonitorSelector}'
{"matchLabels":{"release":"prometheus"}}
The label is release: prometheus.
Step 2: Install Panopticon (optional — only for Grafana dashboards & Alerting)
Panopticon is the Appscode operator that reads MetricsConfiguration objects (created by kubedb-metrics) and exposes them to Prometheus. It is not required for the Prometheus Operator / ServiceMonitor scraping covered on this page — skip it, and skip enabling kubedb-metrics too, if you only need raw metrics in Prometheus. Install it only if you also plan to use the phase/status panels of the KubeDB Grafana dashboards, or phase-based Alerting — and install it before enabling kubedb-metrics.
$ helm repo add appscode https://charts.appscode.com/stable/
$ helm repo update
$ helm upgrade --install panopticon appscode/panopticon \
--version v2026.4.30 \
--namespace kubeops --create-namespace \
--set monitoring.enabled=true \
--set monitoring.agent=prometheus.io/operator \
--set monitoring.serviceMonitor.labels.release=prometheus \
--set-file license=/path/to/kubedb-license.txt \
--wait --timeout 5m0s
Verify panopticon is running:
$ kubectl get pods -n kubeops
NAME READY STATUS RESTARTS AGE
panopticon-xxxx 1/1 Running 0 1m
Find out required labels for ServiceMonitor
We need to know the labels used to select ServiceMonitor by a Prometheus crd. We are going to provide these labels in spec.monitor.prometheus.labels field of PostgreSQL crd so that KubeDB creates ServiceMonitor object accordingly.
At first, let’s find out the available Prometheus server in our cluster.
$ kubectl get prometheus --all-namespaces
NAMESPACE NAME AGE
monitoring prometheus 18m
If you don’t have any Prometheus server running in your cluster, deploy one following the guide specified in Before You Begin section.
Now, let’s view the YAML of the available Prometheus server prometheus in monitoring namespace.
$ kubectl get prometheus -n monitoring prometheus -o yaml
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"monitoring.coreos.com/v1","kind":"Prometheus","metadata":{"annotations":{},"labels":{"prometheus":"prometheus"},"name":"prometheus","namespace":"monitoring"},"spec":{"replicas":1,"resources":{"requests":{"memory":"400Mi"}},"serviceAccountName":"prometheus","serviceMonitorSelector":{"matchLabels":{"release":"prometheus"}}}}
creationTimestamp: 2019-01-03T13:41:51Z
generation: 1
labels:
prometheus: prometheus
name: prometheus
namespace: monitoring
resourceVersion: "44402"
selfLink: /apis/monitoring.coreos.com/v1/namespaces/monitoring/prometheuses/prometheus
uid: 5324ad98-0f5d-11e9-b230-080027f306f3
spec:
replicas: 1
resources:
requests:
memory: 400Mi
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
release: prometheus
Notice the spec.serviceMonitorSelector section. Here, release: prometheus label is used to select ServiceMonitor crd. So, we are going to use this label in spec.monitor.prometheus.labels field of PostgreSQL crd.
Deploy PostgreSQL with Monitoring Enabled
At first, let’s deploy an PostgreSQL database with monitoring enabled. Below is the PostgreSQL object that we are going to create.
apiVersion: kubedb.com/v1
kind: Postgres
metadata:
name: coreos-prom-postgres
namespace: demo
spec:
version: "18.3"
deletionPolicy: WipeOut
storage:
storageClassName: "standard"
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
monitor:
agent: prometheus.io/operator
prometheus:
serviceMonitor:
labels:
release: prometheus
interval: 10s
Here,
monitor.agent: prometheus.io/operatorindicates that we are going to monitor this server using Prometheus operator.monitor.prometheus.namespace: monitoringspecifies that KubeDB should createServiceMonitorinmonitoringnamespace.monitor.prometheus.labelsspecifies that KubeDB should createServiceMonitorwith these labels.monitor.prometheus.intervalindicates that the Prometheus server should scrape metrics from this database with 10 seconds interval.
Let’s create the PostgreSQL object that we have shown above,
$ kubectl create -f https://github.com/kubedb/docs/raw/v2026.7.10/docs/examples/postgres/monitoring/coreos-prom-postgres.yaml
postgresql.kubedb.com/coreos-prom-postgres created
Now, wait for the database to go into Running state.
$ kubectl get pg -n demo coreos-prom-postgres
NAME VERSION STATUS AGE
coreos-prom-postgres 10.2-v5 Running 38s
KubeDB will create a separate stats service with name {PostgreSQL crd name}-stats for monitoring purpose.
$ kubectl get svc -n demo --selector="app.kubernetes.io/instance=coreos-prom-postgres"
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
coreos-prom-postgres ClusterIP 10.107.102.123 <none> 5432/TCP 58s
coreos-prom-postgres-replicas ClusterIP 10.109.11.171 <none> 5432/TCP 58s
coreos-prom-postgres-stats ClusterIP 10.110.218.172 <none> 56790/TCP 51s
Here, coreos-prom-postgres-stats service has been created for monitoring purpose.
Let’s describe this stats service.
$ kubectl describe svc -n demo coreos-prom-postgres-stats
Name: coreos-prom-postgres-stats
Namespace: demo
Labels: app.kubernetes.io/name=postgreses.kubedb.com
app.kubernetes.io/instance=coreos-prom-postgres
Annotations: monitoring.appscode.com/agent: prometheus.io/operator
Selector: app.kubernetes.io/name=postgreses.kubedb.com,app.kubernetes.io/instance=coreos-prom-postgres
Type: ClusterIP
IP: 10.110.218.172
Port: prom-http 56790/TCP
TargetPort: prom-http/TCP
Endpoints: 172.17.0.7:56790
Session Affinity: None
Events: <none>
Notice the Labels and Port fields. ServiceMonitor will use these information to target its endpoints.
KubeDB will also create a ServiceMonitor crd in monitoring namespace that select the endpoints of coreos-prom-postgres-stats service. Verify that the ServiceMonitor crd has been created.
$ kubectl get servicemonitor -n monitoring
NAME AGE
kubedb-demo-coreos-prom-postgres 1m
Let’s verify that the ServiceMonitor has the label that we had specified in spec.monitor section of PostgreSQL crd.
$ kubectl get servicemonitor -n monitoring kubedb-demo-coreos-prom-postgres -o yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
creationTimestamp: 2019-01-03T15:47:08Z
generation: 1
labels:
release: prometheus
monitoring.appscode.com/service: coreos-prom-postgres-stats.demo
name: kubedb-demo-coreos-prom-postgres
namespace: monitoring
resourceVersion: "53969"
selfLink: /apis/monitoring.coreos.com/v1/namespaces/monitoring/servicemonitors/kubedb-demo-coreos-prom-postgres
uid: d3c419ad-0f6e-11e9-b230-080027f306f3
spec:
endpoints:
- honorLabels: true
interval: 10s
path: /metrics
port: prom-http
namespaceSelector:
matchNames:
- demo
selector:
matchLabels:
app.kubernetes.io/name: postgreses.kubedb.com
app.kubernetes.io/instance: coreos-prom-postgres
Notice that the ServiceMonitor has label release: prometheus that we had specified in PostgreSQL crd.
Also notice that the ServiceMonitor has selector which match the labels we have seen in the coreos-prom-postgres-stats service. It also, target the prom-http port that we have seen in the stats service.
Verify Monitoring Metrics
At first, let’s find out the respective Prometheus pod for prometheus Prometheus server.
$ kubectl get pod -n monitoring -l=app=prometheus
NAME READY STATUS RESTARTS AGE
prometheus-prometheus-0 3/3 Running 1 63m
Prometheus server is listening to port 9090 of prometheus-prometheus-0 pod. We are going to use port forwarding to access Prometheus dashboard.
Run following command on a separate terminal to forward the port 9090 of prometheus-prometheus-0 pod,
$ kubectl port-forward -n monitoring prometheus-prometheus-0 9090
Forwarding from 127.0.0.1:9090 -> 9090
Forwarding from [::1]:9090 -> 9090
Now, we can access the dashboard at localhost:9090. Open http://localhost:9090 in your browser. You should see prom-http endpoint of coreos-prom-postgres-stats service as one of the targets.

Check the endpoint and service labels marked by red rectangle. It verifies that the target is our expected database. Now, you can view the collected metrics and create a graph from homepage of this Prometheus dashboard. You can also use this Prometheus server as data source for Grafana and create beautiful dashboard with collected metrics.
Cleaning up
To cleanup the Kubernetes resources created by this tutorial, run following commands
# cleanup database
kubectl delete -n demo pg/coreos-prom-postgres
# cleanup panopticon
helm uninstall panopticon -n kubeops
# if you also completed the Grafana Dashboard tutorial, clean that up first —
# see https://kubedb.com/docs/guides/postgres/monitoring/grafana-dashboard.md#cleaning-up
# cleanup prometheus resources
kubectl delete -n monitoring prometheus prometheus
kubectl delete -n monitoring clusterrolebinding prometheus
kubectl delete -n monitoring clusterrole prometheus
kubectl delete -n monitoring serviceaccount prometheus
kubectl delete -n monitoring service prometheus-operated
# cleanup prometheus operator resources
kubectl delete -n monitoring deployment prometheus-operator
kubectl delete -n dmeo serviceaccount prometheus-operator
kubectl delete clusterrolebinding prometheus-operator
kubectl delete clusterrole prometheus-operator
# delete namespace
kubectl delete ns monitoring
kubectl delete ns demo
Next Steps
- Monitor your PostgreSQL database with KubeDB using built-in Prometheus.
- Want to hack on KubeDB? Check our contribution guidelines.
































