redis数据库之--sentinel哨兵模式搭建

本文详细介绍了在三台CentOS7.5服务器上部署Redis哨兵模式的全过程,包括环境搭建、哨兵模式配置、启动脚本、停止脚本的编写,以及通过哨兵命令进行状态查询和故障转移的操作方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、环境
三台CentOS7.5服务器(每台服务器两个实例,以一台服务器为例,另外两台服务器配置相同,安装相同)
192.168.10.176 主
192.168.10.177 从
192.168.10.178 从
二、哨兵模式(sentinel)
1、下载redis安装包,创建包存放目录

mkdir /tools
mkdir /depend
cd /tools
wget http://download.redis.io/releases/redis-3.2.12.tar.gz
tar xzf redis-3.2.12.tar.gz -C /depend/

2、安装redis配置环境变量

cd /depend/redis-3.2.12/
make -j 8 && make install PREFIX=/opt/redis
vim /etc/profile
# redis
export DB_HOME=/opt/redis/bin
export PATH=$PATH:$DB_HOME
source /etc/profile

3、安装成功,创建目录拷贝配置文件

[root@localhost conf]# mkdir -pv /opt/redis/data/6379
[root@localhost conf]# mkdir -pv /opt/redis/data/sentinel_6479
[root@localhost conf]# mkdir /opt/redis/logs
[root@localhost conf]# mkdir /opt/redis/conf
[root@localhost conf]# mkdir /opt/redis/pid
[root@localhost conf]# mkdir /opt/redis/scripts

# 回到解压目录拷贝配置文件到redis目录
[root@localhost redis-3.2.12]# cp redis.conf  sentinel.conf /opt/redis/conf/
[root@localhost redis-3.2.12]# cd /opt/redis/conf/
[root@localhost conf]# mv redis.conf redis_6379.conf
[root@localhost conf]# mv sentinel.conf sentinel_6479.conf

# 主配置文件
[root@localhost conf]# cat redis_6379.conf
protected-mode yes
bind 192.168.10.176 127.0.0.1
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/opt/redis/pid/redis_6379.pid"
loglevel notice
logfile "/opt/redis/logs/6379.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/opt/redis/data/6379"
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
maxmemory 16gb
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
maxclients 4064

[root@localhost conf]# cat sentinel_6479.conf
port 6479
pidfile "/opt/redis/pid"
dir "/opt/redis/data/sentinel_6479"
daemonize yes
protected-mode no
logfile "/opt/redis/logs/sentinel_6479.log"
sentinel monitor mymaster 192.168.10.176 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

# 从配置文件
[root@localhost redis]# cat conf/redis_6379.conf
protected-mode yes
bind 192.168.10.177 127.0.0.1
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/opt/redis/pid/redis_6379.pid"
loglevel notice
logfile "/opt/redis/logs/6379.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/opt/redis/data/6379"
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slaveof 192.168.10.176 6379 # 跟主配置文件多了一行
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
maxmemory 16gb
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
maxclients 4064

4、启动并查看redis

nohup redis-server conf/redis-6379.conf
nohup redis-server conf/redis-6479.conf
ps aux|grep redis
root     17090  0.0  0.0 136976  7508 ?        Ssl  10:54   0:00 redis-server 192.168.10.176:6379
root     17181  0.0  0.0 136976  7536 ?        Ssl  10:56   0:00 redis-sentinel *:6479 [sentinel]

5、验证redis

[root@localhost redis]# redis-cli -c -p 6379 info | egrep "role|slave*"
role:master
connected_slaves:2
slave0:ip=192.168.10.178,port=6379,state=online,offset=684549,lag=1
slave1:ip=192.168.10.177,port=6379,state=online,offset=684691,lag=0

[root@localhost redis]# redis-cli -c -p 6379 
127.0.0.1:6379> set name test
OK
[root@localhost redis]# redis-cli -c -h 192.168.10.178 -p 6379 
192.168.10.178:6379> get name
"test"

6、启动脚本及停止脚本

# 启动脚本
[root@localhost scripts]# cat start.sh
#!/bin/bash
read -p "plese [redis|sentinel]: " redis
if [ $redis = redis ];then
  nohup redis-server /opt/redis/conf/redis-6379.conf
fi
if [ $redis = sentinel ];then
  nohup redis-sentinel /opt/redis/conf/sentinel_6479.conf
fi
if [ $redis = all ];then
 nohup redis-server /opt/redis/conf/redis-6379.conf
 nohup redis-sentinel /opt/redis/conf/sentinel_6479.conf
fi

# 停止脚本
[root@localhost scripts]# cat 6379_stop.exp 
#!/usr/bin/expect
set user "root"
set host [lindex $argv 0]
set timeout 1000
set password "test@123"
set db_pass "test@123"
spawn ssh $user@192.168.10.$host
expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "root@192.168*" {send "$password\r"; exp_continue}
  "*]*" {send "redis-cli -c -h 192.168.10.$host -p 6379\r"}
}
expect "192.168*>" {send "auth $db_pass\r"}
expect "192.168*>" {send "shutdown\r"}
expect "not*" {send "quit\r"}
expect eof

[root@localhost scripts]# cat 6479_stop.exp 
#!/usr/bin/expect
set user "root"
set host [lindex $argv 0]
set timeout 1000
set password "test@123"
set db_pass "test@123"
spawn ssh $user@192.168.10.$host
expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "root@192.168*" {send "$password\r"; exp_continue}
  "*]*" {send "redis-cli -c -h 192.168.10.$host -p 6479\r"}
}
expect "192.168*>" {send "auth $db_pass\r"}
expect "192.168*>" {send "shutdown\r"}
expect "not*" {send "quit\r"}
expect eof

[root@localhost scripts]# cat all_stop.exp 
#!/usr/bin/expect
set user "root"
set host [lindex $argv 0]
set port1 [lindex $argv 1]
set port2 [lindex $argv 2]
set timeout 1000
set password "test@123"
set db_pass "test@123"
spawn ssh $user@192.168.10.$host
expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "root@192.168*" {send "$password\r"; exp_continue}
  "*]*" {send "redis-cli -c -h 192.168.10.$host -p $port1\r"; exp_continue}
  "192.168*>" {send "auth $db_pass\r"}
  #"192.168*>" {send "shutdown\r"; exp_continue}
  #"not*" {send "quit\r"}
}
expect "192.168*>" {send "shutdown\r"}
expect "not*" {send "quit\r"}
expect { 
  "*]*" {send "redis-cli -c -h 192.168.10.$host -p $port2\r"; exp_continue}
  "192.168.10*>" {send "auth $db_pass\r"}
  #"192.168.10.$host:6479>" { send "auth $db_pass\r"}
#  "192.168.10.$host:6479>" { send "shutdown\r"; exp_continue}
#  "not*" {send "quit\r"}
}
expect "192.168.10.$host:6479>" { send "shutdown\r"}
expect "not*" {send "quit\r"}
expect eof

[root@localhost scripts]# cat shutdown.sh 
#!/bin/bash
read -p "plese [6379|6479|all]: " redis
if [ $redis = 6379 ];then
  ./6379_stop.exp 176
fi 
if [ $redis = 6479 ];then
  ./6479_stop.exp 176
fi
if [ $redis = all ];then
  ./all_stop.exp 176 6379 6479
fi

7、sentinel命令详解

		①INFO 
			sentinel的基本状态信息 
		②SENTINEL masters 
			列出所有被监视的主服务器,以及这些主服务器的当前状态 
		③ SENTINEL slaves 
			列出给定主服务器的所有从服务器,以及这些从服务器的当前状态 
		④SENTINEL get-master-addr-by-name 
			返回给定名字的主服务器的 IP 地址和端口号 
		⑤SENTINEL reset 
			重置所有名字和给定模式 pattern 相匹配的主服务器。
			重置操作清除主服务器目前的所有状态, 包括正在执行中的故障转移, 
			并移除目前已经发现和关联的, 主服务器的所有从服务器和 Sentinel 。 
		⑥SENTINEL failover 
			当主服务器失效时, 在不询问其他 Sentinel 意见的情况下, 
			强制开始一次自动故障迁移,但是它会给其他sentinel发送一个最新的配置,
			其他sentinel会根据这个配置进行更新
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值