zkt25/z2/shop
2025-04-23 11:21:51 +02:00
..
controllers Second Assignment 2025-04-23 06:14:47 +02:00
data Second Assignment 2025-04-23 06:14:47 +02:00
kubernetes Second Assignment 2025-04-23 06:14:47 +02:00
models Second Assignment 2025-04-23 06:14:47 +02:00
public Second Assignment 2025-04-23 06:14:47 +02:00
routes Second Assignment 2025-04-23 06:14:47 +02:00
util Second Assignment 2025-04-23 06:14:47 +02:00
views Second Assignment 2025-04-23 06:14:47 +02:00
.gitignore Second Assignment 2025-04-23 06:10:34 +02:00
app.js Second Assignment 2025-04-23 06:10:34 +02:00
Dockerfile Second Assignment 2025-04-23 06:10:34 +02:00
package-lock.json Second Assignment 2025-04-23 06:10:34 +02:00
package.json Second Assignment 2025-04-23 06:10:34 +02:00
README.md added README 2025-04-23 11:21:51 +02:00

Book App

A simple online bookstore application where administrators can add or delete books, and registered users can browse the catalog and add books to their shopping cart. The app is containerized with Docker and deployed to Kubernetes using a dedicated namespace, a Deployment for the Node.js app, a StatefulSet for MongoDB (with PersistentVolume and PersistentVolumeClaim), and Services to expose the application.


Table of Contents

  1. Features
  2. Tech Stack
  3. Project Structure
  4. Kubernetes Manifests & Scripts
  5. Prerequisites
  6. Local Development
  7. Docker Instructions
  8. Kubernetes Deployment
  9. How to Pause / Delete
  10. Accessing the App

Features

  • Admin
    • Add new books (title, author, price, stock)
    • Delete existing books
  • User
    • Browse book catalog
    • Add books to a shopping cart
    • View cart contents

Tech Stack

  • Node.js & Express
  • MongoDB (document database)
  • EJS templating for serverside views
  • Docker & Dockerfile
  • Kubernetes (kubectl, Minikube)

Project Structure

/
├── app.js                   # Express app entry point
├── package.json             # npm dependencies & scripts
├── Dockerfile               # Build instructions for Node.js app
├── controllers/             # Route handlers
│   ├── admin.js
│   └── shop.js
├── models/                  # Mongoose schemas
│   ├── book.js
│   └── user.js
├── routes/                  # Route definitions
│   ├── admin.js
│   └── shop.js
├── views/                   # EJS templates
│   ├── admin/
│   ├── shop/
│   └── includes/
├── public/                  # Static assets (CSS, JS)
├── kubernetes/              # K8s manifests & helper scripts
│   ├── namespace.yaml
│   ├── deployment.yaml
│   ├── service.yaml
│   ├── statefulset.yaml
│   ├── prepare-app.sh
│   ├── start-app.sh
│   └── stop-app.sh
└── README.md                # This documentation

Kubernetes Manifests & Scripts

  • namespace.yaml
    Defines a dedicated Namespace (bookstore-ns) for all resources.

  • deployment.yaml
    A Deployment object for the Node.js Book App.
    Sets replicas, container image, port, and environment variables.

  • service.yaml
    A Service of type NodePort (book-service) to expose the Book App on the cluster.

  • statefulset.yaml

    • A PersistentVolume for MongoDB data storage.
    • A PersistentVolumeClaim bound to the PV.
    • A StatefulSet for MongoDB, ensuring stable network IDs and storage.
  • prepare-app.sh
    Builds the Docker image (book-app:latest) and loads it into the Minikube Docker daemon.
    Creates the Namespace in the cluster.

  • start-app.sh
    Applies all Kubernetes manifests in the proper order:

    1. Namespace
    2. PV & PVC
    3. MongoDB StatefulSet
    4. Book App Deployment
    5. Services
  • stop-app.sh
    Deletes all Kubernetes objects to clean up:

    • Services
    • Deployment & StatefulSet
    • PVC & PV
    • Namespace

Prerequisites

  • Node.js v16+ and npm
  • Docker
  • Minikube (or any Kubernetes cluster)
  • kubectl configured to talk to your cluster

Local Development

  1. Clone the repo
    git clone <repo-url> && cd book-app
    
  2. Install dependencies
    npm install
    
  3. Start MongoDB locally (e.g., mongod --dbpath ./data)
  4. Run the app
    npm start
    
  5. Open your browser at http://localhost:3000

Docker Instructions

Build and tag the image:

docker build -t book-app:latest .

Run the container:

docker run -d -p 3000:3000 --name book-app book-app:latest

Stop and remove:

docker stop book-app
docker rm book-app

Kubernetes Deployment

  1. Enter the Kubernetes folder
    cd kubernetes
    
  2. Prepare the cluster (build image, create namespace)
    ./prepare-app.sh
    
  3. Deploy all resources
    ./start-app.sh
    

How to Pause / Delete

To tear down the entire application and free resources:

cd kubernetes
./stop-app.sh

Accessing the App

Once deployed, run:

minikube service book-service -n bookstore-ns

This command opens your default browser to the NodePortexposed Book App service.

Enjoy managing your book catalog!// filepath: README.md