Redis集群与高可用的三种模式:
Redis主从复制
Redis哨兵(Sentinel)
Redis Cluster
Redis 哨兵(Sentinel) Sentinel实现故障转移过程
多个 sentinel 发现并确认master有问题
选举出一个 sentinel 作为领导
选出一个 slave 作为 master
通知其余 slave 成为新 master 的 slave
通知客户端主从变化
等待旧的 master 复活成为新 master 的 slave
Redis Sentinel 节点与普通 Redis 没有区别,要实现读写分离依赖于客户端程序
Sentinel 机制类似于MySQL中的MHA功能,只解决master和slave角色的自动故障转移问题,但单个Master 的性能瓶颈问题并没有解决
Redis 3.0 之前版本中,生产环境一般使用哨兵模式较多,Redis 3.0后推出Redis cluster功能,可以支持更大规模的高并发环境
使用哨兵只能解决redis高可用问题,实现自动故障转移,无法解决master节点的性能瓶颈问题。要解决单机性能瓶颈,提高整体性能,可以使用分布式集群的解决方案
实现哨兵配置案例: 前提:实现哨兵的前提是已经实现了redis的主从复制。master 配置文件中 masterauth 和 slave都必须相同,哨兵节点必须大于等于3,最好为奇数
使用脚本编译安装:
master
host41
192.168.23.41
slave
host42
192.168.23.42
slave
host43
192.168.23.43
1、实现主从复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' \ -e 's/^# masterauth .*$/masterauth 12345678/' \ -e 's/^# requirepass .*$/requirepass 12345678/' \ /apps/redis/etc/redis.conf sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' \ -e 's/^# masterauth .*$/masterauth 12345678/' \ -e 's/^# requirepass .*$/requirepass 12345678/' \ -e 's/^# replicaof .*$/replicaof 192.168.23.41 6379/' \ /apps/redis/etc/redis.conf ;\ systemctl restart redis 192.168.23.42:6379> INFO REPLICATION role:slave master_host:192.168.23.41 master_port:6379 master_link_status:up master_last_io_seconds_ago:8 master_sync_in_progress:0 slave_read_repl_offset:98 slave_repl_offset:98 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:91831adacc78acd95246d7bf4a38c048c37b0931 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:98 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:98 192.168.23.41:6379> INFO REPLICATION role:master connected_slaves:2 slave0:ip=192.168.23.42,port=6379,state=online,offset=70,lag=0 slave1:ip=192.168.23.43,port=6379,state=online,offset=70,lag=0 master_failover_state:no-failover master_replid:91831adacc78acd95246d7bf4a38c048c37b0931 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
2、配置哨兵配置文件,在所有节点执行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 root@host41:~ root@host41:~ sed -i -e 's/^sentinel monitor mymaster .*$/sentinel monitor mymaster 192.168.23.41 6379 2/' \ -e 's/^# sentinel auth-pass mymaster .*$/sentinel auth-pass mymaster 12345678/' \ -e 's/^sentinel down-after-milliseconds .*$/sentinel down-after-milliseconds mymaster 3000/' \ /apps/redis/etc/sentinel.conf daemonize yes pidfile /apps/redis/run/redis-sentinel.pid logfile /apps/redis/log/redis-sentinel.logdir /apps/redis/data/ sentinel monitor mymaster 192.168.23.41 6379 2 sentinel auth-pass mymaster 12345678 sentinel down-after-milliseconds mymaster 3000 root@host41:~ root@host41:~
3、配置启动服务,在所有节点执行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 root@host41:~cat > /lib/systemd/system/redis-sentinel.service <<EOF [Unit] Description=Redis Sentinel Documentation=https://redis.io/documentation Wants=network-online.target After=network-online.target [Service] ExecStart=/apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf --supervised systemd ExecStop=/bin/kill -s QUIT $MAINPID LimitNOFILE=1000000 NoNewPrivileges=yes Type=notify TimeoutStartSec=infinity TimeoutStopSec=infinity User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target EOF root@host41:/apps/redis/log root@host41:~ root@host41:~
4、验证哨兵服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 root@host41:~ State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 511 0.0.0.0:6379 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 511 0.0.0.0:26379 0.0.0.0:* LISTEN 0 1024 127.0.0.53%lo:53 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 511 [::1]:6379 [::]:* LISTEN 0 511 [::]:26379 [::]:* root@host41:~ 1167:X 19 Jun 2024 02:23:39.819 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1167, just started 1167:X 19 Jun 2024 02:23:39.819 * Configuration loaded 1167:X 19 Jun 2024 02:23:39.819 * monotonic clock: POSIX clock_gettime 1167:X 19 Jun 2024 02:23:39.864 * Running mode=sentinel, port=26379. 1167:X 19 Jun 2024 02:23:39.865 * Sentinel ID is a42023cc2a11b727d57e4152df7e571e0d2c3c4f 1167:X 19 Jun 2024 02:23:39.865 1167:X 19 Jun 2024 02:23:42.859 1167:X 19 Jun 2024 02:23:42.859 1167:X 19 Jun 2024 02:25:33.924 1167:X 19 Jun 2024 02:34:47.798 root@host41:~ 127.0.0.1:26379> info sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_tilt_since_seconds:-1 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=192.168.23.41:6379,slaves=2,sentinels=3
5、检查故障转移 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 root@host41:~ root@host41:~ root@host42:~ 127.0.0.1:26379> info sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_tilt_since_seconds:-1 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=192.168.23.41:6379,slaves=2,sentinels=3 127.0.0.1:26379> info sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_tilt_since_seconds:-1 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=192.168.23.43:6379,slaves=2,sentinels=3 root@host42:~ 656:X 19 Jun 2024 03:08:48.074 656:X 19 Jun 2024 03:08:48.537 * Sentinel new configuration saved on disk 656:X 19 Jun 2024 03:08:48.537 656:X 19 Jun 2024 03:08:48.646 * Sentinel new configuration saved on disk 656:X 19 Jun 2024 03:08:48.646 656:X 19 Jun 2024 03:08:49.224 656:X 19 Jun 2024 03:08:49.224 * Next failover delay: I will not start a failover before Wed Jun 19 03:14:48 2024 656:X 19 Jun 2024 03:08:49.530 656:X 19 Jun 2024 03:08:49.530 656:X 19 Jun 2024 03:08:49.531 * +slave slave 192.168.23.42:6379 192.168.23.42 6379 @ mymaster 192.168.23.43 6379 656:X 19 Jun 2024 03:08:49.531 * +slave slave 192.168.23.41:6379 192.168.23.41 6379 @ mymaster 192.168.23.43 6379 656:X 19 Jun 2024 03:08:49.913 * Sentinel new configuration saved on disk 656:X 19 Jun 2024 03:08:52.547 656:X 19 Jun 2024 03:09:06.525
1 2 3 4 5 6 7 8 9 10 11 12 root@host41:~ root@host41:~ 856:X 19 Jun 2024 03:16:39.889 * Supervised by systemd. Please make sure you set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit. 856:X 19 Jun 2024 03:16:39.889 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 856:X 19 Jun 2024 03:16:39.889 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=856, just started 856:X 19 Jun 2024 03:16:39.890 * Configuration loaded 856:X 19 Jun 2024 03:16:39.891 * monotonic clock: POSIX clock_gettime 856:X 19 Jun 2024 03:16:39.892 * Running mode=sentinel, port=26379. 856:X 19 Jun 2024 03:16:39.893 * Sentinel ID is a42023cc2a11b727d57e4152df7e571e0d2c3c4f 856:X 19 Jun 2024 03:16:39.893
1 2 3 4 5 6 7 8 9 10 11 12 13 root@host41:~ replicaof 192.168.23.43 6379 root@host42:~ replicaof 192.168.23.43 6379 root@host41:~ sentinel monitor mymaster 192.168.23.43 6379 2 root@host42:~ sentinel monitor mymaster 192.168.23.43 6379 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 在新的主节点(192.168.23.43)检查主从状态 root@host43:~ Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> info replication role:master connected_slaves:2 slave0:ip=192.168.23.42,port=6379,state=online,offset=481380,lag=0 slave1:ip=192.168.23.41,port=6379,state=online,offset=481239,lag=0 master_failover_state:no-failover master_replid:21bca4b1979437ee3e37477174274c20a59a8f33 master_replid2:39c37024640c52126cbbc6ab64e39e0b5d489024 master_repl_offset:481380 second_repl_offset:231581 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:3318 repl_backlog_histlen:478063