#!/bin/bash
if [[ "" = $1 ]] ; then
echo "==========>>>>>>>>>> error:参数1,环境(dev/test/pro)不能为空"
exit 0
fi
if [[ "" = $2 ]] ; then
echo "==========>>>>>>>>>> error:参数2,起始端口号不能为空"
exit 0
fi
echo "==========>>>>>>>>>>参数1,环境(dev/test/pro):$1"
echo "==========>>>>>>>>>>参数2,起始端口号:$2"
install_path=/usr/local/redis_cluster/$1
# 下载 Redis
echo "==========>>>>>>>>>>开始下载 Redis"
# wget http://download.redis.io/releases/redis-4.0.10.tar.gz
tar -xzvf redis-4.0.10.tar.gz
cd redis-4.0.10
echo "==========>>>>>>>>>>开始安装依赖工具"
yum -y install gcc gcc-c++
# 启动集群参数
start_cluster="./src/redis-trib.rb create --replicas 1"
# IP
ip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
# 当前路径
pwd=`pwd`
echo "==========>>>>>>>>>>编译安装"
for ((i = 0; i < 6; i++))
do
((port_folder=$2+$i))
make MALLOC=libc PREFIX=$install_path/$port_folder install
echo "==========>>>>>>>>>>"
echo "==========>>>>>>>>>>端口号为 $port_folder 的 Redis 安装完毕"
echo "==========>>>>>>>>>>"
# 处理配置文件
cp ../redis.conf $install_path/$port_folder
echo "bind 0.0.0.0" >> $install_path/$port_folder/redis.conf
echo "port $port_folder" >> $install_path/$port_folder/redis.conf
echo "daemonize yes" >> $install_path/$port_folder/redis.conf
echo "pidfile ./redis.pid" >> $install_path/$port_folder/redis.conf
echo "logfile ./redis.log" >> $install_path/$port_folder/redis.conf
echo "cluster-enabled yes" >> $install_path/$port_folder/redis.conf
echo "cluster-config-file nodes-$port_folder.conf" >> $install_path/$port_folder/redis.conf
echo "cluster-node-timeout 5000" >> $install_path/$port_folder/redis.conf
echo "cluster-require-full-coverage no" >> $install_path/$port_folder/redis.conf
cd "$install_path/$port_folder"
# 启动 Redis
start_commod="./bin/redis-server redis.conf"
${start_commod}
echo "==========>>>>>>>>>>端口为 $port_folder 的 Redis 启动完毕"
cd $pwd
start_cluster="$start_cluster $ip:$port_folder"
done
# 安装 Ruby 脚本工具
yum -y install ruby ruby-devel rubygems
gem install ../redis-3.2.1.gem
gem list --local
# 启动集群
echo "==========>>>>>>>>>>启动集群命令:$start_cluster"
${start_cluster}
((end_port=$2+6))
echo "==========>>>>>>>>>>Copy 一键启动脚本"
echo "#!/bin/bash" > ../start.sh
echo "for ((i = $2; i < $end_port; i++))" >> ../start.sh
echo "do" >> ../start.sh
echo " cd $install_path/\$i && ./bin/redis-server redis.conf" >> ../start.sh
echo "done" >> ../start.sh
cp ../start.sh $install_path
# 清理
rm -rf ../redis-4.0.10
rm -rf ../redis-4.0.10.tar.gz
echo "==========>>>>>>>>>>安装完成"
echo "==========>>>>>>>>>>安装路径:$install_path"