zoukankan      html  css  js  c++  java
  • redis配置哨兵模式

    环境:
    redis版本:4.0.14
    os:Centos7

    主:  192.168.1.85:8001
    从:  192.168.1.85:8002
    哨兵:192.168.1.85:8999

    1.主节点安装部署
    [root@localhost src]# cd /soft/redis-4.0.14/src
    [root@localhost src]# make PREFIX=/opt/redis-shaobing/master install
    [root@localhost src]# cp redis-trib.rb /opt/redis-shaobing/master/

    [root@localhost src]# mkdir -p /opt/redis-shaobing/master/conf
    [root@localhost src]# mkdir -p /opt/redis-shaobing/master/logs
    [root@localhost src]# mkdir -p /opt/redis-shaobing/master/run
    [root@localhost src]# mkdir -p /opt/redis-shaobing/master/data


    2.从节点安装部署
    [root@localhost src]# cd /soft/redis-4.0.14/src
    [root@localhost src]# make PREFIX=/opt/redis-shaobing/slave install
    [root@localhost src]# cp redis-trib.rb /opt/redis-shaobing/slave/

    [root@localhost src]# mkdir -p /opt/redis-shaobing/slave/conf
    [root@localhost src]# mkdir -p /opt/redis-shaobing/slave/logs
    [root@localhost src]# mkdir -p /opt/redis-shaobing/slave/run
    [root@localhost src]# mkdir -p /opt/redis-shaobing/slave/data


    3.哨兵节点安装
    [root@localhost src]# cd /soft/redis-4.0.14/src
    [root@localhost src]# make PREFIX=/opt/redis-shaobing/sentinel install
    [root@localhost src]# cp redis-trib.rb /opt/redis-shaobing/sentinel/
    [root@localhost src]# mkdir -p /opt/redis-shaobing/sentinel/conf
    [root@localhost src]# mkdir -p /opt/redis-shaobing/sentinel/logs

    [root@localhost src]# mkdir -p /opt/redis-shaobing/sentinel/data


    4.主节点配置文件(8001.conf)

    [root@localhost conf]# more 8001.conf 
    bind 192.168.1.85
    daemonize yes
    pidfile "/opt/redis-shaobing/master/run/redis-8001.pid"
    port 8001
    tcp-backlog 511
    timeout 300
    tcp-keepalive 300
    loglevel notice
    logfile "/opt/redis-shaobing/master/logs/redis-8001.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 "/opt/redis-shaobing/master/data"
    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
    appendonly yes
    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 ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-entries 512
    list-max-ziplist-value 64
    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
    masterauth "hxlpasswd"
    requirepass "hxlpasswd"
    protected-mode yes
    

     

    5.从节点配置文件

    [root@localhost conf]# more 8002.conf 
    bind 192.168.1.85
    daemonize yes
    pidfile "/opt/redis-shaobing/slave/run/redis-8002.pid"
    port 8002
    tcp-backlog 511
    timeout 300
    tcp-keepalive 300
    loglevel notice
    logfile "/opt/redis-shaobing/slave/logs/redis-8002.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 "/opt/redis-shaobing/slave/data"
    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
    appendonly yes
    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 ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-entries 512
    list-max-ziplist-value 64
    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
    masterauth "hxlpasswd"
    requirepass "hxlpasswd"
    protected-mode yes
    slaveof 192.168.1.85 8001
    

    注意这里slaveof

    slaveof 192.168.1.85 8001

    6.哨兵节点配置文件sentinel.conf

    port 8999
    daemonize yes
    dir /opt/redis-shaobing/sentinel/data
    sentinel myid 3154c22b52f5fdca833d7a972bb0104e11e63b82 ##myid可以自己定义
    sentinel monitor mymaster 192.168.1.85 8001 1  ##主节点信息
    sentinel config-epoch mymaster 1
    sentinel leader-epoch mymaster 1
    sentinel down-after-milliseconds mymaster 15000
    sentinel auth-pass mymaster hxlpasswd ##若是有密码验证的 这里必须加上该项 否则主从无法切换
    logfile "/opt/redis-shaobing/sentinel/logs/sentinel.log"
    protected-mode no
    
    # Generated by CONFIG REWRITE ##下面的选择项是系统切换后自动生成的,开始配置的时候不需要
    sentinel leader-epoch mymaster 6
    sentinel known-slave mymaster 192.168.1.85 8002
    sentinel current-epoch 6
    

     

    7.启动主从节点
    主节点
    /opt/redis-shaobing/master/bin/redis-server /opt/redis-shaobing/master/conf/8001.conf
    从节点
    /opt/redis-shaobing/slave/bin/redis-server /opt/redis-shaobing/slave/conf/8002.conf


    验证主从节点的正确性
    主库设置key
    /opt/redis-shaobing/master/bin/redis-cli -h 192.168.1.85 -p 8001 -a hxlpasswd
    192.168.1.85:7001> set name hxl

    从库读取key
    /opt/redis-shaobing/slave/bin/redis-cli -h 192.168.1.85 -p 8002 -a hxlpasswd
    192.168.1.85:7002> get name
    "hxl"

    8.启动哨兵
    /opt/redis-shaobing/sentinel/bin/redis-sentinel /opt/redis-shaobing/sentinel/conf/sentinel.conf

    登陆哨兵
    /opt/redis-shaobing/sentinel/bin/redis-cli -h 192.168.1.85 -p 8999
    查看信息

    [root@localhost logs]# /opt/redis-shaobing/sentinel/bin/redis-cli -h 192.168.1.85 -p 8999
    192.168.1.85:8999> info
    # Server
    redis_version:4.0.14
    redis_git_sha1:00000000
    redis_git_dirty:0
    redis_build_id:4af9eba2e02c1ef9
    redis_mode:sentinel
    os:Linux 3.10.0-862.el7.x86_64 x86_64
    arch_bits:64
    multiplexing_api:epoll
    atomicvar_api:atomic-builtin
    gcc_version:4.8.5
    process_id:17899
    run_id:1b23616c45b7e0ff82aad6d5ea34f5065827c301
    tcp_port:8999
    uptime_in_seconds:12
    uptime_in_days:0
    hz:15
    lru_clock:7441241
    executable:/opt/redis-shaobing/sentinel/bin/redis-sentinel
    config_file:/opt/redis-shaobing/sentinel/conf/sentinel.conf
    
    # Clients
    connected_clients:1
    client_longest_output_list:0
    client_biggest_input_buf:0
    blocked_clients:0
    
    # CPU
    used_cpu_sys:0.02
    used_cpu_user:0.01
    used_cpu_sys_children:0.00
    used_cpu_user_children:0.00
    
    # Stats
    total_connections_received:1
    total_commands_processed:0
    instantaneous_ops_per_sec:0
    total_net_input_bytes:31
    total_net_output_bytes:60
    instantaneous_input_kbps:0.00
    instantaneous_output_kbps:0.00
    rejected_connections:0
    sync_full:0
    sync_partial_ok:0
    sync_partial_err:0
    expired_keys:0
    expired_stale_perc:0.00
    expired_time_cap_reached_count:0
    evicted_keys:0
    keyspace_hits:0
    keyspace_misses:0
    pubsub_channels:0
    pubsub_patterns:0
    latest_fork_usec:0
    migrate_cached_sockets:0
    slave_expires_tracked_keys:0
    active_defrag_hits:0
    active_defrag_misses:0
    active_defrag_key_hits:0
    active_defrag_key_misses:0
    
    # 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=192.168.1.85:8001,slaves=0,sentinels=1
    

     

    9.模拟主节点down掉
    /opt/redis-shaobing/master/bin/redis-cli  -h 192.168.1.85 -p 8001 -a hxlpasswd shutdown


    再次登陆哨兵查看信息,发现现在的主节点完成了切换
    [root@localhost logs]# /opt/redis-shaobing/sentinel/bin/redis-cli -h 192.168.1.85 -p 8999
    192.168.1.85:8999> info
    .
    .
    .
    前面输出省略
    # 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=192.168.1.85:8002,slaves=1,sentinels=1

    10.原来down掉的节点重启启动
    启动后发现原来停掉的节点不会重新成为主节点,而是成为从节点

    11.python程序连接哨兵模式

    # !/usr/bin/env python
    # -*- coding:utf-8 -*-
    import redis
    from redis.sentinel import Sentinel
    
    # 连接哨兵服务器(主机名也可以用域名)
    sentinel = Sentinel([('192.168.1.85', 8999)],socket_timeout=0.5)
    
    # 获取主服务器地址
    master = sentinel.discover_master('mymaster')
    print(master)
    # 获取从服务器地址
    slave = sentinel.discover_slaves('mymaster')
    print(slave)
    # 获取主服务器进行写入
    master = sentinel.master_for('mymaster', socket_timeout=0.5, password='hxlpasswd')
    w_ret = master.set('foo', 'bar')
    # 输出:True
    # # 获取从服务器进行读取(默认是round-roubin)
    slave = sentinel.slave_for('mymaster', socket_timeout=0.5, password='hxlpasswd')
    r_ret = slave.get('foo')
    print(r_ret)
    # # 输出:bar
  • 相关阅读:
    Linux中的邮件发送
    Python学习笔记18-发送邮件
    Ansible学习笔记
    eclipse使用maven打包时去掉测试类
    CentOS安装redis
    spring boot 部署
    sprint boot 配置
    spring-boot 学习笔记一
    不要容忍破窗户
    阿里云安装Oracle
  • 原文地址:https://www.cnblogs.com/hxlasky/p/13744626.html
Copyright © 2011-2022 走看看