溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

redis組建集群的操作步驟

發(fā)布時間:2020-04-24 16:00:18 來源:億速云 閱讀:765 作者:小新 欄目:關(guān)系型數(shù)據(jù)庫

這篇文章主要為大家詳細(xì)介紹了redis組建集群的操作步驟,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下。

redis組建集群的操作步驟

簡要說明

2018年十月 Redis 發(fā)布了穩(wěn)定版本的 5.0 版本,推出了各種新特性,其中一點(diǎn)是放棄 Ruby的集群方式,改為 使用 C語言編寫的 redis-cli的方式,是集群的構(gòu)建方式復(fù)雜度大大降低。關(guān)于集群的更新可以在 Redis5 的版本說明中看到,如下:

The cluster manager was ported from Ruby (redis-trib.rb) to C code inside redis-cli. check `redis-cli --cluster help ` for more info.

可以查看Redis官網(wǎng)查看集群搭建方式,連接如下

https://redis.io/topics/cluster-tutorial

以下步驟是在一臺 Linux 服務(wù)器上搭建有6個節(jié)點(diǎn)的 Redis集群。

操作步驟

創(chuàng)建目錄

新建目錄:/root/software/redis

下載源碼并解壓編譯

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

創(chuàng)建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

配置文件的內(nèi)容為:

port 7001  #端口
cluster-enabled yes #啟用集群模式
cluster-config-file nodes.conf
cluster-node-timeout 5000 #超時時間
appendonly yes
daemonize yes #后臺運(yùn)行
protected-mode no #非保護(hù)模式
pidfile  /var/run/redis_7001.pid

其中 port 和 pidfile 需要隨著 文件夾的不同調(diào)增

啟動節(jié)點(diǎn)

/root/software/redis/redis-5.0.0/src/redis-server  /root/software/redis/redis-cluster-conf/7001/redis.conf
/root/software/redis/redis-5.0.0/src/redis-server  /root/software/redis/redis-cluster-conf/7002/redis.conf
/root/software/redis/redis-5.0.0/src/redis-server  /root/software/redis/redis-cluster-conf/7003/redis.conf
/root/software/redis/redis-5.0.0/src/redis-server  /root/software/redis/redis-cluster-conf/7004/redis.conf
/root/software/redis/redis-5.0.0/src/redis-server  /root/software/redis/redis-cluster-conf/7005/redis.conf
/root/software/redis/redis-5.0.0/src/redis-server  /root/software/redis/redis-cluster-conf/7006/redis.conf

啟動集群

/root/software/redis/redis-5.0.0/src/redis-cli --cluster create 192.168.2.40:7001 192.168.2.40:7002 192.168.2.40:7003 192.168.2.40:7004 192.168.2.40:7005 192.168.2.40:7006 --cluster-replicas 1

啟動后,可看到成功信息,如下:

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.2.40:7004 to 192.168.2.40:7001
Adding replica 192.168.2.40:7005 to 192.168.2.40:7002
Adding replica 192.168.2.40:7006 to 192.168.2.40:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 191c645200a8b4d267f71e3354c8248dbb533dde 192.168.2.40:7001
   slots:[0-5460] (5461 slots) master
M: 400a08d4e5a534c1b609988105d3e045395fbd12 192.168.2.40:7002
   slots:[5461-10922] (5462 slots) master
M: 684f6aa0fbccda295ce6818a8c01ee7255a7b002 192.168.2.40:7003
   slots:[10923-16383] (5461 slots) master
S: f2701549ae98315b432d73b49d139ee77d5685b4 192.168.2.40:7004
   replicates 684f6aa0fbccda295ce6818a8c01ee7255a7b002
S: 9fdc1e375436767ab815cbddd3df674f3bc2ca99 192.168.2.40:7005
   replicates 191c645200a8b4d267f71e3354c8248dbb533dde
S: e7742888ed85b37cff4a98e861e99bb16e8bae2c 192.168.2.40:7006
   replicates 400a08d4e5a534c1b609988105d3e045395fbd12
Can I set the above configuration? (type 'yes' to accept): yes
>>> 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 192.168.2.40:7001)
M: 191c645200a8b4d267f71e3354c8248dbb533dde 192.168.2.40:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 684f6aa0fbccda295ce6818a8c01ee7255a7b002 192.168.2.40:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 9fdc1e375436767ab815cbddd3df674f3bc2ca99 192.168.2.40:7005
   slots: (0 slots) slave
   replicates 191c645200a8b4d267f71e3354c8248dbb533dde
S: e7742888ed85b37cff4a98e861e99bb16e8bae2c 192.168.2.40:7006
   slots: (0 slots) slave
   replicates 400a08d4e5a534c1b609988105d3e045395fbd12
M: 400a08d4e5a534c1b609988105d3e045395fbd12 192.168.2.40:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: f2701549ae98315b432d73b49d139ee77d5685b4 192.168.2.40:7004
   slots: (0 slots) slave
   replicates 684f6aa0fbccda295ce6818a8c01ee7255a7b002
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

至此,Reids集群搭建完成。

關(guān)于redis組建集群的操作步驟就分享到這里了,希望以上內(nèi)容可以對大家有一定的參考價值,可以學(xué)以致用。如果喜歡本篇文章,不妨把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI