4.6 KiB
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.
- battleship-app
-
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.
- PersistentVolume (mysql-pv): Provides 1Gi storage (using a hostPath at
- battleship-mysql: Manages the MySQL pods with stable identities and persistent storage. It includes:
-
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 namedapp-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 asDB_HOST
,DB_PORT
,DB_USER
,DB_PASSWORD
, andDB_NAME
to connect to the MySQL database. - MySQL Container:
Configured with variablesMYSQL_ROOT_PASSWORD
andMYSQL_DATABASE
to initialize the database.
- Backend Container:
-
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 useimagePullPolicy: Never
assuming images are built locally.
How to Prepare, Run, Pause, and Remove the Application
Preparation
- Build Docker Images:
Execute theprepare-app.sh
script to build the Docker images for the backend and frontend:./prepare-app.sh
Running the Application
- Deploy the Application:
Use thestart-app.sh
script to deploy the namespace, Deployments, StatefulSet, and Services:./start-app.sh
Pausing the Application
- Scale Down the Application:
If you need to temporarily pause the application without deleting objects (thus preserving the data), use:
Note: This script scales the Deployments and StatefulSet to 0 replicas../stop-app.sh
Removing the Application
- Delete All Kubernetes Objects:
To completely remove the application—including Deployments, StatefulSet, Services, and the Namespace—execute:
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./remove-app.sh
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:
Replacehttp://<Node-IP>:30001
<Node-IP>
with the IP address of one of your Kubernetes nodes.