首頁  >  文章  >  資料庫  >  Centos7下redis5集群搭建與使用的方法

Centos7下redis5集群搭建與使用的方法

WBOY
WBOY轉載
2023-06-01 11:37:061522瀏覽

1、簡單說明

叢集中應該至少有三個節點,每個節點都有一個備份節點。需要6台伺服器。

如果條件有限,可以建構偽分佈式,以下步驟是在一台 linux 伺服器上建構一個有6個節點的 redis叢集。

2、建立叢集步驟

##2.1、建立目錄

        新目錄:

mkdir /usr/local /redis-cluster

2.2、下載原始碼並解壓縮編譯
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar xzf redis-5.0.0.tar.gz
cd redis-5.0.0
make
make install prefix=/usr/local/redis

3、建立6個redis設定檔

#    6個設定檔不能在同一個目錄,這裡我們定義如下:

/root/software/redis/redis-cluster-conf/7001/redis.conf

/ root/software/redis/redis-cluster-conf/7002/redis.conf
/root/software/redis/redis-cluster-conf/7003/redis.conf
#/root/software/redis/redis- cluster-conf/7004/redis.conf
/root/software/redis/redis-cluster-conf/7005/redis.conf
/root/software/redis/redis-cluster-conf/7006/redis. conf

一些操作指令僅供參考:

cp redis.conf /usr/local/redis/bin
cd /usr/local/redis/
cp -r bin ../redis-cluster/redis01
cd /usr/local/redis-cluster/redis01
rm dump.rdb #删除快照
vim redis.conf

設定檔的內容為:

port 7001 #端口
cluster-enabled yes #启用集群模式
cluster-config-file nodes.conf
cluster-node-timeout 5000 #超时时间
appendonly yes
daemonize yes #后台运行
protected-mode no #非保护模式
pidfile /var/run/redis_7001.pid
bind 172.20.10.7 #127.0.0.1改为本机ip地址,可用ifconfig查看ip

其中port 和pidfile 需要隨著資料夾的不同調增。

建立剩餘5個實例:

[root@master redis-cluster]# cp -r redis01/ redis02
[root@master redis-cluster]# cp -r redis01/ redis03
[root@master redis-cluster]# cp -r redis01/ redis04
[root@master redis-cluster]# cp -r redis01/ redis05
[root@master redis-cluster]# cp -r redis01/ redis06

分別修改redis02 ~ redis06 的redis.conf下的port 和pidfile

##4、啟動節點

分別進入redis01、redis02、...redis06目錄,執行: ./redis-server ./redis.conf

建立一個批次文件,同時啟動六個redis

vim startall.sh

新增以下內容:

cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
cd ..

然後執行

chmod u x start-all.sh

start -all.sh變成可執行檔在目前目錄下啟動: 

./startall.sh

檢視:

ps aux|grep redis

<img src="https://img.php.cn/upload/article/000/887/227/168559062896660.jpg" alt="Centos7下redis5集群搭建與使用的方法">

5、啟動叢集

因為我們使用的5.0.0的版本的redis搭建的叢集只要把編譯後的redis目錄中的這個redis-cli檔拷貝到redis-cluster目錄過來即可。 (redis版本5.0以後都是用c語言直接啟動)

/usr/local/redis-cluster/redis-cli --cluster create 172.20.10.7:7001 172.20.10.7:7002 10.7:7001 172.20.10.7:7002 172.20. 10.7:7003 172.20.10.7:7004 172.20.10.7:7005 172.20.10.7:7006 --cluster-replicas 1

啟動後,可看到成功訊息,如下:#rrrr #輸入yes回車
>>> performing hash slots allocation on 6 nodes...
master[0] -> slots 0 - 5460
master[1] -> slots 5461 - 10922
master[2] -> slots 10923 - 16383
adding replica 172.20.10.7:7004 to 172.20.10.7:7001
adding replica 172.20.10.7:7005 to 172.20.10.7:7002
adding replica 172.20.10.7:7006 to 172.20.10.7:7003
>>> trying to optimize slaves allocation for anti-affinity
[warning] some slaves are in the same host as their master
m: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001
 slots:[0-5460] (5461 slots) master
m: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002
 slots:[5461-10922] (5462 slots) master
m: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003
 slots:[10923-16383] (5461 slots) master
s: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004
 replicates a4128b5e581c3722acd9b093c5f29f5056f680b0
s: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005
 replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26
s: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006
 replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413
can i set the above configuration? (type &#39;yes&#39; to accept):yes

至此,reids5 叢集建置完成。

6、叢集的操作

6.1、關閉叢集

方法一:  redis5 提供了關閉叢集的工具,在以下目錄:

/root/redis-5.0.0/utils/create-cluster

   開啟此檔案修改連接埠為我們自己的,如下所示:

連接埠prot設定為7000,nodes為6,工具會自動累加1 產生7001-7006 六個節點用於操作。

Centos7下redis5集群搭建與使用的方法往下看再修改路徑與新增ip位址,如果不加會預設本機127.0.0.1

修改後,執行下列指令關閉叢集:

Centos7下redis5集群搭建與使用的方法

/root/redis-5.0.0/utils/create-cluster/create-cluster stop

方法二:#create-cluster目錄下編寫腳本檔:vim shutdown.sh

內容如下:

>>> nodes configuration updated
>>> assign a different config epoch to each node
>>> sending cluster meet messages to join the cluster
waiting for the cluster to join
......
>>> performing cluster check (using node 172.20.10.7:7001)
m: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001
 slots:[0-5460] (5461 slots) master
 1 additional replica(s)
m: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002
 slots:[5461-10922] (5462 slots) master
 1 additional replica(s)
s: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004
 slots: (0 slots) slave
 replicates a4128b5e581c3722acd9b093c5f29f5056f680b0
m: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003
 slots:[10923-16383] (5461 slots) master
 1 additional replica(s)
s: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005
 slots: (0 slots) slave
 replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26
s: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006
 slots: (0 slots) slave
 replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413
[ok] all nodes agree about slots configuration.
>>> check for open slots...
>>> check slots coverage...
[ok] all 16384 slots covered.

然後執行

chmod u x shutdown.sh
將shutdown.sh變成可執行檔

#在目前目錄下啟動: ./shutdown.sh

查看:ps aux|grep redis

官方:/usr/local/redis-cluster/redis-cli -a xxx -c - h 192.168.5.100 -p 8001

提示:-a存取服務端密碼,-c表示叢集模式,-h指定ip位址,-p指定連接埠號碼

6.2、重新啟動叢集

/root/redis-5.0.0/utils/create-cluster/create-cluster start

6.3、使用腳本檔案啟動叢集

vim startall.sh 追加以下內容:(記得改自己ip位址)

/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7001 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7002 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7003 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7004 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7005 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7006 shutdown

 啟動:./startall.sh

# 7.測試群集

redis-cluster目錄下執行

redis01/redis-cli -h 192.168.25.153 -p 7002 -c

#其中-c表示以叢集方式連接redis,-h指定ip位址,-p指定連接埠號碼

cluster nodes 查詢叢集結點資訊

cluster info 查詢叢集狀態資訊

#

以上是Centos7下redis5集群搭建與使用的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除