搭建redis集群,IP,端口规划如表:
主机名 | IP地址 | 端口号 |
---|---|---|
client | 192.168.4.50 | 6379 |
redisA | 192.168.4.51 | 6351 |
redisB | 192.168.4.52 | 6352 |
redisC | 192.168.4.53 | 6353 |
redisD | 192.168.4.54 | 6354 |
redisE | 192.168.4.55 | 6355 |
redisF | 192.168.4.56 | 6356 |
redisH | 192.168.4.57 | 6357 |
redisI | 192.168.4.58 | 6358 |
配置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集群节点服务器
[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
[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
[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
[root@ip51 ~]# redis-trib.rb info 192.168.4.52:6352
[root@ip51 ~]# redis-trib.rb check 192.168.4.52:6352
[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服务器中。然后自动同步到对应的从服务器中
[root@ip53 ~]# /etc/init.d/redis_6379 stop
[root@ip51 ~]# redis-trib.rb check 192.168.4.52:6352
给54新增加数据
发现从库54升级为主库
[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)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
[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)把删除的节点再添加到集群里
/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
[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
[root@ip51 ~]# redis-trib.rb add-node --slave 192.168.4.57:6357 192.168.4.51:6351