shell脚本搭建redis集群
搭建环境
系统:centos7.2
服务器A: 192.168.2.223 1核 2GB
服务器B: 192.168.2.225 1核 2GB
服务器A安装脚本
#!/bin/bash
IP="192.168.2.223"
port1="7001"
port2="7002"
port3="7003"
dirPackage="/home/tools"
log="/home/tools/log"
function installPackage() {
FILENAME=$(basename $1)
echo "build & install $FILENAME"
{
cd ${dirPackage}
if [ -d "$2" ]; then
rm -rf "$2"
fi
case "$1" in
*.tar.gz|*.tgz) tar xzvf $1;;
*.tar.bz2) tar xjvf $1;;
*) tar xvf $1;;
esac
cd "$2"
case "$3" in
jdk)
jdkPath=$(pwd)
sed -i -e "\$a export JAVA_HOME=${jdkPath}" \
-e "\$a export CLASSPATH=\$JAVA_HOME/lib:\$CLASSPATH" \
-e "\$a export PATH=\$JAVA_HOME/bin:\$PATH" \
/etc/profile
source /etc/profile
;;
redis)
cd src
make
make install
cp redis-trib.rb /usr/local/bin
;;
ruby)
./configure --prefix=/usr/local/ruby
make
make install
ln -sf /usr/local/ruby/bin/ruby /usr/bin/ruby
ln -sf /usr/local/ruby/bin/gem /usr/bin/gem
gem sources --add https://mirrors.huaweicloud.com/repository/rubygems/ --remove https://rubygems.org/
gem install redis
;;
esac
} &> ${log}/install-$FILENAME.log
}
function redisConfig(){
cd ${dirPackage}/$1
dirname="${port1} ${port2} ${port3}"
for i in ${dirname}; do
mkdir -p redis_cluster/${i}
dodir=${dirPackage}/$1/redis_cluster/${i}
cp redis.conf ${dodir}/
sed -e "s@^port 6379@port ${i}@" \
-e "s@^bind 127.0.0.1@bind ${IP}@" \
-e "s@^daemonize no@daemonize yes@" \
-e "s@^# cluster-enabled yes@cluster-enabled yes@" \
-e "s@^# cluster-config-file nodes-6379.conf@cluster-config-file nodes-${i}.conf@" \
-e "s@^pidfile /var/run/redis_6379.pid@pidfile /var/run/redis_${i}.pid@" \
-e "s@^appendonly no@appendonly yes@" \
-i.bak \
redis_cluster/${i}/redis.conf
done
}
function redisStart(){
cd ${dirPackage}/$1
redis-server redis_cluster/${port1}/redis.conf
redis-server redis_cluster/${port2}/redis.conf
redis-server redis_cluster/${port3}/redis.conf
sleep 2
}
function getPackage(){
{
wget -P ${dirPackage} https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/8/jdk/x64/linux/OpenJDK8U-jdk_x64_linux_hotspot_8u265b01.tar.gz
wget -P ${dirPackage} https://mirrors.huaweicloud.com/redis/redis-4.0.9.tar.gz
wget -P ${dirPackage} https://mirrors.huaweicloud.com/ruby/ruby/2.3/ruby-2.3.5.tar.gz
} &> ${log}/wget.log
}
[ ! -d ${dirPackage} ] && mkdir -p ${dirPackage}
[ ! -d ${log} ] && mkdir -p ${log}
yum list installed | grep -w gcc-c++
[ $? -ne 0 ] && yum install gcc-c++ -y
getPackage
installPackage OpenJDK8U-jdk_x64_linux_hotspot_8u265b01.tar.gz jdk8u265-b01 jdk
installPackage redis-4.0.9.tar.gz redis-4.0.9 redis
installPackage ruby-2.3.5.tar.gz ruby-2.3.5 ruby
redisConfig redis-4.0.9
redisStart redis-4.0.9
服务器B安装脚本
#!/bin/bash
IP="192.168.2.225"
port1="7004"
port2="7005"
port3="7006"
dirPackage="/home/tools"
log="/home/tools/log"
function installPackage() {
FILENAME=$(basename $1)
echo "build & install $FILENAME"
{
cd ${dirPackage}
if [ -d "$2" ]; then
rm -rf "$2"
fi
case "$1" in
*.tar.gz|*.tgz) tar xzvf $1;;
*.tar.bz2) tar xjvf $1;;
*) tar xvf $1;;
esac
cd "$2"
case "$3" in
jdk)
jdkPath=$(pwd)
sed -i -e "\$a export JAVA_HOME=${jdkPath}" \
-e "\$a export CLASSPATH=\$JAVA_HOME/lib:\$CLASSPATH" \
-e "\$a export PATH=\$JAVA_HOME/bin:\$PATH" \
/etc/profile
source /etc/profile
;;
redis)
cd src
make
make install
cp redis-trib.rb /usr/local/bin
;;
ruby)
./configure --prefix=/usr/local/ruby
make
make install
ln -sf /usr/local/ruby/bin/ruby /usr/bin/ruby
ln -sf /usr/local/ruby/bin/gem /usr/bin/gem
gem sources --add https://mirrors.huaweicloud.com/repository/rubygems/ --remove https://rubygems.org/
gem install redis
;;
esac
} &> ${log}/install-$FILENAME.log
}
function redisConfig(){
cd ${dirPackage}/$1
dirname="${port1} ${port2} ${port3}"
for i in ${dirname}; do
mkdir -p redis_cluster/${i}
dodir=${dirPackage}/$1/redis_cluster/${i}
cp redis.conf ${dodir}/
sed -e "s@^port 6379@port ${i}@" \
-e "s@^bind 127.0.0.1@bind ${IP}@" \
-e "s@^daemonize no@daemonize yes@" \
-e "s@^# cluster-enabled yes@cluster-enabled yes@" \
-e "s@^# cluster-config-file nodes-6379.conf@cluster-config-file nodes-${i}.conf@" \
-e "s@^pidfile /var/run/redis_6379.pid@pidfile /var/run/redis_${i}.pid@" \
-e "s@^appendonly no@appendonly yes@" \
-i.bak \
redis_cluster/${i}/redis.conf
done
}
function redisStart(){
cd ${dirPackage}/$1
redis-server redis_cluster/${port1}/redis.conf
redis-server redis_cluster/${port2}/redis.conf
redis-server redis_cluster/${port3}/redis.conf
sleep 2
}
function getPackage(){
{
wget -P ${dirPackage} https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/8/jdk/x64/linux/OpenJDK8U-jdk_x64_linux_hotspot_8u265b01.tar.gz
wget -P ${dirPackage} https://mirrors.huaweicloud.com/redis/redis-4.0.9.tar.gz
wget -P ${dirPackage} https://mirrors.huaweicloud.com/ruby/ruby/2.3/ruby-2.3.5.tar.gz
} &> ${log}/wget.log
}
[ ! -d ${dirPackage} ] && mkdir -p ${dirPackage}
[ ! -d ${log} ] && mkdir -p ${log}
yum list installed | grep -w gcc-c++
[ $? -ne 0 ] && yum install gcc-c++ -y
getPackage
installPackage OpenJDK8U-jdk_x64_linux_hotspot_8u265b01.tar.gz jdk8u265-b01 jdk
installPackage redis-4.0.9.tar.gz redis-4.0.9 redis
installPackage ruby-2.3.5.tar.gz ruby-2.3.5 ruby
redisConfig redis-4.0.9
redisStart redis-4.0.9
服务器A/B启动集群脚本
#!/bin/bash
IP1="192.168.2.223"
IP2="192.168.2.225"
yum list installed | grep -w expect &>/dev/null
[ $? -ne 0 ] && yum install expect -y
/usr/bin/expect <<-END &>redis_trib.log
spawn redis-trib.rb create --replicas 1 ${IP1}:7001 ${IP1}:7002 ${IP1}:7003 \
${IP2}:7004 ${IP2}:7005 ${IP2}:7006
expect {
"(type 'yes' to accept):" {send "yes\r";}
}
expect eof
END
服务器A/B停止集群脚本
#!/bin/bash
dirname="/home/tools/redis-4.0.9/"
ps -ef|grep -v grep|grep -w "redis"|awk '{print $2}'|xargs -I % kill %
cd ${dirname} && mv -f nodes-700?.conf /tmp/trash