搭建Redis集群(主从+哨兵),通俗易懂!
使⽤redis作为缓存数据库使⽤⽬的是为了提升数据加载速度、降低对数据库的访问压⼒,我们需要保证redis的可⽤性
- 主从配置
- 哨兵模式
- 集群配置
1.主从配置
主从配置:在多个redis实例建⽴起主从关系,当主redis 中的数据发⽣变化, 从redis中的数据也会同步变化
- 通过主从配置可以实现redis数据的备份( 从redis 就是对 主redis 的备份),保证 数据的安全性
- 通过主从配置可以实现redis的读写分离
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RVeEgH4u-1684317206689)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\image-20230517160938017.png)]
1.1主从配置示例
- 启动三个redis实例
## 在redis-5.0.5⽬录下创建 msconf ⽂件夹
[root@theo redis-5.0.5]# mkdir msconf
## 拷⻉redis.conf⽂件 到 msconf⽂件夹 ---> redis-master.conf
[root@theo redis-5.0.5]# cat redis.conf |grep -v "#" | grep -v "^$"
> msconf/redis-master.conf
## 修改 redis-master.conf 端⼝及远程访问设置
[root@theo msconf]# vim redis-master.conf
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-91AZdMzr-1684317206690)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\image-20230517161048996.png)]
## 将 redis-master.conf 拷⻉两份分别为:redis-slave1.conf redisslave2.conf
[root@theo msconf]# sed 's/6380/6381/g' redis-master.conf > redisslave1.conf
[root@theo msconf]# sed 's/6380/6382/g' redis-master.conf > redisslave2.conf
## 修改redis-slave1.conf redis-slave2.conf,设置“跟从”---127.0.0.1 6380
[root@theo msconf]# vim redis-slave1.conf
[root@theo msconf]# vim redis-slave2.conf
## 启动三个redis实例
[root@theo msconf]# redis-server redis-master.conf &
[root@theo msconf]# redis-server redis-slave1.conf &
[root@theo msconf]# redis-server redis-slave2.conf &
2.哨兵模式
哨兵模式:⽤于监听主库,当确认主库宕机之后,从备库(从库)中选举⼀个转备为主
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yhEAgBco-1684317206691)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\image-20230517161240023.png)]
2.1哨兵模式配置
##⾸先实现三个redis实例之间的主从配置(如上)
## 创建并启动三个哨兵
## 拷⻉sentinel.conf⽂件三份:sentinel-26380.conf sentinel-26382.conf
sentinel-26382.conf
## 创建sentinelconf⽬录
[root@theo redis-5.0.5]# mkdir sentinelconf
## 拷⻉sentinel.conf⽂件到 sentinelconf⽬录:sentinel-26380.conf
[root@theo redis-5.0.5]# cat sentinel.conf | grep -v "#" | grep -v "^$"
> sentinelconf/sentinel-26380.conf
[root@theo redis-5.0.5]# cd sentinelconf/
[root@theo sentinelconf]# ll
total 4
-rw-r--r-- 1 root root 326 May 19 17:09 sentinel-26380.conf
## 编辑 sentinelconf/sentinel-26380.conf⽂件
[root@theo sentinelconf]# vim sentinel-26380.conf
修改配置文件中的内容
port 26380
daemonize no
pidfile "/var/run/redis-sentinel-26380.pid"
logfile ""
dir "/tmp"
sentinel deny-scripts-reconfig yes
# 此处配置默认的主库的ip 和端⼝ 最后的数字是哨兵数量的⼀半多⼀个
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 1
protected-mode no
[root@theo sentinelconf]# sed 's/26380/26381/g' sentinel-26380.conf >
sentinel-26381.conf
[root@theo sentinelconf]# sed 's/26380/26382/g' sentinel-26380.conf >
sentinel-26382.conf
再依次启动三个哨兵:
[root@theo sentinelconf]# redis-sentinel sentinel-26380.conf
3.集群配置
⾼可⽤:保证redis⼀直处于可⽤状态,即时出现了故障也有备⽤⽅案保证可⽤性 ⾼并发:⼀个redis实例已经可以⽀持多达11w并发读操作或者8.1w并发写操作;但是如 果对于有更⾼并发需求的应⽤来说,我们可以通过 读写分离 、 集群配置 来解决⾼并发问题
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fMxA9LZ2-1684317206691)(C:\Users\86177\Desktop\图片管理\image-20230517161606488.png)]
Redis集群
- Redis集群中每个节点是对等的,⽆中⼼结构
- 数据按照slots分布式存储在不同的redis节点上,节点中的数据可共享,可以动态调整数 据的分布
- 可扩展性强,可以动态增删节点,最多可扩展⾄1000+节点
- 集群每个节点通过主备(哨兵模式)可以保证其⾼可⽤性
3.1 集群搭建
3.1.1.创建文件夹
[root@theo ~]# cd /usr/local/redis-5.0.5
[root@theo redis-5.0.5]# mkdir cluster-conf
[root@theo redis-5.0.5]# cat redis.conf | grep -v "#"|grep -v "^$" >
cluster-conf/redis-7001.conf
[root@theo redis-5.0.5]# cd cluster-conf/
[root@theo cluster-conf]# ls
redis-7001.conf
[root@theo cluster-conf]# vim redis-7001.conf
3.1.2修改配置文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-N1acgSGA-1684317206692)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\image-20230517161851176.png)]
拷⻉6个⽂件,端⼝分别为 7001-7006
[root@theo cluster-conf]# sed 's/7001/7002/g' redis-7001.conf > redis7002.conf
[root@theo cluster-conf]# sed 's/7001/7003/g' redis-7001.conf > redis7003.conf
[root@theo cluster-conf]# sed 's/7001/7004/g' redis-7001.conf > redis7004.conf
[root@theo cluster-conf]# sed 's/7001/7005/g' redis-7001.conf > redis7005.conf
[root@theo cluster-conf]# sed 's/7001/7006/g' redis-7001.conf > redis7006.conf
3.1.3启动6个redis实例
[root@theo cluster-conf]# redis-server redis-7001.conf &
[root@theo cluster-conf]# redis-server redis-7002.conf &
[root@theo cluster-conf]# redis-server redis-7003.conf &
[root@theo cluster-conf]# redis-server redis-7004.conf &
[root@theo cluster-conf]# redis-server redis-7005.conf &
[root@theo cluster-conf]# redis-server redis-7006.conf &
3.1.4启动集群
[root@theo cluster-conf]# redis-cli --cluster create 47.96.11.185:7001
47.96.11.185:7002 47.96.11.185:7003 47.96.11.185:7004 47.96.11.185:7005
47.96.11.185:7006 --cluster-replicas 1
这里改成自己的公网ip地址即可,但是一定注意要在云服务器上放行端口
3.1.5连接集群
[root@theo cluster-conf]# redis-cli -p 7001 -c
3.2集群管理
创建集群:
[root@theo cluster-conf]# redis-cli --cluster create
47.96.11.185:7001 47.96.11.185:7002 47.96.11.185:7003
47.96.11.185:7004 47.96.11.185:7005 47.96.11.185:7006 --clusterreplicas
查看集群状态
[root@theo cluster-conf]# redis-cli --cluster info 47.96.11.185:7001
47.96.11.185:7001 (4678478a...) -> 2 keys | 5461 slots | 1 slaves.
47.96.11.185:7002 (e26eaf2a...) -> 0 keys | 5462 slots | 1 slaves.
47.96.11.185:7003 (5752eb20...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
平衡节点的数据槽数
[root@theo cluster-conf]# redis-cli --cluster rebalance
47.96.11.185:7001
>>> Performing Cluster Check (using node 47.96.11.185:7001)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.00% threshold.
删除节点
[root@theo cluster-conf]# redis-cli --cluster del-node
47.96.11.185:7001 4678478aa66b6d37b23944cf7db0ac07298538a4
>>> Removing node 4678478aa66b6d37b23944cf7db0ac07298538a4 from
cluster 47.96.11.185:7001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@theo cluster-conf]# redis-cli --cluster info 47.96.11.185:7002
47.96.11.185:7002 (e26eaf2a...) -> 1 keys | 8192 slots | 2 slaves.
47.96.11.185:7003 (5752eb20...) -> 2 keys | 8192 slots | 1 slaves.
[OK] 3 keys in 2 masters.
0.00 keys per slot on average.
添加节点
[root@theo cluster-conf]# redis-cli --cluster add-node
47.96.11.185:7007 47.96.11.185:7002