一主二从三哨兵

1、修改从机配置文件

vim /usr/local/redis/redis.config

# 1. bind 127.0.0.1
# 2. daemonize no 改成 yes
# 3. 这一行是配置redis的日志文件
logfile "/usr/local/src/redis/server.log"
# 4. replicaof <masterip> <masterport> 
replicaof 192.168.182.101 6379
# # Redis 5.0.0 之前的版本,那么请使用SLAVEOF命令
# slaveof 10.16.10.172 6379

masterauth 123456
requirepass 123456

2、修改主机配置文件

同上

不需要主副本复制 replicaof

3、启动redis服务

service redisd start

4、测试一主二从通信,查看各自角色、主从信息

通过 redis-cli连接

cd /usr/local/redis/bin

主节点测试

[root@localhost bin]# ./redis-cli -c -h node1 -p 6379
node1:6379> AUTH 123456
OK
node1:6379> role
1) "master"
2) (integer) 1302
3) 1) 1) "192.168.182.102"
2) "6379"
3) "1302"
2) 1) "192.168.182.103"
2) "6379"
3) "1302"
   node1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.182.102,port=6379,state=online,offset=1316,lag=0
slave1:ip=192.168.182.103,port=6379,state=online,offset=1316,lag=1
master_replid:b07d2e77358402d61d250a88facb13b9878de0db
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1316
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1316
node1:6379> exit

从节点测试

[root@localhost bin]# ./redis-cli -c -h node3 -p 6379
node3:6379> AUTH 123456
OK
node3:6379> role
1) "slave"
2) "192.168.182.101"
3) (integer) 6379
4) "connected"
5) (integer) 546
   node3:6379> info replication
# Replication
role:slave
master_host:192.168.182.101
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:602
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b07d2e77358402d61d250a88facb13b9878de0db
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:602
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:588
node3:6379>

主节点写数据成功,从节点读到写入数据;主写从读测试成功~

5、配置哨兵模式

Redis的哨兵(sentinel) 系统用于管理多个 Redis 服务器,该系统执行以下三个任务:

  1. 监控(Monitoring): 哨兵(sentinel) 会不断地检查你的Master和Slave是否运作正常。
  2. 提醒(Notification):当被监控的某个 Redis出现问题时, 哨兵(sentinel) 可以通过 API 向管理员或者其他应用程序发送通知。
  3. 自动故障迁移(Automatic failover):当一个Master不能正常工作时,哨兵(sentinel) 会开始一次自动故障迁移操作,它会将失效Master的其中一个Slave升级为新的Master, 并让失效Master的其他Slave改为复制新的Master;如果修复好的master重新启动后,原master变成slave。epoch

哨兵的配置文件我已经拷贝到/usr/local/redis/bin/目录下

# 把这行打开,让三个哨兵可以通信
protected-mode no
# 这里配置的都是主节点的IP地址和端口,2代表大于等于2票即可成为主节点
sentinel monitor mymaster 192.168.182.101 6379 2 
# 这里是配置集群的密码
sentinel auth-pass mymaster 123456

port 26379

然后启动三台机器的哨兵;在/usr/local/redis/目录下,执行命令./bin/redis-sentinel sentinel.conf

这里记得放开sentinel端口26379

主节点挂掉,可以看到会从从节点中选举一个主节点,原主节点重新启动,变成从节点

注:

replicaof参数

Master-Replica replication. Use replicaof to make a Redis instance a copy of
another Redis server. A few things to understand ASAP about Redis replication.
主副本复制。使用 replicaof 使 Redis 实例成为另一个 Redis 服务器的副本。关于 Redis 复制的一些事情要尽快理解。

±-----------------+ ±--------------+
 | Master | —> | Replica |
 | (receive writes) | | (exact copy) |
 ±-----------------+ ±--------------+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 replicas.
 Redis 复制是异步的,但您可以将 master 配置为在它似乎没有与至少给定数量的副本连接时停止接受写入。2. Redis replicas 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.
 如果复制链接在相对较短的时间内丢失,Redis 副本能够与主服务器执行部分重新同步。您可能希望根据您的需要使用合理的值配置复制积压大小(请参阅此文件的下一部分)。3. Replication is automatic and does not need user intervention. After a
 network partition replicas automatically try to reconnect to masters
 and resynchronize with them.
 复制是自动的,不需要用户干预。在网络分区后,副本会自动尝试重新连接到主服务器并与它们重新同步。