Redis的Replication(主从复制)和sentinel机制

Replication

特点:
一个Master可以有多个slave主机,支持链式复制;
Master以非阻塞方式同步数据至slave主机;

配置参数:

配置slave节点:
slaveof 主机ip 主机port
设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启 动时,它会自动从master进行数据同步;
masterauth 主机password
当master服务设置了密码保护时,slav服务连接master的密码
实验:环境:centos7.3
准备两个节点master :192.168.23.148
slave: 192.168.23.149
配置slave节点:

[root@cento7 ~]# vim /etc/redis.conf 

# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1
bind 192.168.23.149 #为了方便看清自己的ip 又绑定一个ip
# slaveof   #指定主机的IP地址
# 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  # 如果主机ip有密码的话  指定主机的iP密码
#当然也可以在客户端设置
[root@cento7 ~]# redis-cli -h 192.168.23.149
192.168.23.149:6379> slaveof 192.168.23.148 6379
OK
192.168.23.149:6379> config set masterauth magedu
OK

从节点查看info信息记日志:

192.168.23.149:6379> info replication #INFO信息
# Replication
role:slave
master_host:192.168.23.148
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:841
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
[root@cento7 ~]# tail /var/log/redis/redis.log #日志信息
1916:S 16 Sep 15:38:52.335 * Connecting to MASTER 192.168.23.148:6379
1916:S 16 Sep 15:38:52.335 * MASTER <-> SLAVE sync started
1916:S 16 Sep 15:38:52.336 * Non blocking connect for SYNC fired the event.
1916:S 16 Sep 15:38:52.336 * Master replied to PING, replication can continue...
1916:S 16 Sep 15:38:52.337 * Partial resynchronization not possible (no cached master)
1916:S 16 Sep 15:38:52.339 * Full resync from master: 9efb89fac553574f70e5151a4f419750e47768bc:631
1916:S 16 Sep 15:38:52.438 * MASTER <-> SLAVE sync: receiving 259 bytes from master
1916:S 16 Sep 15:38:52.438 * MASTER <-> SLAVE sync: Flushing old data
1916:S 16 Sep 15:38:52.439 * MASTER <-> SLAVE sync: Loading DB in memory
1916:S 16 Sep 15:38:52.439 * MASTER <-> SLAVE sync: Finished with success

主节点的info信息记日志:

192.168.23.148:6379> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.23.149,port=6379,state=online,offset=113,lag=0
master_repl_offset:127
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:126
192.168.23.148:6379> exit
[root@centos7 ~]# tail /var/log/redis/redis.log  # 日志信息
7195:M 16 Sep 23:50:25.961 * DB loaded from disk: 0.000 seconds
7195:M 16 Sep 23:50:25.961 * The server is now ready to accept connections on port 6379
7195:M 16 Sep 23:50:26.307 * Slave 192.168.23.149:6379 asks for synchronization
7195:M 16 Sep 23:50:26.307 * Partial resynchronization not accepted: Runid mismatch (Client asked for runid '9efb89fac553574f70e5151a4f419750e47768bc', my runid is '943571b65e4f7f2e287b69a4495193244ca473a1')
7195:M 16 Sep 23:50:26.307 * Starting BGSAVE for SYNC with target: disk
7195:M 16 Sep 23:50:26.308 * Background saving started by pid 7198
7198:C 16 Sep 23:50:26.309 * DB saved on disk
7198:C 16 Sep 23:50:26.310 * RDB: 2 MB of memory used by copy-on-write
7195:M 16 Sep 23:50:26.365 * Background saving terminated with success
7195:M 16 Sep 23:50:26.366 * Synchronization with slave 192.168.23.149:6379 succeeded

sentinel机制

作用:
用于管理多个Redis服务实现HA
1.监控主服务器Master;
2.通知;
3.自动故障转移;
协议:
流言协议、投票协议
工作过程
1)服务器自身初始化,运行于redis-server中专用于sentinel功能的代码;
2)初始化sentinel状态,根据给定的配置文件,初始化监控的master服务器列表;
3)创建连向master的连接;

配置项:
1. sentinel monitor < master-name > < ip > < redis-port> < quorum >
#连接的主节点,可以有多行,quorum表示法定票数,建议sentinel节点为奇数个
#只需要指明主节点即可,从节点会通过主节点自动获取
示例:sentinel monitor mymaster 127.0.0.1 6379 2
2. sentinel down-after-milliseconds < master-name > < milliseconds >
#判断某主节点不在线的超时时长
示例:sentinel down-after-milliseconds mymaster 30000 (单位:毫秒)
3. sentinel parallel-syncs < master-name > < numslaves >
#执行故障转移时(即刚刚设定为新主服务器时),允许最多有多少个从服务器可以向主服务器发起连接请求
示例: sentinel parallel-syncs mymaster 1
4. sentinel failover-timeout < master-name > < milliseconds >
#故障转移的超时时间,即当主服务器出现故障时,提升新的从服务器为主服务器的超时时间;
示例: sentinel failover-timeout mymaster 180000
5. sentinel auth-pass < master-name> < password>
# 连接主节点的认证密码
示例:sentinel auth-pass mymaster magedu

环境:

192.168.23.148 既为master节点 又为 sentinel节点
192.168.23.149和192.168.23.100 分别为从节点
一.配置 redis文件 /etc/redis.conf
由于从节点的配置是一样的所以这里只显示一个文件配置:

[root@cent7 ~]# vim /etc/redis.conf 

#    network partition slaves automatically try to reconnect to masters
#    and resynchronize with them.
#
 slaveof 192.168.23.148 6379 #从节点指向主节点ip 和 端口

# 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 magedu  #主节点的密码

1.1分别查看从节点的info
从节点192.168.23.149 的信息

192.168.23.149:6379> info replication
# Replication
role:slave
master_host:192.168.23.148
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:1051
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

1.2 从节点192.168.23.150 的info信息

192.168.23.150:6379> info replication
# Replication
role:slave
master_host:192.168.23.148
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:99
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

1.3 主节点的info信息:

[root@centos7 ~]# redis-cli -h 192.168.23.148
192.168.23.148:6379> auth magedu
OK
192.168.23.148:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.23.150,port=6379,state=online,offset=155,lag=1
slave1:ip=192.168.23.149,port=6379,state=online,offset=155,lag=1
master_repl_offset:155
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:154

2.在主节点配置并启动sentinel机制,编辑配置文件/etc/redis-sentinel.conf

[root@centos7 ~]# vim /etc/redis-sentinel.conf 
sentinel monitor mymaster 192.168.23.148 6379 1 #要连接主节点的ip和端口
sentinel down-after-milliseconds mymaster 6000 # 判断主节点不在线的超时长
sentinel failover-timeout mymaster 60000 #故障转移的超时时长

2.1 启动sentinel 服务并查看端口

[root@centos7 ~]# systemctl restart redis-sentinel.service
[root@centos7 ~]# ss -ntlp | grep "sentinel"
LISTEN     0      128          *:26379                    *:*                   users:(("redis-sentinel",pid=3480,fd=5))
LISTEN     0      128         :::26379                   :::*                   users:(("redis-sentinel",pid=3480,fd=4))

2.3 连接sentinel 需要bind主节点ip

[root@centos7 ~]# redis-cli -h 192.168.23.148 -p 26379
192.168.23.148: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=odown,address=192.168.23.148:6379,slaves=0,sentinels=1
192.168.23.148:26379> sentinel masters #获取主节点的 sentinel信息
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "192.168.23.148"
    5) "port"
    6) "6379"
    7) "runid"
    8) ""
    9) "flags"
   10) "s_down,o_down,master,disconnected"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "183350"
   17) "last-ok-ping-reply"
   18) "183350"
   19) "last-ping-reply"
   20) "11658"
   21) "s-down-time"
   22) "177345"
   23) "o-down-time"
   24) "177345"
   25) "down-after-milliseconds"
   26) "6000"
   27) "info-refresh"
   28) "1505814771824"
   29) "role-reported"
   30) "master"
   31) "role-reported-time"
   32) "183350"
   33) "config-epoch"
   34) "0"
   35) "num-slaves"
   36) "0"
   37) "num-other-sentinels"
   38) "0"
   39) "quorum"
   40) "1"
   41) "failover-timeout"
   42) "60000"
   43) "parallel-syncs"
   44) "1"

2.4 获取从服务信息 注意:要在所有的redis-sentinel文件中加入密码 即
sentinel auth-pass mymaster magedu

[root@centos7 ~]# redis-cli -h 192.168.23.148 -p 26379
192.168.23.148:26379> sentinel slaves mymaster
1)  1) "name"
    2) "192.168.23.150:6379"
    3) "ip"
    4) "192.168.23.150"
    5) "port"
    6) "6379"
    7) "runid"
    8) "5ae3e3ae957c2a3932aaaad5088acaede917e192"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "759"
   19) "last-ping-reply"
   20) "759"
   21) "down-after-milliseconds"
   22) "6000"
   23) "info-refresh"
   24) "2310"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "12374"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.23.148"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "26696"
2)  1) "name"
    2) "192.168.23.149:6379"
    3) "ip"
    4) "192.168.23.149"
    5) "port"
    6) "6379"
    7) "runid"
    8) "e7de20e44506d9813d1fc610c03a63d005f88855"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "759"
   19) "last-ping-reply"
   20) "759"
   21) "down-after-milliseconds"
   22) "6000"
   23) "info-refresh"
   24) "2310"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "12374"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.23.148"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "26696"

2.5 让主节点下线 再查看主机的sentinel 信息 发现主节点已经是原来的从节点地址

[root@centos7 ~]# systemctl stop redis.service 
[root@centos7 ~]# redis-cli -h 192.168.23.148 -p 26379
192.168.23.148: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=odown,address=192.168.23.148:6379,slaves=2,sentinels=1
192.168.23.148:26379> sentinel masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "192.168.23.150"
    5) "port"
    6) "6379"
    7) "runid"
    8) "5ae3e3ae957c2a3932aaaad5088acaede917e192"
    9) "flags"
   10) "master"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "433"
   19) "last-ping-reply"
   20) "433"
   21) "down-after-milliseconds"
   22) "6000"
   23) "info-refresh"
   24) "4299"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "185550"
   29) "config-epoch"
   30) "14"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "0"
   35) "quorum"
   36) "1"
   37) "failover-timeout"
   38) "60000"
   39) "parallel-syncs"
   40) "1"

你可能感兴趣的:(redis)