redis sentinel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis sentinel相关的知识,希望对你有一定的参考价值。
一、环境
centos7:
vm1:192.168.122.21:6379(redis主)
vm2:192.168.122.22:6379(redis从)
vm3:192.168.122.23:6379(redis从)
vm1:192.168.122.21:26379(sentinel)
vm2:192.168.122.22:26379(sentinel)
vm3:192.168.122.23:26379(sentinel)
软件:
wget http://download.redis.io/releases/redis-4.0.6.tar.gz
二、搭建redis主从复制
vm1+vm2+vm3:
yum -y install tcl gcc automake autoconf libtool make wget
tar xf redis-4.0.8.tar.gz -C /usr/local/
cd /usr/local/redis-4.0.8 && make MALLOC=libc && make test
mkdir /usr/local/redis
cd /usr/local/redis-4.0.8/src
cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis- sentinel redis-server /usr/local/redis/
cat > /etc/profile.d/redis.sh << ‘HERE‘
export PATH=$PATH:/usr/local/redis
HERE
source /etc/profile.d/redis.sh
mkdir -p /redisdir/{data,log,run}
mkdir /etc/redis
cat > /usr//lib/systemd/system/redis.service << ‘HERE‘
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
#ExecStart=/usr/local/redis/redis-server /etc/redis/redis.conf --protected- mode no
ExecStart=/usr/local/redis/redis-server /etc/redis/redis.conf
ExecReload=/usr/local/redis/redis-server -s reload
ExecStop=/usr/local/redis/redis-server -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
HERE
vm1主:
vim /etc/redis/redis.conf
daemonize yes
pidfile /redisdir/run/redis.pid
logfile /redisdir/log/redis.log
dir /redisdir/data
vm2从+vm3从:
daemonize yes
pidfile "/redisdir/run/redis.pid"
logfile "/redisdir/log/redis.log"
dir /redisdir/data
slaveof 192.168.122.21 6379
验证:
redis-cli -h 192.168.122.21 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.122.22,port=6379,state=online,offset=70,lag=1
slave1:ip=192.168.122.23,port=6379,state=online,offset=70,lag=1
master_replid:4e14c7fb04173df1522ad412748dd022ecb0e9e7
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
主从已经搭建完成,但是我们在代码里面需要指定固定的redis的IP,如果主出现故障,从出现故障或者增加新的redis 从节点,代码需要变化,就很麻烦,这时出现了redis sentinel,代码只需要指向高可用的redis sentinel,获取当前有效的主从ip。
三、redis sentinel
vm1+vm2+vm3(一样)
vim /etc/redis/sentinel.conf
#
protected-mode no
port 26379
dir "/redisdir/data"
sentinel monitor mymaster 192.168.122.22 6379 2
pidfile "/redisdir/run/redis-sentinel.pid"
logfile "/redisdir/log/redis.log"
cat /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/redis-server /etc/redis/redis.conf
ExecReload=/usr/local/redis/redis-server -s reload
ExecStop=/usr/local/redis/redis-server -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动redis-sentinel并查看集群信息
systemctl start redis-sentinel
redis-cli -h 192.168.122.21 -p 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=192.168.122.21:6379,slaves=2,sentinels=3
四、模拟实验(把vm1的redis关掉,查看master是否改变)
systemctl stop redis
redis-cli -h 192.168.122.21 -p 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=192.168.122.22:6379,slaves=2,sentinels=3
已经变成192.168.122.22了,成功。
以上是关于redis sentinel的主要内容,如果未能解决你的问题,请参考以下文章
Redis Sentinel 源码分析 - Sentinel 选举机制和主备倒换
Spring整合redis,通过sentinel进行主从切换