zoukankan      html  css  js  c++  java
  • Redis主从备份&哨兵设置

    Redis 主从备份,哨兵设置

    环境 Centos 7,redis 4.0.6

    三个centos 7实例
    节点 IP 端口
    redis-master 172.31.193.221 6379
    redis-slave 172.31.193.222 6379
    redis-LBS 172.31.193.224 6379
    redis-sentinel-master 172.31.193.221 26379
    redis-sentinel-slave 172.31.193.222 26379
    redis-sentinel-LBS 172.31.193.224 26379
    1.将redis解压到目录(/bak/soft/)

    tar -zxvf 压缩包名 解压目录

    2.创建日志和数据恢复目录
    mkdir -p /bak/soft/logs/redis
    mkdir -p /bak/soft/cdata/redis
    mkdir -p /bak/soft/logs/redis-sentinel
    mkdir -p /bak/soft/cdata/redis-sentinel
    
    3.配置redis-master.conf
    bind 127.0.0.1 172.31.193.221
    daemonize yes
    masterauth "ADMINadmin123"
    requirepass "ADMINadmin123"
    pidfile "/var/run/redis_6379.pid"
    loglevel notice
    logfile "/bak/soft/logs/redis/redis.log"
    
    #下面不用复制,作为参考,实际部署中根据需要配置
    bind 127.0.0.1 172.31.193.221
    protected-mode yes
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize yes
    supervised no
    pidfile "/var/run/redis_6379.pid"
    loglevel notice
    logfile "/bak/soft/logs/redis/redis.log"
    databases 16
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename "dump.rdb"
    dir "/bak/soft/cdata/redis"
    masterauth "rdhkdcX5xCoHNg86MV0Y"
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    requirepass "rdhkdcX5xCoHNg86MV0Y"
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events "xE"
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes
    # Generated by CONFIG REWRITE
    
    
    4.配置redis-slave.conf
    bind 127.0.0.1 172.31.193.222
    daemonize yes
    masterauth "ADMINadmin123"
    requirepass "ADMINadmin123"
    dbfilename "dump.rdb"
    dir "/bak/soft/cdata/redis"
    pidfile "/var/run/redis_6379.pid"
    loglevel notice
    logfile "/bak/soft/logs/redis/redis.log"
    slaveof 172.31.193.221 6379
    
    
    #下面不用复制,作为参考,实际部署中根据需要配置
    bind 127.0.0.1 172.31.193.221
    protected-mode yes
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize yes
    supervised no
    pidfile "/var/run/redis_6379.pid"
    loglevel notice
    logfile "/bak/soft/logs/redis/redis.log"
    databases 16
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename "dump.rdb"
    dir "/bak/soft/cdata/redis"
    masterauth "rdhkdcX5xCoHNg86MV0Y"
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    requirepass "rdhkdcX5xCoHNg86MV0Y"
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events "xE"
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes
    # Generated by CONFIG REWRITE
    slaveof 172.31.193.221 6379
    
    
    5.配置redis-LBS.conf
    bind 127.0.0.1 172.31.193.224
    daemonize yes
    masterauth "ADMINadmin123"
    requirepass "ADMINadmin123"
    dbfilename "dump.rdb"
    dir "/bak/soft/cdata/redis"
    pidfile "/var/run/redis_6379.pid"
    loglevel notice
    logfile "/bak/soft/logs/redis/redis.log"
    slaveof 172.31.193.221 6379
    
    #下面不用复制,作为参考,实际部署中根据需要配置
    bind 127.0.0.1 172.31.193.224
    protected-mode yes
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize yes
    supervised no
    pidfile "/var/run/redis_6379.pid"
    loglevel notice
    logfile "/bak/soft/logs/redis/redis.log"
    databases 16
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename "dump.rdb"
    dir "/bak/soft/cdata/redis"
    masterauth "rdhkdcX5xCoHNg86MV0Y"
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    requirepass "rdhkdcX5xCoHNg86MV0Y"
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events "xE"
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes
    # Generated by CONFIG REWRITE
    
    slaveof 172.31.193.221 6379
    
    
    6.配置sentinel.conf文件master&slave&LBS配置可以一样
    protected-mode no
    port 26379
    daemonize yes
    sentinel monitor mymaster 172.31.193.221 6379 2
    logfile "/bak/soft/logs/redis/sentinel.log"
    dir "/bak/soft/cdata/redis-sentinel"
    sentinel auth-pass mymaster ADMINadmin123
    
    
    #下面不用复制,myid为自动生成,slave节点也是自动添加
    protected-mode no
    port 26379
    daemonize yes
    sentinel myid f7564fc40667bb5e336c6566acc28654d0c40d39
    sentinel monitor mymaster 172.31.193.221 6379 2
    logfile "/bak/soft/logs/redis/sentinel.log"
    dir "/bak/soft/cdata/redis-sentinel"
    sentinel auth-pass mymaster ADMINadmin123
    sentinel config-epoch mymaster 4
    sentinel leader-epoch mymaster 4
    sentinel known-slave mymaster 172.31.193.222 6379
    sentinel known-slave mymaster 172.31.193.224 6379
    sentinel known-sentinel mymaster 172.31.193.222 26379 96d7940391c5f9f386dedc1eb7a5b221e458428a
    sentinel known-sentinel mymaster 172.31.193.224 26379 d5202361f59b4695ebe8a7cefd26c61466637554
    sentinel current-epoch 4
    
    7.配置redis.properties连接信息master&slave&LBS可以一样
    redis.address=172.31.193.221:26379;172.31.193.222:26379;172.31.193.224:26379
    redis.pool.testOnBorrow=true
    redis.pool.testOnReturn=false
    redis.pool.testWhileIdle=false
    redis.pool.maxIdle=300
    redis.pool.minIdle=15
    redis.pool.maxActive=1000
    redis.pool.maxWait=10000
    redis.pool.timeout=10000
    redis.pool.numTestsPerEvictionRun=0
    redis.pool.timeBetweenEvictionRunsMillis=0
    redis.pool.minEvictableIdleTimeMillis=0
    redis.password=ADMINadmin123
    redis.type=sentinel
    redis.master.name=mymaster
    
    8.修改防火墙规则,开放端口
    #查看防火墙状态
    service iptables status
    
    #测试端口是否开放
    telnet ip 端口
    
    #修改防火墙规则
    vim /etc/sysconfig/iptables
    -A INPUT -p tcp -m tcp --dport 6379 -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 26379 -j ACCEPT
    
    service iptables restart
    
    #若已经启动redis,kill进程重启服务
    ps aux |grep redis
    kill -9 进程号
    
    
    9.启动redis顺序,master—>slave&LBS—>三个哨兵
    /bak/soft/redis-4.0.6/redis-server /etc/redis.conf
    /bak/soft/redis-4.0.6/redis-sentinel /etc/sentinel.conf
    
    #查看日志
    tail 100 /bak/soft/logs/redis/redis.log
    
    #查看复制信息
    redis-cli -a ADMINadmin123 info replication
    
    

    image

    10.测试停止master节点,slave或LBS节点将被选举为master节点

    image

    再重启master自动变成slave

    image

  • 相关阅读:
    python(day1-11)
    如何进行测试管理
    为什么你参加了那么多培训,却依然表现平平?
    数据库总结
    为何大量网站不能抓取?爬虫突破封禁的6种常见方法
    re--读书笔记【转】
    re--findall 【转】
    re--参考手册
    bs4--官文--遍历文档树
    rhel6+apache2.4+mysql5.7+php5.6部署LAMP架构
  • 原文地址:https://www.cnblogs.com/binjz/p/11169492.html
Copyright © 2011-2022 走看看