This project implements a distributed algorithm for communication between nodes in a network using **Sockets**. The code is designed to be containerized using Docker and deployed on different hosts, such as AWS VMs.
## Prerequisites
- Python 3.x
- Docker and Docker Compose
- AWS account
- Dockerhub account
## Initial Setup
1.**Create Virtual Machines**:
- Create *n* EC2 instances on Amazon.
- Save the private IP addresses in the file `instance_privateIPs.txt`
2.**Install Docker and Docker Compose**:
- Use the provided script `install-docker.sh` to install Docker and Docker Compose on each VM. **Note:** This script is for installing Docker and Docker Compose on Ubuntu.
- First, upload the `install-docker.sh` script to each VM.
- Run the following commands to make the script executable and run it
```bash
chmod +x install-docker.sh
./install-docker.sh
```
3.**Create a Docker Swarm**:
- On one of the VMs, initialize the Docker Swarm and note the join token:
```bash
docker swarm init
```
- Copy the `docker swarm join` command displayed after initialization.
- On all other VMs, join the Docker Swarm using the command copied from the previous step:
To deploy your distributed algorithm, you need to create and build a Docker image using the provided `Dockerfile` and push it to Docker Hub. Follow these steps:
1.**Prepare Dockerfile**:
- Use the provided `Dockerfile` to define the specifications of your Docker image. Ensure that the source code is on a machine with the same CPU architecture as your target devices. For simplicity, you can use one of the VMs.
2.**Build Docker Image**:
- Navigate to the directory containing your Dockerfile and execute the following command to build the Docker image. Replace `<your_image_name>` with a suitable name for your image.
```
docker build -t <your_image_name> .
```
3.**Push to Docker Hub**:
- Log in to your Docker Hub account using the command:
```
docker login
```
- Push your Docker image to Docker Hub using the following command. Replace `<your_image_name>` with the name of the image you built and `<your_dockerhub_username>` with your Docker Hub username.
```
docker tag <your_image_name> <your_dockerhub_username>/<your_image_name>:<tag>
**Note:** Ensure that the source code is on a machine with the same CPU architecture as your target device. For the sake of simplicity, use one of the VMs.
## Deployment
These deployment steps should be performed on the master node of the Docker Swarm:
1.**Create Neighbors Configuration**:
- Create a `neighbors.json` file that specifies the neighbors for each node in your distributed algorithm network.
2.**Create Docker Config**:
- Use the `docker config create` command to create a Docker config from your `neighbors.json` file:
```bash
docker config create <config_name> neighbors.json
```
3.**Generate Docker Compose File**:
- Use the provided script `generate-docker-compose.py` to generate a Docker Compose configuration for deploying your distributed algorithm.
- Before running the script, ensure to replace line 10 in the script with the URL of your previously created Docker image:
```python
image="<Docker Hub ID>/<registry ID>:latest"
```
- Additionally, replace lines 12 and 38 in the script `generate-docker-compose.py` with the `<config_name>` created
- Run the script:
```bash
python generate-docker-compose.py
```
4.**Deploy the Stack**:
- Once the Docker Compose file is generated, deploy the stack to your Docker Swarm using the following command: