Distributed IoT Gateway System with Edge ML for Distrubuted Systems 2025
CONTENTS:
- Prerequisites
- Setup Instructions
- Running the Server
- Running the Client
- Verifying Data in MongoDB
- Troubleshooting
- PREREQUISITES
- Python 3.11+ installed
- A local or remote MongoDB instance:
- Local: Download & install MongoDB Community Edition
- Remote: MongoDB Atlas account with a connection URI
- Installed Python dependencies (see requirements.txt):
- grpcio, grpcio-tools, pymongo
- Installed Java (JDK)
- Installed Kafka
- Installed Docker (http://localhost:9090)
- Installed/updated WSL 2 if needed (Windows)
- Installed Grafana (http://localhost:3000 )
- Install wait-for-it.sh in the current working directory for holding the other services until Kafka runs
- curl -O https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh
- SETUP INSTRUCTIONS
-
CLONE THE REPOSITORY git clone <REPO_URL> cd <PROJECT_FOLDER>
-
CREATE AND ACTIVATE A VIRTUAL ENVIRONMENT python -m venv venv
venv\Scripts\activate
source venv/bin/activate
-
INSTALL REQUIRED DEPENDENCIES pip install -r requirements.txt
-
ENSURE MONGODB IS RUNNING
- Local:
- mongod
- mongosh --host localhost --port 27017
- MongoDB Atlas: Make sure your connection URI is correct in server.py
- Local:
-
KAFKA INSTALLATION AND SETUP (macOS)
- Install: brew install kafka
- Start Kafka Service:
- brew services start zookeeper
- brew services start kafka
- Verify Kafka Service is Running: kafka-topics --list --bootstrap-server localhost:9092
- Install JDK: brew install openjdk
KAFKA SETUP (Windows)
- Start Zookeeper
- In powershell: cd C:\kafka_2.13-3.9.0 .\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties
- Start Kafka
- In another powershell window: cd C:\kafka_2.13-3.9.0 .\bin\windows\kafka-server-start.bat .\config\server.properties
-
CREATE KAFKA TOPIC kafka-topics --create --topic workflow-events --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
CREATE KAFKA TOPIC (Windows)
- in powershell: .\bin\windows\kafka-topics.bat --create --topic topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
- RUNNING THE SERVER
LOCALLY:
- Open a terminal in the project folder.
- Start the gRPC server: python server.py
- The server listens on port 50051 by default.
- Start the Kafka Consumer to Listen to Events: python kafka_consumer.py
- Metrics at http://127.0.0.1:8000/metrics
DOCKER: in the terminal from the project directory docker-compose down --volumes # Stop and clean up docker-compose build # Rebuild the images docker-compose up -d # Start in detached mode docker-compose logs -f # Follow logs to ensure services are running
- RUNNING THE CLIENT
-
Open another terminal (keep the server running).
-
Run the client to send synthetic sensor data: python client.py
The client sends multiple data points to the gRPC server and kafka consumer, which in turn stores them in MongoDB. The client uses an OpenAI API to get insights on anomalous data detected.
- VERIFYING DATA IN MONGODB
A) MONGODB COMPASS:
- Launch MongoDB Compass.
- Connect to your MongoDB instance (e.g., mongodb://localhost:27017).
- Find the "iot_db" database and "sensor_data" collection.
- Check for newly inserted documents containing fields: device_id, timestamp, temperature, humidity, etc.
B) MONGO SHELL (OPTIONAL):
- Run: mongo
- Switch to the database: use iot_db
- Query the collection: db.sensor_data.find().pretty()
- TROUBLESHOOTING
-
If you receive "Connection refused" or "Failed to connect to server":
- Make sure your MongoDB instance is running (mongod).
- Check the database URI in server.py for typos.
-
If "ModuleNotFoundError" for grpcio or pymongo:
- Make sure you've installed dependencies with: pip install -r requirements.txt
- Confirm you are in the correct Python virtual environment.
-
If data doesn't appear in Compass:
- Refresh Compass or ensure you're looking at the correct database and collection.
- Check the server logs for any errors during insertion.
-
For Kafka, we need to install Kafka-python-ng otherwise, it shows a module not found error.
- Docker installed.
- Kubernetes cluster ( Minikube and Kind).
- Python and pip installed for local development.
-
docker exec -it py-spy top --pid 1 Set Up Monitoring and apply Prometheus and Grafana configurations:
-
kubectl apply -f prometheus.yml
-
kubectl apply -f grafana.yml Forward Prometheus and Grafana ports:
kubectl port-forward svc/prometheus 9090:9090 kubectl port-forward svc/grafana 3000:3000 Access Prometheus at http://localhost:9090/targets and Grafana at http://localhost:3000.
- Install Locust:
- pip install locust
- locust -f locustfile.py --host=http://localhost:8000
- Open the Locust web interface at http://localhost:8089 to configure and start the test.
- Prerequisites
-
Kubernetes cluster (e.g., Minikube or Kind).
-
kubectl configured to interact with the cluster.
-
All Docker images pushed to a container registry (e.g., Docker Hub).
-
Steps to Reproduce
-
Apply Kubernetes Manifests :
-
Deploy all services using the provided YAML files:
- kubectl apply -f ZookeeperDeployment.yml
- kubectl apply -f CRUDServiceDeployment.yml
- kubectl apply -f GrafanaDeployment.yml
- kubectl apply -f MongoDBDeployment.yml
- kubectl apply -f prometheus-deployment.yml
- kubectl apply -f gRPCServiceDeployment.yml
- kubectl apply -f KafkaConsumerDeployment.yml
- kubectl apply -f KafkaDeployment.yml
- kubectl apply -f HPAforCRUDService.yml
- kubectl apply -f gRPCService.yml
-
-
Verify Pods and Services :
- kubectl get pods
-
Check service status:
- kubectl get svc
-
Test Horizontal Pod Autoscaling (HPA) : Verify HPA configuration:
- kubectl get hpa
- kubectl describe hpa crud-service-hpa
-
Simulate traffic using Locust:
-
locust -f locustfile.py --host=http://:8000 Observe scaling behavior:
-
kubectl get pods -l app=crud-service --watch
-
-
Access Monitoring Tools : Forward Prometheus and Grafana ports:
- kubectl port-forward svc/prometheus 9090:9090
- kubectl port-forward svc/grafana 3000:3000
- Access Prometheus at http://localhost:9090/targets and Grafana at http://localhost:3000.
-
Test Fault Tolerance :
- kubectl delete pod -l app=crud-service
- kubectl delete pod -l app=kafka
- kubectl delete pod -l app=mongodb
-
Verify recovery:
- kubectl get pods --watch
-
Optional: Test DNS Resolution : From the Prometheus pod:
- kubectl exec -it -- nslookup crud-service From any pod:
- kubectl exec -it -- curl http://crud-service:8000/metrics
-
Ensure all Docker images are built and pushed to a registry before deploying:
-
docker build -t /iot_gateway-:latest -f Dockerfile. .
-
docker push /iot_gateway-:latest
-
If using Minikube, ensure the Metrics Server is installed for HPA:
- minikube addons enable metrics-server
For
-
kubectl exec -it prometheus-669ffdb99b-xs7hc -- nslookup crud-service
-
kubectl get pods -n kube-system | grep metrics-server
- chmod +x apply-all.sh
- ./apply-all.sh
- kubectl get pods
- kubectl get svc
- kubectl get hpa
- kubectl describe hpa crud-service-hpa
- kubectl port-forward svc/prometheus 9090:9090
- kubectl port-forward svc/grafana 3000:3000
- locust -f locustfile.py --host=http://:8000
LocustVideo.mp4
- kubectl delete pod -l app=crud-service
- kubectl delete pod -l app=kafka
- kubectl delete pod -l app=mongodb
kubectl apply -f ZookeeperDeployment.yml kubectl apply -f CRUDServiceDeployment.yml kubectl apply -f GrafanaDeployment.yml kubectl apply -f MongoDBDeployment.yml kubectl apply -f gRPCServiceDeployment.yml kubectl apply -f KafkaConsumerDeployment.yml kubectl apply -f KafkaDeployment.yml kubectl apply -f HPAforCRUDService.yml kubectl apply -f gRPCService.yml kubectl apply -f prometheus-deployment.yml
kubectl apply -f prometheus-pvc.yml kubectl apply -f mongo-pvc.yml kubectl apply -f kafka-pvc.yml kubectl apply -f gRPCServicePVC.yml kubectl apply -f gRPCClientPVC.yml kubectl apply -f grafana-pvc.yml kubectl apply -f zookeeper-pvc.yml


