redis集群搭建脚本
> 注意点1: centos7.x的系统,低版本没有尝试,失败一般发生在编译redis源码的时候缺少依赖
> 注意点2: IPADDRS的服务器间通信必须正常,防火墙关闭或开放对应集群端口
> 注意点3: !!!!!! 必须在IPADDRS中的某一台服务器上用root账号执行此脚本 !!!!!!
执行时必须在此节点执行的命令: yum install sshpass -y
安装完成后不使用此脚本可以remove掉sshpass,自己决定
> 注意点4: EXEPATH指定的目录下不建议存在redis开头的任何文件夹,不然此脚本会影响你的
文件,删除集群时可能造成误删除.
> 注意点5: IPADDRS和PASSWORD的顺序必须一一对应,FIRSTPORT是每台服务器的起始端口号
安装几个实例取决于NODES参数,端口号以此递增
eg:NODES=3时 端口是6379 6380 6381
> 注意点6: 运行start时,redis集群创建存在交互,需要输入yes完成节点载入集群
使用方法
#1. 安装依赖环境
脚本.sh install
#2. 目录配置和编译源码
脚本.sh make
#3. 启动集群
脚本.sh start
#4. 停止集群
脚本.sh stop
#5. 卸载集群
脚本.sh delete
#6. 把集群所有节点注册成服务(不太建议使用,后期可能不方便)
脚本.sh service
#!/bin/bash
# 基本设置
#单台服务器起始端口号 eg:如果NODES=2 则端口号为6379,6380,为3的话就多一个6381
FIRSTPORT=6379
#超时时间配置
TIMEOUT=2000
#单台服务器实例数
NODES=2
#master的备份节点数
REPLICAS=1
#服务器ip集合
IPADDRS=(192.168.37.10 192.168.37.11 192.168.37.12)
#服务器密码集合,与ip对应
PASSWORD=(10pwd 11pwd 12pwd)
#实例安装根目录
EXEPATH=/opt
if [ -a config.sh ]
then
source "config.sh"
fi
#安装redis集群依赖环境
if [ "$1" == "install" ]
then
for p in ${!IPADDRS[@]}; do
IPADDR=${IPADDRS[$p]}
echo -e "\033[43;31m install $IPADDR Start \033[0m"
sshpass -p ${PASSWORD[p]} ssh -o StrictHostKeychecking=no -n root@$IPADDR "yum install gcc-c++ tcl libstdc++-devel wget -y"
echo -e "\033[43;31m install $IPADDR End \033[0m"
done
exit 0
fi
#编译redis集群
if [ "$1" == "make" ]
then
for p in ${!IPADDRS[@]}; do
IPADDR=${IPADDRS[$p]}
echo -e "\033[43;31m download $IPADDR Start \033[0m"
sshpass -p ${PASSWORD[p]} ssh -o StrictHostKeychecking=no -n root@$IPADDR "cd $EXEPATH && wget http://download.redis.io/releases/redis-5.0.5.tar.gz && tar -zxf $EXEPATH/redis-5.0.5.tar.gz && rm -rf $EXEPATH/redis-5.0.5.tar.gz && cd $EXEPATH/redis-5.0.5 && make MALLOC=libc "
echo -e "\033[43;31m download $IPADDR End \033[0m"
for(( i=0;i<$NODES;i++)); do
PORT=$((FIRSTPORT+i))
echo -e "\033[43;31m make $IPADDR:$PORT Start \033[0m"
sshpass -p ${PASSWORD[p]} ssh -o StrictHostKeychecking=no -n root@$IPADDR "cd $EXEPATH/redis-5.0.5 && make install PREFIX=$EXEPATH/redis_$PORT && mkdir $EXEPATH/redis_$PORT/etc && mkdir $EXEPATH/redis_$PORT/pid && mkdir $EXEPATH/redis_$PORT/logs && mkdir $EXEPATH/redis_$PORT/data && cp $EXEPATH/redis-5.0.5/redis.conf $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '69c bind 0.0.0.0' $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '88c protected-mode no' $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '92c port $PORT' $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '136c daemonize yes' $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '158c pidfile $EXEPATH/redis_$PORT/pid/redis_$PORT.pid' $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '171c logfile $EXEPATH/redis_$PORT/logs/redis.log' $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '263c dir $EXEPATH/redis_$PORT/data' $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '832c cluster-enabled yes' $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '840c cluster-config-file $EXEPATH/redis_$PORT/etc/nodes_$PORT.conf' $EXEPATH/redis_$PORT/etc/redis.conf && sed -i '846c cluster-node-timeout $TIMEOUT' $EXEPATH/redis_$PORT/etc/redis.conf "
echo -e "\033[43;31m make $IPADDR:$PORT End \033[0m"
done;
sshpass -p ${PASSWORD[p]} ssh -o StrictHostKeychecking=no -n root@$IPADDR "rm -rf $EXEPATH/redis-5.0.5"
done
exit 0
fi
#运行集群
if [ "$1" == "start" ]
then
HOSTS=""
for p in ${!IPADDRS[@]}; do
IPADDR=${IPADDRS[$p]}
for(( i=0;i<$NODES;i++)); do
PORT=$((FIRSTPORT+i))
HOSTS="$HOSTS $IPADDR:$PORT"
echo -e "\033[43;31m Starting $IPADDR:$PORT Start \033[0m"
sshpass -p ${PASSWORD[$p]} ssh -o StrictHostKeychecking=no -n root@$IPADDR "$EXEPATH/redis_$PORT/bin/redis-server $EXEPATH/redis_$PORT/etc/redis.conf"
echo -e "\033[43;31m Starting $IPADDR:$PORT End \033[0m"
done;
done
echo -e "\033[43;31m create cluster: $HOSTS \033[0m"
$EXEPATH/redis_$FIRSTPORT/bin/redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS
exit 0
fi
#停止集群,建议先停止从节点,再停止主节点
if [ "$1" == "stop" ]
then
for p in ${!IPADDRS[@]}; do
IPADDR=${IPADDRS[$p]}
for((i=($NODES-1);i>=0;i--)); do
PORT=$((FIRSTPORT+i))
echo -e "\033[43;31m Stopping $IPADDR:$PORT Start \033[0m"
sshpass -p ${PASSWORD[$p]} ssh -o StrictHostKeychecking=no -n root@$IPADDR "$EXEPATH/redis_$PORT/bin/redis-cli -h $IPADDR -p $PORT shutdown nosave"
echo -e "\033[43;31m Stopping $IPADDR:$PORT End \033[0m"
done;
done
exit 0
fi
#删除集群
if [ "$1" == "delete" ];then
read -p "你确定要删除整个集群么?删除前请先stop,该操作会删除所有数据,输入[yes/no]?" input
echo $input
if [ $input = "yes" ];then
for p in ${!IPADDRS[@]}; do
IPADDR=${IPADDRS[$p]}
echo -e "\033[43;31m Delete $IPADDR:$PORT Start \033[0m"
sshpass -p ${PASSWORD[$p]} ssh -o StrictHostKeychecking=no -n root@$IPADDR "rm -rf redis-5.0.5.tar.gz && rm -rf redis-5.0.5 && rm -rf $EXEPATH/redis_*"
echo -e "\033[43;31m Delete $IPADDR:$PORT End \033[0m"
done
fi
exit 0
fi
if [ "$1" == "service" ];then
read -p "设置成服务会重启新系统,我也不建议你注册成服务,确定要操作么,输入[yes/no]?" input
echo $input
if [ $input = "yes" ];then
for p in ${!IPADDRS[@]}; do
IPADDR=${IPADDRS[$p]}
for((i=($NODES-1);i>=0;i--)); do
PORT=$((FIRSTPORT+i))
echo -e "\033[43;31m service $IPADDR:$PORT Start \033[0m"
#sshpass -p ${PASSWORD[$p]} ssh -o StrictHostKeychecking=no -n root@$IPADDR "touch /usr/lib/systemd/system/redis_$PORT.service && echo '[Unit]' >> /usr/lib/systemd/system/redis_$PORT.service && echo 'Description=Redis-Cluster' >> /usr/lib/systemd/system/redis_$PORT.service && echo 'After=network.target' >> /usr/lib/systemd/system/redis_$PORT.service && echo '[Service]' >> /usr/lib/systemd/system/redis_$PORT.service && echo 'Type=forking' >> /usr/lib/systemd/system/redis_$PORT.service && echo 'PIDFile=$EXEPATH/redis_$PORT/pid/redis_$PORT.pid' >> /usr/lib/systemd/system/redis_$PORT.service && echo 'ExecStart=$EXEPATH/redis_$PORT/bin/redis-server $EXEPATH/redis_$PORT/etc/redis.conf' >> /usr/lib/systemd/system/redis_$PORT.service && echo 'ExecReload=/bin/kill -s HUP $MAINPID' >> /usr/lib/systemd/system/redis_$PORT.service && echo 'ExecStop=/bin/kill -s QUIT $MAINPID' >> /usr/lib/systemd/system/redis_$PORT.service && echo 'PrivateTmp=true' >> /usr/lib/systemd/system/redis_$PORT.service && echo '[Install]' >> /usr/lib/systemd/system/redis_$PORT.service && echo 'WantedBy=multi-user.target' >> /usr/lib/systemd/system/redis_$PORT.service "
echo -e "\033[43;31m service $IPADDR:$PORT End \033[0m"
done;
echo -e "\033[43;31m systemctl $IPADDR Start \033[0m"
#sshpass -p ${PASSWORD[$p]} ssh -o StrictHostKeychecking=no -n root@$IPADDR "systemctl daemon-reload && reboot"
echo -e "\033[43;31m systemctl $IPADDR End \033[0m"
done
fi
exit 0
fi
echo "Usage: $0 [install|make|start|stop|delete|server]"
echo "install -- step1:yum安装redis-5.0.5集群所需要的环境,最好配置阿里的yum源"
echo "make -- step2:编译redis源码并初始化redis集群实例和配置"
echo "start -- step3:运行集群所有节点并构建成集群,注意要关闭防火墙或开放对应的端口"
echo "stop -- step4:停止集群所有节点"
echo "delete -- step5:删除整个集群所有数据(删除的是配置的个目录下所有以redis_开头的文件夹和源码包),删除前请先stop,谨慎操作!"