提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器。前者称为主节点(Master) ,后者称为从节点(slave);数据的复制是单向的,只能由主节点到从节点
默认情况下,每台Redis服务器都是主节点;且一个主节点可以有多个从节点(或没有从节点),但一个从节点只能有一个主节点
数据冗余:主从复制实现了数据的热备份,是持久化之外的一种数据冗余方式
故障恢复:当主节点出现问题时,可以由从节点提供服务,实现快速的故障恢复;实际上是一种服务的冗余
负载均衡:在主从复制的基础上,配合读写分离,可以由主节点提供写服务,由从节点提供读服务(即写Redis数据时应用连接主节点,读Redis数据时应用连接从节点),分担服务器负载:尤其是在写少读多的场景下,通过多个从节点分担读负载,可以大大提高Redis服务器的并发量
高可用基石:除了上述作用以外,主从复制还是哨兵和集群能够实施的基础,因此说主从复制是Redis高可用的基础
若启动一个slave机器进程,则它会向Master机器发送一个"sync command"命令,请求同步连接
无论是第一次连接还是重新连接,Master机器都会启动一个后台进程,将数据快照保存到数据文件中(执行rdb操作),同时Master还会记录改数据的所有命令并缓存在数据文件中
后台进程完成缓存操作之后,Master机器就会向slave机器发送数据文件,slave端机器将数据文件保存到硬盘上,然后将其加载到内存中,接着Master机器就会将修改数据的所有操作一并发送给slave端机器。若slave出现故障导致宕机,则恢复正常后会自动重新连接
Master机器收到slave端机器的连接后,将其完整的数据文件发送给slave端机器,如果Master同时收到多个slave发来的同步请求,则Master会在后台启动一个进程以保存数据文件,然后将其发送给所有的slave端机器,确保所有的slave端机器都正常
作用 | IP地址 | 安装服务 |
---|---|---|
主服务器 | 192.168.13.10 | redis |
从服务器1 | 192.168.13.20 | redis |
从服务器2 | 192.168.13.30 | redis |
systemctl stop firewalld.service
setenforce 0
yum install gcc gcc-c++ make -y
tar zxvf redis-5.0.7.tar.gz
cd redis-5.0.7/
make -j 2 && make PREFIX=/usr/local/redis install
cd utils/
./install_server.sh
四次回车后。输入:/usr/local/redis/bin/redis-server回车
ln -s /usr/local/redis/bin/* /usr/local/bin/
vim /etc/redis/6379.conf
70行:bind 192.168.13.10
137行:daemonize yes
172行:logfile /var/log/redis_6379.log
264行:dir /var/lib/redis/6379
700行:appendonly yes
287行:replicaof 192.168.13.10 6379
/etc/init.d/redis_6379 restart
systemctl stop firewalld.service
setenforce 0
yum install gcc gcc-c++ make -y
tar zxvf redis-5.0.7.tar.gz
cd redis-5.0.7/
make -j 2 && make PREFIX=/usr/local/redis install
cd utils/
./install_server.sh
四次回车后。输入:/usr/local/redis/bin/redis-server回车
ln -s /usr/local/redis/bin/* /usr/local/bin/
vim /etc/redis/6379.conf
70行:bind 192.168.13.10
137行:daemonize yes
172行:logfile /var/log/redis_6379.log
264行:dir /var/lib/redis/6379
700行:appendonly yes
287行:replicaof 192.168.13.10 6379
/etc/init.d/redis_6379 restart
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install gcc gcc-c++ make -y
[root@localhost opt]# tar zxvf redis-5.0.7.tar.gz
[root@localhost opt]# cd redis-5.0.7/
[root@localhost opt]# make -j 2 && make PREFIX=/usr/local/redis install
[root@localhost redis-5.0.7]# cd utils/
[root@localhost utils]# ./install_server.sh
########################################################
四次回车后。输入:/usr/local/redis/bin/redis-server回车
Please select the redis executable path [] /usr/local/redis/bin/redis-server
#########################################################
[root@localhost utils]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@localhost utils]# vim /etc/redis/6379.conf
70行:bind 192.168.13.10
137行:daemonize yes
172行:logfile /var/log/redis_6379.log
264行:dir /var/lib/redis/6379
700行:appendonly yes
287行:replicaof 192.168.13.10 6379
/etc/init.d/redis_6379 restart
作用 | IP地址 | 安装服务 |
---|---|---|
主服务器 | 192.168.13.10 | redis |
从服务器1 | 192.168.13.20 | redis |
从服务器2 | 192.168.13.30 | redis |
master,slave1,slave2服务器设置
[root@localhost ~]# vim /opt/redis-5.0.7/sentinel.conf
protected-mode no #17行,关闭保护模式
port 26379 #21行,Redis哨兵默认的监听端口
daemonize yes #26行,指定sentinel为后台启动
logfile "/var/log/sentinel.log" #36行,指定日志存放路径
dir "/var/lib/redis/6379" #65行,指定数据库存放路径
sentinel monitor mymaster 192.168.80.10 6379 2 #84行, 修改
指定该哨兵节点监控192.168.80.10:6379这个主节点,该主节点的名称是mymaster,最后的2的含义与主节点的故障判定有关:至少需要2个哨兵节点同意,才能判定主节点故障并进行故障转移
sentinel down-after-milliseconds mymaster 30000 #113行,判定服务器down掉的时间周期,默认30000毫秒(30秒)
sentinel failover-timeout mymaster 180000 #146行,故障节点的最大超时时间为180000 (180秒 )
:wq
启动redis,注意优先启动master服务器,然后启动slave1和slave2
[root@localhost ~]# cd /opt/redis-5.0.7/
[root@localhost redis-5.0.7]# redis-sentinel sentinel.conf &
[1] 3835
验证哨兵模式
[root@localhost redis-5.0.7]# redis-cli -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.237.128:6379,slaves=2,sentinels=3
[1]+ 完成 redis-sentinel sentinel.conf
模拟破坏
master服务器:
[root@localhost redis-5.0.7]# ps -ef|grep redis #查找进程号
root 3286 1 0 16:35 ? 00:00:04 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 3836 1 0 17:28 ? 00:00:01 redis-sentinel *:26379 [sentinel]
root 3902 2790 0 17:34 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis-5.0.7]# ps -ef|grep redis
root 3286 1 0 16:35 ? 00:00:04 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 3836 1 0 17:28 ? 00:00:01 redis-sentinel *:26379 [sentinel]
root 3902 2790 0 17:34 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis-5.0.7]# kill -9 3286 #杀死主进程
[root@localhost redis-5.0.7]# ps -ef|grep redis
root 3836 1 0 17:28 ? 00:00:01 redis-sentinel *:26379 [sentinel]
root 3921 2790 0 17:37 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis-5.0.7]# tail -f /var/log/sentinel.log #查看master日志,发现master已经切换成slave2
3836:X 14 Jun 2022 17:37:30.085 # +promoted-slave slave 192.168.237.148:6379 192.168.237.148 6379 @ mymaster 192.168.237.128 6379
3836:X 14 Jun 2022 17:37:30.085 # +failover-state-reconf-slaves master mymaster 192.168.237.128 6379
3836:X 14 Jun 2022 17:37:30.146 * +slave-reconf-sent slave 192.168.237.129:6379 192.168.237.129 6379 @ mymaster 192.168.237.128 6379
3836:X 14 Jun 2022 17:37:30.667 # -odown master mymaster 192.168.237.128 6379
3836:X 14 Jun 2022 17:37:31.089 * +slave-reconf-inprog slave 192.168.237.129:6379 192.168.237.129 6379 @ mymaster 192.168.237.128 6379
3836:X 14 Jun 2022 17:37:31.089 * +slave-reconf-done slave 192.168.237.129:6379 192.168.237.129 6379 @ mymaster 192.168.237.128 6379
3836:X 14 Jun 2022 17:37:31.166 # +failover-end master mymaster 192.168.237.128 6379
3836:X 14 Jun 2022 17:37:31.166 # +switch-master mymaster 192.168.237.128 6379 192.168.237.148 6379
3836:X 14 Jun 2022 17:37:31.166 * +slave slave 192.168.237.129:6379 192.168.237.129 6379 @ mymaster 192.168.237.148 6379
3836:X 14 Jun 2022 17:37:31.166 * +slave slave 192.168.237.128:6379 192.168.237.128 6379 @ mymaster 192.168.237.148 6379
3836:X 14 Jun 2022 17:38:01.232 # +sdown slave 192.168.237.128:6379 192.168.237.128 6379 @ mymaster 192.168.237.148 6379
slave服务器:
[root@localhost redis-5.0.7]# redis-cli -p 26379 INFO Sentinel #查看到master换成slave2
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.237.148:6379,slaves=2,sentinels=3
[1]+ 完成 redis-sentinel sentinel.conf
集群,心即Redis Cluster,是Redis 3.0开始引入的分布式存储方案。
集群由多个节点(Node)组成,Redis的数据分布在这些节点中。集群中的节点分为主节点和从节点:只有主节点负责读写请求和集群信息的维护,从节点只进行主节点数据和状态信息的复制。
数据分区:数据分区(或称数据分片)是集群最核心的功能。集群将数据分散到多个节点,一方面突破了Redis单机内存大小的限制,存储容量大大增加,另一方面每个主节点都可以对外提供读服务和写服务,大大提高了集群的响应能力,Redis单机内存大小受限问题,在介绍持久化和主从复制时都有提及;例如,如果单机内存太大,bgsave和bgrewriteaof的fork操作可能导致主进程阻塞,主从环境下主机切换时可能导致从节点长时间无法提供服务,全量复制阶段主节点的复制缓冲区可能溢出
高可用:集群支持主从复制和主节点的自动故障转移(与哨兵类似)﹔当任一节点发生故障时,集群仍然可以对外提供服务
Redis集群引入了哈希槽的概念
Redis集群有16384个哈希槽(编号0-16383)
集群的每个节点负责一部分哈希槽
每个Key通过cRc16校验后对16384取余来决定放置哪个哈希槽,通过这个值,去找到对应的插槽所对应的节点,然后直接自动跳转到这个对应的节点上进行存取操作
节点A包含0到5460号哈希槽
节点B包含5461到10922号哈希槽
节点c包含10923到16383号哈希槽
集群中具有A、B、c三个节点,如果节点B失败了,整个集群就会因缺少5461-10922这个范围的槽而不可以用
为每个节点添加一个从节点A1、Bl、C1整个集群便有三个Master节点和三个slave节点组成,在节点a失败后,集群选举L位为的主节点继续服务。当B和B1都失败后,集群将不可用
所有服务器上相同操作,这里仅演示一台服务器上全部操作
[root@localhost ~]# mkdir -p redis-cluster/redis6001 #目录名根据节点名称需求更改
[root@localhost ~]# cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis6001 #复制配置文件
[root@localhost redis6001]# cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis6001
[root@localhost ~]# cd /etc/redis/redis-cluster/redis6001
[root@localhost redis6001]# vim redis.conf #修改配置文件
cd /etc/redis/redis-cluster/redis6001
vim redis.conf
bind 192.168.237.128 #69行,bind项,更改为本服务器IP
protected-mode no #88行,修改,关闭保护模式
port 6001 #92行,修改,redis监听端口
daemonize yes #136行,开启守护进程,以独立进程启动
appendonly yes #699行,修改,开启AOF持久化
cluster-enabled yes #832行,取消注释,开启群集功能
cluster-config-file nodes-6001.conf #840行,取消注释,群集名称文件设置
cluster-node-timeout 15000 #846行,取消注释群集超时时间设置
:wq
[root@localhost redis6001]# redis-server redis.conf #启动节点
[root@localhost redis6001]# ps -ef|grep redis
root 1230 1 0 14:50 ? 00:00:07 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 2659 1 0 15:39 ? 00:00:01 redis-server *:6001 [cluster]
root 2744 1871 0 15:48 pts/0 00:00:00 grep --color=auto redis
注:所有服务器节点端口不一样,创建目录和修改配置文件,按自己的端口来
#启动redis集群
[root@localhost redis6001]# redis-cli --cluster create 192.168.237.128:6001 192.168.237.129:6002 192.168.237.148:6003 192.168.237.138:6004 192.168.237.141:6005 192.168.237.130:6006 --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.237.141:6005 to 192.168.237.128:6001
Adding replica 192.168.237.130:6006 to 192.168.237.129:6002
Adding replica 192.168.237.138:6004 to 192.168.237.148:6003
M: a782e81edd840f2ee61023ced35eed01f310951d 192.168.237.128:6001
slots:[0-5460] (5461 slots) master
M: b8d3861378ce050b6f23becfa25f97e5e5c6b51d 192.168.237.129:6002
slots:[5461-10922] (5462 slots) master
M: e51b2ad9fb531e255d3493fc9f275f51999043f8 192.168.237.148:6003
slots:[10923-16383] (5461 slots) master
S: 5fefe52598ead1b3894af30ec972623e3e7e536b 192.168.237.138:6004
replicates e51b2ad9fb531e255d3493fc9f275f51999043f8
S: e0602ad1d40dcdd1d34011acf86fb4274aee92f6 192.168.237.141:6005
replicates a782e81edd840f2ee61023ced35eed01f310951d
S: 12ede45b42d8767f7cc0b5edc9a095e66cd6cf03 192.168.237.130:6006
replicates b8d3861378ce050b6f23becfa25f97e5e5c6b51d
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.237.128:6001)
M: a782e81edd840f2ee61023ced35eed01f310951d 192.168.237.128:6001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 12ede45b42d8767f7cc0b5edc9a095e66cd6cf03 192.168.237.130:6006
slots: (0 slots) slave
replicates b8d3861378ce050b6f23becfa25f97e5e5c6b51d
M: b8d3861378ce050b6f23becfa25f97e5e5c6b51d 192.168.237.129:6002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e0602ad1d40dcdd1d34011acf86fb4274aee92f6 192.168.237.141:6005
slots: (0 slots) slave
replicates a782e81edd840f2ee61023ced35eed01f310951d
M: e51b2ad9fb531e255d3493fc9f275f51999043f8 192.168.237.148:6003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 5fefe52598ead1b3894af30ec972623e3e7e536b 192.168.237.138:6004
slots: (0 slots) slave
replicates e51b2ad9fb531e255d3493fc9f275f51999043f8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
测试连接不同节点
[root@localhost redis6001]# redis-cli -h 192.168.237.129 -c -p 6002
192.168.237.129:6002>
[root@localhost redis6001]# redis-cli -h 192.168.237.141 -c -p 6005
192.168.237.141:6005>