Files
AFC-Demo/DEPLOYMENT.md
T

341 lines
11 KiB
Markdown
Raw Normal View History

# 🚀 Laravel Application - Azure Kubernetes Deployment
Diese Anwendung ist bereit für das Deployment auf Azure Kubernetes Service (AKS) mit vollständiger Terraform-Automatisierung.
## 📁 Struktur
```
frontend/
├── Dockerfile # Production-ready multi-stage Docker build
├── docker-compose.yml # Lokale Entwicklung
├── deploy/ # Docker-spezifische Konfiguration
│ ├── docker-entrypoint.sh
│ ├── nginx/
│ └── supervisord.conf
├── terraform/ # Terraform Infrastructure-as-Code
│ ├── main.tf # Haupt-Konfiguration
│ ├── variables.tf # Variablen
│ ├── outputs.tf # Outputs
│ ├── postgresql.tf # Azure PostgreSQL
│ ├── kubernetes.tf # K8s Resources
│ ├── ingress.tf # Nginx Ingress Controller
│ ├── db-restore.tf # Database Restore Job
│ ├── terraform.tfvars.example # Beispiel-Konfiguration
│ ├── README.md # Ausführliche Dokumentation
│ ├── QUICKSTART.md # 30-Minuten Schnellstart
│ ├── GITHUB_ACTIONS_SETUP.md # CI/CD Setup
│ └── scripts/
│ ├── deploy.sh # Automatisches Deployment
│ └── restore-db.sh # Database Restore
├── .github/workflows/
│ └── deploy-azure.yml # GitHub Actions CI/CD Pipeline
└── backups/ # PostgreSQL Backup-Dateien
└── *.dump
```
## 🎯 Features
### Infrastruktur
-**Azure PostgreSQL Flexible Server** (16) mit automatischen Backups
-**Azure Kubernetes Service (AKS)** mit Auto-Scaling
-**Nginx Ingress Controller** mit LoadBalancer
-**Let's Encrypt SSL/TLS** (optional)
-**Horizontal Pod Autoscaler** (CPU & Memory basiert)
-**Azure Monitor Integration** mit Log Analytics
### Container
-**Multi-Stage Docker Build** (optimiert für Production)
-**Alpine Linux** (minimales Image)
-**PHP 8.2 + Nginx + Supervisor**
-**PostgreSQL & SQLite Support**
-**Vite Assets** werden beim Build kompiliert
### Deployment
-**Terraform Infrastructure-as-Code**
-**Kubernetes Manifests** als Terraform Resources
-**Automatische DB-Migration** bei Deployment
-**Database Backup Restore** als Kubernetes Job
-**Zero-Downtime Rolling Updates**
-**GitHub Actions CI/CD Pipeline**
## 🚀 Schnellstart
### Option 1: Automatisches Deployment (Empfohlen)
```bash
# 1. Azure Login
az login
az account set --subscription "77677a80-2dea-493d-9867-f1c961b80fb3"
# 2. Container Registry Setup
az acr create --resource-group trusted_ai_demo_rg --name mylaravelregistry --sku Basic
az aks update --resource-group trusted_ai_demo_rg --name trai_k8s_cluster --attach-acr mylaravelregistry
az acr login --name mylaravelregistry
# 3. Docker Image bauen
docker build -t mylaravelregistry.azurecr.io/laravel-app:v1.0.0 .
docker push mylaravelregistry.azurecr.io/laravel-app:v1.0.0
# 4. Terraform konfigurieren
cd terraform
cp terraform.tfvars.example terraform.tfvars
nano terraform.tfvars # Wichtige Werte anpassen
# 5. Deployment ausführen
./scripts/deploy.sh
# 6. Datenbank wiederherstellen
./scripts/restore-db.sh
```
**Deployment Zeit**: ~30 Minuten
### Option 2: Manuelle Schritte
Siehe [terraform/QUICKSTART.md](terraform/QUICKSTART.md) für detaillierte Anleitung.
## 📖 Dokumentation
| Datei | Beschreibung |
|-------|--------------|
| [terraform/QUICKSTART.md](terraform/QUICKSTART.md) | 30-Minuten Schnellstart-Guide |
| [terraform/README.md](terraform/README.md) | Ausführliche Dokumentation (Troubleshooting, Wartung, etc.) |
| [terraform/GITHUB_ACTIONS_SETUP.md](terraform/GITHUB_ACTIONS_SETUP.md) | CI/CD Pipeline Setup |
## 🔧 Wichtige Konfiguration
### Terraform Variables ([terraform/terraform.tfvars](terraform/terraform.tfvars.example))
```hcl
# Docker Image (von ACR)
docker_image = "mylaravelregistry.azurecr.io/laravel-app:v1.0.0"
# Laravel APP_KEY (generiere mit: php artisan key:generate --show)
app_key = "base64:..."
# PostgreSQL
postgresql_admin_username = "pgadmin"
postgresql_admin_password = "YourSecurePassword123!"
# Ingress & SSL
ingress_enabled = true
ssl_enabled = false # Auf true für Production mit Domain
# Database Restore
db_restore_enabled = true
db_backup_file_path = "../backups/backup_backend_20251203_101741.dump"
```
## 🏗️ Architektur
```
┌─────────────────────────────────────────────────┐
│ Azure Cloud (germanywestcentral) │
│ │
│ ┌────────────────────────────────────────────┐ │
│ │ AKS Cluster: trai_k8s_cluster │ │
│ │ │ │
│ │ • Laravel App (2-6 Pods, Auto-Scaling) │ │
│ │ • Nginx Ingress (LoadBalancer) │ │
│ │ • ConfigMaps & Secrets │ │
│ └────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────┐ │
│ │ PostgreSQL Flexible Server │ │
│ │ • PostgreSQL 16 │ │
│ │ • 32 GB Storage │ │
│ │ • Auto Backups (7 days) │ │
│ └────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────┘
```
## 🎮 Häufige Befehle
### Deployment
```bash
# Neues Image deployen
docker build -t mylaravelregistry.azurecr.io/laravel-app:v1.0.1 .
docker push mylaravelregistry.azurecr.io/laravel-app:v1.0.1
kubectl set image deployment/laravel-app laravel-app=mylaravelregistry.azurecr.io/laravel-app:v1.0.1 -n laravel-app
# Terraform apply
cd terraform && terraform apply
# Database Restore
cd terraform && ./scripts/restore-db.sh
```
### Monitoring
```bash
# kubectl credentials abrufen
az aks get-credentials --resource-group trusted_ai_demo_rg --name trai_k8s_cluster
# Pods prüfen
kubectl get pods -n laravel-app
# Logs anzeigen
kubectl logs -n laravel-app -l app=laravel-app -f
# Service Status
kubectl get svc -n laravel-app
kubectl get ingress -n laravel-app
# LoadBalancer IP
kubectl get svc ingress-nginx-controller -n ingress-nginx
```
### Troubleshooting
```bash
# Pod Status detailliert
kubectl describe pod <pod-name> -n laravel-app
# Events prüfen
kubectl get events -n laravel-app --sort-by='.lastTimestamp'
# Shell in Pod
kubectl exec -it -n laravel-app <pod-name> -- /bin/sh
# Port-forward für lokalen Zugriff
kubectl port-forward -n laravel-app svc/laravel-app 8080:80
```
## 🔄 CI/CD Pipeline
### GitHub Actions
Die Pipeline wird automatisch ausgeführt bei:
- Push auf `main` Branch (Staging)
- Push auf `production` Branch (Production)
- Manuell über GitHub UI
**Setup**: Siehe [terraform/GITHUB_ACTIONS_SETUP.md](terraform/GITHUB_ACTIONS_SETUP.md)
### Workflow Steps
1. **Build & Push** - Docker Image bauen und zu ACR pushen
2. **Terraform Deploy** - Infrastruktur mit Terraform deployen
3. **Smoke Tests** - Basis-Tests nach Deployment
4. **Notifications** - Status-Benachrichtigungen
## 📊 Kosten-Übersicht
**Staging/Development:**
- AKS: ~30-50€/Monat (2 Nodes, B2s)
- PostgreSQL: ~15-20€/Monat (Basic tier)
- Load Balancer: ~5€/Monat
- **Total: ~50-75€/Monat**
**Production:**
- AKS: ~100-150€/Monat (3 Nodes, D2s_v3)
- PostgreSQL: ~50-80€/Monat (General Purpose)
- Load Balancer: ~5€/Monat
- **Total: ~155-235€/Monat**
## 🔒 Sicherheit
### Implementiert
- ✅ Secrets Management via Kubernetes Secrets
- ✅ PostgreSQL Firewall Rules
- ✅ Nginx Rate Limiting
- ✅ Resource Limits für Pods
- ✅ Rolling Updates (Zero Downtime)
### Empfohlen für Production
- [ ] Azure Key Vault Integration
- [ ] VNet Integration für PostgreSQL
- [ ] Private Endpoints
- [ ] Network Policies
- [ ] Pod Security Policies
- [ ] RBAC für Kubernetes
- [ ] Azure AD Integration
## 🚦 Status Checks
### Deployment erfolgreich?
```bash
# Pods running?
kubectl get pods -n laravel-app
# Sollte: 2/2 Running
# Service erreichbar?
kubectl get svc ingress-nginx-controller -n ingress-nginx
# Sollte: EXTERNAL-IP anzeigen
# Database connected?
kubectl logs -n laravel-app -l app=laravel-app | grep -i "database"
# Anwendung im Browser öffnen
terraform output app_url
```
## 🆘 Support
### Bei Problemen
1. **Terraform Issues**: Siehe [terraform/README.md#troubleshooting](terraform/README.md#troubleshooting)
2. **Kubernetes Issues**: `kubectl describe pod <pod-name> -n laravel-app`
3. **Database Issues**: Prüfe Firewall Rules und Secrets
4. **Ingress Issues**: `kubectl logs -n ingress-nginx -l app.kubernetes.io/component=controller`
### Nützliche Logs
```bash
# Application Logs
kubectl logs -n laravel-app -l app=laravel-app -f
# Database Restore Logs
kubectl logs -n laravel-app -l job-type=database-restore
# Ingress Controller Logs
kubectl logs -n ingress-nginx -l app.kubernetes.io/component=controller -f
# All Events
kubectl get events -n laravel-app --sort-by='.lastTimestamp'
```
## 📚 Nächste Schritte
Nach erfolgreichem Deployment:
1. **Domain konfigurieren**
- DNS A-Record auf LoadBalancer IP
- SSL aktivieren in terraform.tfvars
- `terraform apply`
2. **Monitoring einrichten**
- Azure Monitor im Portal prüfen
- Alert Rules konfigurieren
- Application Insights (optional)
3. **CI/CD Pipeline**
- GitHub Actions Secrets konfigurieren
- Branch Protection Rules
- Staging → Production Workflow
4. **Security Hardening**
- Azure Key Vault
- VNet Integration
- Private Endpoints
- RBAC
5. **Performance Optimization**
- Redis für Cache/Sessions
- CDN für Static Assets
- Database Query Optimization
## 🎉 Fertig!
Deine Laravel-Anwendung läuft jetzt produktionsbereit auf Azure Kubernetes Service!
**Deployment Command**: `cd terraform && ./scripts/deploy.sh`
---
**Erstellt**: Dezember 2024
**Version**: 1.0.0
**Status**: Production Ready ✅