# Kubernetes Migration Guide: Local Dev to Production (MicroK8s) ## Overview This guide covers migrating the Bakery IA platform from local development environment to production on a Clouding.io VPS. **Current Setup (Local Development):** - macOS with Colima - Kind (Kubernetes in Docker) - NGINX Ingress Controller - Local storage - Development domains (localhost, bakery-ia.local) **Target Setup (Production):** - Ubuntu VPS (Clouding.io) - MicroK8s - MicroK8s NGINX Ingress - Persistent storage - Production domains (your actual domain) --- ## Key Differences & Required Adaptations ### 1. **Ingress Controller** - **Local:** Custom NGINX installed via manifest - **Production:** MicroK8s ingress addon - **Action Required:** Enable MicroK8s ingress addon ### 2. **Storage** - **Local:** Kind uses `standard` storage class (hostPath) - **Production:** MicroK8s uses `microk8s-hostpath` storage class - **Action Required:** Update storage class in PVCs ### 3. **Image Registry** - **Local:** Images built locally, no push required - **Production:** Need container registry (Docker Hub, GitHub Container Registry, or private registry) - **Action Required:** Setup image registry and push images ### 4. **Domain & SSL** - **Local:** localhost with self-signed certs - **Production:** Real domain with Let's Encrypt certificates - **Action Required:** Configure DNS and update ingress ### 5. **Resource Allocation** - **Local:** Minimal resources (development mode) - **Production:** Production-grade resources with HPA - **Action Required:** Already configured in prod overlay ### 6. **Build Process** - **Local:** Skaffold with local build - **Production:** CI/CD or manual build + push - **Action Required:** Setup deployment pipeline --- ## Pre-Migration Checklist ### VPS Requirements - [ ] Ubuntu 20.04 or later - [ ] Minimum 8GB RAM (16GB+ recommended) - [ ] Minimum 4 CPU cores (6+ recommended) - [ ] 100GB+ disk space - [ ] Public IP address - [ ] Domain name configured ### Access Requirements - [ ] SSH access to VPS - [ ] Domain DNS access - [ ] Container registry credentials - [ ] SSL certificate email address --- ## Step-by-Step Migration Guide ## Phase 1: VPS Setup ### Step 1: Install MicroK8s on Ubuntu VPS ```bash # SSH into your VPS ssh user@your-vps-ip # Update system sudo apt update && sudo apt upgrade -y # Install MicroK8s sudo snap install microk8s --classic --channel=1.28/stable # Add your user to microk8s group sudo usermod -a -G microk8s $USER sudo chown -f -R $USER ~/.kube # Restart session newgrp microk8s # Verify installation microk8s status --wait-ready # Enable required addons microk8s enable dns microk8s enable hostpath-storage microk8s enable ingress microk8s enable cert-manager microk8s enable metrics-server microk8s enable rbac # Optional but recommended microk8s enable prometheus microk8s enable registry # If you want local registry # Setup kubectl alias echo "alias kubectl='microk8s kubectl'" >> ~/.bashrc source ~/.bashrc # Verify kubectl get nodes kubectl get pods -A ``` ### Step 2: Configure Firewall ```bash # Allow necessary ports sudo ufw allow 22/tcp # SSH sudo ufw allow 80/tcp # HTTP sudo ufw allow 443/tcp # HTTPS sudo ufw allow 16443/tcp # Kubernetes API (optional, for remote access) # Enable firewall sudo ufw enable # Check status sudo ufw status ``` --- ## Phase 2: Configuration Adaptations ### Step 3: Update Storage Class Create a production storage patch: ```bash # On your local machine cat > infrastructure/kubernetes/overlays/prod/storage-patch.yaml < ~/.kube/config-merged mv ~/.kube/config-merged ~/.kube/config # Deploy using skaffold skaffold run -f skaffold-prod.yaml --kube-context=microk8s ``` ### Step 10: Verify Deployment ```bash # Check all pods are running kubectl get pods -n bakery-ia # Check services kubectl get svc -n bakery-ia # Check ingress kubectl get ingress -n bakery-ia # Check persistent volumes kubectl get pvc -n bakery-ia # Check logs kubectl logs -n bakery-ia deployment/gateway -f # Test database connectivity kubectl exec -n bakery-ia deployment/auth-db -it -- psql -U postgres -c "\l" ``` --- ## Phase 5: SSL Certificate Configuration ### Step 11: Let's Encrypt SSL Certificates The cert-manager addon is already enabled. Configure production certificates: ```bash # Verify cert-manager is running kubectl get pods -n cert-manager # Check cluster issuer kubectl get clusterissuer # If letsencrypt-production issuer doesn't exist, create it: cat < ~/backup-databases.sh <<'EOF' #!/bin/bash BACKUP_DIR="/backups/$(date +%Y-%m-%d)" mkdir -p $BACKUP_DIR # Get all database pods DBS=$(kubectl get pods -n bakery-ia -l app.kubernetes.io/component=database -o name) for db in $DBS; do DB_NAME=$(echo $db | cut -d'/' -f2) echo "Backing up $DB_NAME..." kubectl exec -n bakery-ia $db -- pg_dump -U postgres > "$BACKUP_DIR/${DB_NAME}.sql" done # Compress backups tar -czf "$BACKUP_DIR.tar.gz" "$BACKUP_DIR" rm -rf "$BACKUP_DIR" # Keep only last 7 days find /backups -name "*.tar.gz" -mtime +7 -delete echo "Backup completed: $BACKUP_DIR.tar.gz" EOF chmod +x ~/backup-databases.sh # Setup daily cron job (crontab -l 2>/dev/null; echo "0 2 * * * ~/backup-databases.sh") | crontab - ``` ### Step 14: Setup Log Aggregation (Optional) ```bash # Enable Loki for log aggregation microk8s enable observability # Or use external logging service like ELK, Datadog, etc. ``` --- ## Phase 7: Post-Deployment Verification ### Step 15: Health Checks ```bash # Test frontend curl -k https://bakery.example.com # Test API curl -k https://api.example.com/health # Test database connectivity kubectl exec -n bakery-ia deployment/auth-service -- curl localhost:8000/health # Check all services are healthy kubectl get pods -n bakery-ia -o wide # Check resource usage kubectl top pods -n bakery-ia kubectl top nodes ``` ### Step 16: Performance Testing ```bash # Install hey (HTTP load testing tool) go install github.com/rakyll/hey@latest # Test API endpoint hey -n 1000 -c 10 https://api.example.com/health # Monitor during load test kubectl top pods -n bakery-ia ``` --- ## Ongoing Operations ### Updating the Application ```bash # On local machine # 1. Make code changes # 2. Build and push new images skaffold build -f skaffold-prod.yaml # 3. Update image tags in prod kustomization # 4. Apply updates kubectl apply -k infrastructure/kubernetes/overlays/prod # 5. Rolling update status kubectl rollout status deployment/auth-service -n bakery-ia ``` ### Scaling Services ```bash # Manual scaling kubectl scale deployment auth-service -n bakery-ia --replicas=5 # Or update in kustomization.yaml and reapply ``` ### Database Migrations ```bash # Run migration job kubectl apply -f infrastructure/kubernetes/base/migrations/auth-migration-job.yaml # Check migration status kubectl get jobs -n bakery-ia kubectl logs -n bakery-ia job/auth-migration ``` --- ## Troubleshooting Common Issues ### Issue 1: Pods Not Starting ```bash # Check pod status kubectl describe pod POD_NAME -n bakery-ia # Common causes: # - Image pull errors: Check registry credentials # - Resource limits: Check node resources # - Volume mount issues: Check PVC status ``` ### Issue 2: Ingress Not Working ```bash # Check ingress controller kubectl get pods -n ingress # Check ingress resource kubectl describe ingress bakery-ingress-prod -n bakery-ia # Check if port 80/443 are open sudo netstat -tlnp | grep -E '(80|443)' # Check NGINX logs kubectl logs -n ingress -l app.kubernetes.io/name=ingress-nginx ``` ### Issue 3: SSL Certificate Issues ```bash # Check certificate status kubectl describe certificate bakery-ia-prod-tls-cert -n bakery-ia # Check cert-manager logs kubectl logs -n cert-manager deployment/cert-manager # Verify DNS dig bakery.example.com # Manual certificate request kubectl delete certificate bakery-ia-prod-tls-cert -n bakery-ia kubectl apply -f infrastructure/kubernetes/overlays/prod/prod-ingress.yaml ``` ### Issue 4: Database Connection Errors ```bash # Check database pod kubectl get pods -n bakery-ia -l app.kubernetes.io/component=database # Check database logs kubectl logs -n bakery-ia deployment/auth-db # Test connection from service pod kubectl exec -n bakery-ia deployment/auth-service -- nc -zv auth-db 5432 ``` ### Issue 5: Out of Resources ```bash # Check node resources kubectl describe node # Check resource requests/limits kubectl describe pod POD_NAME -n bakery-ia # Adjust resource limits in prod kustomization or scale down ``` --- ## Security Hardening Checklist - [ ] Change all default passwords - [ ] Enable pod security policies - [ ] Setup network policies - [ ] Enable audit logging - [ ] Regular security updates - [ ] Implement secrets rotation - [ ] Setup intrusion detection - [ ] Enable RBAC properly - [ ] Regular backup testing - [ ] Implement rate limiting - [ ] Setup DDoS protection - [ ] Enable security scanning --- ## Performance Optimization ### For VPS with Limited Resources If your VPS has limited resources, consider: ```yaml # Reduce replica counts in prod kustomization.yaml replicas: - name: auth-service count: 2 # Instead of 3 - name: gateway count: 2 # Instead of 3 # Adjust resource limits resources: requests: memory: "256Mi" # Reduced from 512Mi cpu: "100m" # Reduced from 200m ``` ### Database Optimization ```bash # Tune PostgreSQL for production kubectl exec -n bakery-ia deployment/auth-db -it -- psql -U postgres # Inside PostgreSQL: ALTER SYSTEM SET shared_buffers = '256MB'; ALTER SYSTEM SET effective_cache_size = '1GB'; ALTER SYSTEM SET maintenance_work_mem = '64MB'; ALTER SYSTEM SET checkpoint_completion_target = '0.9'; ALTER SYSTEM SET wal_buffers = '16MB'; ALTER SYSTEM SET default_statistics_target = '100'; # Restart database pod kubectl rollout restart deployment/auth-db -n bakery-ia ``` --- ## Rollback Procedure If something goes wrong: ```bash # Rollback deployment kubectl rollout undo deployment/DEPLOYMENT_NAME -n bakery-ia # Rollback to specific revision kubectl rollout history deployment/DEPLOYMENT_NAME -n bakery-ia kubectl rollout undo deployment/DEPLOYMENT_NAME --to-revision=2 -n bakery-ia # Restore from backup tar -xzf /backups/2024-01-01.tar.gz kubectl exec -n bakery-ia deployment/auth-db -- psql -U postgres < auth-db.sql ``` --- ## Quick Reference ### Useful Commands ```bash # View all resources kubectl get all -n bakery-ia # Get pod logs kubectl logs -f POD_NAME -n bakery-ia # Execute command in pod kubectl exec -it POD_NAME -n bakery-ia -- /bin/bash # Port forward for debugging kubectl port-forward svc/SERVICE_NAME 8000:8000 -n bakery-ia # Check events kubectl get events -n bakery-ia --sort-by='.lastTimestamp' # Resource usage kubectl top nodes kubectl top pods -n bakery-ia # Restart deployment kubectl rollout restart deployment/DEPLOYMENT_NAME -n bakery-ia # Scale deployment kubectl scale deployment/DEPLOYMENT_NAME --replicas=3 -n bakery-ia ``` ### Important File Locations on VPS ``` /var/snap/microk8s/current/credentials/ # Kubernetes credentials /var/snap/microk8s/common/default-storage/ # Default storage location ~/kubernetes/ # Your manifests /backups/ # Database backups ``` --- ## Next Steps After Migration 1. **Setup CI/CD Pipeline** - GitHub Actions or GitLab CI - Automated builds and deployments - Automated testing 2. **Implement Monitoring Dashboards** - Setup Grafana dashboards - Configure alerts - Setup uptime monitoring 3. **Disaster Recovery Plan** - Document recovery procedures - Test backup restoration - Setup off-site backups 4. **Cost Optimization** - Monitor resource usage - Right-size deployments - Implement auto-scaling 5. **Documentation** - Document custom configurations - Create runbooks for common tasks - Train team members --- ## Support and Resources - **MicroK8s Documentation:** https://microk8s.io/docs - **Kubernetes Documentation:** https://kubernetes.io/docs - **cert-manager Documentation:** https://cert-manager.io/docs - **NGINX Ingress:** https://kubernetes.github.io/ingress-nginx ## Conclusion This migration moves your application from a local development environment to a production-ready deployment. Remember to: - Test thoroughly before going live - Have a rollback plan ready - Monitor closely after deployment - Keep regular backups - Stay updated with security patches Good luck with your deployment! 🚀