Spaces:
Sleeping
Sleeping
File size: 8,521 Bytes
aeb3f7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# Deployment Guide
## Prerequisites
- Docker 20.10+ and Docker Compose 2.0+
- Python 3.9+ (for local deployment)
- 4GB RAM minimum (8GB recommended)
- 10GB disk space for models and cache
## Quick Deploy with Docker
### 1. Prepare Environment
```bash
# Clone repository
git clone https://github.com/yourusername/writing-studio.git
cd writing-studio
# Copy and configure environment
cp .env.example .env
nano .env # Edit configuration
```
### 2. Deploy Application
```bash
# Start application
docker-compose up -d
# View logs
docker-compose logs -f
# Check status
docker-compose ps
```
### 3. Verify Deployment
```bash
# Check application health
curl http://localhost:7860
# Check metrics endpoint
curl http://localhost:8000
```
## Production Deployment
### Environment Configuration
```bash
# .env for production
ENVIRONMENT=production
DEBUG=false
LOG_LEVEL=INFO
# Security
SECRET_KEY=<generate-with-openssl-rand-base64-32>
ALLOWED_ORIGINS=https://yourdomain.com
ENABLE_AUTH=true
RATE_LIMIT_PER_MINUTE=30
# Performance
ENABLE_CACHE=true
CACHE_MAX_SIZE=1000
SERVER_WORKERS=4
# Monitoring
ENABLE_METRICS=true
LOG_FORMAT=json
```
### Reverse Proxy Setup (Nginx)
```nginx
# /etc/nginx/sites-available/writing-studio
upstream writing_studio {
server 127.0.0.1:7860;
}
server {
listen 80;
server_name writing.yourdomain.com;
# Redirect to HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name writing.yourdomain.com;
# SSL configuration
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
# Proxy settings
location / {
proxy_pass http://writing_studio;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
# Metrics endpoint (restrict access)
location /metrics {
deny all;
}
}
```
### SSL/TLS Setup
```bash
# Using Let's Encrypt
sudo apt-get install certbot python3-certbot-nginx
sudo certbot --nginx -d writing.yourdomain.com
```
## Cloud Deployments
### AWS ECS Deployment
1. **Build and Push Image**
```bash
# Tag for ECR
docker tag writing-studio:latest \
<account-id>.dkr.ecr.<region>.amazonaws.com/writing-studio:latest
# Push to ECR
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/writing-studio:latest
```
2. **ECS Task Definition** (`task-definition.json`)
```json
{
"family": "writing-studio",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "writing-studio",
"image": "<account-id>.dkr.ecr.<region>.amazonaws.com/writing-studio:latest",
"portMappings": [
{"containerPort": 7860, "protocol": "tcp"},
{"containerPort": 8000, "protocol": "tcp"}
],
"environment": [
{"name": "ENVIRONMENT", "value": "production"},
{"name": "LOG_LEVEL", "value": "INFO"}
],
"secrets": [
{
"name": "SECRET_KEY",
"valueFrom": "arn:aws:secretsmanager:region:account:secret:writing-studio/secret-key"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/writing-studio",
"awslogs-region": "<region>",
"awslogs-stream-prefix": "ecs"
}
},
"healthCheck": {
"command": ["CMD-SHELL", "curl -f http://localhost:7860 || exit 1"],
"interval": 30,
"timeout": 5,
"retries": 3
}
}
],
"requiresCompatibilities": ["FARGATE"],
"cpu": "1024",
"memory": "4096"
}
```
### Google Cloud Run
```bash
# Build for Cloud Run
gcloud builds submit --tag gcr.io/PROJECT-ID/writing-studio
# Deploy
gcloud run deploy writing-studio \
--image gcr.io/PROJECT-ID/writing-studio \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--memory 4Gi \
--cpu 2 \
--port 7860 \
--set-env-vars ENVIRONMENT=production
```
### Kubernetes Deployment
**deployment.yaml**:
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: writing-studio
spec:
replicas: 3
selector:
matchLabels:
app: writing-studio
template:
metadata:
labels:
app: writing-studio
spec:
containers:
- name: writing-studio
image: writing-studio:latest
ports:
- containerPort: 7860
name: http
- containerPort: 8000
name: metrics
env:
- name: ENVIRONMENT
value: "production"
- name: SECRET_KEY
valueFrom:
secretKeyRef:
name: writing-studio-secrets
key: secret-key
resources:
requests:
memory: "2Gi"
cpu: "1000m"
limits:
memory: "4Gi"
cpu: "2000m"
livenessProbe:
httpGet:
path: /
port: 7860
initialDelaySeconds: 60
periodSeconds: 30
readinessProbe:
httpGet:
path: /
port: 7860
initialDelaySeconds: 30
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: writing-studio
spec:
selector:
app: writing-studio
ports:
- name: http
port: 80
targetPort: 7860
- name: metrics
port: 8000
targetPort: 8000
type: LoadBalancer
```
## Monitoring Setup
### Prometheus Configuration
```yaml
# prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'writing-studio'
static_configs:
- targets: ['writing-studio:8000']
metrics_path: '/metrics'
```
### Grafana Dashboard
Import the provided dashboard:
```bash
# Import from grafana.com or use provided JSON
curl -X POST http://admin:admin@localhost:3000/api/dashboards/db \
-H "Content-Type: application/json" \
-d @configs/grafana-dashboard.json
```
## Backup and Recovery
### Data Backup
```bash
# Backup logs
tar -czf logs-backup-$(date +%Y%m%d).tar.gz logs/
# Backup models
tar -czf models-backup-$(date +%Y%m%d).tar.gz models/
# Backup configuration
cp .env .env.backup
```
### Database Backup (if using)
```bash
# PostgreSQL
pg_dump writing_studio > backup-$(date +%Y%m%d).sql
# Restore
psql writing_studio < backup-20240101.sql
```
## Scaling Strategies
### Horizontal Scaling
```bash
# Docker Compose
docker-compose up -d --scale app=3
# Kubernetes
kubectl scale deployment writing-studio --replicas=5
```
### Load Balancing
```nginx
upstream writing_studio {
least_conn;
server app1:7860 weight=3;
server app2:7860 weight=3;
server app3:7860 weight=2;
}
```
## Troubleshooting
### Common Issues
**Container won't start**:
```bash
# Check logs
docker-compose logs app
# Check resources
docker stats
# Verify environment
docker-compose config
```
**High memory usage**:
```bash
# Reduce cache size
CACHE_MAX_SIZE=50
# Use smaller model
DEFAULT_MODEL=distilgpt2
# Limit workers
SERVER_WORKERS=2
```
**Slow response times**:
```bash
# Enable caching
ENABLE_CACHE=true
# Increase workers
SERVER_WORKERS=8
# Use faster model
DEFAULT_MODEL=distilgpt2
```
## Security Checklist
- [ ] Change default SECRET_KEY
- [ ] Enable HTTPS/TLS
- [ ] Configure CORS properly
- [ ] Enable rate limiting
- [ ] Set up authentication
- [ ] Restrict metrics endpoint
- [ ] Regular security updates
- [ ] Monitor logs for suspicious activity
- [ ] Use non-root Docker user
- [ ] Implement network policies
## Maintenance
### Regular Tasks
```bash
# Update dependencies
pip install --upgrade -r requirements.txt
# Clean old logs
find logs/ -name "*.log" -mtime +30 -delete
# Clear old models
find models/ -name "*" -mtime +90 -delete
# Restart service
docker-compose restart app
```
### Updates
```bash
# Pull latest changes
git pull origin main
# Rebuild image
docker-compose build
# Deploy with zero downtime
docker-compose up -d --no-deps --build app
```
|