redis学习笔记6(主从复制)

时间:2022-05-31 15:34:06

实际应用中,一台redis服务器是无法满足需求的。原因如下:
从结构上,单个Redis服务器会发生单点故障,并且一台服务器需要处理所有的请求负载,压力较大;
从容量上,单个Redis服务器内存容量有限,就算一台Redis服务器内容容量为256G,也不能将所有内容用作Redis存储内存,一般来说,单台Redis最大使用内存不应该超过20G。
一.主从复制(Master/slave)
Master以写为主、slave以读为主;

主机(Master):(redis在设置主从复制时,主机不用做任何操作)

127.0.0.1:6379> INFO replication
# Replication
// 可以看到当前端口号6379角色是主机,连接了2个从机。
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=407,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=407,lag=0
master_repl_offset:407
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:406

redis把一台机器设为从机非常简单,执行”slaveof masterip masterport”:

127.0.0.1:6381> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6381> INFO replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:407
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

正常情况下,主从建立好关系,主机写入,从机读取。如果在建立主从关系之前,主机有写入的key;建立连接后从机也会同步。redis主从复制,从机不会覆盖主机写入信息。当主机挂掉之后,从机会待命直到主机恢复继续正常工作。从机挂掉之后,如果没有再conf中配置,需要再次配置主从关系。
如果主机挂掉之后,从机想转换身份为主机:

#使当前数据库停止与其他数据库同步,转成主数据库
127.0.0.1:6380> SLAVEOF no one
OK
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:3077
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

哨兵模式(sentinel)
在上面例子中,如果主机挂了,从机转换身份需要执行命令SLAVEOF no one非常不方便;自动实现身份转换就是哨兵模式。

启用哨兵模式需要配置一个文件sentinel.conf;

[root@wdj redis]# vim sentinel.conf 
[root@wdj redis]# cat sentinel.conf
# sentinel monitor 自定义名 主机ip 端口号 1的意思是投票大于1的时候就是主机了
sentinel monitor host6379 127.0.0.1 6379 1

配置完成后,启动

[root@wdj redis]# ./redis-sentinel sentinel.conf 
4916:X 02 Jan 16:06:52.025 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'
` _.-'| Port: 26379
|
`-._ `._ / _.-' | PID: 4916
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
|
`-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-'
_.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'


4916:X 02 Jan 16:06:52.034 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4916:X 02 Jan 16:06:52.034 # Sentinel runid is 6a3eeb99833f49080d0b1fdf9c9ee820ae93a3a8
4916:X 02 Jan 16:06:52.034 # +monitor master host6379 127.0.0.1 6379 quorum 1
4916:X 02 Jan 16:06:53.031 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:06:53.033 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379

这个时候把主机挂掉,再看从机80和81机器。可以看到,启用哨兵模式后,自动把81选为主机了。相当于81机器我们手动执行了下slaveof no one,80机器执行了slaveof 127.0.0.1 6381.

127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1116
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:30180
127.0.0.1:6380>

127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=1515,lag=1
master_repl_offset:1648
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1647

#################### sentinel.conf 在选出主机时打印的日志信息 ###########################
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'
` _.-'| Port: 26379
| `-._ `._ / _.-'
| PID: 4916
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'
_.-' | http://redis.io
`-._ `-._`-.__.-'
_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'
_.-' |
`-._ `-._`-.__.-'
_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'


4916:X 02 Jan 16:06:52.034 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4916:X 02 Jan 16:06:52.034 # Sentinel runid is 6a3eeb99833f49080d0b1fdf9c9ee820ae93a3a8
4916:X 02 Jan 16:06:52.034 # +monitor master host6379 127.0.0.1 6379 quorum 1
4916:X 02 Jan 16:06:53.031 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:06:53.033 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:44.824 # +sdown master host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:44.824 # +odown master host6379 127.0.0.1 6379 #quorum 1/1
4916:X 02 Jan 16:10:44.824 # +new-epoch 1
4916:X 02 Jan 16:10:44.824 # +try-failover master host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:44.830 # +vote-for-leader 6a3eeb99833f49080d0b1fdf9c9ee820ae93a3a8 1
4916:X 02 Jan 16:10:44.830 # +elected-leader master host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:44.830 # +failover-state-select-slave master host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:44.914 # +selected-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:44.914 * +failover-state-send-slaveof-noone slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:44.977 * +failover-state-wait-promotion slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:45.128 # +promoted-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:45.128 # +failover-state-reconf-slaves master host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:45.192 * +slave-reconf-sent slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:46.177 * +slave-reconf-inprog slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:46.177 * +slave-reconf-done slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:46.234 # +failover-end master host6379 127.0.0.1 6379
4916:X 02 Jan 16:10:46.234 # +switch-master host6379 127.0.0.1 6379 127.0.0.1 6381
4916:X 02 Jan 16:10:46.234 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
4916:X 02 Jan 16:10:46.235 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
4916:X 02 Jan 16:11:16.286 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381

这个时候如果我们把6379再启动之后,哨兵模式会把6379变为从机。

[root@wdj redis]# redis-server redis6379.conf 
[root@wdj redis]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:31029
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0