From d02b953e9e6374ec69fdded8565490dc5421c044 Mon Sep 17 00:00:00 2001 From: KIMJiho <52690007+CHOUMnote@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:26:44 +0900 Subject: [PATCH] feat: dynamic members docker packaging and compose setup (#130) * 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 --- .dockerignore | 6 +++ Dockerfile | 9 ++++ .../dynamic-members/docker-compose.yml | 39 ++++++++++++++++ .../static-members/docker-compose.yml | 46 +++++++++++++++++++ examples/memstore/static-members/src/utils.rs | 10 +++- misc/generate-static-cluster-config.sh | 24 ++++++++++ 6 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 examples/memstore/dynamic-members/docker-compose.yml create mode 100644 examples/memstore/static-members/docker-compose.yml create mode 100755 misc/generate-static-cluster-config.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..f95fbb52 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.dockerignore +Dockerfile +.git +.gitignore + +target/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..162f030d --- /dev/null +++ b/Dockerfile @@ -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 diff --git a/examples/memstore/dynamic-members/docker-compose.yml b/examples/memstore/dynamic-members/docker-compose.yml new file mode 100644 index 00000000..da6d04a4 --- /dev/null +++ b/examples/memstore/dynamic-members/docker-compose.yml @@ -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 diff --git a/examples/memstore/static-members/docker-compose.yml b/examples/memstore/static-members/docker-compose.yml new file mode 100644 index 00000000..d2455265 --- /dev/null +++ b/examples/memstore/static-members/docker-compose.yml @@ -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" diff --git a/examples/memstore/static-members/src/utils.rs b/examples/memstore/static-members/src/utils.rs index ec3fbea1..e86f30a5 100644 --- a/examples/memstore/static-members/src/utils.rs +++ b/examples/memstore/static-members/src/utils.rs @@ -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)] @@ -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)); } diff --git a/misc/generate-static-cluster-config.sh b/misc/generate-static-cluster-config.sh new file mode 100755 index 00000000..a545bc27 --- /dev/null +++ b/misc/generate-static-cluster-config.sh @@ -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