一、Redis群集
- 单节点Redis服务器带来的问题
单点故障,服务不可用
无法处理大量的并发数据请求
数据丢失——大灾难 - 解决办法
搭建Redis群集
1.1 Redis群集介绍
- Redis集群是一 个提供在多个Redis间节点间共享数据的程序集
- Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高
负载的情况下可能会导致不可预料的错误 - Redis集群通过分区来提供一定程度的可用性 在实际环境中当某个节,点宕机或者不可达的情况下可继续处理命令
1.2 Redis群集的优势与实现方法
- Redis群集的优势
自动分割数据到不同的节点上
整个集群的部分节点失败或者不可达的情况下能够继续处理命令 - Redis群集的实现方法
客户端分片
代理分片
服务器端分片
1.3 Redis-Cluster数据分片
- Redis集群没有使用一致性hash, 而是引入了哈希槽概念
- Redis集群有16384个哈希槽
- 每个key通过CRC16校验后对1 6384取模来决定放置槽
- 集群的每个节负责一部分哈希槽
以3个节点组成的集群为例
节点A包含0到5500号哈希槽
节点B包含5501到11000号哈希槽
节点C包含11001到16384号哈希槽
支持添加或者删除节点
添加删除节点无需停止服务 - 例如
如果想新添加个节点D,需要移动节点A, B, C中的部分槽到D上
如果想移除节点A,需要将A中的槽移到B和C节点上,再将没有任何槽的A节点从集群中移除
1.4 Redis-Cluster的主从复制模型
- 集群中具有A,B,C三个节点,如果节点B失败了,整个集群就会因缺少5501-11000这个范围的槽而不可用
为每个节点添加一个从节点A1, B1, C1, 整个集群便有三个master节点和三个slave节点组成,在节点B失败后,集
群便会选举B 1为新的主节点继续服务 - 当B和B1都失败后,集群将不可用
二、Redis群集的搭建
2.1 实验环境
- 六台CentOS 7.6 的虚拟机 模拟 Redis服务六个节点
master1: 192.168.233.100:6379 master2 : 192.168.233.200:6379 master3 : 192.168.233.180:6379
slave1: 192.168.233.30:6379 slave2:192.168.233.127:6379 slave3: 192.168.233.50:6379 - 案例拓补图
2.2 安装Redis
- 六台参与群集架构的都要装
oot@localhost ~]# yum install gcc gcc-c++ -y
准备安装包
redis-5.0.7.tar.gz
[root@localhost ~]# tar zxvf redis-5.0.7.tar.gz -C /opt/
[root@localhost ~]# cd /opt/redis-5.0.7/
[root@localhost ~]# make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis install
[root@localhost redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@localhost redis-5.0.7]# cd /opt/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh
………………一直回车
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
[root@localhost utils]# netstat -lnupt | grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 18495/redis-server
[root@localhost ~]# /etc/init.d/redis_6379 stop ## 停止
Stopping ...
Redis stopped
[root@localhost ~]# /etc/init.d/redis_6379 start ## 开启
Starting Redis server...
[root@localhost ~]# /etc/init.d/redis_6379 restart ## 重启
[root@localhost ~]# /etc/init.d/redis_6379 status ## 查看状态
2.3 Redis 配置文件的修改
- 所有节点都要操作
[root@localhost utils]# iptables -F
[root@localhost utils]# setenforce 0
[root@localhost ~]# vim /etc/redis/6379.conf
[root@localhost utils]# vim /etc/redis/6379.conf
70 #bind 127.0.0.1
89 protected-mode no
93 port 6379
137 daemonize yes
833 cluster-enabled yes
841 cluster-config-file nodes-6379.conf
847 cluster-node-timeout 15000
700 appendonly yes
[root@localhost utils]# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
[root@localhost utils]# ls /var/lib/redis/6379/
appendonly.aof dump.rdb nodes-6379.conf
2.4 Ruby的安装
- 仅在其中一台master上操作
[root@localhost utils]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
将rvm-installer.sh拖入
rvm-installer.sh ## 这是一个安装环境的脚本
[root@localhost ~]# chmod +x rvm-installer.sh
[root@localhost ~]# ./rvm-installer.sh
[root@localhost ~]# source /etc/profile.d/rvm.sh
[root@localhost ~]# rvm list known ## 列出Ruby可安装的版本
[root@localhost ~]# rvm install 2.4.10 ## 安装Ruby 2.4.10 版本
[root@localhost ~]# rvm use 2.4.10 ## 使用Ruby 2.4.10 版本
[root@localhost ~]# ruby -v
ruby 2.4.10p364 (2020-03-31 revision 67879) [x86_64-linux]
[root@localhost ~]# gem install redis ## 再次安装Reids
2.5 Redis群集生成
[root@localhost network-scripts]# redis-cli --cluster create 192.168.233.100:6379 192.168.233.200:6379 192.168.233.180:6379 192.168.233.50:6379 192.168.233.30:6379 192.168.233.127:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.233.30:6379 to 192.168.233.100:6379
Adding replica 192.168.233.127:6379 to 192.168.233.200:6379
Adding replica 192.168.233.50:6379 to 192.168.233.180:6379
M: 4c537e463b1d9e1d30393b15ce58f6f69e474bc4 192.168.233.100:6379
slots:[0-5460] (5461 slots) master
M: 3203ad99bbc69639f11e78bf2d4015149451741c 192.168.233.200:6379
slots:[5461-10922] (5462 slots) master
M: 59b3bc06a0164476e69e501ded3017a692b55fe9 192.168.233.180:6379
slots:[10923-16383] (5461 slots) master
S: edace5223a02e84618a82d678b3e2ffd153a017e 192.168.233.50:6379
replicates 59b3bc06a0164476e69e501ded3017a692b55fe9
S: 5138699efcba932c92592a5525a5cfb5ddbdd111 192.168.233.30:6379
replicates 4c537e463b1d9e1d30393b15ce58f6f69e474bc4
S: 7ed06d93be7be6ed415b0138cbb066b24d98abc9 192.168.233.127:6379
replicates 3203ad99bbc69639f11e78bf2d4015149451741c
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
......
>>> Performing Cluster Check (using node 192.168.233.100:6379)
M: 4c537e463b1d9e1d30393b15ce58f6f69e474bc4 192.168.233.100:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: edace5223a02e84618a82d678b3e2ffd153a017e 192.168.233.50:6379
slots: (0 slots) slave
replicates 59b3bc06a0164476e69e501ded3017a692b55fe9
S: 7ed06d93be7be6ed415b0138cbb066b24d98abc9 192.168.233.127:6379
slots: (0 slots) slave
replicates 3203ad99bbc69639f11e78bf2d4015149451741c
M: 59b3bc06a0164476e69e501ded3017a692b55fe9 192.168.233.180:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 5138699efcba932c92592a5525a5cfb5ddbdd111 192.168.233.30:6379
slots: (0 slots) slave
replicates 4c537e463b1d9e1d30393b15ce58f6f69e474bc4
M: 3203ad99bbc69639f11e78bf2d4015149451741c 192.168.233.200:6379
slots:[5461-10922] (5462 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.
2.6 测试
192.168.233.200:6379> set name zhangsan ## 写入键值对
OK
192.168.233.200:6379> get name
"zhangsan"
192.168.233.200:6379>
- 每台挨着找 用keys * 只有对应的slave能查看到,看哪台有,哪台服务器就是master对应的slave。
- 用get 看数据 哪台都能查找到并跳转
2.7 群集的其他命令
-
群集信息与状态查看
CLUSTER NODES 查看群集集群信息
CLUSTER INFO 查看群集状态 -
集群增加删除节点
CLUSTER MEET 192.168.30.129 7007 集群中增加节点
CLUSTER FORGET 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 # 可以删除其他master和 slave节点,不能删除自己的节点 -
更改节点身份
node-id 查看 ## 用cluster nodes 查看
CLUSTER REPLICATE e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 ## 将登录的这个节点作为node-id为e51ab166bc0f33026887bcf8eba0dff3d5b0bf14的slave
redis-cli -c -h 192.168.30.130 -p 7008 cluster replicate e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 ## 不登陆 在外面将192.168.23.130 作为node-id为e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 的slave -
保存节点配置
CLUSTER SAVECONFIG #将节点配置信息保存到硬盘