Skip to main content
We’ve already installed and configured a Prometheus server to monitor our Kubernetes infrastructure. Next, we’ll demonstrate how to configure Prometheus (installed via the Prometheus Operator) to monitor a simple Node.js application running inside Kubernetes. This walkthrough covers:
  • A minimal Node.js app instrumented with swagger-stats for Prometheus metrics
  • Containerizing and publishing the image to Docker Hub
  • Kubernetes manifests (Deployment + Service) to run the app
  • Verifying the deployment and exposing the metrics endpoint for Prometheus scraping
Relevant links:

1) Example Node.js application (index.js)

This minimal Express app exposes a few endpoints and uses swagger-stats to provide a Prometheus-compatible metrics endpoint at /swagger-stats/metrics.
Ensure your application listens on the same port you expose in the container and in the Kubernetes Service (below). This example uses port 3000 and exposes metrics at /swagger-stats/metrics.

2) Dockerfile

A lightweight Dockerfile to build the image for this app:
Build and push steps (example). Replace your-dockerhub-username/prometheus-demo with your repository:
Example push output (truncated for brevity):

3) Kubernetes manifest (Deployment + Service)

Create a file called api-deploy.yaml. This single manifest contains:
  • A Deployment (2 replicas) that runs the container image
  • A ClusterIP Service that forwards traffic to port 3000 on the pods
Replace your-dockerhub-username/prometheus-demo:latest with the image you pushed to Docker Hub.
Using a ClusterIP service means the application is not exposed externally by default. If you need external access, consider NodePort, LoadBalancer, or an Ingress depending on your environment.

4) Apply and verify

Apply the manifest and verify deployments and services:
Expected output:
Check services:
Example output (truncated):
Check deployments:
Example output:
If a replica shows 1/2 initially, wait a few seconds and re-run kubectl get deployment — the second pod often takes a moment to become ready.

5) Prometheus scrape configuration (Prometheus Operator)

When using the Prometheus Operator, you’ll typically create a ServiceMonitor to tell Prometheus to scrape the application. The important parts are:
  • endpoints.path should be /swagger-stats/metrics
  • endpoints.port should match the service port name (web in the manifest above)
  • selector.matchLabels should match the Service labels (e.g. job: node-api or app: api)
Example ServiceMonitor (illustrative):
Note: Adjust metadata.labels.release or selector to match your Prometheus Operator setup.

6) Quick reference table

7) Next steps

  • Apply a ServiceMonitor (or PodMonitor) so the Prometheus Operator discovers and scrapes /swagger-stats/metrics.
  • Explore the metrics in Prometheus UI (typically http://<prometheus-service>:9090) and create Grafana dashboards.
  • Consider securing the metrics endpoint (if needed) and tuning scrape intervals.
References and further reading:

Watch Video