Redis Sentinel模式简介
Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案时,假如master宕机了,redis本身(以及其很多客户端)都没有实现自动进行主备切换,而redis-sentinel本身也是独立运行的进程,可以部署在其他与redis集群可通讯的机器中监控redis集群
- 主要功能有一下几点
1、不时地监控redis是否按照预期良好地运行;
2、如果发现某个redis节点运行出现状况,能够通知另外一个进程(例如它的客户端);
3、能够进行自动切换。当一个master节点不可用时,能够选举出master的多个slave(如果有超过一个slave的话)中的一个来作为新的master,其它的slave节点会将它所追随的master的地址改为被提升为master的slave的新地址。
4、哨兵为客户端提供服务发现,客户端链接哨兵,哨兵提供当前master的地址然后提供服务,如果出现切换,也就是master挂了,哨兵会提供客户端一个新地址。
Redis Sentinel 安装
- 环境准备:
192.168.0.106 CentOS6.9
安装Redis
CentOS6 单节点安装Redis Sentinel
- 下载redis
virsh attach-disk kvm-6 /dev/vg_shkvm3/kvm-6-data vdb –driver qemu –mode shareable
wget http://download.redis.io/releases/redis-@H_301_49@3.2@H_301_49@.8.tar.gz -P /usr/local/src
- 解压、指定目录
tar xvf redis-@H_301_49@3.2@H_301_49@.8.tar.gz -C /usr/local/
cd /usr/local/
ln -sv redis-@H_301_49@3.2@H_301_49@.8 redis
- 设置系统内核参数
echo @H_301_49@512 > /proc/sys/net/core/somaxconn
sysctl vm.overcommit_memory=@H_301_49@1
- 编译
yum groupinstall -y "Development Tools"
yum -y install tcl # 安装tcl依赖防止make test报错
解决办法:
1,只用单核运行 make test:
taskset -c 1 sudo make test
2,更改 tests/integration/replication-psync.tcl 文件:
vi tests/integration/replication-psync.tcl
把对应报错的那段代码中的 after后面的数字,从100改成 500。我个人觉得,这个参数貌似是等待的毫秒数。
cd redis && make && make test
- 为多实例redis配置启动环境
mkdir -pv /data/redis-sentinel/{@H_301_49@9000,@H_301_49@9001,@H_301_49@9002}
cp src/{redis-server,redis-sentinel} /data/redis-sentinel/@H_301_49@9000/
cp src/{redis-server,redis-sentinel} /data/redis-sentinel/@H_301_49@9001/
cp src/{redis-server,redis-sentinel} /data/redis-sentinel/@H_301_49@9002/
cp redis.conf sentinel.conf /data/redis-sentinel/@H_301_49@9000/
cp redis.conf sentinel.conf /data/redis-sentinel/@H_301_49@9001/
cp redis.conf sentinel.conf /data/redis-sentinel/@H_301_49@9002/
# vim /data/redis-sentinel/9000/redis.conf
bind @H_301_49@192.168@H_301_49@.0@H_301_49@.106
protected-mode no
port @H_301_49@9000
daemonize yes
appendonly yes
# \cp /data/redis-sentinel/9000/redis.conf /data/redis-sentinel/9001/redis.conf
# \cp /data/redis-sentinel/9000/redis.conf /data/redis-sentinel/9002/redis.conf
sed -i 's/9000/9001/g' /data/redis-sentinel/@H_301_49@9001/redis.conf
# vim /data/redis-sentinel/9001/redis.conf
port @H_301_49@9001
slaveof @H_301_49@192.168@H_301_49@.0@H_301_49@.106 @H_301_49@9000
# vim /data/redis-sentinel/9002/redis.conf
sed -i 's/9000/9002/g' /data/redis-sentinel/@H_301_49@9002/redis.conf
port @H_301_49@9002
slaveof @H_301_49@192.168@H_301_49@.0@H_301_49@.106 @H_301_49@9000
- 啟動三個redis實例
# cd /data/redis-sentinel/9000/
# ./redis-server redis.conf
# cd ../9001/
# ./redis-server redis.conf
# cd ../9002/
# ./redis-server redis.conf
# ps -ef | grep redis
root @H_301_49@4218 @H_301_49@1 @H_301_49@0 @H_301_49@17:@H_301_49@03 ? @H_301_49@00:@H_301_49@00:@H_301_49@00 ./redis-server @H_301_49@192.168@H_301_49@.0@H_301_49@.106:@H_301_49@9000
root @H_301_49@4238 @H_301_49@1 @H_301_49@0 @H_301_49@17:@H_301_49@03 ? @H_301_49@00:@H_301_49@00:@H_301_49@00 ./redis-server @H_301_49@192.168@H_301_49@.0@H_301_49@.106:@H_301_49@9001
root @H_301_49@4252 @H_301_49@1 @H_301_49@0 @H_301_49@17:@H_301_49@03 ? @H_301_49@00:@H_301_49@00:@H_301_49@00 ./redis-server @H_301_49@192.168@H_301_49@.0@H_301_49@.106:@H_301_49@9002
- 查看主从状态
# /usr/local/redis/src/redis-cli -h 192.168.0.106 -p 9000
@H_301_49@192.168@H_301_49@.0@H_301_49@.106:@H_301_49@9000> set name aniu
OK
@H_301_49@192.168@H_301_49@.0@H_301_49@.106:@H_301_49@9000> get name
"aniu"
@H_301_49@192.168@H_301_49@.0@H_301_49@.106:@H_301_49@9000> info replication
# Replication
@H_301_49@192.168@H_301_49@.0@H_301_49@.106:@H_301_49@9000> info replication
# Replication
role:master
connected_slaves:@H_301_49@2
slave0:ip=@H_301_49@192.168@H_301_49@.0@H_301_49@.106,port=@H_301_49@9001,state=online,offset=@H_301_49@1121,lag=@H_301_49@0
slave1:ip=@H_301_49@192.168@H_301_49@.0@H_301_49@.106,port=@H_301_49@9002,lag=@H_301_49@1
master_repl_offset:@H_301_49@1121
repl_backlog_active:@H_301_49@1
repl_backlog_size:@H_301_49@1048576
repl_backlog_first_byte_offset:@H_301_49@2
repl_backlog_histlen:@H_301_49@1120
部署sentinel
- 部署sentinel 1
# cd /data/redis-sentinel/9000
# vim sentinel.conf
protected-mode no
port @H_301_49@29000
daemonize yes
dir /data/redis-sentinel/@H_301_49@9000/logs
sentinel monitor mymaster @H_301_49@192.168@H_301_49@.0@H_301_49@.106 @H_301_49@9000 @H_301_49@2
# mkdir logs
# ./redis-sentinel sentinel.conf
# \cp sentinel.conf ../9001/
# \cp sentinel.conf ../9002/
- 部署sentinel 2
# cd ../9001/
port @H_301_49@29001
dir "/data/redis-sentinel/9001/logs"
# mkdir logs
# ./redis-sentinel sentinel.conf
- 部署sentinel @H_301_49@3
# cd ../9002/
# vim sentinel.conf
port @H_301_49@29002
dir "/data/redis-sentinel/9002/logs"
# mkdir logs
# ./redis-sentinel sentinel.conf
- 验证
# /usr/local/redis/src/redis-cli -h 192.168.0.106 -p 29000
[root@sh-kvm-@H_301_49@3-@H_301_49@6 @H_301_49@9002]# /usr/local/redis/src/redis-cli -h 192.168.0.106 -p 29000
@H_301_49@192.168.@H_301_49@0.@H_301_49@106:@H_301_49@29000> sentinel masters
@H_301_49@1) @H_301_49@1) "name"
@H_301_49@2) "mymaster"
@H_301_49@3) "ip"
@H_301_49@4) "192.168.0.106"
@H_301_49@5) "port"
@H_301_49@6) "9000"
@H_301_49@7) "runid"
@H_301_49@8) "03427a4cd4f22401f55d07952424ba4b31144b57"
@H_301_49@9) "flags"
@H_301_49@10) "master"
@H_301_49@11) "link-pending-commands"
@H_301_49@12) "0"
@H_301_49@13) "link-refcount"
@H_301_49@14) "1"
@H_301_49@15) "last-ping-sent"
@H_301_49@16) "0"
@H_301_49@17) "last-ok-ping-reply"
@H_301_49@18) "542"
@H_301_49@19) "last-ping-reply"
@H_301_49@20) "542"
@H_301_49@21) "down-after-milliseconds"
@H_301_49@22) "30000"
@H_301_49@23) "info-refresh"
@H_301_49@24) "3335"
@H_301_49@25) "role-reported"
@H_301_49@26) "master"
@H_301_49@27) "role-reported-time"
@H_301_49@28) "515500"
@H_301_49@29) "config-epoch"
@H_301_49@30) "0"
@H_301_49@31) "num-slaves"
@H_301_49@32) "2"
@H_301_49@33) "num-other-sentinels"
@H_301_49@34) "0"
@H_301_49@35) "quorum"
@H_301_49@36) "2"
@H_301_49@37) "failover-timeout"
@H_301_49@38) "180000"
@H_301_49@39) "parallel-syncs"
@H_301_49@40) "1"
@H_301_49@192.168.@H_301_49@0.@H_301_49@106:@H_301_49@29000> sentinel slaves mymaster
@H_301_49@1) @H_301_49@1) "name"
@H_301_49@2) "192.168.0.106:9002"
@H_301_49@3) "ip"
@H_301_49@4) "192.168.0.106"
@H_301_49@5) "port"
@H_301_49@6) "9002"
@H_301_49@7) "runid"
@H_301_49@8) "42ec153d250bf9aff05765346e0fdc2ee33d1257"
@H_301_49@9) "flags"
@H_301_49@10) "slave"
@H_301_49@11) "link-pending-commands"
@H_301_49@12) "0"
@H_301_49@13) "link-refcount"
@H_301_49@14) "1"
@H_301_49@15) "last-ping-sent"
@H_301_49@16) "0"
@H_301_49@17) "last-ok-ping-reply"
@H_301_49@18) "510"
@H_301_49@19) "last-ping-reply"
@H_301_49@20) "510"
@H_301_49@21) "down-after-milliseconds"
@H_301_49@22) "30000"
@H_301_49@23) "info-refresh"
@H_301_49@24) "6271"
@H_301_49@25) "role-reported"
@H_301_49@26) "slave"
@H_301_49@27) "role-reported-time"
@H_301_49@28) "548469"
@H_301_49@29) "master-link-down-time"
@H_301_49@30) "0"
@H_301_49@31) "master-link-status"
@H_301_49@32) "ok"
@H_301_49@33) "master-host"
@H_301_49@34) "192.168.0.106"
@H_301_49@35) "master-port"
@H_301_49@36) "9000"
@H_301_49@37) "slave-priority"
@H_301_49@38) "100"
@H_301_49@39) "slave-repl-offset"
@H_301_49@40) "57920"
@H_301_49@2) @H_301_49@1) "name"
@H_301_49@2) "192.168.0.106:9001"
@H_301_49@3) "ip"
@H_301_49@4) "192.168.0.106"
@H_301_49@5) "port"
@H_301_49@6) "9001"
@H_301_49@7) "runid"
@H_301_49@8) "afdc283bae3f48fb0ceb6f8dd9af8248928406ec"
@H_301_49@9) "flags"
@H_301_49@10) "slave"
@H_301_49@11) "link-pending-commands"
@H_301_49@12) "0"
@H_301_49@13) "link-refcount"
@H_301_49@14) "1"
@H_301_49@15) "last-ping-sent"
@H_301_49@16) "0"
@H_301_49@17) "last-ok-ping-reply"
@H_301_49@18) "510"
@H_301_49@19) "last-ping-reply"
@H_301_49@20) "510"
@H_301_49@21) "down-after-milliseconds"
@H_301_49@22) "30000"
@H_301_49@23) "info-refresh"
@H_301_49@24) "6271"
@H_301_49@25) "role-reported"
@H_301_49@26) "slave"
@H_301_49@27) "role-reported-time"
@H_301_49@28) "548486"
@H_301_49@29) "master-link-down-time"
@H_301_49@30) "0"
@H_301_49@31) "master-link-status"
@H_301_49@32) "ok"
@H_301_49@33) "master-host"
@H_301_49@34) "192.168.0.106"
@H_301_49@35) "master-port"
@H_301_49@36) "9000"
@H_301_49@37) "slave-priority"
@H_301_49@38) "100"
@H_301_49@39) "slave-repl-offset"
@H_301_49@40) "57920"
jedis中使用哨兵
Redis常用安全设置
- 设置密码
requirepass yourpassword
- 禁用高危命令
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command KEYS ""