Python SDK Example for QWorker
This guide demonstrates how to use the qscaler_sdk
Python library to create and deploy a worker that processes tasks from a queue. The worker integrates seamlessly with QScaler to support dynamic scaling and resource optimization.
Example Code
Here is an example Python worker implementation using the qscaler_sdk
:
import logging
import time
from typing import Dict, Any
from qscaler_sdk.worker import Worker
worker = Worker()
@worker.shutdown
def shutdown():
print("Shutting down worker...")
@worker.task
def example(task: Dict[str, Any]) -> Any:
print("hello this is an example")
time.sleep(5)
if __name__ == "__main__":
logging.basicConfig()
worker.k8s_client.extract_secret_value("redis", "redis-password")
worker.run()
Explanation
-
Initialization:
- A
Worker
instance is created to manage tasks and integration with QScaler.
- A
-
Task Definition:
- The
@worker.task
decorator defines a task function. This function processes individual tasks pulled from the queue.
- The
-
Shutdown Hook:
- The
@worker.shutdown
decorator defines a cleanup function to be executed during shutdown.
- The
-
Run the Worker:
- The
worker.run()
method starts the worker loop, which continuously pulls and processes tasks.
- The
-
Kubernetes Integration:
- The
worker.k8s_client.extract_secret_value
method retrieves secrets, such as the Redis password, from Kubernetes.
- The
Example QWorker Resource
Below is the YAML configuration for deploying the worker in Kubernetes:
apiVersion: quickube.com/v1alpha1
kind: QWorker
metadata:
labels:
app.kubernetes.io/name: qworker
name: qworker-example
spec:
podSpec:
serviceAccountName: qscaler-worker
containers:
- name: pyworker
image: localhost:5001/worker:latest
imagePullPolicy: Always
scaleConfig:
activateVPA: true
queue: "queue1"
minReplicas: 1
maxReplicas: 5
scalerConfigRef: redis-config
scalingFactor: 1
Key Points
-
podSpec
:- Defines the container image and resource requirements for the worker pod.
-
scaleConfig
:- Configures scaling parameters, such as the queue to monitor, minimum and maximum replicas, and scaling factor.
Dockerfile for Worker
Here is the Dockerfile
to containerize the Python worker:
FROM python:3.11-alpine
WORKDIR /app
RUN pip install poetry
COPY . .
RUN poetry env use python
RUN poetry install
COPY ./examples/worker.py ./worker.py
CMD ["poetry", "run", "python3", "/app/worker.py"]
Build and Push Image
-
Build the Docker image:
-
Push the image to your local registry:
Deploying the Worker
-
Apply the
QWorker
resource to your Kubernetes cluster: -
Verify the deployment: ```bash kubectl get pods -l app.kubernetes.io/name=qworker