一、Redis相关路径

Redis路径: /opt/redis/redis-stable

Redis启动脚本路径: /opt/redis/start.sh

节点配置路径: /opt/redis/redis-stable/conf/redis_7000.conf

节点数据路径: /opt/redis/redis-stable/data/7000

日志路径: /opt/redis/redis-stable/logs/7000

2、部署

1) 配置文件

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
logfile /opt/redis/redis-stable/logs/7000/7000.log
loglevel notice
slowlog-log-slower-than 100000
slowlog-max-len 128
# rdb文件存储名称
dbfilename dump7000.rdb
# rdb文件存储位置
dir /opt/redis/redis-stable/data/7000/
repl-diskless-sync no
maxmemory-policy noeviction
pidfile /var/run/redis_7000.pid
daemonize yes
requirepass admin123
masterauth admin123
protected-mode no
# after 86400 sec (1d) if at least 1 key changed
save 86400 1
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes

2) 部署脚本

# 准备文件夹
mkdir -p /opt/redis/redis-stable/conf ;
for i in {7000..7005};do mkdir -p /opt/redis/redis-stable/data/$i;mkdir /opt/redis/redis-stable/logs/$i;done

cd /opt/redis

# 创建配置文件
for i in {7000..7005};do sed -e "s/7000/$i/g" /opt/redis/redis.conf > /opt/redis/redis-stable/conf/redis_$i.conf;done

# 检测创建成功: 
find /opt/redis/redis-stable/conf -type f -exec cat {} \;

# 编译
wget http://download.redis.io/releases/redis-6.2.6.tar.gz
tar xvzf redis-6.2.6.tar.gz
mv redis-6.2.6 redis-stable
cd redis-stable
yum install gcc -y
make

3) redis启动脚本

脚本

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
source /etc/init.d/functions
REDISPORT="$2"
EXEC=/opt/redis/redis-stable/src/redis-server
CLIEXEC=/opt/redis/redis-stable/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/redis/redis-stable/conf/redis_${REDISPORT}.conf"
AUTH="admin123"
BIND_IP='0.0.0.0'

start() {

if [ -f $PIDFILE ]; then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server in port: ${REDISPORT} ..."
$EXEC $CONF
fi
if [ "$?" = "0" ]; then
echo "Redis in port: ${REDISPORT} is running..."
fi

}

stop() {

if [ ! -f $PIDFILE ]; then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping redis in port: ${REDISPORT} ..."
$CLIEXEC -h $BIND_IP -a $AUTH -p $REDISPORT SHUTDOWN
sleep 1
while [ -x /proc/${PID} ]; do
echo "Waiting for Redis in port: ${REDISPORT} to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
}

restart() {
stop
start
}

status() {

ps -ef | grep -E "redis-server.*${REDISPORT}" | grep -v grep >/dev/null 2>&1

if [ $? -eq 0 ]; then
echo "redis server in port: ${REDISPORT} is running"
else
echo "redis server in port: ${REDISPORT} is stopped"
fi

}

case "$1" in
start)
start
;;
stop)
stop
;;

restart)
restart
;;

status)
status
;;
*)
echo "Usage: /opt/redis/start.sh {start|stop|status|start} {7000|7001|..} " >&2
exit 1
;;
esac

启动/停止命令

# 启动
./start.sh start 7000
./start.sh start 7001
./start.sh start 7002
# 停止
./start.sh stop 7003
./start.sh stop 7004
./start.sh stop 7005

4) 组建Cluster

# *为指定的主节点IP,数字1是主从比例,-a后面写设定的密码
/opt/redis/redis-stable/src/redis-cli --cluster create 192.168.159.131:7000 192.168.159.131:7001 192.168.159.131:7002 --cluster-replicas 1 -a admin123