Documentation (6 files, 1500+ lines): Tutorials (3): - 01-deploy-rds-database.md: Deploy PostgreSQL in private network - 02-data-persistence.md: Data persistence with named volumes - 03-security-compliance.md: INF-01/02/03/04 compliance How-to Guides (1): - connect-to-postgresql.md: Connection methods Reference (1): - postgresql-commands.md: PostgreSQL command reference Explanation (1): - database-rds-parallels.md: Docker↔RDS parallels with architecture diagrams Key concepts: - PostgreSQL container → RDS Instance - Private network → VPC Private Subnet - Named volume → EBS volume - Resource limits → DB instance class Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
218 lines
4.0 KiB
Markdown
218 lines
4.0 KiB
Markdown
# Tutorial: Deploy Database in VPC Privata (RDS Simulation)
|
|
|
|
In questo tutorial imparerai a deployare un database PostgreSQL in una rete privata simulando RDS in AWS VPC.
|
|
|
|
## Obiettivo
|
|
|
|
Deployare PostgreSQL in Docker private network che simula RDS in VPC privata AWS.
|
|
|
|
## Prerequisiti
|
|
|
|
- Lab 02 (Network & VPC) completato
|
|
- Docker Engine in esecuzione
|
|
- Comandi base: docker-compose, docker ps, docker exec
|
|
|
|
---
|
|
|
|
## Passo 1: Verifica l'ambiente
|
|
|
|
Verifica che le reti private siano già state create.
|
|
|
|
Esegui:
|
|
```bash
|
|
# Verifica reti esistenti
|
|
docker network ls | grep vpc
|
|
|
|
# Atteso:
|
|
# lab05-vpc-private
|
|
# lab05-vpc-public
|
|
```
|
|
|
|
Se le reti non esistono, consulta prima il Lab 02.
|
|
|
|
---
|
|
|
|
## Passo 2: Esamina docker-compose.yml
|
|
|
|
Apri il file `docker-compose.yml` e osserva la configurazione del database.
|
|
|
|
Esegui:
|
|
```bash
|
|
cat docker-compose.yml | grep -A 30 "db:"
|
|
```
|
|
|
|
Configurazione chiave:
|
|
- **image**: postgres:16-alpine
|
|
- **networks**: solo vpc-private (nessuna rete pubblica)
|
|
- **volumes**: db-data per persistenza
|
|
- **ports**: NESSUNA porta esposta
|
|
- **resources**: 2 vCPU, 4GB RAM
|
|
|
|
---
|
|
|
|
## Passo 3: Avvia il database
|
|
|
|
Deploya il database nella rete privata.
|
|
|
|
Esegui:
|
|
```bash
|
|
# Avvia i container
|
|
docker-compose up -d
|
|
|
|
# Verifica che il database sia in esecuzione
|
|
docker ps | grep lab05-db
|
|
```
|
|
|
|
Atteso:
|
|
```
|
|
lab05-db postgres:16-alpine Up 5432/tcp lab05-vpc-private
|
|
```
|
|
|
|
Nota: `5432/tcp` significa che la porta è aperta nel container, ma NON è mappata sull'host.
|
|
|
|
---
|
|
|
|
## Passo 4: Verifica isolamento di rete
|
|
|
|
Il database NON deve essere accessibile dall'host.
|
|
|
|
Esegui:
|
|
```bash
|
|
# Verifica NESSUNA porta mappata su host
|
|
docker port lab05-db
|
|
|
|
# Atteso: (nessun output)
|
|
```
|
|
|
|
Se vedi una porta mappata (es. `0.0.0.0:5432->5432/tcp`), c'è un errore di configurazione.
|
|
|
|
---
|
|
|
|
## Passo 5: Verifica healthcheck
|
|
|
|
PostgreSQL impiega qualche secondo per avviarsi.
|
|
|
|
Esegui:
|
|
```bash
|
|
# Verifica health status
|
|
docker inspect lab05-db --format '{{.State.Health.Status}}'
|
|
|
|
# Oppure usa pg_isready
|
|
docker exec lab05-db pg_isready -U lab05_user
|
|
```
|
|
|
|
Atteso: `healthy` o `accepting connections`
|
|
|
|
---
|
|
|
|
## Passo 6: Connettiti al database
|
|
|
|
Puoi connetterti SOLO da container nella stessa rete privata.
|
|
|
|
Esegui dal container `app`:
|
|
```bash
|
|
# Connettiti dal container app
|
|
docker exec lab05-app psql -h db -U lab05_user -d lab05_db
|
|
|
|
# Una volta connesso, esegui:
|
|
lab05_db=> SELECT version();
|
|
lab05_db=> \q
|
|
```
|
|
|
|
Se provi a connetterti dall'host:
|
|
```bash
|
|
# Questo FALLIRA' (corretto!)
|
|
psql -h localhost -U lab05_user -d lab05_db
|
|
|
|
# Errore: connection refused
|
|
```
|
|
|
|
---
|
|
|
|
## Passo 7: Crea dati di test
|
|
|
|
Crea una tabella e inserisci dati per verificare la persistenza.
|
|
|
|
Esegui:
|
|
```bash
|
|
docker exec lab05-db psql -U lab05_user -d lab05_db -c "
|
|
CREATE TABLE test_table (
|
|
id SERIAL PRIMARY KEY,
|
|
data TEXT,
|
|
created_at TIMESTAMP DEFAULT NOW()
|
|
);
|
|
"
|
|
|
|
docker exec lab05-db psql -U lab05_user -d lab05_db -c "
|
|
INSERT INTO test_table (data) VALUES ('Test RDS simulation');
|
|
"
|
|
```
|
|
|
|
Verifica i dati:
|
|
```bash
|
|
docker exec lab05-db psql -U lab05_user -d lab05_db -c "
|
|
SELECT * FROM test_table;
|
|
"
|
|
```
|
|
|
|
---
|
|
|
|
## Verifica Finale
|
|
|
|
Esegui lo script di verifica finale.
|
|
|
|
Esegui:
|
|
```bash
|
|
cd tests
|
|
./99-final-verification.sh
|
|
```
|
|
|
|
Tutti i test devono PASSARE.
|
|
|
|
---
|
|
|
|
## Parallelismo con AWS RDS
|
|
|
|
| Locale | AWS |
|
|
|--------|-----|
|
|
| PostgreSQL container | RDS Instance |
|
|
| Private network | VPC Private Subnet |
|
|
| Named volume | EBS volume |
|
|
| Resource limits | DB instance class |
|
|
| No host access | Security group restriction |
|
|
|
|
---
|
|
|
|
## Troubleshooting
|
|
|
|
### Database non parte
|
|
```bash
|
|
# Logs del container
|
|
docker logs lab05-db
|
|
|
|
# Verifica risorse
|
|
docker stats lab05-db
|
|
```
|
|
|
|
### Connessione fallita
|
|
```bash
|
|
# Verifica rete
|
|
docker network inspect lab05-vpc-private
|
|
|
|
# Verifica container connessi
|
|
docker inspect lab05-db --format '{{.NetworkSettings.Networks}}'
|
|
```
|
|
|
|
### Persi dati dopo riavvio
|
|
```bash
|
|
# Verifica volume esista
|
|
docker volume ls | grep db-data
|
|
|
|
# Verifica montaggio
|
|
docker inspect lab05-db --format '{{.Mounts}}'
|
|
```
|
|
|
|
---
|
|
|
|
Prossimo tutorial: Implementare healthchecks e dipendenze.
|