1. 程式人生 > >redis單機多節點叢集搭建

redis單機多節點叢集搭建

注意:

        在叢集過程前一定要把字尾為aof,rdb的redis資料儲存檔案

刪除(或者備份到其他資料夾,只要不是/root/下就可以),

則會叢集失敗。

第一步:安裝Redis(redis安裝

Reids安裝包裡有個叢集工具,要複製到/usr/local/bin裡去

cp redis-3.2.9/src/redis-trib.rb /usr/local/bin

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

我們現在要搞六個節點,三主三從,

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

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

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

這裡我們要使用redis叢集,要先修改redis的配置檔案redis.conf

mkdir redis_cluster 新建目錄

[[email protected] ~]# cd redis_cluster/

[[email protected] redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006

[[email protected] redis_cluster]# ll

總用量 0

drwxr-xr-x. 2 root root 6 7月  27 17:18 7001

drwxr-xr-x. 2 root root 6 7月  27 17:18 7002

drwxr-xr-x. 2 root root 6 7月  27 17:18 7003

drwxr-xr-x. 2 root root 6 7月  27 17:18 7004

drwxr-xr-x. 2 root root 6 7月  27 17:18 7005

drwxr-xr-x. 2 root root 6 7月  27 17:18 7006

[[email protected] redis_cluster]# 

先複製一份配置檔案到7001目錄下

[[email protected] redis_cluster]# cd

[[email protected] ~]# cp redis-3.2.9/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,將寫操作記錄儲存到日誌中

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

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7004/

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7005/

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7006/

[[email protected] ~]# vi redis_cluster/7002/redis.conf 

[[email protected] ~]# vi redis_cluster/7003/redis.conf 

[[email protected] ~]# vi redis_cluster/7004/redis.conf 

[[email protected] ~]# vi redis_cluster/7005/redis.conf 

[[email protected] ~]# vi redis_cluster/7006/redis.conf 

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

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

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 

啟動六個節點

[[email protected] ~]# ps -ef | grep redis  

查詢下redis程序

root       9501      1  0 17:38 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]

root       9512      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7002 [cluster]

root       9516      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]

root       9520      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]

root       9524      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]

root       9528      1  0 17:45 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]

說明都啟動成功了

第四步:建立叢集

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

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

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

在此處可能會出現安裝錯誤   redis requires Ruby version >= 2.2.2問題

解決方案

gem install redis

建立叢集

[[email protected] ~]# 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 127.0.0.1:7006

>>> 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.

顯示配置雜湊槽,以及叢集建立成功,可以用了;

第五步:叢集資料測試

我們先連線任意一個節點,然後新增一個key:

redis-cli是redis預設的客戶端工具,啟動時加上`-c`引數,`-p`指定埠,就可以連線到叢集。 

連線任意一個節點埠:

[[email protected] ~]# /usr/local/redis/bin/redis-cli -c -p 7002

127.0.0.1:7002> 

我們連線7002

127.0.0.1:7002> set xxx  'fdafda'

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

OK

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

Redirected to slot [4038] located at 127.0.0.1:7001

我們從其他叢集節點 ,都可以獲取到資料

127.0.0.1:7001> exit

[[email protected] ~]# /usr/local/redis/bin/redis-cli -c -p 7005

127.0.0.1:7005> get xxx

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

"fdafda"

127.0.0.1:7001> 

第六步:叢集宕機測試

假如我們幹掉一個節點,比如7002 這個主節點

[[email protected] ~]#  ps -ef | grep redis

root       9501      1  0 17:38 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]

root       9512      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7002 [cluster]

root       9516      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]

root       9520      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]

root       9524      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]

root       9528      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]

root       9601   2186  0 18:12 pts/0    00:00:00 grep --color=auto redis

[[email protected] ~]# kill -9 9512

[[email protected] ~]#  ps -ef | grep redis

root       9501      1  0 17:38 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]

root       9516      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]

root       9520      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]

root       9524      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]

root       9528      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]

root       9603   2186  0 18:12 pts/0    00:00:00 grep --color=auto redis

[[email protected] ~]# 

然後再來看下叢集的情況

redis-trib.rb check 127.0.0.1:7001

>>> 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: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005

   slots:5461-10922 (5462 slots) master

   0 additional replica(s)

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.

我們發現 7005本來是從節點,由於他對應的主節點掛了,就自動變成主節點master,所有會有最後一個說明

 All 16384 slots covered. 所有雜湊槽都可覆蓋了; 叢集可以正常使用;

假如我們把7005也幹掉,試試看

[[email protected] ~]# kill -9 9524

[[email protected] ~]#  ps -ef | grep redis

root       9501      1  0 17:38 ?        00:00:03 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]

root       9516      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]

root       9520      1  0 17:45 ?        00:00:03 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]

root       9528      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]

root       9610   2186  0 18:16 pts/0    00:00:00 grep --color=auto redis

[[email protected] ~]# 

檢視下叢集情況

redis-trib.rb check 127.0.0.1:7001

QQ鎴浘20170727181721.jpg

這裡我們發現 出事了,因為主從節點都掛了 所以有一部分雜湊槽沒得分配,最後一句

[ERR] Not all 16384 slots are covered by nodes.  沒有安全覆蓋;

所以不能正常使用叢集;