If you're running PostgreSQL and MongoDB databases in your Kubernetes cluster, monitoring them effectively is crucial to ensure high availability and performance. One of the most popular tools for database monitoring is Prometheus, but you don't necessarily need to set up a central Prometheus server just yet to begin monitoring your databases. By installing Prometheus exporters, you can directly access the metrics exposed by the databases.
In this guide, I'll walk you through the steps of installing and accessing Prometheus exporters for PostgreSQL and MongoDB within your Kubernetes cluster.
Prerequisites
A Kubernetes cluster with PostgreSQL and MongoDB already deployed.
kubectl access to your Kubernetes cluster.
minikube (if you're using Minikube locally) or the appropriate method for accessing your Kubernetes node IP.
In my setup, the PostgreSQL and MongoDB instances are running in the my-lab namespace.
Checking Existing Pods
Hereโs a quick check of the pods running in my Kubernetes cluster:
$ kubectl get pods -n my-lab
NAME READY STATUS RESTARTS AGE
my-mongo-mongodb-8688dcdd4b-tvwpl 1/1 Running 0 65m
my-postgres-postgresql-0 1/1 Running 0 73m
We have my-postgres-postgresql-0 for PostgreSQL and my-mongo-mongodb-8688dcdd4b-tvwpl for MongoDB.
Step 1: Install the PostgreSQL Exporter
The PostgreSQL exporter exposes various performance and status metrics, such as database size, query performance, and connection statistics. The wrouesnel/postgres_exporter is one of the most common and widely used exporters for PostgreSQL.
1.1 Create the PostgreSQL Exporter Deployment
First, create a Kubernetes deployment for the PostgreSQL exporter. Save the following YAML definition in a file called postgres-exporter.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-exporter
namespace: my-lab
spec:
replicas: 1
selector:
matchLabels:
app: postgres-exporter
template:
metadata:
labels:
app: postgres-exporter
spec:
containers:
- name: postgres-exporter
image: wrouesnel/postgres_exporter
ports:
- containerPort: 9187
env:
- name: DATA_SOURCE_NAME
value: "postgres://postgres:<YOUR_PASSWORD>@my-postgres-postgresql-0.my-lab.svc.cluster.local:5432/postgres?sslmode=disable"
---
apiVersion: v1
kind: Service
metadata:
name: postgres-exporter
namespace: my-lab
spec:
type: NodePort
ports:
- port: 9187
targetPort: 9187
nodePort: 30087 # This is the external port
selector:
app: postgres-exporter
1.2 Apply the Resources
Next, apply the YAML file to create the PostgreSQL exporter deployment and expose it through a NodePort service:
$ kubectl apply -f postgres-exporter.yaml
This will deploy the exporter and expose the metrics at port 30087 on all Kubernetes nodes.
1.3 Access PostgreSQL Metrics
Once the exporter is deployed and exposed, you can access the PostgreSQL metrics directly by visiting the following URL:
http://<NodeIP>:30087/metrics
If you're using Minikube, you can find the Node IP with:
$ minikube ip
Now, you can see various PostgreSQL performance metrics being exposed in Prometheus-compatible format.
Step 2: Install the MongoDB Exporter
The MongoDB exporter exposes MongoDB-specific metrics, such as memory usage, index statistics, and replication status. A common choice for MongoDB metrics is the percona/mongodb_exporter.
2.1 Create the MongoDB Exporter Deployment
Save the following YAML definition for the MongoDB exporter as mongodb-exporter.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-exporter
namespace: my-lab
spec:
replicas: 1
selector:
matchLabels:
app: mongodb-exporter
template:
metadata:
labels:
app: mongodb-exporter
spec:
containers:
- name: mongodb-exporter
image: percona/mongodb_exporter:0.42.0
ports:
- containerPort: 9216
env:
- name: MONGODB_URI
value: "mongodb://my-mongo-mongodb.my-lab.svc.cluster.local:27017"
# Replace with actual MongoDB URI if authentication is required
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-exporter
namespace: my-lab
spec:
type: NodePort
ports:
- port: 9216
targetPort: 9216
nodePort: 30094 # External port for accessing the exporter
selector:
app: mongodb-exporter
2.2 Apply the Resources
Now apply the mongodb-exporter.yaml to create the MongoDB exporter deployment:
$ kubectl apply -f mongodb-exporter.yaml
This will expose the MongoDB exporter at port 30094 on all nodes.
2.3 Access MongoDB Metrics
You can now access the MongoDB metrics at:
http://<NodeIP>:30094/metrics
Again, if you're using Minikube, you can retrieve the Node IP using:
$ minikube ip
Conclusion
You now have both PostgreSQL and MongoDB exporters set up in your Kubernetes cluster. The metrics are accessible via HTTP, which you can use for manual inspection or scrape with a Prometheus server later on when you're ready to centralize your monitoring.
PostgreSQL metrics: http://:30087/metrics
MongoDB metrics: http://:30094/metrics
This setup allows you to monitor your databases at the agent level without needing a centralized Prometheus server for now. When you're ready to scale up your monitoring, you can simply integrate these exporters with Prometheus for a more centralized approach to monitoring and alerting.
Top comments (0)