File size: 5,874 Bytes
2eef9ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Deployment Guide

## Production Checklist

- [ ] All environment variables configured
- [ ] Database migrations run
- [ ] Redis cache configured
- [ ] API keys secured (use secrets management)
- [ ] CORS configured properly
- [ ] Rate limiting enabled
- [ ] Logging configured
- [ ] SSL/TLS certificates installed
- [ ] Health checks passing
- [ ] Load testing completed

## Deployment Options

## 1. Docker Compose (Recommended for Small Deployments)

```bash
# Build and start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down
```

## 2. Kubernetes

### Prerequisites
- Kubernetes cluster (1.24+)
- kubectl configured
- Container registry access

### Deploy

```bash
# Create namespace
kubectl create namespace multi-agent

# Create secrets for API keys
kubectl create secret generic api-keys \
  --from-literal=openai-api-key=sk-... \
  --from-literal=database-url=postgresql://... \
  -n multi-agent

# Apply deployment
kubectl apply -f k8s/ -n multi-agent

# Check status
kubectl get pods -n multi-agent
kubectl logs -f deployment/backend -n multi-agent
```

## 3. Cloud Platforms

### AWS (ECS/Fargate)

1. Push image to ECR
2. Create ECS task definition
3. Create ECS service
4. Configure RDS for database
5. Configure ElastiCache for Redis

### Google Cloud (Cloud Run)

```bash
# Build and push
gcloud builds submit --tag gcr.io/PROJECT/multi-agent

# Deploy
gcloud run deploy multi-agent \
  --image gcr.io/PROJECT/multi-agent \
  --platform managed \
  --region us-central1
```

### Azure (App Service)

```bash
# Create resource group
az group create -n multi-agent-rg -l eastus

# Create app service plan
az appservice plan create -n multi-agent-plan \
  -g multi-agent-rg --sku B2 --is-linux

# Deploy container
az webapp create -n multi-agent -g multi-agent-rg \
  -p multi-agent-plan --deployment-container-image-name-user-provided
```

### Heroku

```bash
# Login
heroku login

# Create app
heroku create multi-agent

# Set environment variables
heroku config:set OPENAI_API_KEY=sk-...
heroku config:set DATABASE_URL=postgresql://...

# Deploy
git push heroku main

# View logs
heroku logs --tail
```

## Environment Variables

For production, secure these variables:

```bash
# Critical
OPENAI_API_KEY=sk-your-production-key
DATABASE_URL=postgresql://user:pass@host/db
REDIS_URL=redis://host:6379/0

# Security
ENVIRONMENT=production
CORS_ORIGINS=https://yourdomain.com
API_KEY_SECRET=your-secret-key

# Logging
LOG_LEVEL=INFO
SENTRY_DSN=https://...  # Optional: error tracking

# Performance
WORKER_PROCESSES=4
MAX_CONNECTIONS=100
```

## Database Migrations

```bash
# Using Alembic (if configured)
alembic upgrade head

# Or with SQLAlchemy directly
python -c "from backend.core.config import init_db; init_db()"
```

## Monitoring & Logging

### Application Logs

Use structured logging to centralize logs:

```bash
# Docker
docker-compose logs -f backend

# Kubernetes
kubectl logs -f deployment/backend

# Cloud services
# Check respective platform dashboards
```

### Metrics to Monitor

- Response time (p50, p95, p99)
- Error rate
- Task completion rate
- API usage (requests/minute)
- Memory/CPU usage
- Database connection pool health
- Queue depth (if using task queue)

### Recommended Tools

- **Monitoring**: Prometheus + Grafana
- **Logging**: ELK Stack, Datadog, or cloud provider
- **Error Tracking**: Sentry
- **APM**: New Relic, DataDog

## Scaling

### Horizontal Scaling

```bash
# Docker Compose
docker-compose up -d --scale backend=3

# Kubernetes
kubectl scale deployment backend --replicas=3
```

### Performance Optimization

1. **Cache frequently accessed data**
   ```python
   # In backend code
   @cache.cached(timeout=300)
   def expensive_operation():
       pass
   ```

2. **Use connection pooling**
   - Redis already handles this
   - Database: SQLAlchemy creates connection pool automatically

3. **Optimize database queries**
   - Add indexes to frequently queried columns
   - Use query profiling tools

## Security

### API Security

1. **Enable HTTPS/TLS** (required for production)
2. **Add API authentication**
   ```python
   from fastapi import Security, HTTPBearer
   security = HTTPBearer()
   ```

3. **Implement rate limiting**
   ```python
   from slowapi import Limiter
   limiter = Limiter(key_func=get_remote_address)
   ```

4. **Add CORS restrictions**
   ```python
   CORSMiddleware(
       allow_origins=["https://yourdomain.com"],
       allow_credentials=True,
   )
   ```

### Data Security

- Encrypt sensitive data at rest
- Use environment variables for secrets (never commit .env)
- Regular security audits
- Keep dependencies updated

## Backup & Recovery

```bash
# Backup PostgreSQL
pg_dump multi_agent > backup.sql

# Restore
psql multi_agent < backup.sql

# Backup Redis
redis-cli BGSAVE
# RDB file: /var/lib/redis/dump.rdb

# Docker volume backup
docker-compose exec postgres pg_dump multi_agent > backup.sql
```

## Troubleshooting

### Service Won't Start

```bash
# Check logs
docker-compose logs backend

# Verify environment
docker-compose config

# Reset containers
docker-compose down -v
docker-compose up --build
```

### High Memory Usage

- Check for memory leaks in agent code
- Reduce worker processes
- Increase available memory
- Monitor with: `docker stats`

### Slow Queries

```bash
# Enable query logging
# In .env: SQLALCHEMY_ECHO=true

# Analyze slow queries
# Enable PostgreSQL slow query log
```

## Rollback Procedure

```bash
# Docker
docker-compose down
docker-compose pull  # Get previous version
docker-compose up

# Kubernetes
kubectl rollout history deployment/backend
kubectl rollout undo deployment/backend --to-revision=1

# Heroku
heroku releases
heroku rollback v123
```

## Support & Documentation

- GitHub Issues: Report bugs
- Documentation: `./docs/`
- API Docs: http://yourapp.com/docs