-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6335373
commit 29afd93
Showing
4 changed files
with
93 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,4 @@ services: | |
build: | ||
context: . | ||
expose: | ||
- 7000 | ||
- 7001 | ||
- 7002 | ||
- 7003 | ||
- 7004 | ||
- 7005 | ||
- '7000-7005' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |