使用shell编写一个redis集群部署脚本
时间: 2023-12-05 22:04:21 浏览: 105
#!/bin/bash
# 定义redis集群的配置信息
NODES=6 # 集群节点数
REDIS_VERSION=5.0.5 # Redis版本号
PORT_START=7000 # 起始端口号
CONFIG_FILE="redis.conf" # Redis配置文件名
CLUSTER_FILE="nodes.conf" # Redis集群配置文件名
# 定义Redis的安装目录和下载链接
REDIS_HOME="/usr/local/redis"
REDIS_DOWNLOAD_URL="http://download.redis.io/releases/redis-${REDIS_VERSION}.tar.gz"
# 下载并安装Redis
cd /tmp
wget ${REDIS_DOWNLOAD_URL}
tar -xzf redis-${REDIS_VERSION}.tar.gz
cd redis-${REDIS_VERSION}
make
make install PREFIX=${REDIS_HOME}
# 创建Redis节点的配置文件和启动脚本
for ((i=0;i<$NODES;i++))
do
PORT=$(($PORT_START+$i))
mkdir -p ${REDIS_HOME}/conf/${PORT}
cp ${REDIS_HOME}/redis.conf ${REDIS_HOME}/conf/${PORT}/${CONFIG_FILE}
sed -i "s/port 6379/port ${PORT}/g" ${REDIS_HOME}/conf/${PORT}/${CONFIG_FILE}
sed -i "s/daemonize no/daemonize yes/g" ${REDIS_HOME}/conf/${PORT}/${CONFIG_FILE}
echo "pidfile /var/run/redis_${PORT}.pid" >> ${REDIS_HOME}/conf/${PORT}/${CONFIG_FILE}
echo "logfile /var/log/redis_${PORT}.log" >> ${REDIS_HOME}/conf/${PORT}/${CONFIG_FILE}
echo "dir ${REDIS_HOME}/data/${PORT}" >> ${REDIS_HOME}/conf/${PORT}/${CONFIG_FILE}
echo "cluster-config-file ${REDIS_HOME}/conf/${PORT}/${CLUSTER_FILE}" >> ${REDIS_HOME}/conf/${PORT}/${CONFIG_FILE}
echo "#!/bin/bash" > ${REDIS_HOME}/bin/start-${PORT}.sh
echo "${REDIS_HOME}/bin/redis-server ${REDIS_HOME}/conf/${PORT}/${CONFIG_FILE}" >> ${REDIS_HOME}/bin/start-${PORT}.sh
chmod +x ${REDIS_HOME}/bin/start-${PORT}.sh
done
# 启动Redis节点
for ((i=0;i<$NODES;i++))
do
PORT=$(($PORT_START+$i))
${REDIS_HOME}/bin/start-${PORT}.sh &
done
# 等待Redis节点启动完成
sleep 10
# 创建Redis集群
${REDIS_HOME}/bin/redis-cli --cluster create \
127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 \
127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \
--cluster-replicas 1
阅读全文