部署新redis服务器 添加master角色主机到集群里 添加slave角色主机到集群里

2.1 问题

部署新redis服务器
添加master角色主机到集群里
添加slave角色主机到集群里

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署新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
>>> Performing Cluster Check (using node 192.168.4.53:6353)
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6353
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6354
   slots: (0 slots) slave
   replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6352
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6356
   slots: (0 slots) slave
   replicates 324e05df3f143ef97e50d09be0328a695e655986
S: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6351
   slots: (0 slots) slave
   replicates 2d343a9df48f6f6e207949e980ef498466a44dad
M: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.55:6355
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.4.58:6358 to make it join the cluster.
[OK] New node added correctly. //提示添加完成
[root@mgm57 ~]#

2) 在管理主机,查看集群新消息

[root@mgm57 ~]# redis-trib.rb info 192.168.4.53:6353  //查看集群信息
192.168.4.53:6353 (9e44139c...) -> 3 keys | 5461 slots | 1 slaves.
192.168.4.52:6352 (324e05df...) -> 2 keys | 5462 slots | 1 slaves.
192.168.4.58:6358 (4fe1fa46...) -> 0 keys | 0 slots | 0 slaves. //主服务器58
192.168.4.55:6355 (2d343a9d...) -> 3 keys | 5461 slots | 1 slaves.
[OK] 8 keys in 4 masters.
0.00 keys per slot on average.
[root@mgm57 ~]# 

3)在管理主机,检测集群

 [root@mgm57 ~]# redis-trib.rb check 192.168.4.53:6353    //检测集群
>>> Performing Cluster Check (using node 192.168.4.53:6353)
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6353
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6354
   slots: (0 slots) slave
   replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6352
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6356
   slots: (0 slots) slave
   replicates 324e05df3f143ef97e50d09be0328a695e655986
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
   slots: (0 slots) slave
   replicates 2d343a9df48f6f6e207949e980ef498466a44dad
M: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.55:6355
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@mgm57 ~]#

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.
[root@mgm57 ~]#  

步骤三:添加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)
S: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6351
   slots: (0 slots) slave
   replicates 2d343a9df48f6f6e207949e980ef498466a44dad
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6356
   slots: (0 slots) slave
   replicates 324e05df3f143ef97e50d09be0328a695e655986
M: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.55:6355
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6353
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6354
   slots: (0 slots) slave
   replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6352
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 4fe1fa467ad237802021f5aac5f1d5b3e0db47ef 192.168.4.58:6358
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Automatically selected master 192.168.4.58:6358
>>> Send CLUSTER MEET to node 192.168.4.59:6359 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.4.58:6358. //提示添加完成
[OK] New node added correctly.
[root@mgm57 ~]# 

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: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6351
   slots: (0 slots) slave
   replicates 2d343a9df48f6f6e207949e980ef498466a44dad
S: 7f3fa4f20c8c516d5b412ecc22550ed8e7bb8d7a 192.168.4.59:6359 //从服务器
   slots: (0 slots) slave
   replicates 4fe1fa467ad237802021f5aac5f1d5b3e0db47ef //58主机的id值
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6356
   slots: (0 slots) slave
   replicates 324e05df3f143ef97e50d09be0328a695e655986
M: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.55:6355
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6353
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6354
   slots: (0 slots) slave
   replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6352
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 4fe1fa467ad237802021f5aac5f1d5b3e0db47ef 192.168.4.58:6358 //主服务器
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@mgm57 ~]# 
[root@mgm57 ~]#

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>  
发布了324 篇原创文章 · 获赞 12 · 访问量 8209

猜你喜欢

转载自blog.csdn.net/weixin_45843450/article/details/105520359