How to Create a Custom Prometheus Scrapeable Endpoint for testing in kubernetes

Anant SarafAnant Saraf
3 min read

Copy these 2 files in your machine

$ ls -l
total 2
-rw-r--r-- 1 Dell 197121 382 Mar 21 13:23 Dockerfile
-rw-r--r-- 1 Dell 197121 605 Mar 21 13:22 prometheus-scrapping-app.py

Following is app.py

from flask import Flask, Response
from prometheus_client import Counter, generate_latest

app = Flask(__name__)

# Define a counter metric to track requests
REQUESTS = Counter('myapp_requests_total', 'Total number of requests received.')

@app.route('/')
def hello():
    # Increment the requests counter
    REQUESTS.inc()

    # Return a simple response
    return 'Hello, World!'

@app.route('/metrics')
def metrics():
    # Return the Prometheus metrics data
    return Response(generate_latest(), mimetype="text/plain")

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=8080)

Create Dockerfile to create docker image out of it

# Use a minimal base image for Python applications
FROM python:3.8-slim AS base

# Install dependencies
RUN pip install flask prometheus_client

# Set the working directory
WORKDIR /app

# Copy the Python application source code into the container
COPY app.py .

# Expose port 8080 to the outside world
EXPOSE 8080

# Command to run the Python application
CMD ["python", "app.py"]

Run following command (in directory where prometheus-scrapping-app.py and docker files are present ,only then it will work)

$ docker build -t prometheus-python-scrap-matrics-image .
[+] Building 3.8s (10/10) FINISHED                                                                                                             docker:default
 => [internal] load build definition from Dockerfile                                                                                                     0.0s
 => => transferring dockerfile: 421B                                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                                        0.0s
 => => transferring context: 2B                                                                                                                          0.0s
 => [internal] load metadata for docker.io/library/python:3.8-slim                                                                                       3.4s
 => [auth] library/python:pull token for registry-1.docker.io                                                                                            0.0s
 => [1/4] FROM docker.io/library/python:3.8-slim@sha256:95bfecec648356cdd0b28c8b00ce00009baff10c99d1126a82d1aca716453a1a                                 0.0s
 => [internal] load build context                                                                                                                        0.0s
 => => transferring context: 28B                                                                                                                         0.0s
 => CACHED [2/4] RUN pip install flask prometheus_client                                                                                                 0.0s
 => CACHED [3/4] WORKDIR /app                                                                                                                            0.0s
 => CACHED [4/4] COPY app.py .                                                                                                                           0.0s
 => exporting to image                                                                                                                                   0.0s
 => => exporting layers                                                                                                                                  0.0s
 => => writing image sha256:126aab7da11d834ea5272f009600f6b8f1e28a71512b69783038f49edbdda1cd                                                             0.0s
 => => naming to docker.io/library/prometheus-python-scrap-matrics-image
$ docker image ls
REPOSITORY                                       TAG       IMAGE ID       CREATED          SIZE
prometheus-python-scrap-matrics-image            latest    126aab7da11d   30 minutes ago   138MB

Tag image to push it to your docker repo

$ docker tag prometheus-python-scrap-matrics-image anantgsaraf/prometheus-scrape-matrics-python-image:1.0.0
$ docker image ls
REPOSITORY                                           TAG       IMAGE ID       CREATED          SIZE
anantgsaraf/prometheus-scrape-matrics-python-image   1.0.0     126aab7da11d   32 minutes ago   138MB
$ docker push anantgsaraf/prometheus-scrape-matrics-python-image:1.0.0
The push refers to repository [docker.io/anantgsaraf/prometheus-scrape-matrics-python-image]
c02039964101: Pushed
a66e2cf139a1: Pushed
5e3c97c386cf: Pushed
ee5c08f2b2b8: Mounted from library/python
b450bdba9f84: Mounted from library/python
937886fc1a47: Mounted from library/python
c8f253aef560: Mounted from library/python
a483da8ab3e9: Mounted from library/python
1.0.0: digest: sha256:02f26e620ad7c34a35352f71a778609e0fe42bd5162289c379c45e71a35d9e7a size: 1994

You can run it on docker it listens on port 8080

$ docker run -d -p 8080:8080 --name myapp-container myapp
3a1f2cd80cae81601f7ddd6c323d858f27e742c1580fe029b932d866ec2f5363
$ docker ps
CONTAINER ID   IMAGE                                                      COMMAND                  CREATED         STATUS         PORTS
                                                                                                           NAMES
bd1719e493d3   anantgsaraf/prometheus-scrape-matrics-python-image:1.0.0   "python app.py"          9 seconds ago   Up 6 seconds   0.0.0.0:8080->8080/tcp
                                                                                                           prometheus-scrap-container

Test it on browser

Another url for same application

0
Subscribe to my newsletter

Read articles from Anant Saraf directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anant Saraf
Anant Saraf