π§ Terraform
Terraform gestisce tutta l'infrastruttura GCP in modo dichiarativo.
I file sono in infra/terraform_k8s/.
π Struttura Fileβ
infra/terraform_k8s/
βββ main.tf # Provider + VPC + NAT
βββ gke.tf # Cluster GKE + Node Pool
βββ database.tf # Cloud SQL PostgreSQL
βββ secrets.tf # Secret Manager
βββ registry.tf # Artifact Registry
βββ variables.tf # Variabili configurabili
βββ outputs.tf # Output del terraform
βββ terraform.tfvars # Valori delle variabili
βββ terraform.tfstate # Stato (NON TOCCARE)
Spiegazione Fileβ
main.tf - Provider e Networkingβ
| Risorsa | Nome | Scopo |
|---|---|---|
google_compute_network | visla-k8s-vpc | VPC principale |
google_compute_subnetwork | visla-k8s-subnet | Subnet nodi (10.0.0.0/20) |
google_compute_router | visla-k8s-router | Router per NAT |
google_compute_router_nat | visla-k8s-nat | NAT per traffico in uscita |
Range IP Secondari:
pods: 10.1.0.0/16services: 10.2.0.0/20
gke.tf - Cluster Kubernetesβ
| Risorsa | Nome | Scopo |
|---|---|---|
google_container_cluster | visla-k8s-cluster | Cluster GKE zonal |
google_container_node_pool | visla-node-pool | 2 nodi e2-standard-2 |
Caratteristiche:
- Private Nodes: I nodi non hanno IP pubblici
- Public Endpoint: Master accessibile da internet (per kubectl)
- Workload Identity: Link tra K8s SA e GCP SA
database.tf - Cloud SQLβ
| Risorsa | Scopo |
|---|---|
google_compute_global_address | Range IP per VPC peering |
google_service_networking_connection | Connessione privata VPC-SQL |
google_sql_database_instance | PostgreSQL 17 (db-f1-micro) |
google_sql_database | 10 database (auth, billing, ...) |
google_sql_user | 10 utenti con password random |
secrets.tf - Secret Managerβ
| Secret | Uso |
|---|---|
visla-jwt-secret | JWT signing key |
visla-stripe-api-key | Stripe API key |
visla-db-{service}-password | Password DB per servizio |
registry.tf - Artifact Registryβ
Repository Docker per le immagini container:
europe-west8-docker.pkg.dev/visla-k8s-2026/visla/{service}:latest
π Comandiβ
cd infra/terraform_k8s
# Inizializza
terraform init
# Verifica
terraform plan
# Applica
terraform apply
# Visualizza output
terraform output
Attenzione
Non modificare terraform.tfstate manualmente. Cloud SQL puΓ² richiedere 10-15 minuti per la creazione.