• Redis主从复制、哨兵模式


    1.部署主从

    环境:主IP:10.0.0.15,端口6379;从IP:10.0.0.16,端口6379.

    原理:基于RDB持久化的功能来实现主从复制的功能.

    a.linux-redis1(10.0.0.15)

    cd /usr/local/redis/
    grep "^[a-Z]" redis.conf  # 列出几个修改过的配置
    bind 10.0.0.15
    protected-mode no
    port 6379
    daemonize yes
    loglevel notice
    logfile /usr/local/redis/logs/redis.log
    databases 16
    save 900 1
    save 300 10
    save 60 10000
    dir /data/redis
    

    b.linux-redis2(10.0.0.16)

    cd /usr/local/redis/
    grep "^[a-Z]" redis.conf  # 其余与上面的配置保持一致
    bind 10.0.0.16
    slaveof  10.0.0.15 6379
    
    # 启动脚本
    cat /usr/lib/systemd/system/redis.service
    
    IP=`ifconfig eth0 | awk -F"[ ]+" 'NR==2{print $3}'`
    [Unit]
    Description=The redis-server Process Manager
    After=network.target
    
    [Service]
    ExecStart=/usr/local/redis/src/redis-server /usr/local/redis/redis.conf --daemonize no
    ExecStop=/usr/local/redis/src/redis-cli -h $IP shutdown
    
    [Install]
    WantedBy=multi-user.target
    
    systemctl daemon-reload
    systemctl enable redis
    systemctl start redis
    

    c.测试

    主redis上输入数据

    从redis上获取数据

      此时的从redis只能读取数据,不能写入,主redis宕机之后,也不能写入,手动切换:

    主redis上输入shutdown,从redis上输入slaveof no one,就可以写入数据了;

    此时如果有其他的从redis,则输入slaveof 10.0.0.16 6379即可更换主redis;

    此时如果刚才宕掉的redis又恢复了,则还是输入slaveof 10.0.0.16 6379,由主变成从.

    2.利用哨兵模式实现主从自动切换

    cat sentinel.conf  # 当启动模式之后,这个配置文件中会自动增加从redis的信息
    port 26379
    dir "/tmp"
    sentinel myid b84f8aea247ad5f7ea48c6a4921d0cca5ffc915f
    sentinel monitor mymaster 10.0.0.15 6379 1
    sentinel down-after-milliseconds mymaster 10000
    sentinel failover-timeout mymaster 18000
    
    ./src/redis-sentinel sentinel.conf
    # Sentinel ID is b84f8aea247ad5f7ea48c6a4921d0cca5ffc915f
    # +monitor master mymaster 10.0.0.15 6379 quorum 1
    * +slave slave 10.0.0.16:6379 10.0.0.16 6379 @ mymaster 10.0.0.15 6379
    * +slave slave 10.0.0.17:6379 10.0.0.17 6379 @ mymaster 10.0.0.15 6379
    
    # 配置文件中自动生成了这些内容
    # Generated by CONFIG REWRITE
    sentinel config-epoch mymaster 0
    sentinel leader-epoch mymaster 0
    sentinel known-slave mymaster 10.0.0.16 6379
    sentinel known-slave mymaster 10.0.0.17 6379
    sentinel current-epoch 0
    

    在10.0.0.15上停掉主redis:redis-cli -h 10.0.0.15 shutdown

    主redis宕了,其余redis开始进行选举,在经过选举之后,其中的一个从redis会变为主redis,通过日志或者配置文件都可以看出来,此时谁是master.

    cat sentinel.conf 
    port 26379
    dir "/tmp"
    sentinel myid b84f8aea247ad5f7ea48c6a4921d0cca5ffc915f
    sentinel monitor mymaster 10.0.0.17 6379 1
    sentinel down-after-milliseconds mymaster 10000
    sentinel failover-timeout mymaster 18000
    # Generated by CONFIG REWRITE
    sentinel config-epoch mymaster 1
    sentinel leader-epoch mymaster 1
    sentinel known-slave mymaster 10.0.0.15 6379
    sentinel known-slave mymaster 10.0.0.16 6379
    sentinel current-epoch 1
    
    # 此时启动10.0.0.15上的redis,它也只能"俯首称臣"
    convert-to-slave slave 10.0.0.15:6379 10.0.0.15 6379 @ mymaster 10.0.0.17 6379
    
    # 在配置文件sentinel.conf中加上这三行比较合适
    daemonize yes
    protected-mode no
    logfile "/var/log/sentinel.log"
    

    Redis主从利用Keepalived自动切换:http://blog.51cto.com/tryingstuff/2093119

  • 相关阅读:
    1.27
    1.25
    Representation Learning with Contrastive Predictive Coding
    Learning a Similarity Metric Discriminatively, with Application to Face Verification
    噪声对比估计(负样本采样)
    Certified Adversarial Robustness via Randomized Smoothing
    Certified Robustness to Adversarial Examples with Differential Privacy
    Dynamic Routing Between Capsules
    Defending Adversarial Attacks by Correcting logits
    Visualizing Data using t-SNE
  • 原文地址:https://www.cnblogs.com/fawaikuangtu123/p/10388238.html
Copyright © 2020-2023  润新知