Redis5.0.10叢集搭建

小白不怕黑發表於2024-10-26

參考文件

https://www.cnblogs.com/hmwh/p/10289138.html

https://www.cnblogs.com/zgqbky/p/11792141.html

以下操作均需在每臺伺服器上執行

  1. 安裝依賴關係
    yum install make zlib openssl* ImageMagick-devel gcc* rubygems -y

2、建立節點目錄
mkdir -p /opt/app/redis-cluster/redis7376
mkdir -p /opt/app/redis-cluster/redis8376
mkdir -p /opt/app/redis-cluster/redis9376
3、下載redis5.0.10版本並解壓
cd /opt/app/redis-cluster
wget http://download.redis.io/releases/redis-5.0.10.tar.gz

tar -zxvf redis-5.0.10.tar.gz

4、安裝redis
cd redis-5.0.10

make #編譯
make install PREFIX=/opt/app/redis #編譯安裝並將redis放在/opt/app/redis下,這樣就可以直接使用redis

5、配置叢集檔案
將配置檔案複製到安裝目錄
cp redis.conf /opt/app/redis-cluster/redis7376/redis-7376.conf
cp redis.conf /opt/app/redis-cluster/redis8376/redis-8376.conf
cp redis.conf /opt/app/redis-cluster/redis9376/redis-9376.conf
將配置安裝檔案複製到叢集節點
cp /opt/app/redis/bin/* /opt/app/redis-cluster/redis7376/
cp /opt/app/redis/bin/* /opt/app/redis-cluster/redis8376/
cp /opt/app/redis/bin/* /opt/app/redis-cluster/redis9376/
核心調優
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "vm.overcommit_memory = 1" >> /opt/app/sysctl.conf
6、編輯配置檔案
vim /opt/app/redis-cluster/redis7376/redis-7376.conf

port 7376 #埠
cluster-enabled yes #啟用叢集模式
cluster-node-timeout 5000 #超時時間
protected-mode no #關閉網路安全模式
appendonly yes #開啟持久化模式
daemonize yes #後臺執行
bind 10.10.133.47 #127.0.0.0配置為本機IP
cluster-config-file nodes-7376.conf #nodes配置為節點名稱
pidfile /var/run/redis_7376.pid #防止啟動多個程序副本,只啟動7376

更改完成後複製redis-7376.conf至redis-8376和redis-9376目錄,並修改埠、pid和nodes檔名稱

cp /opt/app/redis-cluster/redis7376/redis-7376.conf /opt/app/redis-cluster/redis8376/redis-8376.conf
cp /opt/app/redis-cluster/redis7376/redis-7376.conf /opt/app/redis-cluster/redis7376/redis-9376.conf

vim /opt/app/redis-cluster/redis8376/redis-8376.conf
vim /opt/app/redis-cluster/redis8376/redis-9376.conf
7、啟動redis
cd /opt/app/redis-cluster/redis7376/
./redis-server redis-7376.conf
cd /opt/app/redis-cluster/redis8376/
./redis-server redis-8376.conf
cd /opt/app/redis-cluster/redis9376/
./redis-server redis-9376.conf

7、啟動叢集(在master上)
因為我們使用的5.0.0以上的版本的Redis搭建的叢集只需要把編譯後的redis目錄中的這個redis-cli檔案複製到redis-cluster目錄過來就可以了
至於redis-cli在/opt/app/redis-cluster/redis-5.0.10/src下就可以找到,然後複製到/opt/app/redis-cluster下就可以了
cp -r /opt/app/redis-cluster/redis-5.0.10/src/redis-cli /opt/app/redis-cluster

叢集啟動命令

/opt/app/redis-cluster/redis-cli --cluster create 10.10.133.47:7376 10.10.133.47:8376 10.10.133.47:9376 10.10.133.48:7376 10.10.133.48:8376 10.10.133.48:9376 --cluster-replicas 1

提示如下則成功:
[OK] All nodes agree about slots configuration.

Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.

設定密碼(在主備每個節點執行):
config set masterauth umpay2010
config set requirepass umpay2010
AUTH umpay2010
config rewrite

8、如果出現叢集一直join狀態或第二次啟動叢集時報錯,在每個節點執行以下命令然後再重試
cd /opt/app/redis-cluster/redis7376/

刪除啟動檔案

rm -rf rm -rf appendonly.aof nodes-6379.conf

初始化redis(注意IP和埠)

./redis-cli -h 10.10.133.48 -p 7376
10.10.133.48:7376> flushall
OK
10.10.133.48:7376> cluster reset
OK
10.10.133.48:7376> exit

停止redis

ps -ef |grep redis
kill -9 pid

啟動redis

./redis-server redis-7376.conf

start.sh單節點指令碼

DIR=/opt/app/redis-cluster
cd $DIR/redis7376
./redis-server redis-7376.conf
cd $DIR/redis8376
./redis-server redis-8376.conf
cd $DIR/redis9376
./redis-server redis-9376.conf
ps -ef |grep redis|grep -v grep

shoutdown.sh 停止叢集指令碼

/opt/app/redis-cluster/redis-cli -c -h
10.10.133.47 -p 7376 shutdown
/opt/app/redis-cluster/redis-cli -c -h 10.10.133.47 -p 7376 shutdown
/opt/app/redis-cluster/redis-cli -c -h 10.10.133.47 -p 8376 shutdown
/opt/app/redis-cluster/redis-cli -c -h 10.10.133.47 -p 9376 shutdown
/opt/app/redis-cluster/redis-cli -c -h 10.10.133.48 -p 7376 shutdown
/opt/app/redis-cluster/redis-cli -c -h 10.10.133.48 -p 8376 shutdown
/opt/app/redis-cluster/redis-cli -c -h 10.10.133.48 -p 9376 shutdown

./redis

相關文章