Redis5單機多節點集羣和多機多節點集羣以及出現的問題

第一步:安裝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

第二步:修改配置,創建節點
我們現在要搞六個節點,三主三從,

端口規定分別是7001,7002,7003,7004,7005,7006

我們先在root目錄下新建一個redis_cluster目錄,然後該目錄下再創建6個目錄,

分別是7001,7002,7003,7004,7005,7006,用來存在redis配置文件;

這裏我們要使用redis集羣,要先修改redis的配置文件redis.conf

mkdir redis_cluster 新建目錄

[root@localhost ~]# cd redis_cluster/

[root@localhost redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006

先複製一份配置文件到7001目錄下

[root@localhost redis_cluster]# cd

[root@localhost ~]# cp redis-5.0.0/redis.conf redis_cluster/7001/

我們修改下這個配置文件

vi redis_cluster/7001/redis.conf

修改一下幾個

port 7001 //六個節點配置文件分別是7001-7006

daemonize yes //redis後臺運行

pidfile /var/run/redis_7001.pid //pidfile文件對應7001-7006

cluster-enabled yes //開啓集羣

cluster-config-file nodes_7001.conf //保存節點配置,自動創建,自動更新對應7001-7006

cluster-node-timeout 5000 //集羣超時時間,節點超過這個時間沒反應就斷定是宕機

appendonly yes //存儲方式,aof,將寫操作記錄保存到日誌中。
//下面可以不寫
#若設置密碼,master和slave需同時配置下面兩個參數:
masterauth “sfsxxx” #連接master的密碼
requirepass “jadsfx” #自己的密碼 
大體配置例如

port 7001 #端口
cluster-enabled yes #啓用集羣模式
cluster-config-file nodes_7001.conf #集羣的配置 配置文件首次啓動自動生成
cluster-node-timeout 5000 #超時時間 5秒
appendonly yes #aof日誌開啓 它會每次寫操作都記錄一條日誌
daemonize yes #後臺運行
protected-mode no #非保護模式
pidfile  /var/run/redis_7001.pid

7001下的修改完後,我們把7001下的配置分別複製到7002-7006 然後對應的再修改下配置即可;

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7004/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7005/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7006/

[root@localhost ~]# vi redis_cluster/7002/redis.conf 

[root@localhost ~]# vi redis_cluster/7003/redis.conf 

[root@localhost ~]# vi redis_cluster/7004/redis.conf 

[root@localhost ~]# vi redis_cluster/7005/redis.conf 

[root@localhost ~]# vi redis_cluster/7006/redis.conf 

編輯後面5個配置文件,把 port ,pidfile,cluster-config-file 分別修改下即可;

第三步:啓動六個節點的redis

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 

啓動六個節點

[root@localhost ~]# ps -ef | grep redis

查找下redis進程

第四步:創建集羣

redis官方提供了redis-trib.rb工具,第一步裏已經房到裏bin下 ;

但是在使用之前 需要安裝ruby,以及redis和ruby連接

yum -y install ruby ruby-devel rubygems rpm-build
gem install redis

redis-trib.rb create --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 (redis3.0版本用這個)
redis-cli --cluster create 127.0.0.1:7000 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  --cluster-replicas 1(redis5.0版本)

效果如下:

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:7001

127.0.0.1:7002

127.0.0.1:7003

Adding replica 127.0.0.1:7004 to 127.0.0.1:7001

Adding replica 127.0.0.1:7005 to 127.0.0.1:7002

Adding replica 127.0.0.1:7006 to 127.0.0.1:7003

M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002

   slots:5461-10922 (5462 slots) master

M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004

   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c

S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005

   replicates d61e66e49e669b99d801f22f6461172696fdd1c9

S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006

   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18

Can I set the above configuration? (type 'yes' to accept): 

從運行結果看 主節點就是7001 7002 7003 從節點分別是7004 7005 7006

7001分配到的哈希槽是 0-5460

7002分配到的哈希槽是 5461-10922

7003分配到的哈希槽是 10923-16383

最後問我們是否接受上面的設置,輸入yes 就表示接受,我們輸入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 127.0.0.1:7001)

M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006

   slots: (0 slots) slave

   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18

M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005

   slots: (0 slots) slave

   replicates d61e66e49e669b99d801f22f6461172696fdd1c9

M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004

   slots: (0 slots) slave

   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

顯示配置哈希槽,以及集羣創建成功,可以用了;
若出現如下錯誤:
[ERR] Node 127.0.0.1:7001 is not empty. Either the nodealready knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解決辦法如下:
1)、將需要新增的節點下aof、rdb等本地備份文件刪除;

2)、同時將新Node的集羣配置文件刪除,即:刪除你redis.conf裏面cluster-config-file所在的文件;

3)、再次添加新節點如果還是報錯,則登錄新Node,./redis-cli–h x –p對數據庫進行清除:

127.0.0.1:7001> flushdb #清空當前數據庫
若出現[ERR] Not all 16384 slots are covered by nodes.的錯誤
解決辦法如下:
redis-trib.rb fix 127.0.0.1:7001(3.0版本)
redis-trib.rb check 127.0.0.1:7000(3.0版本)
redis-cli --cluster fix 127.0.0.1:7001(5.0版本)
redis-cli --cluster check 127.0.0.1:7001(5.0版本)
只要輸入任意集羣中節點即可,會自動檢查所有相關節點。可以查看相應的輸出看下是否是每個Master都有了slots,如果分佈不均勻那可以使用下面的方式重新分配。

多節點集羣搭建
一、環境搭建
同一局域網下創建兩臺虛擬機或者其他機器在局域網下。

第二步:修改配置,創建節點

首先我們在192.168.0.5虛擬機裏創建三個節點,端口分別是7001,7002,7003

我們先在root目錄下新建一個redis_cluster目錄,然後該目錄下再創建3個目錄,

分別是7001,7002,7003,用來存redis配置文件;

這裏我們要使用redis集羣,要先修改redis的配置文件redis.conf

mkdir redis_cluster 新建目錄

[root@localhost ~]# cd redis_cluster/

[root@localhost redis_cluster]# mkdir 7001 7002 7003

先複製一份配置文件到7001目錄下

[root@localhost redis_cluster]# cd

[root@localhost ~]# cp redis-5.5.0/redis.conf redis_cluster/7001/

我們修改下這個配置文件

vi redis_cluster/7001/redis.conf

修改一下幾個

port 7001 //六個節點配置文件分別是7001-7003

bind 192.168.0.5 //默認ip爲127.0.0.1 需要改爲其他節點機器可訪問的ip 否則創建集羣時無法訪,和單機集羣有區別

daemonize yes //redis後臺運行

pidfile /var/run/redis_7001.pid //pidfile文件對應7001-7003

cluster-enabled yes //開啓集羣

cluster-config-file nodes_7001.conf //保存節點配置,自動創建,自動更新對應7001-7003

cluster-node-timeout 5000 //集羣超時時間,節點超過這個時間沒反應就斷定是宕機

appendonly yes //存儲方式,aof,將寫操作記錄保存到日誌中

7001下的修改完後,我們把7001下的配置分別複製到7002-7003 然後對應的再修改下配置即可;

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/

[root@localhost ~]# vi redis_cluster/7002/redis.conf 

[root@localhost ~]# vi redis_cluster/7003/redis.conf 

[root@localhost ~]# vi redis_cluster/7004/redis.conf 

編輯後面5個配置文件,把 port ,pidfile,cluster-config-file 分別修改下即可;
同理 192.168.0.6機器,也搞一個redis_cluster目錄,然後再新建7004,7005,7006目錄,
第三步:啓動兩臺機器的六個節點
啓動之前先檢查appendonly.aof和dump.rdp以及node-7001-7006的conf文件是否刪除,避免出現問題
192.168.0.5機器

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 

[root@localhost ~]# ps -ef | grep redis  

root       2242      1  0 19:55 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.5:7001 [cluster]

root       2252      1  0 19:59 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.5:7002 [cluster]

root       2256      1  0 19:59 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.5:7003 [cluster]

root       2260   2214  0 19:59 pts/0    00:00:00 grep --color=auto redis

192.168.0.6機器

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 

[root@localhost ~]# ps -ef | grep redis  

root       2347      1  0 20:31 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.6:7004 [cluster]

root       2351      1  0 20:31 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.6:7005 [cluster]

root       2355      1  0 20:31 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.6:7006 [cluster]

root       2363   2270  0 20:32 pts/0    00:00:00 grep --color=auto redis

說明都啓動OK
第四步:設置防火牆,開放集羣端口

兩臺機器的防火牆我們直接關掉

systemctl stop firewalld.service

第五步:創建集羣

192.168.0.5機器作爲集羣控制端

redis官方提供了redis-trib.rb工具,第一步裏已經房到裏bin下 ;

但是在使用之前 需要安裝ruby,以及redis和ruby連接

yum -y install ruby ruby-devel rubygems rpm-build

gem install redis

開始在05機器創建

redis-cli --cluster create  192.168.0.5:7001 192.168.0.5:7002 192.168.0.5:7003 192.168.0.6:7004 192.168.0.6:7005 192.168.0.6:7006 --cluster-replicas 1

在這裏插入圖片描述
從運行結果看 主節點就是7001 7004 7002 從節點分別是7005 7003 7006

7001分配到的哈希槽是 0-5460

7004分配到的哈希槽是 5461-10922

7002分配到的哈希槽是 10923-16383

最後問我們是否接受上面的設置,輸入yes 就表示接受,我們輸入yes
然後顯示:
在這裏插入圖片描述顯示配置哈希槽,以及集羣創建成功,可以用了;

第六步:集羣數據測試

我們先連接任意一個節點,然後添加一個key:

redis-cli是redis默認的客戶端工具,啓動時加上`-c`參數,-p指定端口,就可以連接到集羣。

這裏還得加-h 指定機器IP

連接任意一個節點端口:

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.0.5 -c -p 7002

192.168.0.5:7002>

連接到7002節點

192.168.1.5:7002> set xxx 'fadfa'

-> Redirected to slot [4038] located at 192.168.0.5:7001

OK

前面說過Redis Cluster值分配規則,所以分配key的時候,它會使用CRC16(‘my_name’)%16384算法,來計算,將這個key 放到哪個節點,這裏分配到了4038slot 就分配到了7001(0-5460)這個節點上。所以有:

Redirected to slot [4038] located at 192.168.0.5:7001

我們從其他集羣節點 ,都可以獲取到數據

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.0.6 -c -p 7005

192.168.0.6:7005>

192.168.0.6:7005> get xxx

-> Redirected to slot [4038] located at 192.168.0.5:7001

“fadfa”

192.168.0.5:7001>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章