目录1.环境概述1.1redis一主二从三哨兵模式1.2环境描述2.配置redis主从2.1修改主机名所有节点2.2安装resis所有节点2.3修改配置文件redis.conf(所有节点)2.4启动redis服务并放行端口所有节点2.5登录redis测试master节点3.配置哨兵模式3.1退出redis环境所有节点3.2配置哨兵模式master节点3.3开启哨兵模式并放行默认端口26379所有节点3.4查看是否配置成功master节点4.测试4.1主从复制测试4.2模拟master宕机4.2.1关闭master服务4.2.2哨兵日志查询4.2.3登录到83redis查看是否变为master4.2.4登录82redis查看是否变为了slave4.3恢复master4.3.1启动master的redis服务4.3.2测试旧master是否可写4.3.3再次查看master83主从信息5.添加redis守护进程所有节点1.环境概述1.1redis一主二从三哨兵模式当你使用Redis作为主从复制的架构并且希望在出现主节点故障时自动进行故障转移时适用于一主二从三哨兵模式。这种架构可以提高系统的可用性和容错性。哨兵模式的主要组件包括主服务器Master负责处理写操作和响应客户端的读写请求。从服务器Slave复制主服务器的数据负责读操作并在主服务器发生故障时接管主服务器的角色。哨兵节点Sentinel监控主服务器和从服务器的状态进行故障检测并在主服务器发生故障时自动进行故障转移和故障恢复在一主而从三哨兵模式中有一个主节点和多个从节点同时还有三个哨兵节点来监控主节点的健康状态。当主节点出现故障时哨兵节点会自动选举一个从节点作为新的主节点并将其他从节点切换到新的主节点上这个过程是自动的不需要手动干预。1.2环境描述主机系统IP地址redis版本Master节点Centos7.910.9.254.81redis-7.0.9Slave1节点Centos7.910.9.254.82redis-7.0.9Slave2节点Centos7.910.9.254.83redis-7.0.9达成的目的在三个节点自行安装 Redis 服务并启动配置 Redis 的访问需要密码密码设置为123456。然后将这三个 Redis 节点配置为 Redis 的一主二从三哨兵架构即一个 Redis 主节点两个从节点三个节点均为哨兵节点。2.配置redis主从2.1修改主机名所有节点[rootpoc ~]# hostnamectl set-hostname master[rootpoc ~]# bash[rootmaster ~]#[rootpoc ~]# hostnamectl set-hostname slave1[rootpoc ~]# bash[rootslave1 ~]#[rootpoc ~]# hostnamectl set-hostname slave2[rootpoc ~]# bash[rootslave2 ~]#2.2安装resis所有节点[rootmaster ~]# yum install -y gcc wget net-tools vim tree[rootmaster ~]# wget -P /data https://download.redis.io/releases/redis-7.0.9.tar.gz[rootmaster ~]# ll /data-rw-r--r-- 1 root root 2984823 Mar 1 2023 redis-7.0.9.tar.gz[rootmaster ~]# cd /data[rootmaster /data]# tar -zxf redis-7.0.9.tar.gz[rootmaster /data]# cd redis-7.0.9[rootmaster /data/redis-7.0.9]# make make install......Hint: Its a good idea to run make test ;)INSTALL redis-serverINSTALL redis-benchmarkINSTALL redis-climake[1]: Leaving directory /data/redis-7.0.9/src[rootmaster /data/redis-7.0.9]# find / -name redis-server/usr/local/bin/redis-server/data/redis-7.0.9/src/redis-server[rootmaster /data/redis-7.0.9]# cp -rf /data/redis-7.0.9/redis.conf /usr/local/bin/[rootmaster /data/redis-7.0.9]# ll /usr/local/bin/redis*-rwxr-xr-x 1 root root 5197776 Sep 15 23:08 /usr/local/bin/redis-benchmarklrwxrwxrwx 1 root root 12 Sep 15 23:08 /usr/local/bin/redis-check-aof - redis-serverlrwxrwxrwx 1 root root 12 Sep 15 23:08 /usr/local/bin/redis-check-rdb - redis-server-rwxr-xr-x 1 root root 5411112 Sep 15 23:08 /usr/local/bin/redis-cli-rw-r--r-- 1 root root 106545 Sep 15 23:12 /usr/local/bin/redis.conflrwxrwxrwx 1 root root 12 Sep 15 23:08 /usr/local/bin/redis-sentinel - redis-server-rwxr-xr-x 1 root root 11428552 Sep 15 23:08 /usr/local/bin/redis-server2.3修改配置文件redis.conf(所有节点)[rootmaster ~]# vim /usr/local/bin/redis.conf# 修改如下信息bind 0.0.0.0protected-mode nodaemonize yes#replicaof 10.9.254.81 6379requirepass 123456masterauth 123456# replicaof 选项Master节点不配置从节点配置[rootmaster ~]# cat /usr/local/bin/redis.conf |grep -v # |grep -v ^;|grep -v ^$bind 0.0.0.0protected-mode noport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize yespidfile /var/run/redis_6379.pidloglevel noticelogfile databases 16always-show-logo noset-proc-title yesproc-title-template {title} {listen-addr} {server-mode}stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbrdb-del-sync-files nodir ./replicaof 10.9.254.81 6379masterauth 123456replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync yesrepl-diskless-sync-delay 5repl-diskless-sync-max-replicas 0repl-diskless-load disabledrepl-disable-tcp-nodelay noreplica-priority 100acllog-max-len 128requirepass 123456lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush nolazyfree-lazy-user-del nolazyfree-lazy-user-flush nooom-score-adj nooom-score-adj-values 0 200 800disable-thp yesappendonly noappendfilename appendonly.aofappenddirname appendonlydirappendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yesaof-timestamp-enabled noslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events hash-max-listpack-entries 512hash-max-listpack-value 64list-max-listpack-size -2list-compress-depth 0set-max-intset-entries 512zset-max-listpack-entries 128zset-max-listpack-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesjemalloc-bg-thread yes2.4启动redis服务并放行端口所有节点[rootmaster ~]# redis-server /usr/local/bin/redis.conf[rootmaster ~]# netstat -tlnp | grep redistcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 17548/redis-server如果开启防火墙需放行6379端口[rootmaster ~]# firewall-cmd --add-port6379/tcp --permanent firewall-cmd --reload2.5登录redis测试master节点[rootmaster ~]# redis-cli -h 10.9.254.81 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.81:6379 pingPONG10.9.254.81:6379 info replication# Replicationrole:masterconnected_slaves:2slave0:ip10.9.254.82,port6379,stateonline,offset14,lag0slave1:ip10.9.254.83,port6379,stateonline,offset14,lag1master_failover_state:no-failovermaster_replid:4ad1b5e0ae06b768bb5ab9427c7e460e08471722master_replid2:0000000000000000000000000000000000000000master_repl_offset:14second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:14slave1[rootslave1 ~]# redis-cli -h 10.9.254.81 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.81:6379 pingPONG10.9.254.81:6379 info replication# Replicationrole:masterconnected_slaves:2slave0:ip10.9.254.82,port6379,stateonline,offset322,lag1slave1:ip10.9.254.83,port6379,stateonline,offset322,lag1master_failover_state:no-failovermaster_replid:4ad1b5e0ae06b768bb5ab9427c7e460e08471722master_replid2:0000000000000000000000000000000000000000master_repl_offset:322second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:322slave2[rootslave2 ~]# redis-cli -h 10.9.254.81 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.81:6379 pingPONG10.9.254.81:6379 info replication# Replicationrole:masterconnected_slaves:2slave0:ip10.9.254.82,port6379,stateonline,offset434,lag0slave1:ip10.9.254.83,port6379,stateonline,offset434,lag0master_failover_state:no-failovermaster_replid:4ad1b5e0ae06b768bb5ab9427c7e460e08471722master_replid2:0000000000000000000000000000000000000000master_repl_offset:434second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:4343.配置哨兵模式3.1退出redis环境所有节点10.9.254.81:6379 quit[rootmaster ~]#3.2配置哨兵模式master节点[rootmaster ~]# vim /usr/local/bin/sentinel.confdaemonize yessentinel monitor master 10.9.254.81 6379 2sentinel auth-pass master 123456logfile /var/log/sentinel.log[rootmaster ~]# scp /usr/local/bin/sentinel.conf root10.9.254.82:/usr/local/bin/sentinel.conf[rootmaster ~]# scp /usr/local/bin/sentinel.conf root10.9.254.83:/usr/local/bin/sentinel.conf3.3开启哨兵模式并放行默认端口26379所有节点[rootmaster ~]# redis-sentinel /usr/local/bin/sentinel.conf[rootmaster ~]# netstat -tlnp | grep 26379tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 1293/redis-sentineltcp6 0 0 :::26379 :::* LISTEN 1293/redis-sentinel如果打开防火墙需放行26379端口[rootmaster ~]# firewall-cmd --add-port26379/tcp --permanent firewall-cmd --reload3.4查看是否配置成功master节点[rootmaster ~]# redis-cli -p 26379 info sentinel# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_tilt_since_seconds:-1sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:namemaster,statusok,address10.9.254.81:6379,slaves2,sentinels3# 发现有3个哨兵节点就OK了4.测试4.1主从复制测试master写[rootmaster ~]# redis-cli -h 10.9.254.81 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.81:6379 pingPONG10.9.254.81:6379 mset name csq age 18 sex manOKslave1读[rootslave1 ~]# redis-cli -h 10.9.254.82 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.82:6379 pingPONG10.9.254.82:6379 mget name age sex1) csq2) 183) manslave2读[rootslave2 ~]# redis-cli -h 10.9.254.83 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.83:6379 pingPONG10.9.254.83:6379 mget name age sex1) csq2) 183) man4.2模拟master宕机4.2.1关闭master服务10.9.254.81:6379 shutdown(0.56s)not connected quit[rootmaster ~]# netstat -ntlpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 1293/redis-sentineltcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 964/sshdtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1044/mastertcp6 0 0 :::26379 :::* LISTEN 1293/redis-sentineltcp6 0 0 :::22 :::* LISTEN 964/sshdtcp6 0 0 ::1:25 :::* LISTEN 1044/master4.2.2哨兵日志查询Sentinel节点通过向Redis实例发送PING命令来监测实例的存活状态。如果主服务器或从服务器无法响应哨兵节点的PING命令哨兵节点会将该实例标记为主观下线。当哨兵节点检测到主服务器下线后它会请求其他哨兵节点对该下线主服务器进行确认。一旦大部分哨兵节点都确认主服务器已下线该主服务器就会被标记为客观下线。哨兵节点会从当前可用的从服务器中选出一个成为新的主服务器并将其他从服务器切换为新的主服务器的从服务器。[rootmaster ~]# vim /var/log/sentinel.log1293:X 16 Sep 2023 00:10:39.515 * Sentinel new configuration saved on disk1293:X 16 Sep 2023 00:26:07.244 # sdown master master 10.9.254.81 6379 #主观认为master节点下线1293:X 16 Sep 2023 00:26:07.328 # odown master master 10.9.254.81 6379 #quorum 2/21293:X 16 Sep 2023 00:26:07.328 # new-epoch 11293:X 16 Sep 2023 00:26:07.328 # try-failover master master 10.9.254.81 63791293:X 16 Sep 2023 00:26:07.362 * Sentinel new configuration saved on disk1293:X 16 Sep 2023 00:26:07.362 # vote-for-leader be070982bb7ad4e47cbcdfed5a1ff4e43a0bb918 1 #投票1293:X 16 Sep 2023 00:26:07.414 # a9f83b17a28beb19870e14aac89646ab5fe4bbc1 voted for be070982bb7ad4e47cbcdfed5a1ff4e43a0bb918 11293:X 16 Sep 2023 00:26:07.414 # 4809f9246034bda7f57e66e22d4d3dda9cfe3ed9 voted for be070982bb7ad4e47cbcdfed5a1ff4e43a0bb918 11293:X 16 Sep 2023 00:26:07.429 # elected-leader master master 10.9.254.81 63791293:X 16 Sep 2023 00:26:07.429 # failover-state-select-slave master master 10.9.254.81 63791293:X 16 Sep 2023 00:26:07.491 # selected-slave slave 10.9.254.83:6379 10.9.254.83 6379 master 10.9.254.81 63791293:X 16 Sep 2023 00:26:07.491 * failover-state-send-slaveof-noone slave 10.9.254.83:6379 10.9.254.83 6379 master 10.9.254.81 63791293:X 16 Sep 2023 00:26:07.568 * failover-state-wait-promotion slave 10.9.254.83:6379 10.9.254.83 6379 master 10.9.254.81 63791293:X 16 Sep 2023 00:26:08.024 * Sentinel new configuration saved on disk1293:X 16 Sep 2023 00:26:08.024 # promoted-slave slave 10.9.254.83:6379 10.9.254.83 6379 master 10.9.254.81 63791293:X 16 Sep 2023 00:26:08.024 # failover-state-reconf-slaves master master 10.9.254.81 63791293:X 16 Sep 2023 00:26:08.081 * slave-reconf-sent slave 10.9.254.82:6379 10.9.254.82 6379 master 10.9.254.81 63791293:X 16 Sep 2023 00:26:08.536 # -odown master master 10.9.254.81 63791293:X 16 Sep 2023 00:26:09.097 * slave-reconf-inprog slave 10.9.254.82:6379 10.9.254.82 6379 master 10.9.254.81 63791293:X 16 Sep 2023 00:26:09.097 * slave-reconf-done slave 10.9.254.82:6379 10.9.254.82 6379 master 10.9.254.81 63791293:X 16 Sep 2023 00:26:09.154 # failover-end master master 10.9.254.81 63791293:X 16 Sep 2023 00:26:09.154 # switch-master master 10.9.254.81 6379 10.9.254.83 6379 #主节点切换为10.9.254.831293:X 16 Sep 2023 00:26:09.154 * slave slave 10.9.254.82:6379 10.9.254.82 6379 master 10.9.254.83 63791293:X 16 Sep 2023 00:26:09.154 * slave slave 10.9.254.81:6379 10.9.254.81 6379 master 10.9.254.83 6379 #新增两个从节点81和821293:X 16 Sep 2023 00:26:09.176 * Sentinel new configuration saved on disk #配置保存到磁盘1293:X 16 Sep 2023 00:26:39.180 # sdown slave 10.9.254.81:6379 10.9.254.81 6379 master 10.9.254.83 6379 #客观认为81下线了4.2.3登录到83redis查看是否变为master[rootslave2 ~]# redis-cli -h 10.9.254.83 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.83:6379 pingPONG10.9.254.83:6379 info replication# Replicationrole:masterconnected_slaves:1slave0:ip10.9.254.82,port6379,stateonline,offset338896,lag0master_failover_state:no-failovermaster_replid:642996d8511488118cb3856ad80e9e061d53564emaster_replid2:4ad1b5e0ae06b768bb5ab9427c7e460e08471722master_repl_offset:339045second_repl_offset:182276repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:281repl_backlog_histlen:3387654.2.4登录82redis查看是否变为了slave[rootslave1 ~]# redis-cli -h 10.9.254.82 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.82:6379 pingPONG10.9.254.82:6379 info replication# Replicationrole:slavemaster_host:10.9.254.83master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_read_repl_offset:364863slave_repl_offset:364863slave_priority:100slave_read_only:1replica_announced:1connected_slaves:0master_failover_state:no-failovermaster_replid:642996d8511488118cb3856ad80e9e061d53564emaster_replid2:4ad1b5e0ae06b768bb5ab9427c7e460e08471722master_repl_offset:364863second_repl_offset:182276repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:267repl_backlog_histlen:3645974.3恢复master4.3.1启动master的redis服务[rootmaster ~]# redis-server /usr/local/bin/redis.conf[rootmaster ~]# netstat -ntlpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1314/redis-server 0tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 1293/redis-sentineltcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 964/sshdtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1044/mastertcp6 0 0 :::26379 :::* LISTEN 1293/redis-sentineltcp6 0 0 :::22 :::* LISTEN 964/sshdtcp6 0 0 ::1:25 :::* LISTEN 1044/master[rootmaster ~]# redis-cli -h 10.9.254.81 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.81:6379 pingPONG10.9.254.81:6379 info replication# Replicationrole:slave #变为了从节点master_host:10.9.254.83master_port:6379master_link_status:up #从节点和主节点连接状态master_last_io_seconds_ago:0master_sync_in_progress:0slave_read_repl_offset:415470slave_repl_offset:415470slave_priority:100slave_read_only:1replica_announced:1connected_slaves:0master_failover_state:no-failovermaster_replid:642996d8511488118cb3856ad80e9e061d53564emaster_replid2:0000000000000000000000000000000000000000master_repl_offset:415470second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:403349repl_backlog_histlen:121224.3.2测试旧master是否可写10.9.254.81:6379 set a b(error) READONLY You cant write against a read only replica. #变为从节点后无法写入4.3.3再次查看master83主从信息[rootslave2 ~]# redis-cli -h 10.9.254.83 -p 6379 -a 123456Warning: Using a password with -a or -u option on the command line interface may not be safe.10.9.254.83:6379 pingPONG10.9.254.83:6379 info replication# Replicationrole:masterconnected_slaves:2 #旧master81已变为从节点slave0:ip10.9.254.82,port6379,stateonline,offset492817,lag1slave1:ip10.9.254.81,port6379,stateonline,offset492952,lag1master_failover_state:no-failovermaster_replid:642996d8511488118cb3856ad80e9e061d53564emaster_replid2:4ad1b5e0ae06b768bb5ab9427c7e460e08471722master_repl_offset:493087second_repl_offset:182276repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:281repl_backlog_histlen:4928075.添加redis守护进程所有节点[rootmaster ~]# vim /lib/systemd/system/redis-server.service[Unit]DescriptionThe redis-server Process ManagerDocumentationhttps://redis.io/Afternetwork.target[Service]Typeforking#根据自己的redis路径启动和停止ExecStart/usr/local/bin/redis-server /usr/local/bin/redis.confExecStop/usr/local/bin/redis-cli -p 6379 -a 123456 shutdown[Install]WantedBymulti-user.target[rootmaster ~]# vim /lib/systemd/system/redis-sentinel.service[Unit]DescriptionThe redis-sentinel Process ManagerDocumentationhttps://redis.io/Afternetwork.target[Service]Typeforking#根据自己的redis路径启动和停止ExecStart/usr/local/bin/redis-sentinel /usr/local/bin/sentinel.confExecStop/usr/local/redis/bin/redis-cli -p 26379 -a 123456 shutdown[Install]WantedBymulti-user.target[rootmaster ~]# systemctl daemon-reload[rootmaster ~]# systemctl enable redis-serverCreated symlink from /etc/systemd/system/multi-user.target.wants/redis-server.service to /usr/lib/systemd/system/redis-server.service.[rootmaster ~]# systemctl enable redis-sentinelCreated symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel.service to /usr/lib/systemd/system/redis-sentinel.service.[rootmaster ~]# systemctl start redis-server[rootmaster ~]# systemctl start redis-sentinel[rootmaster ~]# netstat -ntlpProto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 1502/redis-sentineltcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1490/redis-server 0tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 954/sshdtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1042/mastertcp6 0 0 :::26379 :::* LISTEN 1502/redis-sentineltcp6 0 0 :::22 :::* LISTEN 954/sshdtcp6 0 0 ::1:25 :::* LISTEN 1042/master