- A minimal Node.js app instrumented with
swagger-statsfor 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
- Prometheus: https://prometheus.io/
- Prometheus Operator: https://github.com/prometheus-operator/prometheus-operator
- Kubernetes: https://kubernetes.io/
- Node.js: https://nodejs.org/
- Express: https://expressjs.com/
- swagger-stats: https://github.com/sladkovm/swagger-stats
- Docker Hub: https://hub.docker.com/
- Dockerfile reference: https://docs.docker.com/engine/reference/builder/
1) Example Node.js application (index.js)
This minimal Express app exposes a few endpoints and usesswagger-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:your-dockerhub-username/prometheus-demo with your repository:
3) Kubernetes manifest (Deployment + Service)
Create a file calledapi-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
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: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 aServiceMonitor to tell Prometheus to scrape the application. The important parts are:
endpoints.pathshould be/swagger-stats/metricsendpoints.portshould match the service port name (webin the manifest above)selector.matchLabelsshould match the Service labels (e.g.job: node-apiorapp: api)
metadata.labels.release or selector to match your Prometheus Operator setup.
6) Quick reference table
7) Next steps
- Apply a
ServiceMonitor(orPodMonitor) 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.
- Prometheus documentation: https://prometheus.io/docs/
- Prometheus Operator: https://github.com/prometheus-operator/prometheus-operator
- Kubernetes Services: https://kubernetes.io/docs/concepts/services-networking/service/
- swagger-stats: https://github.com/sladkovm/swagger-stats