單個機器部署redis集群模式(一鍵部署腳本)


一、檢查機器是否安裝gcc、unzip、wget

 

二、部署模式

#模式1: 將所有主從節點以及sentinel節點部署在同一台機器上

#模式2: 將一個數據節點和一個sentinel節點部署在一台機器上,如master+sentinel1,slave1+sentinel2

#模式3: 將所有節點分開部署

 

三、一鍵部署腳本

1、在/root/test/目錄下創建目錄7001 7002 7003 7004 7005 7006 以及env

##7001~7006目錄用來存放redis的配置文件redis.conf,env用來存放生成的公共配置文件redis-env.conf

for ((i=1;i<=6;i++))
do
mkdir -p /root/test/700${i} env
done

2、進入env目錄,創建公共配置文件redis-env.conf

cd env

echo -e "port 7001\ncluster-enabled yes\ndir /root/test/7001\ncluster-config-file nodes-7001.conf\npidfile "/root/test/7001/redis-7001.pid"\nlogfile "/root/test/7001/redis-7001.log"\nappendonly yes" > "redis-env.conf"

注意:綁定本機IP的配置得加上注釋,不然別的IP訪問不了你的redis服務;也可以使用本機的內容IP

Redis配置文件之include、network、general 可以參考https://www.jianshu.com/p/51c0ee9317b3

3、拷貝公共配置文件redis-env.con 到7001~7006目錄下,並根據端口修改redis-env.conf 傳遞的端口值以及文件名包含7001的字段

for ((i=1;i<=6;i++))
do

cp /root/test/env/redis.conf /root/test/700${i}
sed -i "s/7001/700${i}/g" /root/test/700${i}/redis.conf
done

4、檢查wget是否已經安裝

check_results1=`rpm -qa | grep "wget"`
if [[ $check_results1 =~ "wget" ]]
then
    echo "package wget has already installed. "
else
    echo "This is going to install package wget"
        yum install wget -y
fi

5、下載redis安裝包,並解壓

#安裝redis
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
#解壓安裝包
tar -zxvf redis-5.0.5.tar.gz

6、進入解壓目錄,編譯

#編譯
cd redis-5.0.5
make
#make install

7、啟動redis各個節點

(1)方式一:進入redis解壓得到的目錄,創建腳本 start.sh

cd /root/test/redis-5.0.5
vim start.sh
# 內容如下

./src/redis-server /root/test/7001/redis.conf &
./src/redis-server /root/test/7002/redis.conf &
./src/redis-server /root/test/7003/redis.conf &
./src/redis-server /root/test/7004/redis.conf &
./src/redis-server /root/test/7005/redis.conf &
./src/redis-server /root/test/7006/redis.conf &

 

保存退出,並且賦予腳本權限

chmod +x start.sh

檢查redis啟動是否成功

ps -ef |grep redis

 

(2)方式二:進入/root/test 目錄創建 start.sh 腳本

#啟動redis各個節點
for((i=1;i<=6;i++))
do
cd 700${i}
/root/test/redis-5.0.5/src/redis-server /root/test/700${i}/redis.conf &
cd ..
done

保存退出,並且賦予腳本權限

chmod +x start.sh

檢查redis啟動是否成功

ps -ef |grep redis

8、創建redis集群

./redis-5.0.5/src/redis-cli --cluster create --cluster-replicas 1 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 127.0.0.1:7006

現在高版本的redis-cli已經支持集群了,不再需要安裝ruby了。

9、判斷 Can I set the above configuration? (type 'yes' to accept): 則輸入yes

 

10、一鍵關閉redis-server腳本

ps -ef | grep redis-server | grep -v grep | awk '{print $2}' | xargs kill -9

 11、刪除redis集群

(1)關閉redis-server服務

(2)將各個節點的cluster-config-file nodes-7001.conf 文件刪除掉

(3)重新啟動redis

(4)創建redis集群

 

四、整理代碼

1、一鍵部署代碼

##1、在/root/test/目錄下創建目錄7001 7002 7003 7004 7005 7006 以及env
##7001~7006目錄用來存放redis的配置文件redis.conf,env用來存放生成的公共配置文件redis-env.conf
for ((i=1;i<=6;i++))
do
mkdir -p /root/test/700${i} /root/test/env
done

##2、進入env目錄
cd /root/test/env

echo -e "port 7001\ncluster-enabled yes\ndir /root/test/7001\ncluster-config-file nodes-7001.conf\npidfile "/root/test/7001/redis-7001.pid"\nlogfile "/root/test/7001/redis-7001.log"\nappendonly yes" > "redis-env.conf"

##3、拷貝公共配置文件redis-env.con 到7001~7006目錄下,並根據端口修改7001
for ((i=1;i<=6;i++))
do

cp /root/test/env/redis-env.conf /root/test/700${i}/redis.conf
sed -i "s/7001/700${i}/g" /root/test/700${i}/redis.conf
done

##4、下載安裝redis


#檢查wget
check_results1=`rpm -qa | grep "wget"`
if [[ $check_results1 =~ "wget" ]]
then
echo "package wget has already installed. "
else
echo "This is going to install package wget"
yum install wget -y
fi

cd /root/test
#安裝redis
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
#解壓安裝包
tar -zxvf redis-5.0.5.tar.gz
#編譯
cd /root/test/redis-5.0.5
make
#make install

cd /root/test/redis-5.0.5

echo '打印當前路徑'

pwd

##5啟動redis各個節點
for((i=1;i<=6;i++))
do
./src/redis-server /root/test/700$i/redis.conf &
sleep 1
done

cd /root/test/redis-5.0.5
sleep 1
#創建集群
./src/redis-cli --cluster create --cluster-replicas 1 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 127.0.0.1:7006

 

# 注意創建集群時,需要手動輸入yes

 2、關閉redis-server 服務,並刪除創建的7001~7006 目錄下的nodes文件

echo "正在關閉redis-server..."
ps -ef | grep redis-server | grep -v grep | awk '{print $2}' | xargs kill -9
ps -ef |grep redis
sleep 3
echo "正在刪除redis的nodes文件..."
for ((i=1;i<=6;i++))
do
rm -rf  /root/test/700${i}/nodes-700${i}.conf
echo "刪除700${i}目錄下的nodes文件"
done

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM