美文网首页
Redis入门到高可用-7.Redis Sentinel

Redis入门到高可用-7.Redis Sentinel

作者: 笨鸡 | 来源:发表于2019-09-14 11:16 被阅读0次

1.概要

  • 主从复制高可用?
  • 架构说明
  • 安装配置
  • 客户端连接
  • 实现原理
  • 常见开发运维问题

2.主从复制高可用?

Redis主从复制问题1.png
Redis主从复制问题2.png
Redis主从复制问题3.png
Redis主从复制问题4.png

3.Redis Sentinel架构说明

Redis Sentinel架构1.png
Redis Sentinel架构2.png
Redis Sentinel架构3.png
Redis Sentinel架构4.png
Redis Sentinel架构5.png

4.Redis Sentinel安装与配置

  • 配置开启主从节点
    [root@master ~]# sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf
    [root@master ~]# sed "s/7000/7002/g" redis-7000.conf > redis-7002.conf
    [root@master ~]# ll
    总用量 68
    -rw-------. 1 root root  1290 5月   1 17:12 anaconda-ks.cfg
    -rw-r--r--. 1 root root    92 9月   7 20:48 dump.rdb
    -rw-r--r--. 1 root root  6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm
    -rw-r--r--. 1 root root  6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm.1
    -rw-r--r--. 1 root root   109 9月   7 20:53 redis-7000.conf
    -rw-r--r--. 1 root root   109 9月   7 20:54 redis-7001.conf
    -rw-r--r--. 1 root root   109 9月   7 20:54 redis-7002.conf
    -rw-r--r--. 1 root root 29508 8月   1 23:28 zookeeper.out
    [root@master ~]# echo "replicaof 127.0.0.1 7000" >> redis-7001.conf
    [root@master ~]# echo "replicaof 127.0.0.1 7000" >> redis-7002.conf
    [root@master ~]# cat redis-7001.conf 
    port 7001
    daemonize yes
    pidfile /var/run/redis-7001.pid
    logfile "7001.log"
    dir "/opt/soft/redis/redis/data/"
    replicaof 127.0.0.1 7000
    [root@master ~]# redis-server redis-7000.conf 
    [root@master ~]# redis-server redis-7001.conf
    [root@master ~]# redis-server redis-7002.conf
    [root@master ~]# redis-cli -p 7000 info replication
    # Replication
    role:master
    connected_slaves:2
    slave0:ip=127.0.0.1,port=7001,state=online,offset=42,lag=0
    slave1:ip=127.0.0.1,port=7002,state=online,offset=42,lag=0
    master_replid:ae8f9676aad83a6515e489cc0ed80eb402b75113
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:42
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:42
    
  • 配置开启sentinel监控主节点。(sentinel是特殊的redis)
    [root@master ~]# cat sentinel.conf | grep -v "#" | grep -v "^$" > redis-sentinel-26379.conf
    [root@master ~]# ll
    总用量 84
    -rw-r--r--. 1 root root     0 9月   7 20:58 7000.log
    -rw-r--r--. 1 root root     0 9月   7 20:58 7001.log
    -rw-r--r--. 1 root root     0 9月   7 20:58 7002.log
    -rw-------. 1 root root  1290 5月   1 17:12 anaconda-ks.cfg
    -rw-r--r--. 1 root root    92 9月   7 20:48 dump.rdb
    -rw-r--r--. 1 root root  6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm
    -rw-r--r--. 1 root root  6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm.1
    -rw-r--r--. 1 root root   109 9月   7 20:53 redis-7000.conf
    -rw-r--r--. 1 root root   134 9月   7 20:56 redis-7001.conf
    -rw-r--r--. 1 root root   134 9月   7 20:56 redis-7002.conf
    -rw-r--r--. 1 root root   283 9月   7 21:04 redis-sentinel-26379.conf
    -rw-r--r--. 1 root root  9710 9月   7 21:03 sentinel.conf
    -rw-r--r--. 1 root root 29508 8月   1 23:28 zookeeper.out
    [root@master ~]# vim redis-sentinel-26379.conf 
    [root@master ~]# cat redis-sentinel-26379.conf 
    port 26379
    daemonize yes
    pidfile "/var/run/redis-sentinel-26379.pid"
    logfile "26379.log"
    dir "/opt/soft/redis/redis/data"
    sentinel deny-scripts-reconfig yes
    sentinel monitor mymaster 127.0.0.1 7000 2
    sentinel config-epoch mymaster 0
    sentinel leader-epoch mymaster 0
    # Generated by CONFIG REWRITE
    protected-mode no
    sentinel known-replica mymaster 127.0.0.1 7001
    sentinel known-replica mymaster 127.0.0.1 7002
    sentinel current-epoch 0
    [root@master ~]# sed "s/26379/26380/g" redis-sentinel-26379.conf > redis-sentinel-26380.conf 
    [root@master ~]# sed "s/26379/26381/g" redis-sentinel-26379.conf > redis-sentinel-26381.conf
    [root@master ~]# redis-sentinel redis-sentinel-26379.conf
    [root@master ~]# redis-sentinel redis-sentinel-26380.conf 
    [root@master ~]# redis-sentinel redis-sentinel-26381.conf
    [root@master ~]# redis-cli -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=127.0.0.1:7000,slaves=2,sentinels=3
    [root@master ~]# ps -ef | grep redis-server
    root      1962     1  0 21:34 ?        00:00:00 redis-server *:7000
    root      1969     1  0 21:34 ?        00:00:00 redis-server *:7001
    root      1978     1  0 21:34 ?        00:00:00 redis-server *:7002
    root      2091  1669  0 21:36 pts/0    00:00:00 grep --color=auto redis-server
    [root@master ~]# kill 1962
    [root@master ~]# redis-cli -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=127.0.0.1:7002,slaves=2,sentinels=3
    
  • 实际应该多机器
  • 详细配置节点

5.Java客户端连接sentinel

Java客户端连接sentinel1.png
Java客户端连接sentinel2.png
Java客户端连接sentinel3.png
Java客户端连接sentinel4.png
Java客户端连接sentinel5.png
Java客户端连接sentinel6.png
Java客户端连接sentinel7.png

6.Python连接sentinel

Python连接sentinel.png

7.故障转移演练

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class RedisSentinelTest {

    private static Logger log = LoggerFactory.getLogger(RedisSentinelTest.class);

    public static void main(String[] args) {
        String masterName = "mymaster";
        Set<String> sentinels = new HashSet<String>(){{
            add("127.0.0.1:26379");
            add("127.0.0.1:26380");
            add("127.0.0.1:26381");
        }};
        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels);
        int counter = 0;
        while(true){
            counter++;
            Jedis jedis = null;
            try{
                jedis = jedisSentinelPool.getResource();
                int index = new Random().nextInt(100000);
                String key = "k-" + index;
                String value = "v-" + index;
                jedis.set(key, value);
                if (counter % 100 == 0){
                    log.info("{} value is {}", key, jedis.get(key));
                }
                TimeUnit.MILLISECONDS.sleep(10);
            }catch (Exception e){
                log.error(e.getMessage(), e);
            }finally {
                if(jedis != null){
                    jedis.close();
                }
            }
        }
    }
}

8.三个定时任务

Redis Sentinel三个定时任务1.png
Redis Sentinel三个定时任务2.png
Redis Sentinel三个定时任务3.png
Redis Sentinel三个定时任务4.png

9.主观下线与客观下线

Redis Sentinel主观下线与客观下线.png

10.领导者选举

Redis Sentinel领导者选举1.png
Redis Sentinel领导者选举2.png

11.故障转移(sentinel领导者节点完成)

Redis Sentinel故障转移1.png
Redis Sentinel故障转移2.png

12.Redis Sentinel开发运维问题

  • 节点运维
    • 节点下线和上线

      • 主节点

        Redis节点运维1.png
      • 从节点

      • Sentinel节点

        Redis节点运维2.png
        Redis节点运维3.png
    • 机器下线:例如过保等情况

    • 机器性能不足:例如CPU、内存、硬盘、网络等

    • 节点自身故障:例如服务不稳定等

  • 高可用读写分离
    Redis高可用读写分离1.png
    Redis高可用读写分离2.png
    Redis高可用读写分离3.png

13.总结

Redis Sentinel总结1.png
Redis Sentinel总结2.png
Redis Sentinel总结3.png

相关文章

网友评论

      本文标题:Redis入门到高可用-7.Redis Sentinel

      本文链接:https://www.haomeiwen.com/subject/pzyyyctx.html