第一步:安裝Redisnode
前面已經安裝過了 不解釋,redis
Reids安裝包裏有個集羣工具,要複製到/usr/local/bin裏去算法
cp redis-3.2.9/src/redis-trib.rb /usr/local/bin安全
第二步:修改配置,建立節點ruby
咱們如今要搞六個節點,三主三從,app
端口規定分別是7001,7002,7003,7004,7005,7006工具
咱們先在root目錄下新建一個redis_cluster目錄,而後該目錄下再建立6個目錄,測試
分別是7001,7002,7003,7004,7005,7006,用來存在redis配置文件;ui
這裏咱們要使用redis集羣,要先修改redis的配置文件redis.confspa
mkdir redis_cluster 新建目錄
[root@localhost ~]# cd redis_cluster/
[root@localhost redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006
[root@localhost 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
[root@localhost redis_cluster]#
先複製一份配置文件到7001目錄下
[root@localhost redis_cluster]# cd
[root@localhost ~]# 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 而後對應的再修改下配置便可;
[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進程
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
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 127.0.0.1:7006
建立集羣
[root@localhost ~]# 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`指定端口,就能夠鏈接到集羣。
鏈接任意一個節點端口:
[root@localhost ~]# /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
[root@localhost ~]# /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 這個主節點
[root@localhost ~]# 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
[root@localhost ~]# kill -9 9512
[root@localhost ~]# 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
[root@localhost ~]#
而後再來看下集羣的狀況
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也幹掉,試試看
[root@localhost ~]# kill -9 9524
[root@localhost ~]# 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
[root@localhost ~]#
查看下集羣狀況
redis-trib.rb check 127.0.0.1:7001
這裏咱們發現 出事了,由於主從節點都掛了 因此有一部分哈希槽沒得分配,最後一句
[ERR] Not all 16384 slots are covered by nodes. 沒有安全覆蓋;
因此不能正常使用集羣;