这里我们采用的集群整体架构就是主从结构+哨兵(sentinel),实现容灾的自动切换,如下图所示:

redis 一主几从 redis集群一主两从_redis

一个主节点(master)可拥有多个从节点(slave),从节点实现对主节点的复制,保证数据同步。

而哨兵(sentinel)则对各节点进行监控,主要包括主节点存活检测、主从运行情况检测等,一旦主节点宕机,哨兵可自动进行故障转移 (failover)、主从切换。
接下来就开始搭建这样一个集群,首先是主从结构,然后是哨兵模式

接下来搭建 Redis 1主2从集群

Redis 集群搭建

配置如下

主机配置

bind:0.0.0.0
port:6379
protected-mode:no
daemonize:yes
logfile:./redis.log

从机配置

bind:0.0.0.0
port:6380
protected-mode:no
daemonize:yes
logfile:./redis.log
replicaof 127.0.0.1 6379
  • bind:0.0.0.0
    Redis 默认只允许本机访问,把 bind 修改为 0.0.0.0 表示允许所有远程访问。如果想指定限制访问,可设置对应的 ip。
  • port:6379
    监听端口默认为6379,想改其他也行
  • protected-mode:no
    关闭保护模式,可以外部访问
  • daemonize:yes
    设置为后台启动。
  • logfile:./redis.log
    redis 日志文件,生成后在 bin 目录下可找到。
  • replicaof 127.0.0.1 6379
    指定当本机为 slave 服务时,设置 master 服务的IP地址及端口,在 redis 启动的时候会自动跟 master 进行数据同步,所以两台从机都这样配置即可。

配置完之后,启动 Redis 集群

[root@localhost bin]# ./redis-server redis.conf 
[root@localhost bin]# ./redis-server slave_1.conf 
[root@localhost bin]# ./redis-server slave_2.conf

查看集群信息

[root@localhost bin]# ./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=70,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=70,lag=0
master_failover_state:no-failover
master_replid:c7a5b3dcb54f241ebd42765e2f7ac998cbc6dead
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70
127.0.0.1:6379> exit
[root@localhost bin]# ./redis-cli -p 6380
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_read_repl_offset:346
slave_repl_offset:346
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c7a5b3dcb54f241ebd42765e2f7ac998cbc6dead
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:346
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:346

主从验证

127.0.0.1:6379> set hello codeman
127.0.0.1:6380> get hello
127.0.0.1:6381> get hello

哨兵集群搭建

配置文件如下

//端口默认为26379。
port:26379
//关闭保护模式,可以外部访问。
protected-mode:no
//设置为后台启动。
daemonize:yes
//日志文件。
logfile:./sentinel.log
//指定主机IP地址和端口,并且指定当有2台哨兵认为主机挂了,则对主机进行容灾切换。
sentinel monitor mymaster 127.0.0.1 6379 2
//当在Redis实例中开启了requirepass,这里就需要提供密码。
//sentinel auth-pass mymaster pwdtest@2019
//这里设置了主机多少秒无响应,则认为挂了。
sentinel down-after-milliseconds mymaster 3000
//主备切换时,最多有多少个slave同时对新的master进行同步,这里设置为默认的1。
snetinel parallel-syncs mymaster 1
//故障转移的超时时间,这里设置为三分钟。
sentinel failover-timeout mymaster 180000

启动3个哨兵

[root@localhost bin]# 。、
[root@localhost bin]# ./redis-sentinel etc/sentinel_2.conf 
[root@localhost bin]# ./redis-sentinel etc/sentinel_3.conf

查看哨兵信息

[root@localhost bin]# ./redis-cli -p 26379
127.0.0.1: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=127.0.0.1:6379,slaves=2,sentinels=3
127.0.0.1:26379> exit

容灾切换

模拟主机宕机,关闭 redis 服务

[root@localhost bin]# ./redis-cli -p 6379
127.0.0.1:6379> shutdown
not connected> exit

查看其它服务的集群信息

[root@localhost bin]# ./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=39247,lag=0
master_failover_state:no-failover
master_replid:d04cdf0ec6da075a5c5290f5de60c7b8d62e5d06
master_replid2:c7a5b3dcb54f241ebd42765e2f7ac998cbc6dead
master_repl_offset:39247
second_repl_offset:22621
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:39247
127.0.0.1:6380> exit

重新启动 6379 主节点,发现主节点已经变成从节点了

[root@localhost bin]# ./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:1
master_sync_in_progress:0
slave_read_repl_offset:75831
slave_repl_offset:75831
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:d04cdf0ec6da075a5c5290f5de60c7b8d62e5d06
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:75831
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:57994
repl_backlog_histlen:17838
127.0.0.1:6379> exit
[root@localhost bin]# ./redis-cli -p 6380
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=81858,lag=1
slave1:ip=127.0.0.1,port=6379,state=online,offset=81725,lag=1
master_failover_state:no-failover
master_replid:d04cdf0ec6da075a5c5290f5de60c7b8d62e5d06
master_replid2:c7a5b3dcb54f241ebd42765e2f7ac998cbc6dead
master_repl_offset:81858
second_repl_offset:22621
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:81858