feat(lab-03): complete Phase 4 - Compute & EC2 lab

Phase Plans (5 files):
- 04-RESEARCH.md: Domain research on Docker limits, healthchecks, EC2 parallels
- 04-VALIDATION.md: Success criteria and validation strategy
- 04-01-PLAN.md: Test infrastructure (RED phase)
- 04-02-PLAN.md: Diátxis documentation
- 04-03-PLAN.md: Infrastructure implementation (GREEN phase)

Test Scripts (6 files, 1300+ lines):
- 01-resource-limits-test.sh: Validate INF-03 compliance
- 02-healthcheck-test.sh: Validate healthcheck configuration
- 03-enforcement-test.sh: Verify resource limits with docker stats
- 04-verify-infrastructure.sh: Infrastructure verification
- 99-final-verification.sh: End-to-end student verification
- run-all-tests.sh: Test orchestration with fail-fast
- quick-test.sh: Fast validation (<30s)

Documentation (11 files, 2500+ lines):
Tutorials (3):
- 01-set-resource-limits.md: EC2 instance types, Docker limits syntax
- 02-implement-healthchecks.md: ELB health check parallels
- 03-dependencies-with-health.md: depends_on with service_healthy

How-to Guides (4):
- check-resource-usage.md: docker stats monitoring
- test-limits-enforcement.md: Stress testing CPU/memory
- custom-healthcheck.md: HTTP, TCP, database healthchecks
- instance-type-mapping.md: Docker limits → EC2 mapping

Reference (3):
- compose-resources-syntax.md: Complete deploy.resources reference
- healthcheck-syntax.md: All healthcheck parameters
- ec2-instance-mapping.md: Instance type mapping table

Explanation (1):
- compute-ec2-parallels.md: Container=EC2, Limits=Instance Type, Healthcheck=ELB

Infrastructure:
- docker-compose.yml: 5 services (web, app, worker, db, stress-test)
  All services: INF-03 compliant (cpus + memory limits)
  All services: healthcheck configured
  EC2 parallels: t2.nano, t2.micro, t2.small, t2.medium, m5.large
- Dockerfile: Alpine 3.19 + stress tools + non-root user

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Luca Sacchi Ricciardi
2026-04-03 15:16:58 +02:00
parent 39b9a56850
commit 23a9ffe443
26 changed files with 5457 additions and 1 deletions

View File

@@ -0,0 +1,94 @@
# How-to: Verificare l'Utilizzo delle Risorse
Come monitorare l'utilizzo CPU e memoria dei container Docker.
## Utilizzo Base
### Snapshot Singolo
```bash
docker stats --no-stream
```
Output:
```
CONTAINER NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
12345 lab03-web 0.01% 2.5MiB / 1GiB 0.24% 1.2kB / 0B 0B / 0B 2
```
### Monitoraggio in Tempo Reale
```bash
docker stats
```
Premi `Ctrl+C` per uscire.
### Container Specifico
```bash
docker stats lab03-web
```
## Formattazione Avanzata
### Solo Container e CPU/Memoria
```bash
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"
```
### Output senza header
```bash
docker stats --no-stream --format "{{.Container}}: {{.CPUPerc}}, {{.MemUsage}}"
```
### Output CSV
```bash
docker stats --no-stream --format "{{.Container}},{{.CPUPerc}},{{.MemUsage}}"
```
## Interpretare l'Output
### CPU Percentage
- `0.01%` - Container idle
- `50%` - Container usa mezza CPU
- `100%` - Container usa 1 CPU completa
- `>100%` - Container usa più di 1 CPU (multi-core)
### Memory Usage
- `2.5MiB / 1GiB` - Usati 2.5 MB su 1 GB di limite
- `512MiB / 512MiB` - Al limite (potrebbe causare OOM)
- `980MiB / 1GiB` - Vicino al limite (watch!)
### Memory Percentage
- `<50%` - Sotto l'half del limite (OK)
- `50-80%` - Nella norma (monitorare)
- `>80%` - Vicino al limite (attenzione)
- `>95%` - A rischio di OOM kill
## Troubleshooting
### Container usa 0% CPU
Container potrebbe essere idle o bloccato. Verifica:
```bash
docker exec lab03-web ps aux
```
### Memory usage alto
Identifica il processo che usa più memoria:
```bash
docker exec lab03-web ps aux --sort=-%mem | head -5
```
### Container OOM killed
Cerca "OOM" nei log:
```bash
docker inspect lab03-web --format '{{.State.OOMKilled}}'
```
## Vedi Anche
- How-to: Testare Limits Enforcement
- Reference: Compose Resources Syntax

View File

@@ -0,0 +1,120 @@
# How-to: Scrivere Healthchecks Personalizzati
Come creare healthchecks custom per diversi tipi di servizi.
## Pattern Comuni
### HTTP Healthcheck
```yaml
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost/health || exit 1"]
interval: 15s
timeout: 3s
retries: 3
```
### TCP Port Check
```yaml
healthcheck:
test: ["CMD-SHELL", "nc -z localhost 8080 || exit 1"]
interval: 10s
timeout: 2s
retries: 3
```
### File Existence Check
```yaml
healthcheck:
test: ["CMD-SHELL", "test -f /var/run/app/ready || exit 1"]
interval: 5s
timeout: 1s
retries: 5
```
### Database Connection
```yaml
healthcheck:
test: ["CMD-SHELL", "mysqladmin ping -h localhost || exit 1"]
interval: 10s
timeout: 5s
retries: 5
```
### Redis Check
```yaml
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 3s
retries: 3
```
### Python Script Check
```yaml
healthcheck:
test: ["CMD-SHELL", "python /app/healthcheck.py || exit 1"]
interval: 30s
timeout: 10s
retries: 3
```
## Best Practices
### 1. Check Leggeri
```yaml
# ✓ GOOD - pagina leggera
test: ["CMD", "wget", "--spider", "-q", "http://localhost/health"]
# ✗ AVOID - pagina pesante
test: ["CMD", "wget", "--spider", "-q", "http://localhost/big-data"]
```
### 2. Timeout Appropriati
```yaml
# ✓ GOOD - timeout proporzionato all'interval
interval: 10s
timeout: 3s
# ✗ AVOID - timeout troppo lungo
interval: 10s
timeout: 30s # Riduce frequenza dei check
```
### 3. Retries Adeguati
```yaml
# ✓ GOOD - più retries per servizi lenti (database)
healthcheck:
retries: 5
# ✓ GOOD - meno retries per servizi veloci (web)
healthcheck:
retries: 3
```
## Debugging
### Testare manualmente
```bash
docker exec lab03-web wget --spider -q http://localhost/health
echo $? # 0 = success, !0 = failure
```
### Vedere log healthcheck
```bash
docker inspect lab03-web --format '{{range .State.Health.Log}}{{.Output}} {{end}}'
```
### Reset health status
```bash
docker restart lab03-web
```
## Vedi Anche
- Tutorial: Implementare Healthchecks
- Reference: Healthcheck Syntax

View File

@@ -0,0 +1,97 @@
# How-to: Selezionare l'EC2 Instance Type Corretto
Come scegliere l'istanza EC2 giusta per il tuo workload Docker.
## Decision Tree
### Step 1: Tipo di Carico
**Dev/Test:**
- Usa `t2` (burstable) - economico
- Config: `cpus: '0.5'`, `memory: 512M`
**Web Server:**
- Usa `t2.small` o `t2.medium`
- Config: `cpus: '1'`, `memory: 2G` (o 4G)
**Application Server:**
- Usa `m5.large` - performance consistente
- Config: `cpus: '2'`, `memory: 8G`
**Database:**
- Usa `t2.medium` (dev) o `m5.xlarge` (prod)
- Config: `cpus: '2'`, `memory: 4G` (o 16G)
**Batch Processing:**
- Usa `c5` (compute optimized)
- Config: `cpus: '4'`, `memory: 4G`
### Step 2: Analizza il Tuo Container
```bash
# Monitora il consumo attuale
docker stats lab03-app --no-stream
# Se CPU > 70% → considera più CPU
# Se Memory > 80% → considera più memoria
```
### Step 3: Considera il Costo
| Instance | Costo/ora (us-east-1) | Use Case |
|----------|----------------------|----------|
| t2.nano | ~$0.006 | Micro |
| t2.micro | ~$0.012 | Dev/Test |
| t2.small | ~$0.024 | Web |
| t2.medium | ~$0.048 | Application |
| m5.large | ~$0.096 | Production |
## Docker → EC2 Quick Reference
```yaml
# Dev/Test
cpus: '0.5'; memory: 512M # → t2.nano
cpus: '1'; memory: 1G # → t2.micro
# Web Tier
cpus: '1'; memory: 2G # → t2.small
cpus: '2'; memory: 4G # → t2.medium
# Application Tier
cpus: '2'; memory: 8G # → m5.large
cpus: '4'; memory: 16G # → m5.xlarge
# Database Tier
cpus: '2'; memory: 4G # → t2.medium (dev)
cpus: '4'; memory: 32G # → m5.2xlarge (prod)
```
## Scaling Strategy
### Horizontal Scaling
```yaml
# Più container piccoli (t2.micro)
web:
deploy:
replicas: 4
resources:
limits:
cpus: '1'
memory: 1G
```
### Vertical Scaling
```yaml
# Meno container grandi (m5.large)
web:
deploy:
replicas: 1
resources:
limits:
cpus: '2'
memory: 8G
```
## Vedi Anche
- Reference: EC2 Instance Mapping
- Explanation: Compute-EC2 Parallels

View File

@@ -0,0 +1,88 @@
# How-to: Testare l'Enforcement dei Limiti
Come verificare che i limiti delle risorse siano effettivamente applicati.
## Test 1: Verificare Configurazione
### Controllare nel compose file
```bash
grep -A 10 "deploy:" docker-compose.yml | grep -E "cpus:|memory:"
```
### Controllare nel container
```bash
docker inspect lab03-web --format '{{.HostConfig.NanoCpus}}' # CPU (1e9 = 1 CPU)
docker inspect lab03-web --format '{{.HostConfig.Memory}}' # Memory in bytes
```
## Test 2: Stress Test CPU
### Avviare container con stress
```bash
docker run -d --name stress-test \
--cpus='0.5' \
polinux/stress \
--cpu 1 \
--timeout 30s
```
### Monitorare enforcement
```bash
docker stats stress-test --no-stream
```
**Risultato atteso:** CPU non supera il 50% (0.5 CPU)
### Cleanup
```bash
docker rm -f stress-test
```
## Test 3: Stress Test Memory
### Avviare test OOM
```bash
docker run -d --name mem-test \
--memory='512m' \
polinux/stress \
--vm 1 \
--vm-bytes 600M \
--timeout 60s
```
### Verificare OOM kill
```bash
docker ps -a --filter 'name=mem-test'
```
**Risultato atteso:** Container exited (code 137 = OOM killed)
### Cleanup
```bash
docker rm -f mem-test
```
## Test 4: Verifica con Script
```bash
#!/bin/bash
# verify-limits.sh
for container in lab03-web lab03-app lab03-db; do
echo "Container: $container"
docker inspect "$container" --format ' CPUs: {{.HostConfig.NanoCpus}}'
docker inspect "$container" --format ' Memory: {{.HostConfig.Memory}}'
done
```
## Vedi Anche
- How-to: Check Resource Usage
- Reference: EC2 Instance Mapping