# Kubernetes Deployment Guide

This directory contains Kubernetes manifests for deploying the B2B Flight Ticketing Platform.

## Prerequisites

- Kubernetes cluster (v1.24+)
- kubectl configured to access your cluster
- NGINX Ingress Controller installed
- Persistent Volume provisioner (for storage)
- (Optional) cert-manager for TLS certificates

## Deployment Order

Deploy the manifests in the following order:

### 1. Create Namespace
```bash
kubectl apply -f namespace.yaml
```

### 2. Create ConfigMap and Secrets
**IMPORTANT**: Edit `secret.yaml` and replace placeholder values with your actual credentials before applying.

```bash
kubectl apply -f configmap.yaml
kubectl apply -f secret.yaml
```

### 3. Deploy Infrastructure Services
```bash
kubectl apply -f postgres-deployment.yaml
kubectl apply -f redis-deployment.yaml
kubectl apply -f rabbitmq-deployment.yaml
kubectl apply -f keycloak-deployment.yaml
```

Wait for all services to be ready:
```bash
kubectl wait --for=condition=ready pod -l app=postgres -n b2b-flight-platform --timeout=300s
kubectl wait --for=condition=ready pod -l app=redis -n b2b-flight-platform --timeout=300s
kubectl wait --for=condition=ready pod -l app=rabbitmq -n b2b-flight-platform --timeout=300s
kubectl wait --for=condition=ready pod -l app=keycloak -n b2b-flight-platform --timeout=300s
```

### 4. Deploy Application
```bash
kubectl apply -f app-deployment.yaml
```

Wait for application to be ready:
```bash
kubectl wait --for=condition=ready pod -l app=b2b-flight-app -n b2b-flight-platform --timeout=300s
```

### 5. Deploy Ingress (Optional)
Edit `ingress.yaml` to configure your domain names, then:
```bash
kubectl apply -f ingress.yaml
```

## Verification

Check all pods are running:
```bash
kubectl get pods -n b2b-flight-platform
```

Check services:
```bash
kubectl get services -n b2b-flight-platform
```

Check application logs:
```bash
kubectl logs -f deployment/b2b-flight-app -n b2b-flight-platform
```

## Accessing the Application

### Port Forwarding (for testing)
```bash
# Application
kubectl port-forward service/b2b-flight-app-service 8081:8081 -n b2b-flight-platform

# Keycloak
kubectl port-forward service/keycloak-service 8080:8080 -n b2b-flight-platform

# RabbitMQ Management
kubectl port-forward service/rabbitmq-service 15672:15672 -n b2b-flight-platform
```

### Via Ingress (production)
Access the application at:
- API: https://api.b2b-flight.example.com
- Keycloak: https://keycloak.b2b-flight.example.com
- Swagger UI: https://api.b2b-flight.example.com/swagger-ui.html

## Scaling

### Manual Scaling
```bash
kubectl scale deployment b2b-flight-app --replicas=5 -n b2b-flight-platform
```

### Auto-scaling
The HorizontalPodAutoscaler is configured to scale between 2-10 replicas based on CPU and memory usage.

Check HPA status:
```bash
kubectl get hpa -n b2b-flight-platform
```

## Monitoring

### Health Checks
```bash
# Liveness
kubectl exec -it deployment/b2b-flight-app -n b2b-flight-platform -- wget -qO- http://localhost:8081/actuator/health/liveness

# Readiness
kubectl exec -it deployment/b2b-flight-app -n b2b-flight-platform -- wget -qO- http://localhost:8081/actuator/health/readiness
```

### Metrics
```bash
kubectl top pods -n b2b-flight-platform
kubectl top nodes
```

## Database Migration

Database migrations run automatically on application startup using Flyway.

To manually run migrations:
```bash
kubectl exec -it deployment/b2b-flight-app -n b2b-flight-platform -- java -jar app.jar --spring.flyway.enabled=true
```

## Backup and Restore

### PostgreSQL Backup
```bash
kubectl exec -it deployment/postgres -n b2b-flight-platform -- pg_dump -U flight_user flight_platform > backup.sql
```

### PostgreSQL Restore
```bash
kubectl exec -i deployment/postgres -n b2b-flight-platform -- psql -U flight_user flight_platform < backup.sql
```

## Troubleshooting

### View logs
```bash
# Application logs
kubectl logs -f deployment/b2b-flight-app -n b2b-flight-platform

# Previous container logs (if crashed)
kubectl logs deployment/b2b-flight-app -n b2b-flight-platform --previous

# All pods logs
kubectl logs -l app=b2b-flight-app -n b2b-flight-platform --all-containers=true
```

### Describe resources
```bash
kubectl describe pod <pod-name> -n b2b-flight-platform
kubectl describe deployment b2b-flight-app -n b2b-flight-platform
```

### Execute commands in pod
```bash
kubectl exec -it deployment/b2b-flight-app -n b2b-flight-platform -- /bin/sh
```

### Check events
```bash
kubectl get events -n b2b-flight-platform --sort-by='.lastTimestamp'
```

## Cleanup

To remove all resources:
```bash
kubectl delete namespace b2b-flight-platform
```

Or delete individual resources:
```bash
kubectl delete -f app-deployment.yaml
kubectl delete -f keycloak-deployment.yaml
kubectl delete -f rabbitmq-deployment.yaml
kubectl delete -f redis-deployment.yaml
kubectl delete -f postgres-deployment.yaml
kubectl delete -f configmap.yaml
kubectl delete -f secret.yaml
kubectl delete -f namespace.yaml
```

## Production Considerations

1. **Secrets Management**: Use external secret management (Vault, AWS Secrets Manager, etc.)
2. **TLS Certificates**: Configure cert-manager for automatic certificate management
3. **Monitoring**: Set up Prometheus and Grafana for metrics
4. **Logging**: Configure centralized logging (ELK, Loki, etc.)
5. **Backup**: Implement automated backup strategy for PostgreSQL
6. **High Availability**: Use StatefulSets for databases in production
7. **Network Policies**: Implement network policies for security
8. **Resource Quotas**: Set namespace resource quotas
9. **Pod Security**: Configure Pod Security Standards
10. **Disaster Recovery**: Document and test disaster recovery procedures
