A distributed web scraping system using Dockerized Scrapyd instances with Kubernetes orchestration for processing 5,400 phrases hourly across multiple search engines.
┌─────────────────────────────────────────────────────────────┐
│ Central Job Scheduler │
│ (Distributes 5.4k phrases) │
└─────────────────────┬───────────────────────────────────────┘
│ HTTP API calls
│ (sends job batches)
┌─────────────┼─────────────┐
│ │ │
Scrapyd Pod 1 Scrapyd Pod 2 Scrapyd Pod 3 Scrapyd Pod 4
┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Scrapyd API │ │ Scrapyd API │ │ Scrapyd API │ │ Scrapyd API │
│ Web UI :6800 │ │ Web UI :6800 │ │ Web UI :6800 │ │ Web UI :6800 │
│ │ │ │ │ │ │ │
│ Google Spider│ │ Google Spider│ │ Google Spider│ │ Google Spider│
│ Bing Spider │ │ Bing Spider │ │ Bing Spider │ │ Bing Spider │
│ │ │ │ │ │ │ │
│ Job Queue │ │ Job Queue │ │ Job Queue │ │ Job Queue │
└──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘
- Distributed Processing: Multiple Scrapyd instances for parallel processing
- Auto-scaling: Kubernetes-based horizontal pod autoscaling
- Load Balancing: Intelligent job distribution across healthy services
- Web Monitoring: Built-in Scrapyd web interface and custom dashboard
- Fault Tolerance: Automatic recovery from failed services
- Database Integration: PostgreSQL with optimized phrase management functions
- Hourly Scheduling: Automated phrase activation every hour
- Container Orchestration: Full Docker and Kubernetes support
- Docker and Docker Compose
- Kubernetes cluster (for production)
- PostgreSQL database with
onm_phrasestable - Python 3.11+
- Clone the repository
git clone <your-repo> crawler-cluster
cd crawler-cluster- Start the development cluster
docker-compose -f docker-compose.scrapyd.yml up --build- Access the interfaces
- Scrapyd Web UI: http://localhost:6801-6804
- Job Scheduler API: http://localhost:8080
- Unified Interface: http://localhost:80
- Build and push images
docker build -f Dockerfile.scrapyd -t your-registry/crawler-scrapyd:latest .
docker build -f Dockerfile.scheduler -t your-registry/crawler-scheduler:latest .
docker push your-registry/crawler-scrapyd:latest
docker push your-registry/crawler-scheduler:latest- Deploy to Kubernetes
kubectl apply -f deployments/k8s-scrapyd-deployment.yml- Verify deployment
kubectl get pods -l app=scrapyd
kubectl get servicescrawler-cluster/
├── README.md # This file
├── docs/ # Documentation
│ ├── DEPLOYMENT.md # Deployment guide
│ ├── API.md # API documentation
│ ├── CONFIGURATION.md # Configuration guide
│ └── TROUBLESHOOTING.md # Troubleshooting guide
├── deployments/ # Deployment configurations
│ ├── k8s-scrapyd-deployment.yml # Kubernetes deployment
│ ├── docker-compose.scrapyd.yml # Docker Compose for dev
│ └── nginx.conf # Nginx configuration
├── dockerfiles/ # Docker configurations
│ ├── Dockerfile.scrapyd # Scrapyd container
│ └── Dockerfile.scheduler # Scheduler container
├── job_scheduler.py # Central job scheduler
├── scrapyd.conf # Scrapyd configuration
├── scrapy_crawler/ # Scrapy project
│ ├── spiders/ # Spider implementations
│ ├── settings.py # Scrapy settings
│ └── pipelines.py # Data processing pipelines
├── config/ # Configuration files
│ └── settings.py # Database and API settings
└── requirements.txt # Python dependencies
- Centralized job distribution system
- Manages phrase batches and assigns to least loaded Scrapyd instances
- Provides REST API for monitoring and control
- Handles hourly phrase activation
- Multiple containerized Scrapyd instances
- Each instance can run multiple concurrent spiders
- Built-in web interface for job monitoring
- Auto-discovery and health checking
- PostgreSQL with optimized phrase management
- Atomic phrase claiming with
claim_phrases_for_worker() - Automatic timeout handling and retry logic
- Status tracking and completion management
| Variable | Description | Default |
|---|---|---|
SCRAPYD_SERVICES |
Comma-separated list of Scrapyd URLs | - |
PG_HOST |
PostgreSQL host | localhost |
PG_DATABASE |
Database name | onm |
PG_USER |
Database user | onm_admin |
PG_PASSWORD |
Database password | - |
BATCH_SIZE |
Phrases per job batch | 30 |
MAX_CONCURRENT_JOBS |
Max jobs per Scrapyd instance | 4 |
See scrapyd.conf for detailed Scrapyd settings including:
- Process limits and concurrency
- Log and data storage paths
- API endpoint configurations
-
Scrapyd Web UI (
http://your-domain/scrapyd/)- View running, pending, and finished jobs
- Monitor spider performance
- Access logs and job details
-
Job Scheduler Dashboard (
http://your-domain/scheduler/)- System overview and statistics
- Service health status
- Active job monitoring
GET /health- System health checkGET /stats- Overall system statisticsGET /services- Scrapyd service statusGET /jobs- Active job details
- Scrapyd logs: Available through web interface and container logs
- Scheduler logs: Centralized logging with structured output
- Kubernetes logs:
kubectl logs -l app=scrapyd
- Hourly Activation: Scheduler activates 5,400 phrases every hour
- Batch Creation: Phrases grouped into batches of 30-50
- Service Selection: Least loaded healthy Scrapyd instance selected
- Job Scheduling: Batch sent to selected Scrapyd via REST API
- Spider Execution: Scrapyd runs Google/Bing spider with phrase batch
- Status Updates: Database updated with completion/failure status
- Monitoring: Progress tracked through web interfaces
- Throughput: ~1,350 phrases per server per hour
- Concurrency: 4 jobs per Scrapyd instance × 4 instances = 16 concurrent jobs
- Batch Size: 30 phrases per job for optimal performance
- Processing Time: ~2-3 minutes per batch average
- Create spider in
scrapy_crawler/spiders/search/ - Update scheduler configuration
- Rebuild Docker images
- Deploy updated containers
Horizontal Scaling (more instances):
kubectl scale deployment scrapyd-cluster --replicas=8Vertical Scaling (more resources):
resources:
requests:
memory: "1Gi"
cpu: "500m"
limits:
memory: "2Gi"
cpu: "1000m"-
No phrases being processed
- Check database connectivity
- Verify phrase activation status
- Review scheduler logs
-
Scrapyd instances unhealthy
- Check container resource limits
- Verify network connectivity
- Review Scrapyd logs
-
Jobs stuck in pending
- Check Scrapyd process limits
- Verify spider deployment
- Review job queue status
For detailed troubleshooting, see TROUBLESHOOTING.md.
- Deployment Guide - Detailed deployment instructions
- API Documentation - Complete API reference
- Configuration Guide - Configuration options
- Troubleshooting - Common issues and solutions
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests and documentation
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.