Skip to content

Instantly share code, notes, and snippets.

@misTrasteos
Last active February 2, 2021 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misTrasteos/7356e86018e714d2857cd90b8c766eba to your computer and use it in GitHub Desktop.
Save misTrasteos/7356e86018e714d2857cd90b8c766eba to your computer and use it in GitHub Desktop.
generate a Docker image from a python script

Files

requirements.txt

prometheus-client==0.9.0

client.py

from prometheus_client import start_http_server, Counter
import time

start_http_server(8000)

c = Counter('number_of_something', 'This is the number of times something has happened')

while True:
	time.sleep(1)
	c.inc()	

Dockerfile

FROM python:3.8-slim
WORKDIR /usr/src/app
EXPOSE 8000
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY client.py ./
CMD [ "python", "./client.py" ]

Building the image

docker build -t python-prometheus:1 .

Run it inside kubernetes

As I am using kind, so I first need to load the image into my cluster.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-prometheus-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: python-prometheus
  template:
    metadata:
      labels:
        app: python-prometheus
    spec:
      containers:
      - name: python-prometheus
        image: python_prometheus:1
        ports:
        - name: prometheus
          containerPort: 8000

testing

kubectl get pods
NAME                                            READY   STATUS    RESTARTS   AGE
python-prometheus-deployment-6f694dc8dc-ws75n   1/1     Running   0          9m3s

kubectl port-forward python-prometheus-deployment-6f694dc8dc-ws75n 8000
Forwarding from 127.0.0.1:8000 -> 8000
Forwarding from [::1]:8000 -> 8000
curl http://localhost:8000
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 98.0
python_gc_objects_collected_total{generation="1"} 257.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 35.0
python_gc_collections_total{generation="1"} 3.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="8",patchlevel="7",version="3.8.7"} 1.0
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 2.52416e+08
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 1.9566592e+07
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.6123015108e+09
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.27
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 6.0
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1.048576e+06
# HELP number_of_something_total This is the number of times something has happened
# TYPE number_of_something_total counter
number_of_something_total 513.0
# HELP number_of_something_created This is the number of times something has happened
# TYPE number_of_something_created gauge
number_of_something_created 1.6123015111102307e+09

Some Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment