README
¶
Mithril Deployment Example
This example demonstrates Phase 16: DevOps & Deployment, showcasing production-ready deployment configurations for Mithril applications.
Features Demonstrated
🐳 Docker & Containerization
- Multi-stage Dockerfile: Optimized build and runtime stages
- Docker Compose: Development, staging, testing, and production environments
- Health Checks: Container health monitoring
- Security: Non-root user, minimal attack surface
☸️ Kubernetes Orchestration
- Deployments: Scalable application deployment
- Services: Load balancing and service discovery
- Ingress: External access with SSL termination
- HPA: Horizontal Pod Autoscaling
- PVC: Persistent volume claims for data storage
- ConfigMaps & Secrets: Configuration management
🔄 CI/CD Pipeline
- GitHub Actions: Automated testing, building, and deployment
- Multi-environment: Staging and production deployments
- Security Scanning: Vulnerability assessment with Trivy
- Code Quality: Linting and testing automation
📊 Monitoring & Observability
- Prometheus: Metrics collection and alerting
- Grafana: Visualization and dashboards
- Alertmanager: Alert routing and notification
- Health Checks: Application and infrastructure monitoring
Quick Start
Development Environment
# Start development environment
docker-compose -f docker-compose.dev.yml up -d
# View logs
docker-compose -f docker-compose.dev.yml logs -f app
# Stop environment
docker-compose -f docker-compose.dev.yml down
Staging Environment
# Deploy to staging
docker-compose -f docker-compose.staging.yml up -d
# View staging logs
docker-compose -f docker-compose.staging.yml logs -f app
Production Environment
# Deploy to production
docker-compose -f docker-compose.prod.yml up -d
# Scale application
docker-compose -f docker-compose.prod.yml up -d --scale app=5
Kubernetes Deployment
Prerequisites
- Kubernetes cluster (1.20+)
- kubectl configured
- Helm (optional)
Deploy to Kubernetes
# Create namespace
kubectl apply -f k8s/namespace.yaml
# Apply configurations
kubectl apply -f k8s/configmap.yaml
kubectl apply -f k8s/secret.yaml
kubectl apply -f k8s/pvc.yaml
# Deploy application
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
kubectl apply -f k8s/ingress.yaml
kubectl apply -f k8s/hpa.yaml
# Check deployment status
kubectl get pods -n mithril
kubectl get services -n mithril
kubectl get ingress -n mithril
Scale Application
# Scale manually
kubectl scale deployment mithril-app --replicas=5 -n mithril
# Or let HPA handle it automatically
kubectl get hpa -n mithril
Environment Configuration
Environment Variables
| Variable | Description | Default | Required |
|---|---|---|---|
APP_ENV |
Application environment | development |
No |
APP_DEBUG |
Debug mode | false |
No |
APP_PORT |
Application port | 3000 |
No |
APP_HOST |
Application host | 0.0.0.0 |
No |
DB_HOST |
Database host | localhost |
Yes |
DB_PORT |
Database port | 5432 |
No |
DB_USER |
Database user | mithril |
Yes |
DB_PASSWORD |
Database password | - | Yes |
DB_NAME |
Database name | mithril |
Yes |
REDIS_HOST |
Redis host | localhost |
Yes |
REDIS_PORT |
Redis port | 6379 |
No |
JWT_SECRET |
JWT secret key | - | Yes |
SESSION_SECRET |
Session secret | - | Yes |
Configuration Files
- Development:
.env.development - Staging:
.env.staging - Production:
.env.production
Monitoring & Health Checks
Health Endpoints
- Health Check:
GET /health- Basic health status - Readiness:
GET /ready- Readiness for traffic - Metrics:
GET /metrics- Prometheus metrics - Monitor:
GET /monitor- System monitoring dashboard
Prometheus Metrics
The application exposes metrics at /metrics endpoint:
- HTTP request metrics
- Database connection metrics
- Cache metrics
- Custom business metrics
Grafana Dashboards
Pre-configured dashboards for:
- Application performance
- Infrastructure metrics
- Database performance
- Cache performance
- Error rates and response times
CI/CD Pipeline
GitHub Actions Workflow
The CI/CD pipeline includes:
-
Test Stage:
- Code linting with golangci-lint
- Unit tests with coverage
- Integration tests with test databases
-
Build Stage:
- Multi-architecture Docker builds
- Container registry publishing
- Image vulnerability scanning
-
Deploy Stage:
- Staging deployment (on develop branch)
- Production deployment (on main branch)
- Database migrations
- Health checks
Security Scanning
- Trivy: Container vulnerability scanning
- CodeQL: Code security analysis
- Dependency scanning: Package vulnerability detection
Production Considerations
Security
- Non-root containers: Applications run as non-root user
- Secrets management: Kubernetes secrets for sensitive data
- Network policies: Restrictive network access
- RBAC: Role-based access control
- Image scanning: Regular vulnerability assessments
Performance
- Resource limits: CPU and memory constraints
- Horizontal scaling: Auto-scaling based on metrics
- Load balancing: Nginx load balancer
- Caching: Redis for application caching
- CDN: Static asset delivery
Reliability
- Health checks: Liveness and readiness probes
- Graceful shutdown: Proper signal handling
- Circuit breakers: Failure isolation
- Retry logic: Automatic retry for transient failures
- Backup strategies: Database and file backups
Monitoring
- Application metrics: Custom business metrics
- Infrastructure metrics: System resource usage
- Log aggregation: Centralized logging
- Alerting: Proactive issue detection
- Dashboards: Real-time visibility
Troubleshooting
Common Issues
-
Container won't start:
docker logs <container_id> kubectl describe pod <pod_name> -n mithril -
Database connection issues:
kubectl exec -it <pod_name> -n mithril -- env | grep DB -
Memory issues:
kubectl top pods -n mithril kubectl describe pod <pod_name> -n mithril
Debug Commands
# Check pod status
kubectl get pods -n mithril
# View pod logs
kubectl logs -f deployment/mithril-app -n mithril
# Execute commands in pod
kubectl exec -it <pod_name> -n mithril -- /bin/sh
# Check service endpoints
kubectl get endpoints -n mithril
# View ingress status
kubectl describe ingress mithril-ingress -n mithril
Best Practices
- Use specific image tags instead of
latest - Set resource limits for all containers
- Use health checks for all services
- Implement proper logging with structured logs
- Monitor all critical metrics
- Use secrets management for sensitive data
- Implement backup strategies for data persistence
- Test deployments in staging before production
- Use blue-green deployments for zero-downtime updates
- Implement proper security scanning and policies
This deployment example provides a solid foundation for running Mithril applications in production environments with proper monitoring, scaling, and security measures.
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.