欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Redis哨兵主从配置(半自动)

程序员文章站 2022-04-07 18:07:14
方便起见我们在一台机器上搭建,规划如下表:角色IP端口号master127.0.0.16380slave-1127.0.0.16381slave-2127.0.0.16382sentinel-1127.0.0.126380sentinel-2127.0.0.126381sentinel-3127.0.0.1263821、安装Redis参考:Redis单机版半自动安装2、自定义配置2.1、master配置# rm /d...

方便起见我们在一台机器上搭建,规划如下表:

角色 IP 端口号
master 127.0.0.1 6380
slave-1 127.0.0.1 6381
slave-2 127.0.0.1 6382
sentinel-1 127.0.0.1 26380
sentinel-2 127.0.0.1 26381
sentinel-3 127.0.0.1 26382

1、安装Redis

参考:Redis单机版半自动安装

2、自定义配置

2.1、master配置

# rm /data/redis/sentinel -fr
port=6380
# 过滤掉redis.conf中的注释和空行
# cat redis.conf | grep -v "#" | grep -v "^$" >/data/redis-$port/conf/redis.cnf
basepath="/data/redis/sentinel"
datapath="$basepath/$port/data"
conffile="$basepath/$port/conf/redis$port.conf"
logpath="$basepath/$port/logs"
mkdir $basepath/$port/{conf,logs,data} -p
#redis.conf具体配置
echo "#generated by echo" > $conffile
echo "port $port" >> $conffile
echo "daemonize yes" >> $conffile
echo "logfile $logpath/redis$port.log" >> $conffile
echo "save 10 3" >> $conffile
echo "stop-writes-on-bgsave-error yes"   >> $conffile
echo "rdbcompression yes">> $conffile
echo "rdbchecksum yes"   >> $conffile
echo "dbfilename dump$port.rdb"   >> $conffile
echo "dir $datapath" >> $conffile
echo "appendonly yes">> $conffile
echo "appendfilename appendonly$port.aof"   >> $conffile
echo "appendfsync everysec"  >> $conffile

2.2、启动/停止master

port=6380
basepath="/data/redis/sentinel"
redis-server $basepath/$port/conf/redis$port.conf
redis-cli -p $port ping
# 停止
port=6380
redis-cli -p $port shutdown

2.3、slave配置

slave1 和slave2都执行下面的脚本,就是需要将port修改一下

# rm /data/redis/sentinel -fr
port=6382
masterport=6380
masterip=127.0.0.1
# 过滤掉redis.conf中的注释和空行
# cat redis.conf | grep -v "#" | grep -v "^$" >/data/redis-$port/conf/redis.cnf
basedir="/data/redis/sentinel/$port"
datadir="$basedir/data"
conffile="$basedir/conf/redis$port.conf"
logpath="$basedir/logs"
mkdir $basedir/{conf,logs,data} -p
#redis.conf具体配置
echo "#generated by echo" > $conffile
echo "port $port" >> $conffile
echo "daemonize yes" >> $conffile
echo "logfile $logpath/redis$port.log" >> $conffile
echo "save 10 3" >> $conffile
echo "stop-writes-on-bgsave-error yes"   >> $conffile
echo "rdbcompression yes">> $conffile
echo "rdbchecksum yes"   >> $conffile
echo "dbfilename dump$port.rdb"   >> $conffile
echo "dir $datadir" >> $conffile
echo "appendonly yes">> $conffile
echo "appendfilename appendonly$port.aof"   >> $conffile
echo "appendfsync everysec"  >> $conffile
echo "slaveof $masterip $masterport" >> $conffile

2.4、启动/停止slave1/slave2

# 启动slave1
basepath="/data/redis/sentinel"
port=6381
redis-server $basepath/$port/conf/redis$port.conf
redis-cli -p $port ping

# 启动slave2
basepath="/data/redis/sentinel"
port=6382
redis-server $basepath/$port/conf/redis$port.conf
redis-cli -p $port ping

# 停止
port=6381
redis-cli -p $port shutdown
port=6382
redis-cli -p $port shutdown

3、验证主从

redis-cli -p 6380
set k1 v1

# 看看slave1上是否有了k1
redis-cli -p 6381
keys * 

# 看看slave2上是否有了k1
redis-cli -p 6382
keys *

# 使用info 或者 info replication 查看
info replication 

4、配置sentinel.conf

3个sentinel都执行下面的脚本,同样需要将port修改一下

port=26380
masterport=6380
masterip=127.0.0.1
dirpath="/data/redis/sentinel"
sentinelfile="$dirpath/sentinel$port.conf"
mkdir $dirpath -p
echo "#generated by echo" > $sentinelfile
echo "port $port"  >> $sentinelfile
echo "dir $dirpath"  >> $sentinelfile
echo "sentinel monitor mymaster $masterip $masterport 2"  >> $sentinelfile
# master(slave、sentinel)在30000内都无法访问,则认定为S_DOWN 
echo "sentinel down-after-milliseconds mymaster 30000"  >> $sentinelfile
# 故障转移时,同时连接新master的slave数量,为了避免同时从master同步数据导致在同一时间不可访问,尽量配置小点
echo "sentinel parallel-syncs mymaster 1"  >> $sentinelfile
echo "sentinel failover-timeout mymaster 180000"  >> $sentinelfile

5、启动sentinel

port=26380
dirpath="/data/redis/sentinel"
sentinelfile="$dirpath/sentinel$port.conf"
redis-sentinel $sentinelfile

port=26381
dirpath="/data/redis/sentinel"
sentinelfile="$dirpath/sentinel$port.conf"
redis-sentinel $sentinelfile

port=26382
dirpath="/data/redis/sentinel"
sentinelfile="$dirpath/sentinel$port.conf"
redis-sentinel $sentinelfile

6、验证故障转移

# 停止master
redis-cli -p 6380 shutdown
# 过30秒后查看 两个slave:info replication
info replication
# 重启master
port=6380
redis-server $basepath/$port/conf/redis$port.conf
# 查看老的master现在的角色是否是slave
info replication

本文地址:https://blog.csdn.net/dingshuo168/article/details/109631762

相关标签: Redis