步骤
步骤一:部署新redis服务器 ip为192.168.4.58
1)装包,初始化,启用集群功能,重启服务
]#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 //执行添加命令
>>> 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. //主服务器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服务器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服务器获取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服务器 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 //执行添加命令
>>> 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个从服务器
[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 //从服务器
slots: (0 slots) slave
replicates 4fe1fa467ad237802021f5aac5f1d5b3e0db47ef //58主机的id值
...
M: 4fe1fa467ad237802021f5aac5f1d5b3e0db47ef 192.168.4.58:6358 //主服务器
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
....
[OK] All 16384 slots covered.
5)在客户端,访问从服务器59,查看数据
[root@host50 ~]# redis-cli -c -h 192.168.4.59 -p 6359
192.168.4.59:6359> keys * //自动同步主服务器58的数据
1) "name"
2) "name2"
3) "age"
4) "y"
5) "shcool5"
192.168.4.59:6359>