|
| 1 | +# People API |
| 2 | + |
| 3 | +A RESTful API for managing people records built with modern Java technologies and containerized deployment options. |
| 4 | + |
| 5 | +## 🚀 Technologies |
| 6 | + |
| 7 | +- **Java 21** - Latest LTS version with modern language features |
| 8 | +- **Spring Boot 3.5.7** - Enterprise-grade application framework |
| 9 | +- **Maven** - Dependency management and build tool |
| 10 | +- **PostgreSQL 17** - Relational database |
| 11 | +- **Flyway** - Database migration management |
| 12 | +- **MapStruct** - Type-safe bean mapping |
| 13 | +- **Docker** - Container runtime for development |
| 14 | +- **Kubernetes** - Container orchestration for production |
| 15 | +- **Helm** - Kubernetes package manager |
| 16 | +- **Nginx** - Reverse proxy and load balancer |
| 17 | + |
| 18 | +## 📋 Features |
| 19 | + |
| 20 | +- RESTful CRUD operations for person entities |
| 21 | +- Clean architecture with hexagonal/ports-and-adapters pattern |
| 22 | +- Database migrations with Flyway |
| 23 | +- OpenAPI/Swagger documentation |
| 24 | +- Health checks and actuator endpoints |
| 25 | +- Containerized deployment |
| 26 | +- Production-ready Kubernetes manifests |
| 27 | + |
| 28 | +## 🏗️ Project Structure |
| 29 | + |
| 30 | +``` |
| 31 | +├── src/ |
| 32 | +│ ├── main/java/com/mcqueide/dockertest/ |
| 33 | +│ │ ├── application/ # Application services |
| 34 | +│ │ ├── domain/ # Domain models and ports |
| 35 | +│ │ ├── infrastructure/ # Infrastructure adapters |
| 36 | +│ │ └── interfaces/ # REST controllers and DTOs |
| 37 | +│ └── main/resources/ |
| 38 | +│ ├── db/migration/ # Flyway database migrations |
| 39 | +│ └── application.yml # Application configuration |
| 40 | +├── .k8s/ # Kubernetes manifests |
| 41 | +├── helm/ # Helm charts |
| 42 | +├── nginx/ # Nginx configuration |
| 43 | +├── Dockerfile # Production container image |
| 44 | +├── Dockerfile-dev # Development container image |
| 45 | +└── compose.yaml # Docker Compose for production |
| 46 | +``` |
| 47 | + |
| 48 | +## 🔧 Building the Project |
| 49 | + |
| 50 | +### Prerequisites |
| 51 | + |
| 52 | +- Java 21 or later |
| 53 | +- Maven 3.9+ (or use included Maven wrapper) |
| 54 | +- Docker (for containerized development) |
| 55 | +- Kubernetes cluster (for production deployment) |
| 56 | + |
| 57 | +### Local Build |
| 58 | + |
| 59 | +```bash |
| 60 | +# Using Maven wrapper (recommended) |
| 61 | +./mvnw clean package |
| 62 | + |
| 63 | +# Or with installed Maven |
| 64 | +mvn clean package |
| 65 | +``` |
| 66 | + |
| 67 | +### Running Tests |
| 68 | + |
| 69 | +```bash |
| 70 | +./mvnw test |
| 71 | +``` |
| 72 | + |
| 73 | +The project includes: |
| 74 | +- Unit tests with Mockito |
| 75 | +- Integration tests with Testcontainers |
| 76 | + |
| 77 | +## 🐳 Development with Docker |
| 78 | + |
| 79 | +Docker is the recommended approach for local development as it provides: |
| 80 | +- Consistent development environment |
| 81 | +- Easy database setup |
| 82 | +- Hot reload support |
| 83 | +- No local Java/Maven installation required |
| 84 | + |
| 85 | +### Quick Start |
| 86 | + |
| 87 | +1. **Start the development environment:** |
| 88 | + |
| 89 | +```bash |
| 90 | +docker compose -f compose-dev.yaml up |
| 91 | +``` |
| 92 | + |
| 93 | +This starts: |
| 94 | +- PostgreSQL database on port `5432` |
| 95 | +- Spring Boot app on port `8080` with hot reload |
| 96 | +- Remote debugging on port `5005` |
| 97 | + |
| 98 | +2. **Access the application:** |
| 99 | + |
| 100 | +- API: http://localhost:8080/api/v1/people |
| 101 | +- Swagger UI: http://localhost:8080/swagger-ui.html |
| 102 | +- Health Check: http://localhost:8080/actuator/health |
| 103 | + |
| 104 | +3. **Stop the environment:** |
| 105 | + |
| 106 | +```bash |
| 107 | +docker compose -f compose-dev.yaml down |
| 108 | +``` |
| 109 | + |
| 110 | +### Development Features |
| 111 | + |
| 112 | +- **Hot Reload**: Changes to source code trigger automatic restart |
| 113 | +- **Debug Mode**: Connect your IDE debugger to port `5005` |
| 114 | +- **Persistent Data**: Database data persists in Docker volume |
| 115 | +- **Live Logs**: View application logs with `docker compose logs -f app` |
| 116 | + |
| 117 | +### Production-like Environment |
| 118 | + |
| 119 | +To test the production build locally: |
| 120 | + |
| 121 | +```bash |
| 122 | +# Build and start all services (app, database, nginx) |
| 123 | +docker compose up --build |
| 124 | + |
| 125 | +# Scale the application (2 replicas behind nginx) |
| 126 | +docker compose up -d --scale app=2 |
| 127 | +``` |
| 128 | + |
| 129 | +Access through Nginx reverse proxy: |
| 130 | +- Application: http://localhost:80/api/v1/people |
| 131 | +- Health Check: http://localhost/health |
| 132 | + |
| 133 | +For more Docker deployment options, see `README.Docker.md`. |
| 134 | + |
| 135 | +## ☸️ Production Deployment with Kubernetes |
| 136 | + |
| 137 | +Kubernetes is used for production deployments, providing: |
| 138 | +- High availability with multiple replicas |
| 139 | +- Load balancing |
| 140 | +- Rolling updates with zero downtime |
| 141 | +- Auto-scaling capabilities |
| 142 | +- Resource management |
| 143 | + |
| 144 | +### Prerequisites |
| 145 | + |
| 146 | +- Kubernetes cluster (local or cloud) |
| 147 | +- `kubectl` configured |
| 148 | +- Helm 3.x installed |
| 149 | + |
| 150 | +For local testing, you can use [Kind](https://kind.sigs.k8s.io/): |
| 151 | + |
| 152 | +```bash |
| 153 | +kind create cluster --config=.k8s/kind.yaml --name=people-api |
| 154 | +kubectl cluster-info --context kind-people-api |
| 155 | +``` |
| 156 | + |
| 157 | +### Deploying with Helm (Recommended) |
| 158 | + |
| 159 | +1. **Deploy PostgreSQL database:** |
| 160 | + |
| 161 | +```bash |
| 162 | +# Install the PostgreSQL chart |
| 163 | +helm install people helm/postgres |
| 164 | + |
| 165 | +# Verify deployment |
| 166 | +kubectl get pods |
| 167 | +kubectl get svc |
| 168 | +``` |
| 169 | + |
| 170 | +2. **Deploy the application:** |
| 171 | + |
| 172 | +```bash |
| 173 | +# Apply Kubernetes manifests |
| 174 | +kubectl apply -f .k8s/backend/env.yaml |
| 175 | +kubectl apply -f .k8s/backend/secrect.yaml |
| 176 | +kubectl apply -f .k8s/backend/deployment.yaml |
| 177 | +kubectl apply -f .k8s/backend/service.yaml |
| 178 | + |
| 179 | +# Verify deployment |
| 180 | +kubectl get deployments |
| 181 | +kubectl get pods |
| 182 | +kubectl get svc |
| 183 | +``` |
| 184 | + |
| 185 | +3. **Access the application:** |
| 186 | + |
| 187 | +```bash |
| 188 | +# Port forward to access locally |
| 189 | +kubectl port-forward svc/people-api 8080:8080 |
| 190 | + |
| 191 | +# Access at http://localhost:8080/api/v1/people |
| 192 | +``` |
| 193 | + |
| 194 | +### Deployment Details |
| 195 | + |
| 196 | +The Kubernetes deployment includes: |
| 197 | + |
| 198 | +- **Application**: 3 replicas for high availability |
| 199 | +- **Database**: StatefulSet with persistent storage |
| 200 | +- **Resources**: CPU and memory limits/requests configured |
| 201 | +- **Probes**: Startup, readiness, and liveness checks |
| 202 | +- **Security**: Non-root user, dropped capabilities, security contexts |
| 203 | + |
| 204 | +### Helm Chart Configuration |
| 205 | + |
| 206 | +Customize the PostgreSQL deployment by editing `helm/postgres/values.yaml`: |
| 207 | + |
| 208 | +```yaml |
| 209 | +container: |
| 210 | + resources: |
| 211 | + requests: |
| 212 | + cpu: "500m" |
| 213 | + memory: "512Mi" |
| 214 | + limits: |
| 215 | + cpu: "1" |
| 216 | + memory: "1Gi" |
| 217 | +``` |
| 218 | +
|
| 219 | +### Useful Commands |
| 220 | +
|
| 221 | +```bash |
| 222 | +# View application logs |
| 223 | +kubectl logs -f deployment/people-api |
| 224 | + |
| 225 | +# View database logs |
| 226 | +kubectl logs -f deployment/people-db |
| 227 | + |
| 228 | +# Scale application |
| 229 | +kubectl scale deployment/people-api --replicas=5 |
| 230 | + |
| 231 | +# Update deployment |
| 232 | +kubectl set image deployment/people-api people-api=mcqueide/people-api:new-tag |
| 233 | + |
| 234 | +# Check deployment status |
| 235 | +kubectl rollout status deployment/people-api |
| 236 | + |
| 237 | +# Rollback deployment |
| 238 | +kubectl rollout undo deployment/people-api |
| 239 | + |
| 240 | +# Uninstall Helm release |
| 241 | +helm uninstall people-db |
| 242 | +``` |
| 243 | + |
| 244 | +For detailed Kubernetes instructions, see `README.K8S.md`. |
| 245 | + |
| 246 | +## 📡 API Endpoints |
| 247 | + |
| 248 | +| Method | Endpoint | Description | |
| 249 | +|--------|----------|-------------| |
| 250 | +| POST | `/api/v1/people` | Create a new person | |
| 251 | +| GET | `/api/v1/people/{id}` | Get person by ID | |
| 252 | +| PUT | `/api/v1/people/{id}` | Update person | |
| 253 | +| DELETE | `/api/v1/people/{id}` | Delete person | |
| 254 | +| GET | `/api/v1/people` | List people (paginated) | |
| 255 | + |
| 256 | +### Example Request |
| 257 | + |
| 258 | +```bash |
| 259 | +# Create a person |
| 260 | +curl -X POST http://localhost:8080/api/v1/people \ |
| 261 | + -H "Content-Type: application/json" \ |
| 262 | + -d '{ |
| 263 | + "name": "Grace Hopper", |
| 264 | + "birthday": "1906-12-09", |
| 265 | + "address": "New York", |
| 266 | + "phone": "+1-555-1234" |
| 267 | + }' |
| 268 | + |
| 269 | +# Get all people |
| 270 | +curl http://localhost:8080/api/v1/people?page=0&size=20 |
| 271 | +``` |
| 272 | + |
| 273 | +## 🔒 Security |
| 274 | + |
| 275 | +- **Secrets Management**: Database credentials stored in Kubernetes Secrets |
| 276 | +- **Network Policies**: Backend network isolated from external access |
| 277 | +- **Security Contexts**: Containers run as non-root with dropped capabilities |
| 278 | +- **Resource Limits**: CPU and memory limits prevent resource exhaustion |
| 279 | + |
| 280 | +## 📊 Monitoring |
| 281 | + |
| 282 | +- **Health Checks**: Available at `/actuator/health` |
| 283 | +- **Metrics**: Spring Boot Actuator endpoints exposed |
| 284 | +- **Logs**: Structured JSON logging with rotation |
| 285 | + |
| 286 | +## 📝 License |
| 287 | + |
| 288 | +This project is a demo application for Docker and Kubernetes learning purposes. |
| 289 | + |
| 290 | +## 👤 Author |
| 291 | + |
| 292 | +mcqueide |
0 commit comments