步驟
步驟一:部署新redis服務(wù)器 ip為192.168.4.58
1)裝包角寸,初始化租漂,啟用集群功能窃肠,重啟服務(wù)
]#yum -y? install gcc
]#tar -zxvf redis-4.0.8.tar.gz
]#cd redis-4.0.8/
]#make
]#make install
]#./utils/install_server.sh
]# /etc/init.d/redis_6379? stop
vim /etc/redis/6379.conf
? ? ? ? bind 192.168.4.58
? ? ? ? port 6358
? ? ? ? cluster-enabled? yes? //啟用集群
? ? ? ? cluster-config-file? nodes-6379.conf? ? ? //存儲集群信息文件
? ? ? ? cluster-node-timeout? 5000
:wq
]# vim +43 /etc/init.d/redis_6379
? ? ? ? $CLIEXEC -h 192.168.4.58 -p 6358 shutdown
:wq
]# /etc/init.d/redis_6379? start
]# netstat -utnlp? | grep redis-server
tcp? 0? 0 192.168.4.58:6358? 0.0.0.0:*? LISTEN? ? ? 21201/redis-server?
tcp? 0? 0 192.168.4.58:16358? 0.0.0.0:*? LISTEN? 21201/redis-server?
步驟二:添加master角色主機到集群里
1)在管理主機,添加master角色主機
[root@mgm57 ~]# redis-trib.rb? add-node? 192.168.4.58:6358? 192.168.4.53:6353? //執(zhí)行添加命令
>>> Adding node 192.168.4.58:6358 to cluster 192.168.4.53:6353
...
[OK] New node added correctly. //提示添加完成
2) 在管理主機灌具,查看集群新消息
[root@mgm57 ~]# redis-trib.rb info 192.168.4.53:6353? //查看集群信息
...
192.168.4.58:6358 (4fe1fa46...) -> 0 keys | 0 slots | 0 slaves. //主服務(wù)器58
...
3)在管理主機茂卦,檢測集群
[root@mgm57 ~]# redis-trib.rb check 192.168.4.53:6353? ? //檢測集群
>>> Performing Cluster Check (using node 192.168.4.53:6353)
...
M: 4fe1fa467ad237802021f5aac5f1d5b3e0db47ef 192.168.4.58:6358
? slots: (0 slots) master? //master服務(wù)器58 何什,沒有hash槽
? 0 additional replica(s)
S: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6351
...
[OK] All 16384 slots covered.
4)在管理主機,重新分配hash槽
[root@mgm57 ~]# redis-trib.rb? reshard? 192.168.4.53:6353?
How many slots do you want to move (from 1 to 16384)?4096? //拿出4096個hash 槽給主機192.168.4.58
What is the receiving node ID?? c5e0da48f335c46a2ec199faa99b830f537dd8a0? //主機192.168.4.58的id值
Source node #1:all? ? ? //從當前所有master服務(wù)器獲取hash槽
Do you want to proceed with the proposed reshard plan (yes/no)?yes //同意以上配置
...
Moving slot 12283 from 192.168.4.53:6353 to 192.168.4.58:6358:
Moving slot 12284 from 192.168.4.53:6353 to 192.168.4.58:6358:
Moving slot 12285 from 192.168.4.53:6353 to 192.168.4.58:6358:
Moving slot 12286 from 192.168.4.53:6353 to 192.168.4.58:6358:
Moving slot 12287 from 192.168.4.53:6353 to 192.168.4.58:6358:
5)在管理主機等龙,查看集群信息
[root@mgm57 ~]# redis-trib.rb info 192.168.4.53:6353
192.168.4.53:6353 (9e44139c...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.52:6352 (324e05df...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.58:6358 (4fe1fa46...) -> 4 keys | 4096 slots | 0 slaves. //hash槽4096個
192.168.4.55:6355 (2d343a9d...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 8 keys in 4 masters.
0.00 keys per slot on average.
步驟三:添加slave角色主機到集群里
1)部署新的redis服務(wù)器 192.168.4.59
]#yum -y? install gcc
]#tar -zxvf redis-4.0.8.tar.gz
]#cd redis-4.0.8/
]#make
]#make install
]#./utils/install_server.sh
]# /etc/init.d/redis_6379? stop
vim /etc/redis/6379.conf
? ? ? ? bind 192.168.4.59
? ? ? ? port 6359
? ? ? ? cluster-enabled? yes? ? ? ? ? ? ? ? ? ? ? ? //啟用集群
? ? ? ? cluster-config-file? nodes-6379.conf? ? ? //存儲集群信息文件
? ? ? ? cluster-node-timeout? 5000
:wq
]# vim +43 /etc/init.d/redis_6379
? ? ? ? $CLIEXEC -h 192.168.4.59 -p 6359 shutdown
:wq
]# /etc/init.d/redis_6379? start
]# netstat -utnlp? | grep redis-server
tcp? 0? 0 192.168.4.59:6359? 0.0.0.0:*? LISTEN? ? ? 21201/redis-server?
tcp? 0? 0 192.168.4.59:16359? 0.0.0.0:*? LISTEN? 21201/redis-server?
2)在管理主機处渣,添加slave角色主機
[root@mgm57 ~]# redis-trib.rb add-node? --slave 192.168.4.59:6359? 192.168.4.51:6351? //執(zhí)行添加命令
>>> Adding node 192.168.4.59:6359 to cluster 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
...
>>> Configure node as replica of 192.168.4.58:6358. //提示添加完成
[OK] New node added correctly.
3) 在管理主機,查看集群新消息
[root@mgm57 ~]# redis-trib.rb? info? 192.168.4.51:6351 //查看信息
192.168.4.55:6355 (2d343a9d...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.53:6353 (9e44139c...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.52:6352 (324e05df...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.58:6358 (4fe1fa46...) -> 5 keys | 4096 slots | 1 slaves. //有1個從服務(wù)器
[OK] 13 keys in 4 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#
4)在管理主機而咆,檢測集群
[root@mgm57 ~]# redis-trib.rb check 192.168.4.53:6353? ? //檢測集群
[root@mgm57 ~]# redis-trib.rb? check? 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
...
S: 7f3fa4f20c8c516d5b412ecc22550ed8e7bb8d7a 192.168.4.59:6359 //從服務(wù)器
? slots: (0 slots) slave
? replicates 4fe1fa467ad237802021f5aac5f1d5b3e0db47ef //58主機的id值
...
M: 4fe1fa467ad237802021f5aac5f1d5b3e0db47ef 192.168.4.58:6358 //主服務(wù)器
? slots:0-1364,5461-6826,10923-12287 (4096 slots) master
....
[OK] All 16384 slots covered.
5)在客戶端霍比,訪問從服務(wù)器59,查看數(shù)據(jù)
[root@host50 ~]# redis-cli? -c -h 192.168.4.59 -p 6359
192.168.4.59:6359> keys * //自動同步主服務(wù)器58的數(shù)據(jù)
1) "name"
2) "name2"
3) "age"
4) "y"
5) "shcool5"
192.168.4.59:6359>?