Skip to content

Commit

Permalink
feat: dynamic members docker packaging and compose setup (#130)
Browse files Browse the repository at this point in the history
* add docker file for single node

* create a 3-node cluster with docker compose

* feat: Add static bootstrap example's docker-compose.yml

* chore: Add whitespace on file end

---------

Co-authored-by: Gyubong <[email protected]>
  • Loading branch information
CHOUMnote and jopemachine authored Sep 25, 2024
1 parent 99e0c0a commit d02b953
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.dockerignore
Dockerfile
.git
.gitignore

target/
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM rust:latest AS builder

WORKDIR /raftify

COPY . .

RUN apt-get update && apt-get install -y protobuf-compiler
RUN cargo clean
RUN cargo build --workspace
39 changes: 39 additions & 0 deletions examples/memstore/dynamic-members/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Docker-compose example for running a 3-node cluster.
services:
node1:
build:
context: ../../../
dockerfile: ./Dockerfile
image: raftify_img
container_name: node1
command: ["./target/debug/memstore-dynamic-members", "--raft-addr=node1:60061", "--web-server=0.0.0.0:8001"]
ports:
- "8001:8001"
expose:
- "8001"

node2:
build:
context: ../../../
dockerfile: ./Dockerfile
container_name: node2
command: ["./target/debug/memstore-dynamic-members", "--raft-addr=node2:60062", "--web-server=0.0.0.0:8002", "--peer-addr=node1:60061"]
ports:
- "8002:8002"
expose:
- "8002"
depends_on:
- node1

node3:
build:
context: ../../../
dockerfile: ./Dockerfile
container_name: node3
command: ["./target/debug/memstore-dynamic-members", "--raft-addr=node3:60063", "--web-server=0.0.0.0:8003", "--peer-addr=node1:60061"]
ports:
- "8003:8003"
expose:
- "8003"
depends_on:
- node2
46 changes: 46 additions & 0 deletions examples/memstore/static-members/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Docker-compose example for running a 3-node cluster.
services:
node1:
build:
context: ../../../
dockerfile: ./Dockerfile
container_name: node1
entrypoint: ["/bin/sh","-c"]
command:
- |
./misc/generate-static-cluster-config.sh node1 node2 node3 > ./examples/memstore/static-members/cluster_config.toml
./target/debug/memstore-static-members --raft-addr=node1:60061 --web-server=0.0.0.0:8001
ports:
- "8001:8001"
expose:
- "8001"

node2:
build:
context: ../../../
dockerfile: ./Dockerfile
container_name: node2
entrypoint: ["/bin/sh","-c"]
command:
- |
./misc/generate-static-cluster-config.sh node1 node2 node3 > ./examples/memstore/static-members/cluster_config.toml
./target/debug/memstore-static-members --raft-addr=node2:60062 --web-server=0.0.0.0:8002
ports:
- "8002:8002"
expose:
- "8002"

node3:
build:
context: ../../../
dockerfile: ./Dockerfile
container_name: node3
entrypoint: ["/bin/sh","-c"]
command:
- |
./misc/generate-static-cluster-config.sh node1 node2 node3 > ./examples/memstore/static-members/cluster_config.toml
./target/debug/memstore-static-members --raft-addr=node3:60063 --web-server=0.0.0.0:8003
ports:
- "8003:8003"
expose:
- "8003"
10 changes: 9 additions & 1 deletion examples/memstore/static-members/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::ErrorKind;
use std::net::SocketAddr;
use std::path::Path;
use std::str::FromStr;
use tokio::net::lookup_host;
use toml;

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -54,7 +55,14 @@ pub async fn load_peers(
let mut peers = Peers::with_empty();

for peer_info in raft_config.raft.peers {
let addr = SocketAddr::new(peer_info.ip.parse().unwrap(), peer_info.port);
let addr = match peer_info.ip.parse() {
Ok(ip) => SocketAddr::new(ip, peer_info.port),
Err(_) => {
let mut addrs = lookup_host((peer_info.ip.as_str(), peer_info.port)).await?;
addrs.next().expect("Hostname resolution failed!")
}
};

let role = InitialRole::from_str(&peer_info.role).expect("Invalid role!");
peers.add_peer(peer_info.node_id, addr, Some(role));
}
Expand Down
24 changes: 24 additions & 0 deletions misc/generate-static-cluster-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
# Generate `cluster_config.toml` used in static bootstrap example.
# Used in `docker-compose.yml` of the static members example.

if [ "$#" -lt 1 ]; then
echo "Usage: $0 addr1 addr2 addr3 ..."
exit 1
fi

port=60061
node_id=1

for ip in "$@"
do
echo "[[raft.peers]]"
echo "ip = \"$ip\""
echo "port = $port"
echo "node_id = $node_id"
echo "role = \"voter\""
echo ""

port=$((port + 1))
node_id=$((node_id + 1))
done

0 comments on commit d02b953

Please sign in to comment.