# Google Cloud Platform Deployment Guide for Brainy ## Overview Deploy Brainy on GCP with automatic scaling, global distribution, and zero-config dynamic adaptation. Brainy automatically detects and optimizes for GCP services. ## Quick Start (Zero-Config) ### Option 1: Cloud Run (Serverless Containers) ```bash # Build and deploy with one command gcloud run deploy brainy \ --source . \ --platform managed \ --region us-central1 \ --allow-unauthenticated # Brainy auto-detects Cloud Run and configures: # - Memory-optimized caching # - GCS for storage (if available) # - Cloud SQL for metadata (if available) ``` ### Option 2: Cloud Functions (Serverless) ```javascript // index.js const { Brainy } = require('@soulcraft/brainy') let brain exports.brainyHandler = async (req, res) => { // Zero-config - auto-adapts to Cloud Functions if (!brain) { brain = new Brainy() // Detects GCP environment automatically await brain.init() } const { method, ...params } = req.body try { let result switch(method) { case 'add': result = await brain.add(params) break case 'find': result = await brain.find(params) break case 'relate': result = await brain.relate(params) break default: return res.status(400).json({ error: 'Unknown method' }) } res.json({ result }) } catch (error) { res.status(500).json({ error: error.message }) } } ``` Deploy: ```bash gcloud functions deploy brainy \ --runtime nodejs20 \ --trigger-http \ --entry-point brainyHandler \ --memory 512MB \ --timeout 60s ``` ### Option 3: Google Kubernetes Engine (GKE) ```bash # Create autopilot cluster (fully managed, zero-config) gcloud container clusters create-auto brainy-cluster \ --region us-central1 # Deploy using Cloud Build gcloud builds submit --tag gcr.io/$PROJECT_ID/brainy # Apply Kubernetes manifest kubectl apply -f - < { // Send custom metrics to Cloud Monitoring await monitoring.createTimeSeries({ name: monitoring.projectPath(projectId), timeSeries: [{ metric: { type: `custom.googleapis.com/brainy/${metric.name}`, labels: metric.labels }, points: [{ interval: { endTime: { seconds: Date.now() / 1000 } }, value: { doubleValue: metric.value } }] }] }) } }) ``` ### Cloud Trace Integration ```javascript const brain = new Brainy({ tracing: { enabled: true, sampleRate: 0.1 // Sample 10% of requests } }) ``` ## Security Best Practices ### 1. Workload Identity (GKE) ```yaml # Enable Workload Identity apiVersion: v1 kind: ServiceAccount metadata: name: brainy-sa annotations: iam.gke.io/gcp-service-account: brainy@PROJECT_ID.iam.gserviceaccount.com ``` ### 2. Binary Authorization ```yaml # Ensure only signed container images apiVersion: binaryauthorization.grafeas.io/v1beta1 kind: Policy metadata: name: brainy-policy spec: defaultAdmissionRule: requireAttestationsBy: - projects/PROJECT_ID/attestors/prod-attestor ``` ### 3. VPC Service Controls ```bash # Create VPC Service Perimeter gcloud access-context-manager perimeters create brainy_perimeter \ --resources=projects/PROJECT_NUMBER \ --restricted-services=storage.googleapis.com \ --title="Brainy Security Perimeter" ``` ## Cost Optimization ### 1. Preemptible VMs (80% savings) ```yaml # GKE node pool with preemptible VMs apiVersion: container.cnrm.cloud.google.com/v1beta1 kind: ContainerNodePool metadata: name: brainy-preemptible-pool spec: clusterRef: name: brainy-cluster config: preemptible: true machineType: n2-standard-2 autoscaling: minNodeCount: 1 maxNodeCount: 10 ``` ### 2. Cloud CDN for Static Assets ```bash # Enable Cloud CDN for frequently accessed data gcloud compute backend-buckets create brainy-assets \ --gcs-bucket-name=brainy-static gcloud compute backend-buckets update brainy-assets \ --enable-cdn \ --cache-mode=CACHE_ALL_STATIC ``` ### 3. Committed Use Discounts ```bash # Purchase committed use for predictable workloads gcloud compute commitments create brainy-commitment \ --plan=TWELVE_MONTH \ --resources=vcpu=100,memory=400 ``` ## Deployment Automation ### Cloud Build CI/CD ```yaml # cloudbuild.yaml steps: # Build container - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/$PROJECT_ID/brainy:$SHORT_SHA', '.'] # Push to registry - name: 'gcr.io/cloud-builders/docker' args: ['push', 'gcr.io/$PROJECT_ID/brainy:$SHORT_SHA'] # Deploy to Cloud Run - name: 'gcr.io/cloud-builders/gcloud' args: - 'run' - 'deploy' - 'brainy' - '--image=gcr.io/$PROJECT_ID/brainy:$SHORT_SHA' - '--region=us-central1' - '--platform=managed' # Trigger on push to main trigger: branch: name: main ``` ### Terraform Infrastructure ```hcl # main.tf resource "google_cloud_run_service" "brainy" { name = "brainy" location = "us-central1" template { spec { containers { image = "gcr.io/${var.project_id}/brainy" resources { limits = { cpu = "2" memory = "2Gi" } } env { name = "NODE_ENV" value = "production" } } } } traffic { percent = 100 latest_revision = true } } resource "google_cloud_run_service_iam_member" "public" { service = google_cloud_run_service.brainy.name location = google_cloud_run_service.brainy.location role = "roles/run.invoker" member = "allUsers" } ``` ## Performance Optimization ### 1. Memory Store (Redis Compatible) ```javascript // Brainy can use Memorystore for caching const brain = new Brainy({ cache: { type: 'redis', options: { host: process.env.REDIS_HOST || 'auto-detect', port: 6379 } } }) ``` ### 2. Cloud Spanner for Global Consistency ```javascript const brain = new Brainy({ metadata: { type: 'spanner', options: { instance: 'brainy-instance', database: 'brainy-db' } } }) ``` ## Troubleshooting ### Common Issues 1. **Quota Exceeded** ```bash # Check quotas gcloud compute project-info describe --project=$PROJECT_ID # Request increase gcloud compute project-info add-metadata \ --metadata google-compute-default-region=us-central1 ``` 2. **Cold Starts** ```javascript // Keep minimum instances warm const brain = new Brainy({ warmup: { enabled: true, interval: 60000 // Ping every minute } }) ``` 3. **Memory Pressure** ```javascript // Optimize for GCP memory constraints const brain = new Brainy({ memory: { mode: 'aggressive', // Aggressive garbage collection maxHeap: 0.8 // Use 80% of available memory } }) ``` ## Production Checklist - [ ] Enable Workload Identity for secure access - [ ] Configure Cloud Armor for DDoS protection - [ ] Set up Cloud KMS for encryption keys - [ ] Enable VPC Service Controls - [ ] Configure Cloud IAP for authentication - [ ] Set up Cloud Monitoring dashboards - [ ] Configure Error Reporting - [ ] Enable Cloud Trace - [ ] Set up budget alerts - [ ] Configure backup and disaster recovery ## Support - Documentation: https://brainy.soulcraft.ai/docs - Issues: https://github.com/soulcraft/brainy/issues - GCP Marketplace: https://console.cloud.google.com/marketplace/product/brainy