Redis-05.主从复制与Sentinel

主从复制(master/slave)

主机(master)数据更新后根据配置和策略,自动同步到备机(slave)。通过主从复制,能够实现读写分离容灾恢复

实现主从复制非常简单,只需要在从(slave)执行slaveof <masterip> <masterport>命令即可,也可以在配置文件redis.conf中配置启动redis时自动实现主从复制。

特别注意:如果master设置了requirepass,则slave必须设置masterauth,否则会复制失败

################################# REPLICATION #################################

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition slaves automatically try to reconnect to masters
# and resynchronize with them.
#
# slaveof <masterip> <masterport> # If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>
1.单主机上启动3个Redis实例来演示1主2从

step1:从/etc/redis.conf拷贝3份配置文件,并修改配置,分别用于3个实例

[root@VM_0_171_centos ~]# ls -l
总用量 152
-rw-r--r-- 1 root root 46755 4月 4 15:35 redis-6379.conf
-rw-r--r-- 1 root root 46746 4月 4 15:35 redis-6380.conf
-rw-r--r-- 1 root root 46777 4月 4 15:37 redis-6381.conf
[root@VM_0_171_centos ~]#

配置项

port 6380
daemonize yes
pidfile "redis-6380.pid"
logfile "redis-6380.log"
dbfilename "dump-6380.rdb"
slaveof 127.0.0.1 6379

step2:分别启动3个Redis示例,查看进程发现3个进程都已经启动,连接master查看replication信息发现6379为master,6780、6381为slave

[root@VM_0_171_centos ~]# redis-server redis-6379.conf
[root@VM_0_171_centos ~]# redis-server redis-6380.conf
[root@VM_0_171_centos ~]# redis-server redis-6381.conf
[root@VM_0_171_centos ~]# ps -ef | grep redis
root 30870 1 0 22:37 ? 00:00:00 redis-server 127.0.0.1:6379
root 30881 1 0 22:37 ? 00:00:00 redis-server 127.0.0.1:6380
root 30885 1 0 22:37 ? 00:00:00 redis-server 127.0.0.1:6381
root 30890 28045 0 22:37 pts/0 00:00:00 grep --color=auto redis
[root@VM_0_171_centos ~]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=29,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=29,lag=1
master_repl_offset:29
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:28
127.0.0.1:6379>

step3:验证,连接6379,保存k0;退出,连接6380,查询k0,得到刚才保存的v0,主从复制成功。

127.0.0.1:6379> keys *
1) "k1"
2) "k2"
3) "k5"
4) "k4"
5) "k3"
6) "k6"
127.0.0.1:6379> set k0 v0
OK
127.0.0.1:6379> exit
[root@VM_0_171_centos ~]# redis-cli -p 6380
127.0.0.1:6380> keys *
1) "k0"
2) "k6"
3) "k5"
4) "k2"
5) "k4"
6) "k3"
7) "k1"
127.0.0.1:6380> get k0
"v0"
127.0.0.1:6380>
2.sentinel哨兵模式

sentinel能够后台监控主机是否故障,如果故障了根据投票自动将slave转换为master,master恢复后会自动变成slave。

监控同一master的sentinel自动集群,协同工作。

示例

step1:复制/etc/redis-sentinel.conf,修改配置

#以守护进程启动
daemonize yes
#sentinel默认端口
port 26379
#工作目录
dir "/tmp"
#监控的master,最后的1表示1个sentinel认为master短线就干预,进行主从切换
sentinel monitor mymaster 127.0.0.1 6379 1
#master30000毫秒无响应,sentinel就认为它已经断线
sentinel down-after-milliseconds mymaster 30000
#在执行故障转移时, 最多可以有多少个slave同时对新的master进行同步, 这个数字越小, 完成故障转移所需的时间就越长,但越大就意味着越多的slave因为复制而不可用。可以通过将这个值设为 1 来保证每次只有一个slave处于不能处理命令请求的状态。
sentinel parallel-syncs mymaster 1
#故障转移的毫秒数
sentinel failover-timeout mymaster 180000
#日志文件
logfile /var/log/redis/sentinel.log

step2:后台启动sentinel(daemonize yes)

[root@VM_0_171_centos ~]# redis-sentinel redis-sentinel.conf
[root@VM_0_171_centos ~]# ps -ef | grep redis
root 2169 1 0 23:33 ? 00:00:00 redis-sentinel *:26379 [sentinel]
root 2180 28045 0 23:33 pts/0 00:00:00 grep --color=auto redis
root 31340 1 0 22:43 ? 00:00:01 redis-server 127.0.0.1:6379
root 31344 1 0 22:43 ? 00:00:01 redis-server 127.0.0.1:6380
root 31349 1 0 22:43 ? 00:00:01 redis-server 127.0.0.1:6381
[root@VM_0_171_centos ~]#

step3:停止mster,查看sentinel.log发现6380成了新的master,连接6380查看replication信息进行验证

[root@VM_0_171_centos ~]# redis-cli -p 6379
127.0.0.1:6379> shutdown
not connected> exit
                _._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 2169
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-' 2169:X 04 Apr 23:33:26.845 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2169:X 04 Apr 23:33:26.845 # Sentinel ID is 5a7283b3989cece81dea1e770e1e83b3ee174004
2169:X 04 Apr 23:33:26.845 # +monitor master mymaster 127.0.0.1 6381 quorum 1
2169:X 04 Apr 23:34:16.908 # +sdown master mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:16.908 # +odown master mymaster 127.0.0.1 6381 #quorum 1/1
2169:X 04 Apr 23:34:16.908 # +new-epoch 4
2169:X 04 Apr 23:34:16.908 # +try-failover master mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:16.920 # +vote-for-leader 5a7283b3989cece81dea1e770e1e83b3ee174004 4
2169:X 04 Apr 23:34:16.920 # +elected-leader master mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:16.920 # +failover-state-select-slave master mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:17.020 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:17.020 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:17.091 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:18.048 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:18.048 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:18.117 * +slave-reconf-sent slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:19.093 * +slave-reconf-inprog slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:19.094 * +slave-reconf-done slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:19.176 # +failover-end master mymaster 127.0.0.1 6381
2169:X 04 Apr 23:34:19.176 # +switch-master mymaster 127.0.0.1 6381 127.0.0.1 6380
2169:X 04 Apr 23:34:19.176 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
2169:X 04 Apr 23:34:19.176 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
2169:X 04 Apr 23:35:30.920 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
[root@VM_0_171_centos ~]#
[root@VM_0_171_centos ~]# redis-cli -p 6380
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=37410,lag=0
master_repl_offset:37410
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:7475
repl_backlog_histlen:29936
127.0.0.1:6380>

step4:恢复6379端口的redis实例,发现它成了slave

[root@VM_0_171_centos ~]# redis-server redis-6379.conf
[root@VM_0_171_centos ~]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:45049
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
127.0.0.1:6379>
3.特别说明
1.如果master设置了requirepass,则slave需要设置masterauth

2.slave默认是read only模式,不能写入数据

3.master宕机后,slave正常工作,master恢复后会自动与salve连接

4.slave宕机后,master及其它slave(如果slave>1)继续工作,slave恢复后需要手动通过slaveof <masterip> <masterport>连接(或者在redis.conf中配置了slave of)

5.slaveof no one可以将slave变成master供其它slave连接
上一篇:JavaScript 函数声明,函数表达式,匿名函数,立即执行函数之区别


下一篇:Spring IoC小结