diff --git a/codes/linux/soft/config/redis/cluster/26381/sentinel.conf b/codes/linux/soft/config/redis/cluster/26381/sentinel.conf new file mode 100644 index 0000000..a4b3f94 --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/26381/sentinel.conf @@ -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 \ No newline at end of file diff --git a/codes/linux/soft/config/redis/cluster/26382/sentinel.conf b/codes/linux/soft/config/redis/cluster/26382/sentinel.conf new file mode 100644 index 0000000..db20a61 --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/26382/sentinel.conf @@ -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 \ No newline at end of file diff --git a/codes/linux/soft/config/redis/cluster/26383/sentinel.conf b/codes/linux/soft/config/redis/cluster/26383/sentinel.conf new file mode 100644 index 0000000..fc5af05 --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/26383/sentinel.conf @@ -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 \ No newline at end of file diff --git a/codes/linux/soft/config/redis/cluster/6381/redis.conf b/codes/linux/soft/config/redis/cluster/6381/redis.conf index 943eff0..054c1f0 100644 --- a/codes/linux/soft/config/redis/cluster/6381/redis.conf +++ b/codes/linux/soft/config/redis/cluster/6381/redis.conf @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/6382/redis.conf b/codes/linux/soft/config/redis/cluster/6382/redis.conf index 7f1406c..d5d62e1 100644 --- a/codes/linux/soft/config/redis/cluster/6382/redis.conf +++ b/codes/linux/soft/config/redis/cluster/6382/redis.conf @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/6383/redis.conf b/codes/linux/soft/config/redis/cluster/6383/redis.conf index 22d2aa8..41e10ee 100644 --- a/codes/linux/soft/config/redis/cluster/6383/redis.conf +++ b/codes/linux/soft/config/redis/cluster/6383/redis.conf @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/6384/redis.conf b/codes/linux/soft/config/redis/cluster/6384/redis.conf index b2756e6..b57fef5 100644 --- a/codes/linux/soft/config/redis/cluster/6384/redis.conf +++ b/codes/linux/soft/config/redis/cluster/6384/redis.conf @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/6385/redis.conf b/codes/linux/soft/config/redis/cluster/6385/redis.conf index 28d36cf..7535e79 100644 --- a/codes/linux/soft/config/redis/cluster/6385/redis.conf +++ b/codes/linux/soft/config/redis/cluster/6385/redis.conf @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/6386/redis.conf b/codes/linux/soft/config/redis/cluster/6386/redis.conf index 5ab8bf7..03f1127 100644 --- a/codes/linux/soft/config/redis/cluster/6386/redis.conf +++ b/codes/linux/soft/config/redis/cluster/6386/redis.conf @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/6387/redis.conf b/codes/linux/soft/config/redis/cluster/6387/redis.conf new file mode 100644 index 0000000..31ccfe9 --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/6387/redis.conf @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/6388/redis.conf b/codes/linux/soft/config/redis/cluster/6388/redis.conf new file mode 100644 index 0000000..6f159f9 --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/6388/redis.conf @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/6389/redis.conf b/codes/linux/soft/config/redis/cluster/6389/redis.conf new file mode 100644 index 0000000..e9a17b3 --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/6389/redis.conf @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/README.md b/codes/linux/soft/config/redis/cluster/README.md new file mode 100644 index 0000000..3d37908 --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/README.md @@ -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 个哨兵同意。 \ No newline at end of file diff --git a/codes/linux/soft/config/redis/cluster/create-cluster b/codes/linux/soft/config/redis/cluster/create-cluster new file mode 100644 index 0000000..ac133a5 --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/create-cluster @@ -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 -- Run tail -f of instance at base port + ID." +echo "clean -- Remove all instances data, logs, configs." +echo "clean-logs -- Remove just instances logs." diff --git a/codes/linux/soft/config/redis/cluster/redis-cluster.sh b/codes/linux/soft/config/redis/cluster/redis-cluster.sh deleted file mode 100644 index 23a0051..0000000 --- a/codes/linux/soft/config/redis/cluster/redis-cluster.sh +++ /dev/null @@ -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 -- 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 diff --git a/codes/linux/soft/config/redis/cluster/start-cluster.sh b/codes/linux/soft/config/redis/cluster/start-cluster.sh new file mode 100644 index 0000000..71eb01c --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/start-cluster.sh @@ -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 diff --git a/codes/linux/soft/config/redis/cluster/start-sentinel.sh b/codes/linux/soft/config/redis/cluster/start-sentinel.sh new file mode 100644 index 0000000..9b14673 --- /dev/null +++ b/codes/linux/soft/config/redis/cluster/start-sentinel.sh @@ -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