Deploy agents to production with confidence
Introduction to deploying Fantomu agents to production
Learn how to deploy your Fantomu agents to production environments with confidence and reliability.
// Development configuration
const config = {
environment: 'development',
logLevel: 'debug',
apiUrl: 'https://api-dev.fantomu.com',
database: {
host: 'localhost',
port: 5432,
name: 'fantomu_dev'
}
};
// Staging configuration
const config = {
environment: 'staging',
logLevel: 'info',
apiUrl: 'https://api-staging.fantomu.com',
database: {
host: 'staging-db.fantomu.com',
port: 5432,
name: 'fantomu_staging'
}
};
// Production configuration
const config = {
environment: 'production',
logLevel: 'warn',
apiUrl: 'https://api.fantomu.com',
database: {
host: 'prod-db.fantomu.com',
port: 5432,
name: 'fantomu_prod'
}
};
// Blue-green deployment
const deployment = {
strategy: 'blue-green',
current: 'blue',
target: 'green',
switchover: 'automatic',
rollback: 'automatic'
};
// Rolling deployment
const deployment = {
strategy: 'rolling',
batchSize: 2,
maxUnavailable: 1,
timeout: 300
};
// Canary deployment
const deployment = {
strategy: 'canary',
trafficSplit: 10, // 10% to new version
duration: 3600, // 1 hour
successCriteria: {
errorRate: 0.01,
responseTime: 1000
}
};
# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
# docker-compose.yml
version: '3.8'
services:
agent:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- DATABASE_URL=postgresql://user:pass@db:5432/fantomu
depends_on:
- db
- redis
db:
image: postgres:15
environment:
- POSTGRES_DB=fantomu
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
volumes:
- postgres_data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
postgres_data:
# k8s-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: fantomu-agent
spec:
replicas: 3
selector:
matchLabels:
app: fantomu-agent
template:
metadata:
labels:
app: fantomu-agent
spec:
containers:
- name: agent
image: fantomu/agent:latest
ports:
- containerPort: 3000
env:
- name: NODE_ENV
value: "production"
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: fantomu-secrets
key: database-url
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
# k8s-service.yaml
apiVersion: v1
kind: Service
metadata:
name: fantomu-agent-service
spec:
selector:
app: fantomu-agent
ports:
- port: 80
targetPort: 3000
type: LoadBalancer
// vercel.json
{
"version": 2,
"builds": [
{
"src": "api/**/*.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/api/(.*)",
"dest": "/api/$1"
}
],
"env": {
"FANTOMU_API_KEY": "@fantomu-api-key"
}
}
// lambda.js
const { FantomuClient } = require('@fantomu/sdk');
exports.handler = async (event, context) => {
const client = new FantomuClient({
apiKey: process.env.FANTOMU_API_KEY
});
try {
const result = await client.agents.execute({
agentId: event.agentId,
task: event.task,
context: event.context
});
return {
statusCode: 200,
body: JSON.stringify(result)
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.message })
};
}
};
# .github/workflows/deploy.yml
name: Deploy to Production
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build
run: npm run build
- name: Deploy to AWS
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Deploy
run: aws s3 sync dist/ s3://my-bucket/
// Health check endpoint
app.get('/health', async (req, res) => {
try {
// Check database connection
await db.query('SELECT 1');
// Check external services
await checkExternalServices();
res.status(200).json({
status: 'healthy',
timestamp: new Date().toISOString(),
services: {
database: 'up',
external: 'up'
}
});
} catch (error) {
res.status(503).json({
status: 'unhealthy',
error: error.message
});
}
});
// Metrics collection
const prometheus = require('prom-client');
const httpRequestDuration = new prometheus.Histogram({
name: 'http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds',
labelNames: ['method', 'route', 'status_code']
});
const agentExecutionCounter = new prometheus.Counter({
name: 'agent_executions_total',
help: 'Total number of agent executions',
labelNames: ['agent_id', 'status']
});
# .env.production
NODE_ENV=production
FANTOMU_API_KEY=sk-prod-...
DATABASE_URL=postgresql://user:pass@host:5432/db
REDIS_URL=redis://host:6379
JWT_SECRET=your-jwt-secret
// Use AWS Secrets Manager
const AWS = require('aws-sdk');
const secretsManager = new AWS.SecretsManager();
const getSecret = async (secretName) => {
const result = await secretsManager.getSecretValue({
SecretId: secretName
}).promise();
return JSON.parse(result.SecretString);
};
// Automatic rollback configuration
const rollback = {
enabled: true,
triggers: {
errorRate: 0.05, // 5% error rate
responseTime: 5000, // 5 second response time
healthCheckFailures: 3
},
rollbackTo: 'previous_version',
notification: ['admin@company.com']
};
# Rollback to previous version
kubectl rollout undo deployment/fantomu-agent
# Or using Docker
docker service update --image fantomu/agent:v1.0.0 fantomu-agent
Scale your agents to handle increased load
Learn how to scale your Fantomu agents to handle increased load and ensure optimal performance.
Add more instances to handle increased load.
// Auto-scaling configuration
const scaling = {
type: 'horizontal',
minInstances: 2,
maxInstances: 10,
targetCPU: 70,
targetMemory: 80,
scaleUpCooldown: 300,
scaleDownCooldown: 600
};
Increase resources for existing instances.
// Vertical scaling configuration
const scaling = {
type: 'vertical',
cpu: {
min: '500m',
max: '2000m'
},
memory: {
min: '512Mi',
max: '4Gi'
}
};
# ALB configuration
apiVersion: v1
kind: Service
metadata:
name: fantomu-agent-alb
spec:
type: LoadBalancer
selector:
app: fantomu-agent
ports:
- port: 80
targetPort: 3000
sessionAffinity: ClientIP
// Round robin load balancing
const loadBalancer = {
algorithm: 'round_robin',
healthCheck: {
path: '/health',
interval: 30,
timeout: 5,
healthyThreshold: 2,
unhealthyThreshold: 3
}
};
// Least connections load balancing
const loadBalancer = {
algorithm: 'least_connections',
stickySessions: true,
sessionTimeout: 3600
};
// Read replica configuration
const database = {
primary: {
host: 'db-primary.fantomu.com',
port: 5432,
role: 'master'
},
replicas: [
{
host: 'db-replica-1.fantomu.com',
port: 5432,
role: 'slave'
},
{
host: 'db-replica-2.fantomu.com',
port: 5432,
role: 'slave'
}
]
};
// Connection pool configuration
const pool = {
min: 5,
max: 50,
acquireTimeoutMillis: 60000,
idleTimeoutMillis: 300000,
createTimeoutMillis: 30000,
destroyTimeoutMillis: 5000
};
// Database sharding configuration
const sharding = {
strategy: 'hash',
shards: [
{
id: 'shard-1',
host: 'db-shard-1.fantomu.com',
range: '0-33'
},
{
id: 'shard-2',
host: 'db-shard-2.fantomu.com',
range: '34-66'
},
{
id: 'shard-3',
host: 'db-shard-3.fantomu.com',
range: '67-99'
}
]
};
// Redis cache configuration
const cache = {
type: 'redis',
host: 'redis-cluster.fantomu.com',
port: 6379,
ttl: 3600,
maxMemory: '2gb',
evictionPolicy: 'allkeys-lru'
};
// CDN cache configuration
const cdn = {
provider: 'cloudflare',
cacheRules: [
{
path: '/api/static/*',
ttl: 86400 // 24 hours
},
{
path: '/api/dynamic/*',
ttl: 300 // 5 minutes
}
]
};
// Application cache configuration
const appCache = {
type: 'memory',
maxSize: 1000,
ttl: 1800, // 30 minutes
strategy: 'lru'
};
// Optimize database queries
const optimizedQuery = {
select: ['id', 'name', 'email'], // Only select needed fields
where: { status: 'active' },
limit: 100,
orderBy: 'created_at DESC',
index: 'status_created_at'
};
// Memory optimization
const memoryConfig = {
maxOldSpaceSize: 4096, // 4GB
gcInterval: 10000, // 10 seconds
heapDumpOnCrash: true
};
// CPU optimization
const cpuConfig = {
cluster: {
enabled: true,
workers: require('os').cpus().length
},
compression: {
enabled: true,
level: 6
}
};
// Key performance metrics
const metrics = {
throughput: {
requestsPerSecond: 1000,
transactionsPerSecond: 500
},
latency: {
p50: 100, // 100ms
p95: 500, // 500ms
p99: 1000 // 1s
},
errors: {
errorRate: 0.01, // 1%
timeoutRate: 0.005 // 0.5%
},
resources: {
cpuUsage: 0.7, // 70%
memoryUsage: 0.8, // 80%
diskUsage: 0.6 // 60%
}
};
// Alert configuration
const alerts = {
highCPU: {
threshold: 0.8,
duration: 300, // 5 minutes
action: 'scale_up'
},
highMemory: {
threshold: 0.9,
duration: 180, // 3 minutes
action: 'scale_up'
},
highErrorRate: {
threshold: 0.05,
duration: 60, // 1 minute
action: 'alert_team'
}
};
# Horizontal Pod Autoscaler
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: fantomu-agent-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: fantomu-agent
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
# Custom metrics scaling
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: fantomu-agent-custom-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: fantomu-agent
minReplicas: 2
maxReplicas: 50
metrics:
- type: Pods
pods:
metric:
name: requests_per_second
target:
type: AverageValue
averageValue: "100"