111 lines
4.6 KiB
Markdown
111 lines
4.6 KiB
Markdown
# Battleship Application on Kubernetes
|
||
|
||
## Overview
|
||
This application implements a Battleship game system composed of three main components:
|
||
- **Backend:** Handles game logic and communicates with the database.
|
||
- **Frontend:** Provides the web user interface for playing the game.
|
||
- **MySQL Database:** Stores game data persistently.
|
||
|
||
The application is deployed in a Kubernetes cluster using Deployments, a StatefulSet with persistent storage, and Services for network exposure.
|
||
|
||
## Containers Used
|
||
- **battleship-backend:**
|
||
- **Description:** Contains the backend logic for the game.
|
||
- **Port:** Listens on port 4000.
|
||
- **Configuration:** Uses environment variables to connect to the MySQL database.
|
||
|
||
- **battleship-frontend:**
|
||
- **Description:** Hosts the web user interface for the game.
|
||
- **Port:** Listens on port 80.
|
||
- **Exposure:** Exposed via a NodePort (30001).
|
||
|
||
- **MySQL (battleship-mysql):**
|
||
- **Description:** Runs a MySQL database (version 8.0.30) in a StatefulSet.
|
||
- **Port:** Uses port 3306.
|
||
- **Persistence:** Uses a PersistentVolume (PV) and PersistentVolumeClaim (PVC) for data storage.
|
||
|
||
## Kubernetes Objects
|
||
- **Namespace:**
|
||
- **battleship-app**
|
||
All objects (Deployments, StatefulSet, Services, PV, PVC) are created within this namespace.
|
||
|
||
- **Deployments:**
|
||
- **battleship-backend:** Manages the backend pods.
|
||
- **battleship-frontend:** Manages the frontend pods.
|
||
|
||
- **StatefulSet:**
|
||
- **battleship-mysql:** Manages the MySQL pods with stable identities and persistent storage. It includes:
|
||
- **PersistentVolume (mysql-pv):** Provides 1Gi storage (using a hostPath at `/mnt/data/mysql`).
|
||
- **PersistentVolumeClaim (mysql-pvc):** Claims the persistent storage for MySQL.
|
||
|
||
- **Services:**
|
||
- **MySQL Service:** Exposes the MySQL StatefulSet on port 3306.
|
||
- **Backend Service:** Exposes the backend on port 4000 (via NodePort 30000).
|
||
- **Frontend Service:** Exposes the frontend on port 80 (via NodePort 30001).
|
||
|
||
## Virtual Networks and Named Volumes
|
||
- **Virtual Networks:**
|
||
Kubernetes provides internal networking for the cluster, and Services offer stable endpoints for communication between pods.
|
||
*Note:* In the original Docker Compose configuration, a network named `app-net` was defined; in Kubernetes, the cluster’s internal network is used.
|
||
|
||
- **Named Volumes:**
|
||
The MySQL StatefulSet uses a PersistentVolume and a PersistentVolumeClaim to ensure data persistence. This setup guarantees that data (e.g., the game database) survives pod restarts or rescheduling.
|
||
|
||
## Container Configuration
|
||
- **Environment Variables:**
|
||
- **Backend Container:**
|
||
Configured with variables such as `DB_HOST`, `DB_PORT`, `DB_USER`, `DB_PASSWORD`, and `DB_NAME` to connect to the MySQL database.
|
||
- **MySQL Container:**
|
||
Configured with variables `MYSQL_ROOT_PASSWORD` and `MYSQL_DATABASE` to initialize the database.
|
||
|
||
- **Ports:**
|
||
- **Backend:** Container port 4000, exposed on NodePort 30000.
|
||
- **Frontend:** Container port 80, exposed on NodePort 30001.
|
||
- **MySQL:** Container port 3306, exposed internally on port 3306.
|
||
|
||
- **Image Pull Policy:**
|
||
The backend and frontend Deployments use `imagePullPolicy: Never` assuming images are built locally.
|
||
|
||
## How to Prepare, Run, Pause, and Remove the Application
|
||
|
||
### Preparation
|
||
1. **Build Docker Images:**
|
||
Execute the `prepare-app.sh` script to build the Docker images for the backend and frontend:
|
||
```bash
|
||
./prepare-app.sh
|
||
```
|
||
|
||
### Running the Application
|
||
1. **Deploy the Application:**
|
||
Use the `start-app.sh` script to deploy the namespace, Deployments, StatefulSet, and Services:
|
||
```bash
|
||
./start-app.sh
|
||
```
|
||
|
||
### Pausing the Application
|
||
1. **Scale Down the Application:**
|
||
If you need to temporarily pause the application without deleting objects (thus preserving the data), use:
|
||
```bash
|
||
./stop-app.sh
|
||
```
|
||
*Note:* This script scales the Deployments and StatefulSet to 0 replicas.
|
||
|
||
### Removing the Application
|
||
1. **Delete All Kubernetes Objects:**
|
||
To completely remove the application—including Deployments, StatefulSet, Services, and the Namespace—execute:
|
||
```bash
|
||
./remove-app.sh
|
||
```
|
||
*Important:* Deleting the objects may also remove the associated PersistentVolume depending on its reclaim policy. To preserve data, consider setting the PersistentVolume's reclaim policy to `Retain`.
|
||
|
||
## How to Access the Application in a Web Browser
|
||
- **Frontend Access:**
|
||
The frontend is exposed via a NodePort on port **30001**.
|
||
Open a web browser and navigate to:
|
||
```
|
||
http://<Node-IP>:30001
|
||
```
|
||
Replace `<Node-IP>` with the IP address of one of your Kubernetes nodes.
|
||
|
||
|