Skip to content

Commit

Permalink
Igor/new cluster init (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
hostmaster authored May 3, 2024
1 parent 6335373 commit 29afd93
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 37 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

name: CI

# Controls when the action will run.
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down Expand Up @@ -48,7 +48,6 @@ jobs:
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:latest
cache-to: type=inline
tags: |
${{ env.IMAGE_NAME }}:latest
${{ env.IMAGE_NAME }}:${{ env.VERSION }}
env:
VERSION: ${{ steps.git.outputs.short }}
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# syntax = docker/dockerfile:1
FROM redis:5-alpine
# hadolint ignore=DL3018,DL3028
RUN apk --no-cache --update add runit ruby \
&& gem install redis
&& gem install redis && mkdir -p /etc/service

COPY /entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]
ENTRYPOINT ["/entrypoint.sh"]
CMD ["redis-cluster"]
7 changes: 1 addition & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ services:
build:
context: .
expose:
- 7000
- 7001
- 7002
- 7003
- 7004
- 7005
- '7000-7005'
108 changes: 83 additions & 25 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,94 @@
#!/bin/sh -eu
#!/bin/sh -e

PORTS="7000 7001 7002 7003 7004 7005"
if [ "$1" = 'redis-cluster' ]; then
# Allow passing in cluster IP by argument or environmental variable
IP="${2:-$IP}"

mkdir -p /etc/service /etc/redis
if [ -z "$IP" ]; then # If IP is unset then discover it
IP=$(hostname -i)
fi

for PORT in $PORTS; do
mkdir -p /etc/sv/$PORT
if [ -z "${INITIAL_PORT}" ]; then # Default to port 7000
INITIAL_PORT=7000
fi

cat >"/etc/sv/$PORT/run" <<EOF
#!/bin/sh -eu
exec 2>&1
exec /usr/local/bin/redis-server /etc/redis/$PORT.conf
EOF
if [ -z "$MASTERS" ]; then # Default to 3 masters
MASTERS=3
fi

if [ -z "$SLAVES_PER_MASTER" ]; then # Default to 1 slave for each master
SLAVES_PER_MASTER=1
fi

if [ -z "$BIND_ADDRESS" ]; then # Default to any IPv4 address
BIND_ADDRESS=0.0.0.0
fi

max_port=$((INITIAL_PORT + MASTERS * (SLAVES_PER_MASTER + 1) - 1))

first_standalone=$((max_port + 1))
if [ "$STANDALONE" = "true" ]; then
STANDALONE=2
fi
if [ -n "$STANDALONE" ]; then
max_port=$((max_port + STANDALONE))
fi

for port in $(seq $INITIAL_PORT $max_port); do
mkdir -p "/redis-conf/${port}" "/redis-data/${port}" "/etc/sv/${port}"

if [ -e "/redis-data/${port}/nodes.conf" ]; then
rm "/redis-data/${port}/nodes.conf"
fi

if [ -e "/redis-data/${port}/dump.rdb" ]; then
rm "/redis-data/${port}/dump.rdb"
fi

if [ -e "/redis-data/${port}/appendonly.aof" ]; then
rm "/redis-data/${port}/appendonly.aof"
fi

cat > "/etc/redis/$PORT.conf" <<EOF
if [ "$port" -lt "$first_standalone" ]; then
cat >"/redis-conf/${port}/redis.conf" <<EOF
bind ${BIND_ADDRESS}
port ${port}
daemonize no
port $PORT
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
dir /redis-data/${port}
EOF
chmod +x /etc/sv/$PORT/run
ln -svf /etc/sv/$PORT /etc/service/
done

(
REDIS_IP=$(hostname -i)
sleep 3 && redis-cli --cluster create \
$REDIS_IP:7000 $REDIS_IP:7001 $REDIS_IP:7002 \
$REDIS_IP:7003 $REDIS_IP:7004 $REDIS_IP:7005 \
--cluster-replicas 1 --cluster-yes;
) &

exec /sbin/runsvdir -P /etc/service
nodes="$nodes $IP:$port"
else
cat >"/redis-conf/${port}/redis.conf" <<EOF
bind ${BIND_ADDRESS}
port ${port}
daemonize no
appendonly yes
dir /redis-data/${port}
EOF
fi

cat >"/etc/sv/${port}/run" <<EOF
#!/bin/sh -eu
exec 2>&1
exec /usr/local/bin/redis-server /redis-conf/${port}/redis.conf
EOF

chmod +x "/etc/sv/${port}/run"
ln -sf "/etc/sv/${port}" /etc/service/
done

(
sleep 5
echo "yes" | eval redis-cli --cluster create --cluster-replicas "$SLAVES_PER_MASTER" "$nodes"
) &

exec /sbin/runsvdir -P /etc/service

else

exec "$@"
fi

0 comments on commit 29afd93

Please sign in to comment.