# πŸ—οΈ CloudHost PaaS β€” System Architecture ## Overview CloudHost is a self-service PaaS platform that enables users to deploy Node.js and Laravel applications onto Kubernetes clusters managed by a super admin. --- ## 🧱 High-Level Architecture ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ USERS / ADMINS β”‚ β”‚ (Browser / CLI) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ HTTPS β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ FRONTEND (Next.js) β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Auth UI β”‚ β”‚ Deploy Wizard β”‚ β”‚ Dashboardβ”‚ β”‚Admin Panelβ”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ REST API (JSON) β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ BACKEND (NestJS) β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚Auth β”‚ β”‚Applications β”‚ β”‚Deployments β”‚ β”‚Clusters β”‚ β”‚ β”‚ β”‚Module β”‚ β”‚Module β”‚ β”‚Module β”‚ β”‚Module β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Kubernetes β”‚ β”‚ Build β”‚ β”‚ Logs & β”‚ β”‚ β”‚ β”‚ Service β”‚ β”‚ Service β”‚ β”‚ Metrics Service β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Kubernetes β”‚ β”‚ Container β”‚ β”‚ Cluster(s) β”‚ β”‚ Registry β”‚ β”‚ β”‚ β”‚ (Harbor/ECR) β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚Namespace Aβ”‚ β”‚ β”‚ β”‚ β”Œβ”€Pod────┐│ β”‚ β”‚ β”‚ β”‚App β”‚β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚ β”‚ β”‚ PostgreSQL β”‚ β”‚ β”‚ β”Œβ”€Pod────┐│ β”‚ β”‚ (Metadata DB) β”‚ β”‚ β”‚ β”‚DB β”‚β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚Namespace Bβ”‚ β”‚ β”‚ β”‚ ... β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## πŸ”§ Tech Stack Justification ### Frontend: **Next.js 14 (App Router) + Tailwind CSS** | Reason | Detail | |--------|--------| | **SSR & SEO** | Server-side rendering for fast initial loads | | **App Router** | Modern React Server Components, layouts, loading states | | **Tailwind CSS** | Rapid UI development, consistent design system | | **TypeScript** | End-to-end type safety with shared types | | **React Query** | Efficient server state management, caching, polling for live status | ### Backend: **NestJS (Node.js)** | Reason | Detail | |--------|--------| | **Modular architecture** | Each domain (auth, apps, deployments, clusters) is a self-contained module | | **TypeScript native** | Full type safety, shared interfaces with frontend | | **Decorator-based** | Clean controller/service pattern, guards, interceptors | | **@kubernetes/client-node** | Official K8s client for Node.js β€” direct API interaction | | **Bull/BullMQ** | Redis-backed job queues for async build & deploy pipelines | | **TypeORM** | Mature PostgreSQL ORM with migration support | ### Database: **PostgreSQL** | Reason | Detail | |--------|--------| | **ACID compliance** | Critical for deployment state tracking | | **JSON columns** | Store flexible config/metadata without schema changes | | **Mature ecosystem** | Battle-tested, excellent TypeORM support | | **Scalability** | Read replicas, partitioning for growth | ### Build System: **Kaniko (in-cluster)** | Reason | Detail | |--------|--------| | **No Docker daemon** | Builds images inside K8s pods β€” no Docker-in-Docker security issues | | **Registry push** | Native push to any OCI-compatible registry | | **Caching** | Layer caching for faster rebuilds | ### Container Registry: **Harbor (self-hosted) or cloud-managed (ECR/GCR/ACR)** | Reason | Detail | |--------|--------| | **Private images** | User apps must not be publicly accessible | | **Vulnerability scanning** | Harbor provides built-in image scanning | | **Multi-tenancy** | Project-based access control | ### Queue System: **Redis + BullMQ** | Reason | Detail | |--------|--------| | **Async builds** | Image builds are long-running β€” must not block API | | **Retries** | Failed builds auto-retry with backoff | | **Progress tracking** | Real-time build status updates | --- ## πŸ” Security Architecture ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Security Layers β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚ β”‚ 1. JWT Authentication (access/refresh) β”‚ β”‚ 2. Role-Based Access (User / Admin) β”‚ β”‚ 3. K8s Namespace Isolation per user β”‚ β”‚ 4. K8s RBAC β€” scoped ServiceAccounts β”‚ β”‚ 5. Network Policies between namespaces β”‚ β”‚ 6. Resource Quotas & Limit Ranges β”‚ β”‚ 7. Secrets encryption (K8s Secrets) β”‚ β”‚ 8. Input validation on all user inputs β”‚ β”‚ 9. Rate limiting on API endpoints β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## πŸ”„ Deployment Flow ``` User uploads code ──► API receives ──► Store metadata in PostgreSQL β”‚ β–Ό Queue build job (BullMQ) β”‚ β–Ό Kaniko Pod builds image β”‚ β–Ό Push to Container Registry β”‚ β–Ό Generate K8s manifests from templates β”‚ β–Ό Apply to target cluster via K8s API β”‚ β–Ό Create: Namespace, Deployment, Service, Ingress, PVC, DB, Secrets β”‚ β–Ό Update deployment status in DB β”‚ β–Ό User sees live status in dashboard ``` --- ## πŸ“ Project Structure ``` host/ β”œβ”€β”€ ARCHITECTURE.md β”œβ”€β”€ README.md β”œβ”€β”€ docker-compose.yml β”œβ”€β”€ backend/ # NestJS API β”‚ β”œβ”€β”€ src/ β”‚ β”‚ β”œβ”€β”€ main.ts β”‚ β”‚ β”œβ”€β”€ app.module.ts β”‚ β”‚ β”œβ”€β”€ common/ # Shared utilities, guards, decorators β”‚ β”‚ β”œβ”€β”€ config/ # Environment configuration β”‚ β”‚ β”œβ”€β”€ auth/ # JWT auth, strategies, guards β”‚ β”‚ β”œβ”€β”€ users/ # User management β”‚ β”‚ β”œβ”€β”€ applications/ # App CRUD, code upload β”‚ β”‚ β”œβ”€β”€ deployments/ # Deployment lifecycle β”‚ β”‚ β”œβ”€β”€ clusters/ # K8s cluster management (admin) β”‚ β”‚ β”œβ”€β”€ kubernetes/ # K8s client, manifest generation β”‚ β”‚ β”œβ”€β”€ build/ # Image build pipeline β”‚ β”‚ └── database/ # TypeORM entities, migrations β”‚ β”œβ”€β”€ templates/ # K8s YAML templates (Handlebars) β”‚ β”œβ”€β”€ Dockerfile β”‚ β”œβ”€β”€ package.json β”‚ └── tsconfig.json β”œβ”€β”€ frontend/ # Next.js App β”‚ β”œβ”€β”€ src/ β”‚ β”‚ β”œβ”€β”€ app/ # App Router pages β”‚ β”‚ β”œβ”€β”€ components/ # Reusable UI components β”‚ β”‚ β”œβ”€β”€ lib/ # API client, utilities β”‚ β”‚ β”œβ”€β”€ hooks/ # Custom React hooks β”‚ β”‚ └── types/ # TypeScript interfaces β”‚ β”œβ”€β”€ Dockerfile β”‚ β”œβ”€β”€ package.json β”‚ └── tailwind.config.ts └── k8s/ # Platform's own K8s deployment β”œβ”€β”€ base/ └── overlays/ ``` --- ## πŸš€ Future Scaling Considerations 1. **Multi-cluster support** β€” Deploy to different clusters/regions 2. **Custom domains** β€” Let users bring their own domains with auto TLS 3. **Horizontal Pod Autoscaler** β€” Auto-scale based on metrics 4. **WebSocket/SSE** β€” Real-time build logs streaming 5. **Plugin system** β€” Support Python, Go, Rust runtimes 6. **Marketplace** β€” Pre-built app templates (WordPress, etc.) 7. **Billing integration** β€” Usage-based billing per resource consumption 8. **GitOps** β€” ArgoCD integration for declarative deployments