Redis4.0.12叢集搭建

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

伺服器:
節點1:10.10.175.55 埠:6379/7379
節點2:10.10.175.56 埠:6379/7379
節點3:10.10.175.57 埠:6379/7379

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

  1. 安裝依賴關係

yum install make zlib openssl* ImageMagick-devel gcc* rubygems -y

2、建立節點目錄
mkdir -p /usr/local/redis-cluster/redis6379
mkdir -p /usr/local/redis-cluster/redis7379

3、下載redis-4.0.12版本並解壓
cd /usr/local/redis-cluster

wget http://download.redis.io/releases/redis-4.0.12.tar.gz

tar -zxvf redis-4.0.12.tar.gz

4、安裝redis
cd redis-4.0.12
make #編譯
make install PREFIX=/usr/local/redis #編譯安裝並將redis放在/usr/local/redis下,這樣就可以直接使用redis

5、配置叢集檔案

將配置檔案複製到安裝目錄
cp redis.conf /usr/local/redis-cluster/redis6379/redis-6379.conf
將配置安裝檔案複製到叢集節點
cp /usr/local/redis/bin/* /usr/local/redis-cluster/redis6379/
cp /usr/local/redis/bin/* /usr/local/redis-cluster/redis7379/

核心調優(可無)

echo never > /sys/kernel/mm/transparent_hugepage/enabled

echo "vm.overcommit_memory = 1" >> /usr/local/sysctl.conf

6、編輯配置檔案
vim /usr/local/redis-cluster/redis6379/redis-6379.conf
更改以下配置:
port 6379 #埠
cluster-enabled yes #啟用叢集模式
cluster-node-timeout 5000 #超時時間
protected-mode no
appendonly yes #開啟持久化模式
daemonize yes #後臺執行
bind 10.10.175.55 #127.0.0.0配置為本機IP
cluster-config-file nodes-6379.conf #nodes配置為節點名稱,不輸入的話就是預設port埠
pidfile /var/run/redis_6379.pid #防止啟動多個程序副本,只啟動7376
requirepass Redispass01! #在最後一行加入密碼

更改完成後複製redis-6379.conf至redis-7379並修改埠、pid和nodes檔名稱

cp /usr/local/redis-cluster/redis6379/redis-6379.conf /usr/local/redis-cluster/redis7379/redis-7379.conf

vim /usr/local/redis-cluster/redis7379/redis-7379.conf

6379和7379更改完成後複製至另外兩臺伺服器,並修改pid IP地址資訊
7、安裝ruby(在10.10.175.55主節點安裝就行)
上傳ruby-3.0.1.tar.gz安裝包
tar zxvf ruby-3.0.1.tar.gz
cd ruby-3.0.1
./configure --prefix=/usr/local/ruby-3.0.1
make && make install
rm -f /usr/bin/ruby
rm -f /usr/bin/gem
ln -s /usr/local/ruby-3.0.1/bin/ruby /usr/bin/ruby
ln -s /usr/local/ruby-3.0.1/bin/gem /usr/bin/gem
ruby --version
gem --version
8、安裝redis叢集依賴包
wget http://rubygems.org/downloads/redis-4.0.0.gem
上傳redis-4.0.0.gem包
gem install -l ./redis-4.0.0.gem
9、更改ruby呼叫的redis密碼
find / -name client.rb
vim /usr/local/ruby-3.0.1/lib/ruby/gems/3.0.0/gems/redis-4.0.0/lib/redis/client.rb
password => "Redispass01!"
10、啟動redis
每臺伺服器上編輯startall.sh指令碼
cd /usr/local/redis-cluster

/bin/bash

DIR=/usr/local/redis-cluster
cd $DIR/redis6379
./redis-server redis-6379.conf
cd $DIR/redis7379
./redis-server redis-7379.conf
ps -ef |grep redis|grep -v grep

啟動redis
sh startall.sh
10、啟動redis叢集(主節點)
cd /usr/local/redis-cluster/src/
./redis-trib.rb create --replicas 1 10.10.175.55:6379 10.10.175.55:7379 10.10.175.56:6379 10.10.175.56:7379 10.10.175.57:6379 10.10.175.57:7379
輸入yes
11、測試叢集
[root@bjtn-5g183-151 src]# ./redis-cli -c -h 10.10.183.151 -p 6379 -a Redispass01! 必須輸入-c引數
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.10.183.151:6379> get *
(nil)
10.10.183.151:6379> keys *
(empty list or set)
10.10.183.151:6379> set name lee
-> Redirected to slot [5798] located at 10.10.183.152:6379
OK
10.10.183.152:6379> get name
"lee"
10.10.183.152:6379>
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.152 -p 6379 -a Redispass01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.153 -p 6379 -a Redispass01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.153 -p 7379 -a Redispasswd01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.152 -p 7379 -a Redispasswd01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"
[root@bjtn-5g183-151 src]# redis-cli -c -h 10.10.183.151 -p 7379 -a Redispasswd01! get name
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"lee"

12、停止叢集
每臺伺服器上編輯stopall.sh指令碼
cd /usr/local/redis-cluster

/bin/bash

PID1=ps -ef |grep 6379|grep -v grep|awk '{print $2}'
PID2=ps -ef |grep 7379|grep -v grep|awk '{print $2}'
kill -9 $PID1 $PID2
ps -ef |grep 'redis'
13、再次啟動叢集
注意再次啟動需要刪除每個節點下生成的檔案,示例:
cd /usr/local/redis-cluster/redis6379
rm -rf appendonly.aof nodes-6379.conf dump.rdb
cd /usr/local/redis-cluster/redis7379
rm -rf appendonly.aof nodes-7379.conf dump.rdb

相關文章