This is a React (Frontend) and Node.js (Backend) application using Redis and MongoDB, deployed with Docker Swarm to provide load balancing and high availability.
- Docker and Docker Compose installed
- Docker Swarm initialized
⚠ Important: Ensure that no other application is using ports 3000, 8080, 27017, and 6379 on your machine. These ports are used for the frontend, backend, MongoDB, and Redis services, respectively.
If you haven't cloned the repository yet, you can do so by running:
git clone git@github.com:cuonglamphu/Docker-Orchestration-Practice.git
cd Docker-Orchestration-PracticeIf Docker Swarm is not yet initialized, start it on the main node (your machine) by running:
docker swarm initDocker Swarm does not directly support the build option in docker-compose.yml, so you need to build the images locally before deployment.
docker-compose buildUse Docker Swarm to deploy services with the following command:
docker stack deploy -c docker-compose.yml onlineshop- This command deploys services based on the configuration in
docker-compose.yml. onlineshopis the stack name, which you can modify as desired.
To view the status of services running in the stack, use:
docker stack services onlineshopThis command displays the replica count of each service and their current status.
To view logs of a particular service, use:
docker service logs onlineshop_backend
docker service logs onlineshop_frontendReplace onlineshop_backend and onlineshop_frontend with the name of the service you want to inspect.
- Frontend (React): Access
http://localhost:8080in your browser. - Backend (Node.js): The API is served at
http://localhost:3000.
To test load balancing across service replicas, you can run the following PowerShell script. This script makes 10 requests to the backend API at http://localhost:3000/test and outputs the response for each request. Each request should ideally be served by a different replica if load balancing is correctly configured.
for ($i = 1; $i -le 10; $i++) {
$response = curl http://localhost:3000/test
Write-Output "Response #${i}:`n${response}`n"
Write-Output "----------------------------"
}This test allows you to verify that requests are distributed across backend replicas.
- MongoDB: Data is stored in the
mongo-datavolume to ensure data persistence. - Redis: Used to manage sessions and cache data.
To stop and remove the stack, use the following command:
docker stack rm onlineshopThis command removes all services in the onlineshop stack.
For a video demonstration, please watch here.
With these steps, you can easily start and manage your Node-GK application on Docker Swarm.