# OCI Deployment

Deploy Monetarie PIX on Oracle Cloud Infrastructure using OKE, Autonomous Database or PostgreSQL, and OCI Cache.

## Prerequisites

- OCI tenancy with appropriate policies
- OCI CLI configured
- `kubectl` configured with OKE cluster
- Domain with DNS configured

## Architecture

```mermaid
graph TB
    LB[OCI Load Balancer] --> OKE[OKE Cluster<br/>3 nodes]

    subgraph VCN [Virtual Cloud Network]
        OKE --> PG[(OCI PostgreSQL<br/>or Autonomous DB)]
        OKE --> REDIS[(OCI Cache<br/>Redis)]
        OKE --> NATS[NATS on Compute<br/>3-node cluster]
    end

    OKE -->|Outbound| BACEN[BACEN RSFN]
```

## Infrastructure Setup

### OKE Cluster

```bash
# Create OKE cluster via OCI Console or Terraform
# Region: sa-saopaulo-1 (Sao Paulo)
# Node shape: VM.Standard.E4.Flex (8 OCPU, 32 GB)
# Node count: 3
```

### PostgreSQL Database

Option 1 -- OCI Database with PostgreSQL:

```bash
# Create PostgreSQL database service
# Shape: VM.Standard.E4.Flex
# Storage: 500 GB
# Enable HA (standby in different AD)
```

Option 2 -- Deploy PostgreSQL on OKE using StatefulSet with persistent volumes.

### OCI Cache (Redis)

```bash
# Create OCI Cache with Redis
# Node shape: VM.Standard.E4.Flex (4 OCPU, 16 GB)
# Cluster mode: enabled for HA
```

### NATS Cluster

```bash
# Deploy 3 compute instances for NATS JetStream
# Shape: VM.Standard.E4.Flex (4 OCPU, 16 GB)
# Subnet: private subnet within VCN
```

## Application Deployment

### Container Registry (OCIR)

```bash
# Tag and push images
docker tag pix-backend:latest sa-saopaulo-1.ocir.io/NAMESPACE/monetarie/pix-backend:latest
docker push sa-saopaulo-1.ocir.io/NAMESPACE/monetarie/pix-backend:latest
```

### OCI Vault Secrets

```bash
# Create secrets in OCI Vault
oci vault secret create-base64 \
  --compartment-id ocid1.compartment... \
  --vault-id ocid1.vault... \
  --key-id ocid1.key... \
  --secret-name database-url \
  --secret-content-content "base64-encoded-value"
```

### Deploy to OKE

```bash
kubectl create namespace pix
kubectl apply -f deploy/backend.yaml
kubectl apply -f deploy/admin-frontend.yaml

# Migrate
kubectl exec -n pix deployment/pix-backend -- bin/monetarie_pix eval "Shared.Release.migrate()"
```

## Cost Estimate (sa-saopaulo-1)

| Resource | Spec | Monthly (USD) |
|----------|------|---------------|
| OKE Cluster | Control plane (free) | $0 |
| Compute (3x) | VM.Standard.E4.Flex 8/32 | ~$650 |
| PostgreSQL | VM.Standard.E4.Flex 8/32 | ~$500 |
| OCI Cache | Redis cluster | ~$300 |
| NATS VMs (3x) | VM.Standard.E4.Flex 4/16 | ~$350 |
| Load Balancer | Flexible | ~$25 |
| **Total** | | **~$1,825** |

::: tip OCI FREE TIER
OCI offers Always Free resources (Ampere A1 instances, Autonomous Database). For testing, you can use these to reduce costs significantly.
:::

## Expected Outcome

After completing this deployment:

- OKE cluster in sa-saopaulo-1 with 3 worker nodes
- PostgreSQL database with HA configuration
- Redis cache for rate limiting and session management
- NATS 3-node JetStream cluster
- OCI Load Balancer with TLS termination
