本文将介绍如何安装和配置Redis单机版和扩展集群版。首先,我们会详细说明如何安装Redis单机版,并提供了相应的脚本来简化安装过程。然后,我们会介绍如何在已经安装好的Redis单机版基础上搭建扩展集群,并提供了相应的脚本来帮助您完成这一步骤。最后,我们还会演示一些测试命令以验证集群是否正常工作。
首先,将以下脚本内容复制并保存为/tmp/install_redis.sh文件。
#!/bin/bash
# 安装版本
redis_version="6.2.7"
# 安装目录
#redis_installDir="/opt/module/redis"
redis_installDir="/opt/bigdata/redis"
install_redis() {
local version=$1
local installDir=$2
local redisconf_file=$installDir/redis-$version/redis.conf
# 安装工具包
yum -y install epel-release gcc make tcl
# 下载地址
local downloadUrl="http://download.redis.io/releases/redis-$version.tar.gz"
if [ -z "$(command -v wget)" ]; then
yum -y install wget
if [ $? -eq 0 ]; then
echo "wget安装完成"
else
echo "wget安装失败,请检查"
exit 1
fi
fi
if [ ! -d "${installDir}" ]; then
mkdir -p "${installDir}"
if [ $? -eq 0 ]; then
echo "安装目录${installDir}已创建"
else
echo "请确保您有足够的权限来创建目录,请增加权限后再次执行"
exit 1
fi
fi
if [ ! -f "/tmp/redis-$version.tar.gz" ] ; then
wget "$downloadUrl" -P /tmp
if [ $? -eq 0 ]; then
echo "redis-$version.tar.gz下载成功"
else
echo "redis-$version.tar.gz下载失败,请重试或手动下载到/tmp目录下再次执行"
echo "下载地址:$downloadUrl"
exit 1
fi
else
echo "/tmp/redis-$version.tar.gz文件已存在"
fi
if [ -d "${installDir}/redis-$version" ]; then
echo "${installDir}/redis-$version 已存在,正在删除..."
rm -rf "${installDir}/redis-$version"
fi
tar -zxvf "/tmp/redis-$version.tar.gz" -C "${installDir}"
if [ $? -eq 0 ]; then
echo "redis-$version.tar.gz解压成功"
else
echo "redis-$version.tar.gz解压失败,请查看异常信息后重试"
exit 1
fi
# 编译和安装
cd ${installDir}/redis-$version
make && make install
sed -i "s|daemonize no|daemonize yes|" "$redisconf_file"
sed -i "s|bind 127.0.0.1 -::1|bind 127.0.0.1 -::1\nbind 0.0.0.0|" "$redisconf_file"
sed -i "s|# requirepass foobared|# requirepass foobared|" "$redisconf_file"
# sed -i "s|port 6379|port 6379|" "$redisconf_file"
# sed -i "s|# cluster-enabled yes|cluster-enabled yes|" "$redisconf_file"
# sed -i "s|# cluster-config-file nodes-6379.conf|cluster-config-file nodes.conf|" "$redisconf_file"
# sed -i "s|# cluster-node-timeout 15000|cluster-node-timeout 5000|" "$redisconf_file"
# sed -i "s|appendonly no|appendonly yes|" "$redisconf_file"
echo "redis 单机版下载、安装、配置完成"
echo "redis安装目录:${installDir}/redis-$version"
echo "单机版 进入 redis 交互界面命令:redis-cli -h $(ip addr | grep 'inet ' | awk '{print $2}'| tail -n 1 | grep -oP '\d+\.\d+\.\d+\.\d+') -p 6379"
echo "集群版 进入 redis 交互界面命令:redis-cli -h $(ip addr | grep 'inet ' | awk '{print $2}'| tail -n 1 | grep -oP '\d+\.\d+\.\d+\.\d+') -c -p 30001"
# mkdir -p "${installDir}/redis-$version/cluster-test"
# for cluster_dir in {7000..7005}
# do
# local create_dir="${installDir}/redis-$version/cluster-test/$cluster_dir"
# mkdir -p "$create_dir"
# cp "$redisconf_file" "$create_dir"
# sed -i "s|port 6379|port $cluster_dir|" "$create_dir/redis.conf"
# redis-server "$create_dir/redis.conf"
# echo "$cluster_dir 端口的redis启动成功"
# done
# local dir=${installDir}/redis-$version/utils/create-cluster
# sed -i "s|CLUSTER_HOST=127.0.0.1|CLUSTER_HOST=192.168.145.105|" "$dir/create-cluster"
# sed -i "s|PROTECTED_MODE=yes|PROTECTED_MODE=no|" "$dir/create-cluster"
# $dir/create-cluster start
# $dir/create-cluster create
}
install_redis "$redis_version" "$redis_installDir"
exit 0
在终端中执行以下命令,为脚本添加执行权限。
chmod a+x /tmp/install_redis.sh
执行以下命令,运行脚本开始下载、安装和配置Redis。
/tmp/install_redis.sh
请等待安装完成,如有异常会有提示。
成功如下图所示:
要安装Redis扩展集群版,要先安装好单机版。
首先,将以下脚本内容复制并保存为/opt/bigdata/redis/redis-6.2.7/redis_cluster.sh文件。
#!/bin/bash
# 安装版本
version="6.2.7"
# 安装目录
#installDir="/opt/module/redis"
installDir="/opt/bigdata/redis"
# Settings
BIN_PATH="${installDir}/redis-$version/src/"
CLUSTER_HOST=$(ip addr | grep 'inet ' | awk '{print $2}'| tail -n 1 | grep -oP '\d+\.\d+\.\d+\.\d+')
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""
BIND='0.0.0.0 -::1'
#REQUIREPASS="redisroot"
# 您可能需要将上述配置参数放入config.sh中,以便执行以下命令
# 覆盖默认值而不修改此脚本。
if [ -a config.sh ]
then
source "${installDir}/redis-$version/utils/create-cluster/config.sh"
fi
# 结束端口
ENDPORT=$((PORT+NODES))
if [ "$1" == "start" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
port_dir=${installDir}/redis-$version/redis_cluster/$PORT
mkdir -p "$port_dir"
echo "Starting $PORT"
cd $port_dir
# $BIN_PATH/redis-server --bind $BIND --requirepass $REQUIREPASS --port $PORT --protected-mode $PROTECTED_MODE --cluster-enabled yes --cluster-config-file "$port_dir/nodes-${PORT}.conf" --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename "appendonly-${PORT}.aof" --dbfilename "$port_dir/dump-${PORT}.rdb" --logfile "$port_dir/${PORT}.log" --daemonize yes "${ADDITIONAL_OPTIONS}"
# $BIN_PATH/redis-server --bind $BIND --requirepass $REQUIREPASS --port $PORT --protected-mode $PROTECTED_MODE --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes ${ADDITIONAL_OPTIONS}
$BIN_PATH/redis-server --bind $BIND --port $PORT --protected-mode $PROTECTED_MODE --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes ${ADDITIONAL_OPTIONS}
done
exit 0
fi
if [ "$1" == "create" ]
then
HOSTS=""
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
HOSTS="$HOSTS $CLUSTER_HOST:$PORT"
done
OPT_ARG=""
if [ "$2" == "-f" ]; then
OPT_ARG="--cluster-yes"
fi
$BIN_PATH/redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS $OPT_ARG
exit 0
fi
if [ "$1" == "stop" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Stopping $PORT"
# $BIN_PATH/redis-cli -p $PORT shutdown nosave
$BIN_PATH/redis-cli -p $PORT shutdown
done
exit 0
fi
if [ "$1" == "watch" ]
then
PORT=$((PORT+1))
while [ 1 ]; do
clear
date
$BIN_PATH/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 "${installDir}/redis-$version/redis_cluster/$PORT/${PORT}.log"
exit 0
fi
if [ "$1" == "tailall" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
log_dir="${installDir}/redis-$version/redis_cluster/$PORT/$PORT.log"
tail -f "$log_dir"
done
exit 0
fi
if [ "$1" == "call" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
$BIN_PATH/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
done
exit 0
fi
if [ "$1" == "clean" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
port_dir=${installDir}/redis-$version/redis_cluster/$PORT
rm -rf $port_dir/*.log
rm -rf $port_dir/appendonly*.aof
rm -rf $port_dir/dump*.rdb
rm -rf $port_dir/nodes*.conf
done
exit 0
fi
if [ "$1" == "clean-logs" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
port_dir=${installDir}/redis-$version/redis_cluster/$PORT
rm -rf $port_dir/*.log
done
exit 0
fi
echo "Usage: $0 [start|create|stop|watch|tail|clean|call]"
echo "start -- Launch Redis Cluster instances."
echo "create [-f] -- 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 "tailall -- Run tail -f for all the log files at once."
echo "clean -- Remove all instances data, logs, configs."
echo "clean-logs -- Remove just instances logs."
echo "call <cmd> -- Call a command (up to 7 arguments) on all nodes."
在终端中执行以下命令,为脚本添加执行权限。
cd /opt/bigdata/redis/redis-6.2.7
chmod a+x ./redis_cluster.sh
执行以下命令,运行脚本开始创建Redis扩展集群的目录。
./redis_cluster.sh start
如下图所示:
执行以下命令创建Redis扩展集群。
echo yes | ./redis_cluster.sh create
成功的开始和结束的截图如下图所示:
执行以下命令进入Redis扩展集群交互界面。
redis-cli -h 192.168.145.105 -c -p 30001
使用命令测试是否正常,如下图所示。
cd /opt/bigdata/redis/redis-6.2.7
./redis_cluster.sh start
cd /opt/bigdata/redis/redis-6.2.7
./redis_cluster.sh stop
cd /opt/bigdata/redis/redis-6.2.7
./redis_cluster.sh watch
cd /opt/bigdata/redis/redis-6.2.7
./redis_cluster.sh tail 1
cd /opt/bigdata/redis/redis-6.2.7
./redis_cluster.sh clean
cd /opt/bigdata/redis/redis-6.2.7
./redis_cluster.sh clean-logs
通过本教程,您学习到了如何在Linux系统上进行Redis的安装和配置。首先,您学习到了如何使用脚本自动下载、编译和安装Redis单机版,并对其进行必要的配置修改以满足实际需求。然后,在已经部署好的单机版基础上,您学习到了如何使用另一个脚本创建并启动一个具有多个节点(主节点+从节点)组成的扩展集群。
希望本教程对您有所帮助!如有任何疑问或问题,请随时在评论区留言。感谢阅读!