zoukankan      html  css  js  c++  java
  • redis--主从同步,故障切换,集群搭建

    一 . redis主从同步

      准备三个配置文件,实现一主两从的redis数据库结构(这三个配置文件仅仅端口不一样)

    复制代码
    # redis-6379.conf 文件, 写入下面数据:
    
    port 6379
    daemonize yes
    pidfile /data/6379/redis.pid
    loglevel notice
    logfile "/data/6379/redis.log"
    dbfilename dump.rdb
    dir /data/6379
    protected-mode no
    复制代码

      下面两个文件直接替换就可以

    sed  "s/6379/6380/g"  redis-6379.conf  >  redis-6380.conf
    sed  "s/6379/6381/g"  redis-6379.conf  >  redis-6381.conf
    
    # 把6379配置成主库,6380和6381配置成从库,  需要在6380和6381的conf文件下写入:
    slaveof  127.0.0.1  6379  

      分别启动redis数据库

    redis-server redis-6379.conf 
    redis-server redis-6380.conf 
    redis-server redis-6381.conf

      通过命令查看数据库的身份信息

    127.0.0.1:6379> info  Replication     # 身份是master
    127.0.0.1:6380> info  Replication     # 身份是slave    
    127.0.0.1:6381> info  Replication     # 身份是slave

      测试: 在主库中写入数据,然后咋从库中查看,如果同步即为正常

    二 . 故障切换

      手动进行主从故障切换

    # 演示: 
    1. 手动检查进程, 杀死主库. (6379)
    2.手动切换到其中的一个从库(6380),去掉conf文件中的slave身份
    3.切换到另一个从库(6381), 把conf文件中的slave指向改成 slaveof  127.0.0.1  6380

      自动主从切换, 应用哨兵

    # 哨兵(redis-sentinel)集群自动切换原理简述:
    给数据库配上几个哨兵,让他们监控着数据库, 不时的去问主, 从数据库时候还活着,如果主库在规定时间内没有回回复哨兵信息,证明挂了,
      哨兵会告诉其他的哨兵主库挂啦,然后其他的哨兵去验证一下,如果发现真的挂了,
        那么哨兵会在从库中选举出一个新的主库,然后把其他的从库slaveof 执行这个新的主库.

        1. 环境配置(准备三个redis配置文件)

    复制代码
    # redis-6379.conf   
    port 6379 
    daemonize yes
    logfile "6379.log"
    dbfilename "dump-6379.rdb"
    dir "/opt/redis/data/"
    
    # 另外两个如下
    sed "s/6379/6380/g" redis-6379.conf  > redis-6380.conf 
    sed "s/6379/6381/g" redis-6379.conf  > redis-6381.conf 
    # 然后分别在这两个文件下写上:
    slaveof  127.0.0.1  6379
    复制代码

        2. 分别启动三个redis

    redis-server redis-6379.conf 
    redis-server redis-6380.conf 
    redis-server redis-6381.conf 

        3. 分别查看他们的身份信息

    redis-cli  -p  6379  info  Replication     # master 主库
    redis-cli  -p  6380  info  Replication     # slave  从库
    redis-cli  -p  6380  info  Replication     # slave  从库

        4. 准备三个哨兵(就是三个值班的,检测redis状态)

    复制代码
    # redis-26379.conf   写入下面数据:
    port 26379  
    dir /var/redis/data/
    logfile "26379.log"
    
    #  当前Sentinel节点监控 127.0.0.1:6379 这个主节点
    # 2代表判断主节点失败至少需要2个Sentinel节点节点同意,少数服从多数 
    # s18ms是主节点的别名
    sentinel monitor s18ms 127.0.0.1 6379 2
    
    # 每个Sentinel节点都要定期PING命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过30000毫秒30s且没有回复,则判定不可达
    sentinel down-after-milliseconds s18ms 30000
    
    # 当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,
    # 原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1
    sentinel parallel-syncs s18ms 1
    
    # 故障转移超时时间为180000毫秒
    sentinel failover-timeout s18ms 180000
    daemonize yes
    
    # 另外两个配置信息也只有端口不同
    复制代码

        5.分别启动三个哨兵

    [root@localhost s18msredis]# redis-sentinel redis-26379.conf 
    [root@localhost s18msredis]# redis-sentinel redis-26380.conf 
    [root@localhost s18msredis]# redis-sentinel redis-26381.conf

        6.查看哨兵身份信息

    [root@localhost s18msredis]# redis-cli -p 26379 info sentinel
    [root@localhost s18msredis]# redis-cli -p 26380 info sentinel
    [root@localhost s18msredis]# redis-cli -p 26381 info sentinel

        7.测试

    复制代码
    # 这三个哨兵检测这一主两从
    1.干掉(主库)6379,查看6380和6381这两个的身份信息
    2.发现其中一个从库(如:6380)变成了主库, 另一个从库变成了6380的从库   # 这个选主库是随机的.
    3.当6379复活后,会自动变成6380的从库
    
    # 原理其实就是把6380里面的slave移除啦,然后把6381的slaveof指向了6381, 6379复活后slaveof也指向了6381
    复制代码

    三 . redis-cluster的集群搭建

        

    集群搭建的作用其实就是把一个很大的数据切片分摊, 就好比1吨重物放到一个马车上,马会拉不动的,
      但是分开放到6辆马车上,每辆马车分得重量就会变小,这样马就能拉动啦!

      redis-cluster 虚拟槽分区

    复制代码
    虚拟槽分区巧妙地使用了哈希空间,使用分散度良好的哈希函数把所有的数据映射到一个固定范围内的整数集合,整数定义为槽(slot)。
    
    Redis Cluster槽的范围是0 ~ 16383。
    
    槽是集群内数据管理和迁移的基本单位。采用大范围的槽的主要目的是为了方便数据的拆分和集群的扩展,
    
    每个节点负责一定数量的槽。
    复制代码

      

      搭建redis-cluster

    准备节点(服务端)
    服务端运输数据,分配16384个槽位,管理数据
    ruby脚本自动帮你分配槽位

      环境配置

    复制代码
    # 创建redis-7000.conf  文件并写入如下数据:
    port 7000
    daemonize yes
    dir "/opt/redis/data"
    logfile "7000.log"
    dbfilename "dump-7000.rdb"
    cluster-enabled yes   #开启集群模式
    cluster-config-file nodes-7000.conf  #集群内部的配置文件
    cluster-require-full-coverage no  #redis cluster需要16384个slot都正常的时候才能对外提供服务,
                          换句话说,只要任何一个slot异常那么整个cluster不对外提供服务。 因此生产环境一般为no # 我们一共需要6个节点,也就是需要6个配置文件,3主3从,最少6个才能保证高可用,这6个配置文件仅仅是端口不同
    复制代码

      其他个配置文件

    复制代码
    sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf
    
    redis-7001.conf 
    redis-7002.conf 
    redis-7003.conf 
    redis-7004.conf 
    redis-7005.conf   
    复制代码

      分别启动6个redis数据库

    复制代码
    [root@localhost s18cluster]# redis-server redis-7000.conf 
    [root@localhost s18cluster]# redis-server redis-7001.conf 
    [root@localhost s18cluster]# redis-server redis-7002.conf 
    [root@localhost s18cluster]# redis-server redis-7003.conf 
    [root@localhost s18cluster]# redis-server redis-7004.conf 
    [root@localhost s18cluster]# redis-server redis-7005.conf
    复制代码

      把槽分配给马车,这样马车才能装数据,ruby自动分配

    复制代码
    #安装准备ruby语言的环境,用于自动化创建redis集群
    1.下载ruby
    wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
    2.解压缩
    tar -zxvf ruby-2.3.1.tar.gz
    ./configure --prefix=/opt/ruby/
    3.安装
    make && make install
    复制代码

      配置ruby的path,就是找到ruby的bin目录的路径,然后添加到/etc/profile中的PATH中去

      通过ruby的软件包管理工具安装redis模块 (gem就是类似于python的pip工具)

    wget http://rubygems.org/downloads/redis-3.3.0.gem

      通过gem安装这个redis包

    gem install -l redis-3.3.0.gem

      找到ruby创建redis集群的脚本工具

    [root@localhost s18cluster]# find /opt  -name redis-trib.rb
    /opt/redis-4.0.10/src/redis-trib.rb

      此时就可以通过绝对路径ruby来创建redis的集群工具,进行槽位分配(开启所有集群)

    /opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 
      127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 # --replicas 1 表示每个主节点有一个从节点

      查看集群状态

    复制代码
    redis-cli -p 7000 cluster info 
    redis-cli -p 7000 cluster nodes  #等同于查看nodes-7000.conf文件节点信息
    
    集群主节点状态
    redis-cli -p 7000 cluster nodes | grep master
    集群从节点状态
    redis-cli -p 7000 cluster nodes | grep slave
    复制代码

      集群已经OK,开始测试

    redis-cli -p 7000  -c    # -c 开启集群模式
    !!! 当你在其中一个节点中,创建redis的key,只要redis的key经过了重定向,分配到不同的节点中,代表集群搭建ok
         重定向之后, 只要在集群里有数据 无论在哪个port里面,都可以get出来,比如set name attila 被分配到7001  你登录7000也能get到name
  • 相关阅读:
    112、TensorFlow初始化变量
    111、TensorFlow 初始化变量
    110、TensorFlow张量值的计算
    109、TensorFlow计算张量的值
    108、TensorFlow 类型转换
    107、TensorFlow变量(三)
    106、TensorFlow变量 (二) reshape
    105、TensorFlow的变量(一)
    104、Tensorflow 的变量重用
    103、Linux 编译 Kaldi 语音识别工具
  • 原文地址:https://www.cnblogs.com/uiys/p/10673583.html
Copyright © 2011-2022 走看看