• redis集群搭建


    一、redis集群说明

    集群采用一主三从,总共39从,总共12台,每个机房中放置一主三从

     

    每个机房中放置其他机房的一从,达到高可用性

    二、集群搭建

    1.redis集群的搭建

    1.1修改系统参数

    1.使用root账户修改

      /etc/security/limits.conf

      添加

      * soft nofile 102400

      * hard nofile 102400

      修改 /etc/sysctl.  

      添加net.core.somaxconn=32767

      sysctl -p 生效

      2.创建redis用户

    1.2 redis的安装

      1.下载并编译

      主节点机器上创建文件

       mkdir -p  /home/redis/redis/bin

       mkdir -p  /home/redis/redis/bin

      mkdir -p  /home/redis/redis/log

    在主节点下

     redis目录下

     wget http://download.redis.io/releases/redis-3.2.5.tar.gz

     tar xzf redis-3.2.5.tar.gz

     cd redis-3.2.5

     make

     cp src/redis-server ../redis/bin

     cp src/redis-cli ../redis/bin

     cp src/redis-trib.rb ../redis/bin

     cp redis.conf ../redis/conf

     touch /home/redis/redis/conf/cluster.conf

    2.修改参数redis.conf,具体参数配置见后面附件

    3.在其他机器上创建redis用户

    4.发送到其他机器上去

    scp -r /home/redis/redis redis@slave:/home/redis/
    5.登录到各机器上,启动redis

    (1)修改cluster.conf配置

    配置文件结构

    redis.conf---

    |---cluster.conf(master)

    |---cluster.conf(slave1)

    |---cluster.conf(slave2)

    |---cluster.conf(slave3)

    修改master,slave1,slave2,slave3节点中cluster.conf配置,在cluster.conf配置首行添加参数(各节点cluster.conf参数见附件)

    include /home/redis/redis/conf/redis.conf

    2)启动redis

    cd /home/redis/redis/

    ./bin/redis-server ./conf/cluster.conf

    1.3集群操作

    1.集群创建

    1)添加redis server到cluster

       1) 增加主节点

    ./bin/redis-trib.rb create  master1,master2,master3

       2)登录redis客户端

    ./bin/redis-cli -c -p 6379

       3)查看master node id(类似这种:3eb099e326bc4c3d763223743d4dc90caa975cc8)

     cluster nodes

       4)添加从节点

     ./bin/redis-trib.rb add-node --salve -master-id nodeid /

     slaveip:port ip:port(ip:port为任意一加入集群中的节点ip和port)

    注意:安装中可能遇到问题报:

    /usr/bin/env: ruby: No such file or directory

    则需要安装ruby

    yum install ruby

    yum install rubygems

    wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem

    gem install -l ./redis-3.2.1.gem

    (2)验证集群搭建成功

    ./bin/redis-cli -c  -p 6379

    cluster info

    查看cluster_state:ok则为成功

    2.集群的节点增加

    (1)master节点增加

    1)启动新增的节点

    2)加入到集群中

    ./bin/redis-trib.rb add-node 新增节点ip:port 集群中已存在任意节点ip:prot  

    3)为新节点分配slot

      ./bin/redis-trib.rb reshard

       显示如下内容:

    #根据提示选择要迁移的slot数量  

    How many slots do you want to move (from 1 to 16384)? 500  

    #选择要接受这些slotnode-id  

    What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf  

    #选择slot来源:  

    #all表示从所有的master重新分配,  

    #或者数据要提取slotmaster节点id,最后用done结束  

    Please enter all the source node IDs.  

      Type 'all' to use all the nodes as source nodes for the hash slots.  

      Type 'done' once you entered all the source nodes IDs.  

    Source node #1:all  

    #打印被移动的slot后,输入yes开始移动slot以及对应的数据.  

    #Do you want to proceed with the proposed reshard plan (yes/no)? yes  

    #结束  

    (2)slave节点增加

     ./bin/redis-trib.rb add-node --salve -master-id nodeid /

     slaveip:port ip:port(ip:port为任意一加入集群中的节点ip和port)

     

    3.集群节点的重新负载

    ./bin/redis-trib.rb reshard 旧的masterip:port

    #从该节点移动slot到其他master节点

    How many slots do you want to move (from 1 to 16384)? 500

    #接受slot的节点id

    What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf

    Please enter all the source node IDs.

      Type 'all' to use all the nodes as source nodes for the hash slots.

      Type 'done' once you entered all the source nodes IDs.

    #输入all则分配给所有的master,done则为指定的master

     Source node #1:done

     

    4.节点的删除

    1master节点的删除

    1)清空master上的slot

    #把要删除的master节点slot和数据迁移到随意一台master上  

    redis-trib.rb reshard 192.168.1.151:6379  

    #根据提示选择要迁移的slot数量  

    How many slots do you want to move (from 1 to 16384)? 500(被删除master的所有slot数量)  

    #选择要接受这些slotnode-id(192.168.1.152:6379)  

    What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2

    Please enter all the source node IDs.  

      Type 'all' to use all the nodes as source nodes for the hash slots.  

      Type 'done' once you entered all the source nodes IDs.  

    Source node #1:f51e26b5d5ff74f85341f06f28f125b7254e61bf(被删除masternode-id)  

    Source node #2:done  

    #打印被移动的slot后,输入yes开始移动slot以及对应的数据.  

    #Do you want to proceed with the proposed reshard plan (yes/no)? yes  

    2)删除节点

    ./bin/redis-trib.rb del-node 192.168.1.151:6379

    'f51e26b5d5ff74f85341f06f28f125b7254e61bf'  

    (2)slave节点的删除

    ./bin/redis-trib.rb del-node 192.168.1.151:6380

    ’5d664f56fb2e7763bfca89754271378a0ac3b657’

     

    4.client中关于cluster的命令

    集群  

    CLUSTER INFO 打印集群的信息  

    CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。  

    节点  

    CLUSTER MEET <ip> <port> ip port 所指定的节点添加到集群当中,让它成为集群的一份子。  

    CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。  

    CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。  

    CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。  

    (slot)  

    CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。  

    CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。  

     CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。  

    CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。  

    CLUSTER SETSLOT <slot> IMPORTING <node_id> node_id 指定的节点中导入槽 slot 到本节点。  

    CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。  

    键  

    CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。  

    CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。  

    CLUSTER GETKEYSINSLOT <slot> <count> 返回 count slot 槽中的键。

    5.

     

    2.redis的运维

    实时监控命令

    watch -n 1 -d "/home/redis/redis/bin/redis-cli -c -p 6379 info | grep -e "connected_clients" -e "blocked_clients" -e "used_memory_human" -e "used_memory_peak_human" -e "rejected_connections" -e "evicted_keys" -e "instantaneous""

    监控参数:

    #连接数

    connected_clients:1

    #阻塞连接数

    blocked_clients:0

    #使用内存

    used_memory_human:799.66K

    #使用内存峰值

    used_memory_peak_human:852.35K

    #每秒执行命令个数

    instantaneous_ops_per_sec:0

    #每秒读字节数

    instantaneous_input_kbps:0.01

    #每秒写字节数

    instantaneous_output_kbps:1.23

    #拒绝连接数

    rejected_connections:0

    #因内存大小限制,被驱逐的键个数

    evicted_keys:0

     

     

     

    附件:

     

     

    redis.conf配置文件

    配置项

    说明

    cluster-enabled

    yes

    表示以集群方式运行,为no表示以非集群方式运行

    cluster-node-timeout

    3000

    单位为毫秒:

    repl-ping-slave-period+

    (cluster-node-timeout*

    cluster-slave-validity-factor)

    cluster-slave-validity-factor

    0

    如果要最大的可用性,值设置为0

    repl-ping-slave-period

    1

    slave ping master的时间间隔,单位为秒

    repl-timeout

    10

    复制超时,单位为秒,须大于repl-ping-slave-period的值

    slave-read-only

    yes

    slave是否只读

    slave-serve-stale-data

    yes

    当slave与master断开连接,slave是否继续提供服务

    appendonly

    yes

    开启aof

    daemonize

    yes

    守护线程运行

    protected-mode

    no

    是否限制远程连接

    tcp-backlog

    32767

    取值不能超过系统的/proc/sys/net/core/somaxconn

    auto-aof-rewrite-percentage

    100

    设置自动rewite AOF文件(手工rewrite只需要调用命令BGREWRITEAOF)

    auto-aof-rewrite-min-size

    64mb

    触发rewrite的AOF文件大小,只有大于此大小时才会触发rewrite

    no-appendfsync-on-rewrite

    yes

    子进程在做rewrite时,主进程不调用fsync(由内核默认调度)

    cluster-require-full-coverage

    no

    为no表示有slots不可服务时其它slots仍然继续服务

    repl-backlog-size

    64M

    默认1M,当写入量很大时,backlog溢出会导致增量复制不成功

    client-output-buffer-limit

    normal 256mb 128mb 60

    避免普通客户端进行大批量数据查询,如keys *,lrang等操作下的buffer不够导致链接断开

    client-output-buffer-limit

    slave 512mb 256mb 180

    避免主从复制过程中,buffer过小导致复制链接断开,rdb文件重传或者循环的rdb操作

    stop-writes-on-bgsave-error

    no

    避免redis后台save失败后,redis拒绝写操作,前提是需要有监控软件

    maxmemory 

    8G

    最大内存(可以不设置)

    maxmemory-policy 

    volatile-lru

    内存清理策略

    volatile-lru  使用LRU算法来删除过期的set

    allkeys-lru  删除任何遵循LRU算法的key

    volatile-random 随机地删除过期set中的key

    allkeys-random 随机地删除一个key

    volatile-ttl 删除最近即将过期的key(the nearest expire time (minor TTL))

    noeviction 根本不过期,写操作直接报错

     

    主节点cluster.conf配置文件

    配置项

    说明

    port

    6379

    客户端连接端口,并且总有一个刚好大于10000的端口,这个大的端口用于主从复制和集群内部通讯。

    cluster-config-file

    nodes-6379.conf

     

    pidfile

    /var/run/redis-6379.pid

    只有当daemonize值为yes时,才有意义;并且这个要求对目录/var/run有写权限,否则可以考虑设置为/tmp/redis-6379.pid。

    dir

    /home/redis/redis/data/6379

     

    dbfilename

    dump-6379.rdb

    位于dir指定的目录下

    appendfilename

    "appendonly-6379.aof"

     

    logfile

    /home/redis/redis/log/redis-6379.log

    日志文件,包含目录和文件名

     

    从节点1 cluster.conf 配置文件

    配置项

    说明

    port

    6380

     

    cluster-config-file

    nodes-6380.conf

     

    pidfile

    /var/run/redis-6380.pid

     

    dir

    /home/redis/redis/data/6380

    AOF和RDB文件存放目录

    dbfilename

    dump-6380.rdb

    RDB文件名

    appendfilename

    appendonly-6380.aof

    AOF文件名

    logfile

    /home/redis/redis/log/redis-6380.log

     

     

     

    从节点2 cluster.conf配置文件

    配置项

    说明

    port

    6381

     

    cluster-config-file

    nodes-6381.conf

     

    pidfile

    /var/run/redis-6381.pid

     

    dir

    /home/redis/redis/data/6381

    AOF和RDB文件存放目录

    dbfilename

    dump-6381.rdb

    RDB文件名

    appendfilename

    appendonly-6381.aof

    AOF文件名

    logfile

    /home/redis/redis/log/redis-6381.log

     

     

     

    从节点3  cluster.conf 配置文件

    配置项

    说明

    port

    6382

     

    cluster-config-file

    nodes-6382.conf

     

    pidfile

    /var/run/redis-6382.pid

     

    dir

    /home/redis/redis/data/6382

    AOF和RDB文件存放目录

    dbfilename

    dump-6382.rdb

    RDB文件名

    appendfilename

    appendonly-6382.aof

    AOF文件名

    logfile

    /home/redis/redis/log/redis-6382.log

     

     

  • 相关阅读:
    Map 循环出key 和 value
    Jquery Validate
    Cookie/Session机制详解
    Java根据sessionId获取Session对象
    在线用户统计二
    页面在线访问人数统计&&在线登录人数统计一
    在线会话管理
    oneworld元数据配置
    java map遍历
    黑马程序员——C语言位运算符
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6875470.html
Copyright © 2020-2023  润新知