Redis 主从集群+哨兵模式 的启动脚本、配置

pull/11/head
Zhang Peng 2020-06-24 10:56:56 +08:00
parent 9d9c2e72b6
commit 42f2b4ea8a
17 changed files with 261 additions and 128 deletions

View File

@ -0,0 +1,8 @@
port 26381
daemonize yes
sentinel monitor redis-master 172.22.6.3 6381 2
sentinel down-after-milliseconds redis-master 5000
sentinel failover-timeout redis-master 900000
sentinel parallel-syncs redis-master 1
#sentinel auth-pass redis-master 123456
logfile /usr/local/redis/conf/26381/26381.log

View File

@ -0,0 +1,8 @@
port 26382
daemonize yes
sentinel monitor redis-master 172.22.6.3 6382 2
sentinel down-after-milliseconds redis-master 5000
sentinel failover-timeout redis-master 900000
sentinel parallel-syncs redis-master 1
#sentinel auth-pass redis-master 123456
logfile /usr/local/redis/conf/26382/26382.log

View File

@ -0,0 +1,8 @@
port 26383
daemonize yes
sentinel monitor redis-master 172.22.6.3 6383 2
sentinel down-after-milliseconds redis-master 5000
sentinel failover-timeout redis-master 900000
sentinel parallel-syncs redis-master 1
#sentinel auth-pass redis-master 123456
logfile /usr/local/redis/conf/26383/26383.log

View File

@ -3,10 +3,10 @@ bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/cluster/6381/6381.conf
cluster-config-file /usr/local/redis/conf/6381/6381.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/cluster/6381
pidfile /var/run/redis/redis-6381.pid
logfile /usr/local/redis/cluster/6381/6381.log
dir /usr/local/redis/conf/6381
pidfile /usr/local/redis/conf/6381/6381.pid
logfile /usr/local/redis/conf/6381/6381.log

View File

@ -3,10 +3,10 @@ bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/cluster/6382/6382.conf
cluster-config-file /usr/local/redis/conf/6382/6382.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/cluster/6382
pidfile /var/run/redis/redis-6382.pid
logfile /usr/local/redis/cluster/6382/6382.log
dir /usr/local/redis/conf/6382
pidfile /usr/local/redis/conf/6382/6382.pid
logfile /usr/local/redis/conf/6382/6382.log

View File

@ -3,10 +3,10 @@ bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/cluster/6383/6383.conf
cluster-config-file /usr/local/redis/conf/6383/6383.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/cluster/6383
pidfile /var/run/redis/redis-6383.pid
logfile /usr/local/redis/cluster/6383/6383.log
dir /usr/local/redis/conf/6383
pidfile /usr/local/redis/conf/6383/6383.pid
logfile /usr/local/redis/conf/6383/6383.log

View File

@ -3,10 +3,10 @@ bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/cluster/6384/6384.conf
cluster-config-file /usr/local/redis/conf/6384/6384.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/cluster/6384
pidfile /var/run/redis/redis-6384.pid
logfile /usr/local/redis/cluster/6384/6384.log
dir /usr/local/redis/conf/6384
pidfile /usr/local/redis/conf/6384/6384.pid
logfile /usr/local/redis/conf/6384/6384.log

View File

@ -3,10 +3,10 @@ bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/cluster/6385/6385.conf
cluster-config-file /usr/local/redis/conf/6385/6385.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/cluster/6385
pidfile /var/run/redis/redis-6385.pid
logfile /usr/local/redis/cluster/6385/6385.log
dir /usr/local/redis/conf/6385
pidfile /usr/local/redis/conf/6385/6385.pid
logfile /usr/local/redis/conf/6385/6385.log

View File

@ -3,10 +3,10 @@ bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/cluster/6386/6386.conf
cluster-config-file /usr/local/redis/conf/6386/6386.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/cluster/6386
pidfile /var/run/redis/redis-6386.pid
logfile /usr/local/redis/cluster/6386/6386.log
dir /usr/local/redis/conf/6386
pidfile /usr/local/redis/conf/6386/6386.pid
logfile /usr/local/redis/conf/6386/6386.log

View File

@ -0,0 +1,12 @@
port 6387
bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/conf/6387/6387.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/conf/6387
pidfile /usr/local/redis/conf/6387/6387.pid
logfile /usr/local/redis/conf/6387/6387.log

View File

@ -0,0 +1,12 @@
port 6388
bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/conf/6388/6388.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/conf/6388
pidfile /usr/local/redis/conf/6388/6388.pid
logfile /usr/local/redis/conf/6388/6388.log

View File

@ -0,0 +1,12 @@
port 6389
bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/conf/6389/6389.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/conf/6389
pidfile /usr/local/redis/conf/6389/6389.pid
logfile /usr/local/redis/conf/6389/6389.log

View File

@ -0,0 +1,53 @@
# Redis 集群配置
## 使用方式
集群拓扑:
- 三主六从,每个主节点有两个从节点。
- 三哨兵,分别监听其中一个主节点。
启动方式:
- 先执行 start-cluster.sh会自动根据 6381 ~ 6389 目录启动服务器,并将其配置为集群。
- 再执行 start-sentinel.sh会根据 26381 ~ 26383 目录启动哨兵,监听集群中的三个主节点。
## 配置
1集群服务器配置 redis.conf
```
port 6381
bind 0.0.0.0
daemonize yes
cluster-enabled yes
cluster-config-file /usr/local/redis/conf/6381/6381.conf
cluster-node-timeout 10000
appendonly yes
dir /usr/local/redis/conf/6381
pidfile /usr/local/redis/conf/6381/6381.pid
logfile /usr/local/redis/conf/6381/6381.log
```
端口号、配置目录(`/usr/local/redis/conf`)根据实际情况修改。
2哨兵服务器配置 sentinel.conf
```
port 26383
daemonize yes
sentinel monitor redis-master 172.22.6.3 6383 2
sentinel down-after-milliseconds redis-master 5000
sentinel failover-timeout redis-master 900000
sentinel parallel-syncs redis-master 1
#sentinel auth-pass redis-master 123456
logfile /usr/local/redis/conf/26383/26383.log
```
端口号、配置目录(`/usr/local/redis/conf`)根据实际情况修改。
最重要的配置在于sentinel monitor redis-master 172.22.6.3 6383 2
表示监听的服务器集群名叫 redis-master当前哨兵监听的服务器节点是172.22.6.3:6383这个节点如果是主节点一旦宕机选举新的主节点需要至少 2 个哨兵同意。

View File

@ -0,0 +1,102 @@
#!/bin/bash
# Settings
PORT=6380
TIMEOUT=2000
NODES=6
REPLICAS=1
# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.
if [ -a config.sh ]
then
source "config.sh"
fi
# Computed vars
ENDPORT=$((PORT+NODES))
if [ "$1" == "start" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Starting $PORT"
/opt/redis/src/redis-server /usr/local/redis/conf/${PORT}/redis.conf
done
exit 0
fi
if [ "$1" == "create" ]
then
HOSTS=""
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
HOSTS="$HOSTS 127.0.0.1:$PORT"
done
/opt/redis/src/redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS
exit 0
fi
if [ "$1" == "stop" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Stopping $PORT"
/opt/redis/src/redis-cli -p $PORT shutdown nosave
done
exit 0
fi
if [ "$1" == "watch" ]
then
PORT=$((PORT+1))
while [ 1 ]; do
clear
date
/opt/redis/src/redis-cli -p $PORT cluster nodes | head -30
sleep 1
done
exit 0
fi
if [ "$1" == "tail" ]
then
INSTANCE=$2
PORT=$((PORT+INSTANCE))
tail -f ${PORT}.log
exit 0
fi
if [ "$1" == "call" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
/opt/redis/src/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
done
exit 0
fi
if [ "$1" == "clean" ]
then
rm -rf *.log
rm -rf appendonly*.aof
rm -rf dump*.rdb
rm -rf nodes*.conf
exit 0
fi
if [ "$1" == "clean-logs" ]
then
rm -rf *.log
exit 0
fi
echo "Usage: $0 [start|create|stop|watch|tail|clean]"
echo "start -- Launch Redis Cluster instances."
echo "create -- Create a cluster using redis-cli --cluster create."
echo "stop -- Stop Redis Cluster instances."
echo "watch -- Show CLUSTER NODES output (first 30 lines) of first node."
echo "tail <id> -- Run tail -f of instance at base port + ID."
echo "clean -- Remove all instances data, logs, configs."
echo "clean-logs -- Remove just instances logs."

View File

@ -1,104 +0,0 @@
#!/usr/bin/env bash
# ---------------------------------------------------------------------------------
# 控制台颜色
BLACK="\033[1;30m"
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
PURPLE="\033[1;35m"
CYAN="\033[1;36m"
RESET="$(tput sgr0)"
# ---------------------------------------------------------------------------------
printf "${BLUE}\n"
cat << EOF
###################################################################################
# Redis 集群控制脚本
# @system: 适用于 CentOS7+
# @author: Zhang Peng
###################################################################################
EOF
printf "${RESET}\n"
# Settings
PORT=6380
NODES=6
ENDPORT=$((PORT + NODES))
TIMEOUT=2000
REPLICAS=0
PATH="/usr/local/redis"
######################################## MAIN ########################################
printf "${PURPLE}\n"
printf "Usage: $0 [start|create|stop|watch|tail|clean]\n"
printf "start -- Launch Redis Cluster instances.\n"
printf "create -- Create a cluster using redis-cli --cluster create.\n"
printf "stop -- Stop Redis Cluster instances.\n"
printf "watch -- Show CLUSTER NODES output (first 30 lines) of first node.\n"
printf "tail <id> -- Run tail -f of instance at base port + ID.\n"
printf "clean -- Remove all instances data, logs, configs.\n"
printf "clean-logs -- Remove just instances logs.\n"
printf "${RESET}\n"
case $1 in
"start")
while [[ $((PORT < ENDPORT)) != "0" ]]; do
PORT=$((PORT + 1))
echo "Starting $PORT"
if [[ -e "${PATH}/cluster/${PORT}/redis.conf" ]]; then
${PATH}/src/redis-server "${PATH}/cluster/${PORT}/redis.conf"
fi
done
;;
"create")
HOSTS=""
while [[ $((PORT < ENDPORT)) != "0" ]]; do
PORT=$((PORT + 1))
HOSTS="$HOSTS 127.0.0.1:$PORT"
done
${PATH}/src/redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS
;;
"stop")
while [[ $((PORT < ENDPORT)) != "0" ]]; do
PORT=$((PORT + 1))
echo "Stopping $PORT"
${PATH}/src/redis-cli -p $PORT shutdown nosave
done
;;
"watch")
PORT=$((PORT + 1))
while [[ 1 ]]; do
clear
date
${PATH}/src/redis-cli -p $PORT cluster nodes | head -30
sleep 1
done
;;
"tail")
INSTANCE=$2
PORT=$((PORT + INSTANCE))
tail -f ${PORT}.log
;;
"call")
while [[ $((PORT < ENDPORT)) != "0" ]]; do
PORT=$((PORT + 1))
${PATH}/src/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
done
;;
"clean")
rm -rf **/*.log
rm -rf **/appendonly*.aof
rm -rf **/dump*.rdb
rm -rf **/nodes*.conf
;;
"clean-logs")
rm -rf **/*.log
;;
"exit")
printf "${RED}Invalid option!${RESET}\n"
main
exit 0
;;
esac

View File

@ -0,0 +1,19 @@
/opt/redis/src/redis-server /usr/local/redis/conf/6381/redis.conf
/opt/redis/src/redis-server /usr/local/redis/conf/6382/redis.conf
/opt/redis/src/redis-server /usr/local/redis/conf/6383/redis.conf
/opt/redis/src/redis-server /usr/local/redis/conf/6384/redis.conf
/opt/redis/src/redis-server /usr/local/redis/conf/6385/redis.conf
/opt/redis/src/redis-server /usr/local/redis/conf/6386/redis.conf
/opt/redis/src/redis-server /usr/local/redis/conf/6387/redis.conf
/opt/redis/src/redis-server /usr/local/redis/conf/6388/redis.conf
/opt/redis/src/redis-server /usr/local/redis/conf/6389/redis.conf
/opt/redis/src/redis-cli --cluster create 172.22.6.3:6381 172.22.6.3:6382 172.22.6.3:6383 172.22.6.3:6384 172.22.6.3:6385 172.22.6.3:6386 172.22.6.3:6387 172.22.6.3:6388 172.22.6.3:6389 --cluster-replicas 2

View File

@ -0,0 +1,3 @@
/opt/redis/src/redis-sentinel /usr/local/redis/conf/26381/sentinel.conf
/opt/redis/src/redis-sentinel /usr/local/redis/conf/26382/sentinel.conf
/opt/redis/src/redis-sentinel /usr/local/redis/conf/26383/sentinel.conf