Redis高可用升级
前言:
缓存对应实时性要求比较高的业务需求可谓十分重要,但缓存中的实时数据一旦丢失,将直接影响整个业务功能。考虑到各场景的业务需求,redis推出了高可用的方案,即:主从+哨兵的HA机制。
准备
"redis-4.0.6.tar.gz" 下载地址(https://redis.io/download )
$ tar -zxvf redis-4.0.6.tar.gz
$ ln -s redis-4.0.6 redis
1. 背景
升级之前:
单实例 Jedis Instance
-------------------------------------
HA升级方案:
2. 实现步骤
step1:主节点test-zk02增加如下服务启动:
test-zk02 (master)
src/redis-sentinel.conf
配置文件的内容见“附1”
test-zk03 (slave)
src/redis-server redis6379.conf
复制主节点的配置后在末尾加上一行"slaveof test-zk02 6379"即可
step2:代码层修改如下:
Set<String> sentinels = new HashSet<String>(); sentinels.add("192.168.10.80:26379"); JedisSentinelPool jedisPool = new JedisSentinelPool("mymaster", sentinels);
3. 注意
1).服务升级用户为root
2).配置哨兵日志 sentinel26379.log
3).修改代码层之后需及时同步redis配置
4).升级HA后观察连接数变动 " echo 'info clients' | /opt/redis-cli -h test-zk02 -p 6379"
附1
-------------------------------
sentinel.conf配置内容
bind 127.0.0.1 test-zk02
protected-mode no
daemonize yes
logfile "/opt/redis/s26379.log"
port 26379
dir /tmp
sentinel monitor test-master test-zk02 6379 1
sentinel down-after-milliseconds test-master 30000
sentinel parallel-syncs test-master 1
sentinel failover-timeout test-master 180000
附2
-------------------------------
主从切换调试命令
/opt/redis-cli -h localhost -p 9998 debug sleep 20
主从切换日志
55794:X 01 Jun 15:25:55.813 # +sdown master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.813 # +odown master test-master 192.168.10.80 9999 #quorum 1/1
55794:X 01 Jun 15:25:55.813 # +new-epoch 3
55794:X 01 Jun 15:25:55.813 # +try-failover master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.828 # +vote-for-leader 35e9cce79ec5d7a2f1cd9473d089166b533c4690 3
55794:X 01 Jun 15:25:55.828 # +elected-leader master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.828 # +failover-state-select-slave master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.886 # +selected-slave slave 192.168.10.80:9998 192.168.10.80 9998 @ test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.886 * +failover-state-send-slaveof-noone slave 192.168.10.80:9998 192.168.10.80 9998 @ test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:55.945 * +failover-state-wait-promotion slave 192.168.10.80:9998 192.168.10.80 9998 @ test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:56.894 # +promoted-slave slave 192.168.10.80:9998 192.168.10.80 9998 @ test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:56.894 # +failover-state-reconf-slaves master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:56.969 # +failover-end master test-master 192.168.10.80 9999
55794:X 01 Jun 15:25:56.969 # +switch-master test-master 192.168.10.80 9999 192.168.10.80 9998
55794:X 01 Jun 15:25:56.969 * +slave slave 192.168.10.80:9999 192.168.10.80 9999 @ test-master 192.168.10.80 9998
55794:X 01 Jun 15:26:25.531 * +convert-to-slave slave 192.168.10.80:9999 192.168.10.80 9999 @ test-master 192.168.10.80 9998
参考资料
https://redis.io/topics/sentinel