Skip to main content
What you’ll get out of this: Deploy DeelRx CRM using Docker and Kubernetes across different environments with comprehensive configuration and monitoring.

Overview

This guide covers deploying DeelRx CRM using Docker and Kubernetes across different environments. The application is containerized for maximum flexibility and can be deployed on various platforms.

Prerequisites

  • Docker 20.10+
  • Docker Compose 2.0+
  • Kubernetes 1.20+ (for K8s deployment)
  • kubectl (for K8s deployment)
  • pnpm (for local development)

Quick Start

Local Development

1

Start Development Environment

./scripts/docker-deploy.sh deploy

Production Deployment

1

Build Images

ENVIRONMENT=production ./scripts/docker-build.sh
2

Deploy with Docker Compose

ENVIRONMENT=production ./scripts/docker-deploy.sh deploy

Docker Configuration

Multi-Stage Builds

The Dockerfiles use multi-stage builds for optimal image sizes:
  • Base: Node.js 20 Alpine with pnpm
  • Dependencies: Install all dependencies
  • Builder: Build the application
  • Production: Minimal runtime image

Image Structure

deelrxcrm/
├── Dockerfile                 # Main multi-service build
├── apps/app/Dockerfile       # CRM App
├── apps/api/Dockerfile       # API Service
├── apps/docs/Dockerfile      # Documentation
└── docker-compose.yml        # Production orchestration

Development vs Production

AspectDevelopmentProduction
Base Imagenode:20-alpinenode:20-alpine
DependenciesAll (dev + prod)Production only
Build CacheEnabledDisabled
Hot ReloadYesNo
Health ChecksBasicComprehensive
SecurityRelaxedStrict

Environment Configuration

Environment Variables

Required Variables

# Database
DATABASE_URL=postgresql://user:password@host:port/database

# Redis
REDIS_URL=redis://host:port

# Authentication
NEXTAUTH_SECRET=your-secret-key
CLERK_SECRET_KEY=your-clerk-secret
CLERK_PUBLISHABLE_KEY=your-clerk-publishable-key

# Application
NODE_ENV=production
API_PORT=3001
DOCS_PORT=3004

Optional Variables

# Performance
REDIS_PASSWORD=redis-password
DATABASE_POOL_SIZE=10
CACHE_TTL=3600

# Monitoring
SENTRY_DSN=your-sentry-dsn
LOG_LEVEL=info

# Features
ENABLE_ANALYTICS=true
ENABLE_CACHING=true

Docker Compose Environments

Development (docker-compose.dev.yml)

  • Hot reload enabled
  • Development dependencies included
  • Admin tools (pgAdmin, RedisInsight)
  • Volume mounts for live code changes

Production (docker-compose.yml)

  • Optimized for performance
  • Security hardening
  • Health checks
  • Resource limits
  • Nginx reverse proxy

Kubernetes Deployment

Prerequisites

1

Cluster Setup

# Create namespace
kubectl apply -f k8s/namespace.yaml

# Apply secrets and configmaps
kubectl apply -f k8s/secret.yaml
kubectl apply -f k8s/configmap.yaml
2

Storage

# Deploy PostgreSQL and Redis
kubectl apply -f k8s/postgres.yaml
kubectl apply -f k8s/redis.yaml
3

Applications

# Deploy main services
kubectl apply -f k8s/app.yaml
kubectl apply -f k8s/api.yaml
kubectl apply -f k8s/docs.yaml
4

Ingress

# Configure external access
kubectl apply -f k8s/ingress.yaml

Scaling

Horizontal Pod Autoscaler

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: deelrxcrm-app-hpa
  namespace: deelrxcrm
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: deelrxcrm-app
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

Vertical Pod Autoscaler

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: deelrxcrm-app-vpa
  namespace: deelrxcrm
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: deelrxcrm-app
  updatePolicy:
    updateMode: "Auto"

Monitoring and Observability

Health Checks

All services include comprehensive health checks:
  • Liveness Probe: Ensures container is running
  • Readiness Probe: Ensures service is ready to accept traffic
  • Startup Probe: Ensures slow-starting containers have time to initialize

Logging

# Fluentd configuration for log aggregation
apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-config
  namespace: deelrxcrm
data:
  fluent.conf: |
    <source>
      @type tail
      path /var/log/containers/*deelrxcrm*.log
      pos_file /var/log/fluentd-containers.log.pos
      tag kubernetes.*
      format json
    </source>

Metrics

# Prometheus ServiceMonitor
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: deelrxcrm-monitor
  namespace: deelrxcrm
spec:
  selector:
    matchLabels:
      app: deelrxcrm-app
  endpoints:
  - port: metrics
    interval: 30s
    path: /metrics

Security Considerations

Container Security

  • All containers run as non-root user
  • Production containers use read-only filesystem
  • CPU and memory limits prevent resource exhaustion
  • Restricted capabilities and permissions

Network Security

  • Network policies restrict inter-pod communication
  • SSL/TLS at ingress level
  • Optional Istio integration for advanced networking

Secrets Management

  • Kubernetes secrets encrypted at rest
  • Integration with HashiCorp Vault or AWS Secrets Manager
  • Automated secret rotation policies

Performance Optimization

Resource Allocation

ServiceCPU RequestCPU LimitMemory RequestMemory Limit
App250m500m512Mi1Gi
API200m400m256Mi512Mi
Docs100m200m128Mi256Mi
PostgreSQL250m500m256Mi512Mi
Redis100m200m128Mi256Mi

Caching Strategy

  • Redis clustering for high availability
  • CDN integration for static asset caching
  • Database connection pooling for optimized connection management

Troubleshooting

Common Issues

# Check logs
docker-compose logs <service>
kubectl logs <pod-name> -n deelrxcrm
# Test database connectivity
kubectl exec -it <postgres-pod> -- psql -U deelrxcrm -d deelrxcrm
# Check resource usage
kubectl top pods -n deelrxcrm
docker stats

Debug Commands

# Docker
docker-compose ps
docker-compose logs -f
docker-compose exec <service> sh

# Kubernetes
kubectl get pods -n deelrxcrm
kubectl describe pod <pod-name> -n deelrxcrm
kubectl exec -it <pod-name> -n deelrxcrm -- sh

Backup and Recovery

Database Backups

# Automated backup script
#!/bin/bash
kubectl exec -n deelrxcrm <postgres-pod> -- pg_dump -U deelrxcrm deelrxcrm > backup-$(date +%Y%m%d).sql

Volume Snapshots

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: deelrxcrm-snapshot
  namespace: deelrxcrm
spec:
  source:
    persistentVolumeClaimName: postgres-pvc

Migration Strategies

Zero-Downtime Deployment

  • Blue-Green deployment for switching traffic between versions
  • Rolling updates for gradual pod replacement
  • Canary releases for gradual traffic shifting

Database Migrations

# Run migrations safely
kubectl exec -n deelrxcrm <app-pod> -- pnpm migrate

Cost Optimization

Resource Right-Sizing

  • Monitor usage to determine optimal resource allocation
  • Implement HPA and VPA for dynamic scaling
  • Use spot instances for non-critical workloads

Storage Optimization

  • Regular automated snapshots
  • Automated cleanup of old logs and data
  • Enable compression for stored data

Support and Maintenance

Regular Maintenance

  • Regular base image updates for security
  • Keep dependencies current
  • Regular backup testing

Monitoring

  • Real-time service health dashboards
  • Proactive issue detection with alerting
  • Continuous performance monitoring

Next Steps

CI/CD Integration

Set up automated deployment pipelines for continuous delivery.

Service Mesh

Implement advanced networking and security with Istio.

Multi-Region

Deploy across multiple regions for geographic resilience.

Disaster Recovery

Implement comprehensive backup and recovery procedures.