创建集群 、 管理集群

一、创建集群

1.1 实验环境

在这里插入图片描述

1.2 工作原理

变量存取时:变量通过hash算法得到的数字除以16384取余,余数对应着槽位,就放在哪个服务器的槽位范围(取时也一样)
从服务器是不占hash槽位

1.3 部署管理器

[root@ceph3 ~]# yum -y install rubygems ruby  //由于运行的的脚本是由ruby语言写的,需安装对应的包
[root@ceph3 ~]# gem install redis-3.2.1.gem
[root@ceph3 ~]# mkdir bin   //创建目录检索目录
[root@ceph3 ~]# tar -xvf redis-4.0.8.tar.gz
[root@ceph3 ~]# cp redis-4.0.8/src/redis-trib.rb /root/bin/   //创建管理集群脚本
[root@ceph3 ~]# chmod +x /root/bin/redis-trib.rb
[root@ceph3 ~]# redis-trib.r help    //查看命令帮助

在这里插入图片描述
所有redis服务器都需要先开启集群功能`

[root@proxy ~]# vim /etc/redis/6379.conf  
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000

[root@proxy2 ~]# /etc/init.d/redis_6379   restart   //重启设备

[root@proxy redis-4.0.8]# netstat -untlp  | grep redis-se    //重启后应该要有俩个端口
tcp        0      0 192.168.4.51:16351      0.0.0.0:*               LISTEN      21665/redis-server  
tcp        0      0 192.168.4.51:6351       0.0.0.0:*               LISTEN      21665/redis-server  

管理服务器上需进行如下配置

[root@ceph3 ~]# redis-trib.rb create --replicas 1 \   //1代表定义从服务器的个数
> 192.168.4.51:6351 192.168.4.52:6352 \
> 192.168.4.53:6353 192.168.4.54:6354 \
> 192.168.4.55:6355 192.168.4.56:6356

........
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.     //集群创建完成提示

[root@ceph3 ~]# redis-trib.rb info 192.168.4.51:6351    //查看集群信息
[root@ceph3 ~]# redis-trib.rb check 192.168.4.51:6351   //检查集群详细信息

客户端查看

[root@proxy ~]# redis-cli -h 192.168.4.51 -p 6351  
192.168.4.51:6351> cluster info   //查看集群信息 
192.168.4.51:6351> CLUSTER NODES  //查看集群节点信息

如上信息可以看出, 集群是三主三从,属于分布式存储,分为三组,当一组里面的主从都宕机了,则整个集群都会挂掉;当没有增加或减少redis服务器时,管理器的存在不会影响到集群
存量是由内存来决定的,当内存存满时,会根据配置文件的内存删除策略,来减少数据量,也就是说数据是由时效性的

二、管理集群

2.1 故障切换

在这里插入图片描述
检测集群

51服务停掉
[root@proxy ~]# redis-cli -h 192.168.4.51 -p 6351 shutdown

57管理器查看信息
[root@ceph3 ~]# redis-trib.rb info 192.168.4.52:6352
[root@ceph3 ~]# redis-trib.rb check 192.168.4.52:6352

2.2 添加master服务器

在这里插入图片描述
在这里插入图片描述

添加58设备
[root@ceph3 ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.52:6352
..........
K] New node added correctly.   //提示添加成功

重新分片,移除其他服务器的hash槽位个数,接受hash槽位主机ID,同意配置

[root@ceph3 ~]# redis-trib.rb reshard  192.168.4.52:6352   //重新分片
How many slots do you want to move (from 1 to 16384)? 4099
What is the receiving node ID?
输入设备ID
.........
Source node #1:all    //从所有主服务器移除slots
.........

[root@ceph3 ~]# redis-trib.rb info 192.168.4.52:6352   //查看集群信息

2.3 添加slave服务器

添加59设备
[root@ceph3 ~]# redis-trib.rb add-node --slave --master-id  c17d117951d60603f94b3a32ebb8857301ef3af1  192.168.4.59:6359 192.168.4.52:6352
//如果不指明--master-id ,则新节点随机添加为从节点最少的主库

2.4 移除服务器

在这里插入图片描述

移除59设备
[root@ceph3 ~]# redis-trib.rb del-node 192.168.4.59:6359 \
> 89797a85e9647d7c4fe83779b56286a26a197195
>>> Removing node 89797a85e9647d7c4fe83779b56286a26a197195 from cluster 192.168.4.59:6359
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node     //会停止移除主机的redis服务

在这里插入图片描述

释放53设备槽位
[root@ceph3 ~]# redis-trib.rb reshard 192.168.4.53:6353
..................
How many slots do you want to move (from 1 to 16384)? 4095   //槽位移除的个数
What is the receiving node ID? c17d117951d60603f94b3a32ebb8857301ef3af1     //选择52接受
*** The specified node is not known or not a master, please retry.
What is the receiving node ID? c17d117951d60603f94b3a32ebb8857301ef3af1
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:8f717d488b4d02a42c72a78b1a4714399c00fd10   //指定移除槽位设备为53
Source node #2:done
.................

移除53主机
[root@ceph3 ~]# redis-trib.rb del-node 192.168.4.53:6353 \
>  8f717d488b4d02a42c72a78b1a4714399c00fd10   //53主机名

PS:当移除主机后,想要再添加回来,要么删除该主机下的数据库目录集群信息文件,然后重启服务;要么重置集群信息(进入redis-cli ,cluster reset)

发布了25 篇原创文章 · 获赞 2 · 访问量 574

猜你喜欢

转载自blog.csdn.net/f5500/article/details/104601295