Redis集群

一、环境准备

搭建redis集群,IP,端口规划如表:

主机名IP地址端口号
client192.168.4.506379
redisA192.168.4.516351
redisB192.168.4.526352
redisC192.168.4.536353
redisD192.168.4.546354
redisE192.168.4.556355
redisF192.168.4.566356
redisH192.168.4.576357
redisI192.168.4.586358

配置192.168.4.51~192.168.4.58
redis的ip跟端口为表中所列。

以51主机为例

[root@ip51 ~]# /etc/init.d/redis_6379 stop
[root@ip51 ~]# sed -i 's/^port 6379/port 6351/' /etc/redis/6379.conf
[root@ip51 ~]# sed -i 's/^bind 127.0.0.1/bind 192.168.4.51/' /etc/redis/6379.conf
[root@ip51 ~]# /etc/init.d/redis_6379 start
[root@ip51 ~]# ss -ntulp | grep redis-server

所有主机开启集群配置,以51为例

[root@ip51 ~]# sed -i 's#\# cluster-enabled yes#cluster-enabled yes#' /etc/redis/6379.conf    //启用集群
[root@ip51 ~]# sed -i 's#\# cluster-node-timeout 15000#cluster-node-timeout 5000#' /etc/redis/6379.conf     //请求超时5秒
[root@ip51 ~]# sed -i 's#\# cluster-config-file nodes-6379.conf#cluster-config-file nodes-6351.conf#' /etc/redis/6379.conf   //指定集群信息文件
[root@ip51 ~]# /etc/init.d/redis_6379 stop
[root@ip51 ~]# /etc/init.d/redis_6379 start
[root@ip51 ~]# ss -ntulp | grep redis-server   //集群通信端口,默认为服务端口+10000

51~58每台集群服务器清除目录下的所有数据,以51为例

[root@ip51 ~]# /etc/init.d/redis_6379 stop
[root@ip51 ~]# rm -rf /var/lib/redis/6379/*
[root@ip51 ~]# /etc/init.d/redis_6379 start
[root@ip51 ~]# ss -ntulp | grep redis-server
二、创建集群

(1)查看集群信息

[root@ip51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> cluster info
192.168.4.51:6351> cluster nodes

(2)将51设置为管理服务器跟redis集群节点服务器

  1. 装包
[root@ip51 ~]# yum -y install ruby rubygems
[root@ip51 redis-cluster]# rpm -ivh --nodeps ruby-devel-2.0.0.648-30.el7.x86_64.rpm 
[root@ip51 redis-cluster]# gem install redis-3.2.1.gem
[root@ip51 ~]# cp /root/redis/redis-4.0.8/src/redis-trib.rb /root/bin/
[root@ip51 ~]# redis-trib.rb help
  1. 创建集群
    – replicas 1 主动为每一个master节点分配一个slave节点(默认需要三台master主机),如果需要分配两个节点,则需要9台主机,以此类推
[root@ip51 ~]# redis-trib.rb create --replicas 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
  1. 在任意一台主机上登录,查看集群
[root@ip51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> cluster info
192.168.4.51:6351> cluster nodes
  1. 在管理主机上用脚本查看任意一台主机的信息
[root@ip51 ~]# redis-trib.rb info 192.168.4.52:6352
[root@ip51 ~]# redis-trib.rb check 192.168.4.52:6352
  1. 客户端连接任意一台redis服务器(51~56),测试集群
[root@ip50 ~]# redis-cli -c -h 192.168.4.55 -p 6355
192.168.4.55:6355> set Z  777
-> Redirected to slot [15295] located at 192.168
192.168.4.53:6353> get Z
"777"
192.168.4.51:6351> set A 456
-> Redirected to slot [6373] located at 192.168.4.52:6352
192.168.4.53:6353> get A
-> Redirected to slot [6373] located at 192.168.4.52:6352
"456"

数据会存到master服务器中。然后自动同步到对应的从服务器中

  1. 停掉53主库,查看集群信息
[root@ip53 ~]# /etc/init.d/redis_6379 stop
[root@ip51 ~]# redis-trib.rb check 192.168.4.52:6352

给54新增加数据
发现从库54升级为主库

  1. 开启53,查看集群信息
[root@ip53 ~]# /etc/init.d/redis_6379 stop
[root@ip51 ~]# redis-trib.rb check 192.168.4.52:6352

进去53查看本地数据
发现53自动变为54的从库,并且同步了53宕机期间,54新增加的数据

  1. 存储数据到某个主机上的原理
    总的槽位(slot)是0~16383,总共16384个,平均分配给每个主机
    存储数据的变量名 跟crc16 进行hash计算,将结果与16384取余,得出来的结果就是槽位数,对应哪个主库范围,则这个值就存储在哪个主机上
三、添加新的集群主机

(1)57主机启用集群服务

[root@ip57 ~]# /etc/init.d/redis_6379 stop
[root@ip57 ~]# vim /etc/redis/6379.conf  //启用集群服务
   ... ...
   cluster-enabled yes
   cluster-config-file nodes-6379.conf
   cluster-node-timeout 5000
   ... ...
[root@ip57 ~]# rm -rf /var/lib/redis/6379/*
[root@ip57 ~]# /etc/init.d/redis_6379 start

(2)在管理主机51上添加master服务器,后面写当前集群的任意一个服务器的ip跟端口都行

[root@ip51 ~]# redis-trib.rb add-node 192.168.4.57:6357 192.168.4.51:6351

(3)集群重新分片,给57分配哈希槽

[root@ip51 ~]# redis-trib.rb reshard 192.168.4.51:6351
How many slots do you want to move (from 1 to 16384)? 4096  //这里填的是16384/master的个数
What is the receiving node ID? 3350c60f74ed3427d51e9704645169408d16ad7a  //这里填需要分配哈希槽的主机的ID
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:all  //选择all

(4)给57master主机添加slave主机

58主机需要开集群配置

redis-trib.rb add-node --slave [–master-id id值] ip地址:端口 192.168.4.51:6351 //如果不指定master-id的话,会把新节点随机添加为从节点最少的主的从

[root@ip51 ~]# redis-trib.rb add-node --slave --master-id 3350c60f74ed3427d51e9704645169408d16ad7a 192.168.4.58:6358 192.168.4.51:6351

(5)移除slave主机

[root@ip51 ~]# redis-trib.rb del-node 192.168.4.51:6351 8e3d97e2816a96a04611a966dddb8257a0c3602e   //后面跟删除的节点id

(6)移除master主机

1. 重新分片释放占用的hash槽
[root@ip51 ~]# redis-trib.rb reshard 192.168.4.51:6351
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096  //移除多少个hash槽
What is the receiving node ID? 1cc7afa851d51633e8dfa040697938ea60d92629   //哪个主机ID对应的主机接收这些槽位
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:caf675ab751cf34a6c7888cffb668f99a74a62e9   //从哪些主机ID上开始移除
Source node #2:done
[root@ip51 ~]# redis-trib.rb check 192.168.4.52:6352
  1. 删除主机节点
[root@ip51 ~]# redis-trib.rb del-node 192.168.4.51:6351 caf675ab751cf34a6c7888cffb668f99a74a62e9
[root@ip51 ~]# redis-trib.rb check 192.168.4.52:6352

集群不能用的情况:
有半数或者半数以上的主库机器挂掉,集群就不能用了
把一个从库升级成主,没有从库,集群不能用(前提是:有半数或者半数以上的主库机器挂掉)
一个主库挂掉,它的从库自动顶替为主库,正常使用(前提是:有半数或者半数以上的主库机器能用),挂掉的主库修复好后,会成为从库,不会抢占为主

(7)把删除的节点再添加到集群里

  1. 在删除的节点上启动redis服务
    57、58上执行
/etc/init.d/redis_6379 stop
rm -rf /var/lib/redis/6379/*
/etc/init.d/redis_6379 start

或者登录对应集群
> cluster reset 重置集群信息
> flushall //删除所有数据
> save //马上存盘
2. 将58添加到集群做为主机

[root@ip51 ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.51:6351
  1. 分配hash槽
[root@ip51 ~]# redis-trib.rb check 192.168.4.51:6351
[root@ip51 ~]# redis-trib.rb reshard 192.168.4.51:6351
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096   //分配的hash槽个数
What is the receiving node ID? 2aa15b6111713a33d5dff47938c03d384fe1b670   //接收的主机id(58)
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:1cc7afa851d51633e8dfa040697938ea60d92629  //从哪个主机上拿(51)
Source node #2:done
  1. 给58主机添加slave主机
[root@ip51 ~]# redis-trib.rb add-node --slave 192.168.4.57:6357 192.168.4.51:6351
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值