Hi, it will be useful also have script for quick testing of sentinels like it done for clusters i mean utils/create-cluster script.
Comment From: ljluestc
#!/bin/bash
# Script to quickly set up a Redis Sentinel test environment
# Usage: ./create-sentinel-test.sh <master-port> <sentinel-port-start> <number-of-sentinels>
if [ $# -ne 3 ]; then
echo "Usage: $0 <master-port> <sentinel-port-start> <number-of-sentinels>"
exit 1
fi
MASTER_PORT=$1
SENTINEL_PORT_START=$2
NUM_SENTINELS=$3
# Function to create redis.conf for master and slaves
create_redis_conf() {
local port=$1
local is_master=$2
local role=$(($is_master == 1 ? "master" : "slave"))
cat << EOF > redis_$port.conf
port $port
daemonize yes
pidfile /tmp/redis_$port.pid
logfile /tmp/redis_$port.log
dbfilename dump_$port.rdb
dir /tmp
$(($is_master == 0 ? "slaveof 127.0.0.1 $MASTER_PORT" : ""))
EOF
}
# Function to create sentinel.conf for each sentinel
create_sentinel_conf() {
local port=$1
local master_port=$2
cat << EOF > sentinel_$port.conf
port $port
daemonize yes
pidfile /tmp/sentinel_$port.pid
logfile /tmp/sentinel_$port.log
dir /tmp
sentinel monitor mymaster 127.0.0.1 $master_port 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
EOF
}
# Setup Master
create_redis_conf $MASTER_PORT 1
redis-server redis_$MASTER_PORT.conf
# Setup Slaves
for i in $(seq $((MASTER_PORT+1)) $((MASTER_PORT+2))); do
create_redis_conf $i 0
redis-server redis_$i.conf
done
# Setup Sentinels
for i in $(seq 0 $((NUM_SENTINELS-1))); do
SENTINEL_PORT=$((SENTINEL_PORT_START+i))
create_sentinel_conf $SENTINEL_PORT $MASTER_PORT
redis-sentinel sentinel_$SENTINEL_PORT.conf
done
echo "Sentinel test environment setup completed. Use 'redis-cli -p <port>' to interact."
echo "Master is on port $MASTER_PORT. Sentinels are on ports from $SENTINEL_PORT_START."