• Redis集群


    老男孩的https://www.jianshu.com/p/2f93bb771469  

    Redis配置文件

    #masterauth aaaa        #slave连上master时用的密码
    #requirepass aaaa       #连上数据库后,使用auth认证密码后才能使用
    bind 192.168.1.236     #绑定地址
    protected-mode yes     #保护模式,当没有设置密码和绑定地址时,只能从本机连入
    port 7000           #端口
    timeout 0       #超时时间,默认为0,不超时
    tcp-keepalive 300    #定时发送ack检测存活状态
    daemonize yes     #在后台执行
    pidfile /var/run/redis_7000.pid     #pid文件
    loglevel notice      #日志级别
    logfile "/usr/nacp/cluster/7000/redis7000.log"     #日志文件路径
    databases 16        #数据库数量
    dbfilename dump7000.rdb    #数据库文件名
    appendfilename appendonly.aof    #aof文件名
    dir /usr/nacp/cluster/7000/    #数据存放目录,rdb和aof文件会存在这
    cluster-enabled yes     #开启集群模式
    cluster-config-file nodes-7000.conf   #集群模式配置文件
    
    save 900 1    #900秒内至少1个key值改变,将持久化存储
    save 300 10   #300秒内至少10个key值改变,将持久化存储
    save 60 10000   #60秒内至少10000个key值改变,将持久化存储  

    Redis集群

    5.0版本之前的集群需要ruby环境的支持,貌似5.0使用redis-cli就可以直接创建了

      1.安装这个需要gcc环境和ruby环境,找了好多教程,看到很多都是卡在搞环境上,不过我没有遇到,系统我用的centos7.x,这些环境在安装光盘上都有,把yum源指向光盘就可以直接yum安装了。

    [root@localhost redis]# mount /dev/cdrom /mnt
    [root@localhost redis]# vim /etc/yum.repos.d/CentOS-Media.repo 
    
    [c7-media]
    name=CentOS-$releasever - Media
    baseurl=file:///mnt
    gpgcheck=1
    enabled=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    
    [root@localhost redis]# yum clean all
    [root@localhost redis]# yum install gcc ruby -y 

      2.创建redis目录,官网下载redis归档包,上传至服务器,解压后编译、安装

    [root@localhost redis]# mkdir /root/redis
    [root@localhost redis]# cd /root/redis  
    [root@localhost redis]# tar xvf redis-3.2.7.tar.gz
    [root@localhost redis]# cd redis-3.2.7
    [root@localhost redis]# make && make install
    

      3.创建配置文件

    [root@localhost redis]# vim redis.conf 
    
    port 7001    #端口
    bind 0.0.0.0   #绑定地址,默认是127.0.0.1,这里要改一下
    daemonize yes   #后台运行
    logfile /opt/redis_cluster/7001/redis7001.log
    pidfile /opt/redis_cluster/7001/redis7001.pid
    dir /opt/redis_cluster/7001/data
    dbfilename redis7001.rdb
    cluster-enabled yes    #开启集群模式
    cluster-config-file node_7001.conf  #集群配置文件
    cluster-node-timeout 15000    #集群通讯超时时间

      4.创建最少三个文件夹来组成集群,我这里创建了9个,可以组成3主对应6备,并将配置文件对应修改

    [root@localhost redis]# tree *
    7001
    └── redis.conf
    7002
    └── redis.conf
    7003
    └── redis.conf
    7004
    └── redis.conf
    7005
    └── redis.conf
    7006
    └── redis.conf
    8004
    └── redis.conf
    8005
    └── redis.conf
    8006
    └── redis.conf
    

      5.启动这些实例

    [root@localhost redis]# redis-server redis.conf
    

      5.1手动命令添加集群节点CLUSTER MEET {IP} {PORT}

    10.0.0.51:6380> CLUSTER MEET 10.0.0.51 6381
    OK
    10.0.0.51:6380> CLUSTER MEET 10.0.0.52 6380
    OK
    10.0.0.51:6380> CLUSTER MEET 10.0.0.53 6380
    OK
    10.0.0.51:6380> CLUSTER MEET 10.0.0.52 6381
    OK
    10.0.0.51:6380> CLUSTER MEET 10.0.0.53 6381
    OK
    10.0.0.51:6380> CLUSTER NODES
    d03cb38d612802aead8f727b1726a3359c241818 10.0.0.51:6380 myself,master - 0 0 4 connected
    67c8128df00b2fa304a41bafbadac25a654f196d 10.0.0.51:6381 master - 0 1562059947079 1 connected
    a23ec7d444791a0b258ac454ef15cb4d6ab5abd2 10.0.0.53:6381 master - 0 1562059948087 5 connected
    e57807d4d35daaaca05f4a9705e844eab15c7ce8 10.0.0.52:6381 master - 0 1562059949098 0 connected
    aa9da67a594dfb357195f12ca4c44001804ee470 10.0.0.53:6380 master - 0 1562059945063 3 connected
    5cb6895305520e6a0aa4198a6ea5f2c087530b41 10.0.0.52:6380 master - 0 1562059950108 2 connected

      6.到https://rubygems.org/gems/redis下载对应redis版本的集群插件

      7.把插件上传服务器后,使用gem安装

    [root@localhost redis]# gem install redis-3.2.2.gem
    

      8.在redis归档包中src目录下有集群工具redis-trib.rb,使用这个工具可以创建集群,--replicas 2的意思是1个主节点需要有2个备份

    [root@localhost redis]# ./redis-trib.rb create --replicas 2  192.168.65.128:7001 192.168.65.128:7002 
    192.168.65.128:7003 192.168.65.129:7004 
    192.168.65.129:7005 192.168.65.129:7006 
    192.168.65.129:8004 192.168.65.129:8005 
    192.168.65.129:8006
    >>> Creating cluster
    >>> Performing hash slots allocation on 9 nodes...
    Using 3 masters:
    192.168.65.128:7001
    192.168.65.129:7004
    192.168.65.128:7002
    Adding replica 192.168.65.129:7005 to 192.168.65.128:7001
    Adding replica 192.168.65.129:7006 to 192.168.65.128:7001
    Adding replica 192.168.65.128:7003 to 192.168.65.129:7004
    Adding replica 192.168.65.129:8004 to 192.168.65.129:7004
    Adding replica 192.168.65.129:8005 to 192.168.65.128:7002
    Adding replica 192.168.65.129:8006 to 192.168.65.128:7002
    M: b40d90617e8269632403e51753309e28f8209267 192.168.65.128:7001
       slots:0-5460 (5461 slots) master
    M: 95cb7c567dca107d98dc3da90623d5e53f742962 192.168.65.128:7002
       slots:10923-16383 (5461 slots) master
    S: 15044ef928cce6ced4774477bc83a430765f0d0f 192.168.65.128:7003
       replicates 78941d9a55b406b4d88935cac50661972ff3ff5e
    M: 78941d9a55b406b4d88935cac50661972ff3ff5e 192.168.65.129:7004
       slots:5461-10922 (5462 slots) master
    S: b8d0811e374f76db231bbd754f4a0be5507a7ba7 192.168.65.129:7005
       replicates b40d90617e8269632403e51753309e28f8209267
    S: bf7e1cc93311439e6eaa5657322055fa9a4c84c9 192.168.65.129:7006
       replicates b40d90617e8269632403e51753309e28f8209267
    S: 1a2942513aa21d00610938d9372329e0d3f4e069 192.168.65.129:8004
       replicates 78941d9a55b406b4d88935cac50661972ff3ff5e
    S: 73d08918a9b88e5d7b2582a1603d47a9edd08c31 192.168.65.129:8005
       replicates 95cb7c567dca107d98dc3da90623d5e53f742962
    S: 28f6ba0997a3e83f36af7a6b0056c65d6f594fc8 192.168.65.129:8006
       replicates 95cb7c567dca107d98dc3da90623d5e53f742962
    Can I set the above configuration? (type 'yes' to accept): yes  #需要确认
    >>> Nodes configuration updated
    >>> Assign a different config epoch to each node
    >>> Sending CLUSTER MEET messages to join the cluster
    Waiting for the cluster to join........
    >>> Performing Cluster Check (using node 192.168.65.128:7001)
    M: b40d90617e8269632403e51753309e28f8209267 192.168.65.128:7001
       slots:0-5460 (5461 slots) master
       2 additional replica(s)
    S: b8d0811e374f76db231bbd754f4a0be5507a7ba7 192.168.65.129:7005
       slots: (0 slots) slave
       replicates b40d90617e8269632403e51753309e28f8209267
    S: 15044ef928cce6ced4774477bc83a430765f0d0f 192.168.65.128:7003
       slots: (0 slots) slave
       replicates 78941d9a55b406b4d88935cac50661972ff3ff5e
    S: 1a2942513aa21d00610938d9372329e0d3f4e069 192.168.65.129:8004
       slots: (0 slots) slave
       replicates 78941d9a55b406b4d88935cac50661972ff3ff5e
    S: 73d08918a9b88e5d7b2582a1603d47a9edd08c31 192.168.65.129:8005
       slots: (0 slots) slave
       replicates 95cb7c567dca107d98dc3da90623d5e53f742962
    S: bf7e1cc93311439e6eaa5657322055fa9a4c84c9 192.168.65.129:7006
       slots: (0 slots) slave
       replicates b40d90617e8269632403e51753309e28f8209267
    S: 28f6ba0997a3e83f36af7a6b0056c65d6f594fc8 192.168.65.129:8006
       slots: (0 slots) slave
       replicates 95cb7c567dca107d98dc3da90623d5e53f742962
    M: 78941d9a55b406b4d88935cac50661972ff3ff5e 192.168.65.129:7004
       slots:5461-10922 (5462 slots) master
       2 additional replica(s)
    M: 95cb7c567dca107d98dc3da90623d5e53f742962 192.168.65.128:7002
       slots:10923-16383 (5461 slots) master
       2 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

      9.检查集群状态

    [root@localhost redis]# ./redis-trib.rb check 127.0.0.1:7001
    >>> Performing Cluster Check (using node 127.0.0.1:7001)
    M: b40d90617e8269632403e51753309e28f8209267 127.0.0.1:7001
       slots:0-5460 (5461 slots) master
       2 additional replica(s)
    S: b8d0811e374f76db231bbd754f4a0be5507a7ba7 192.168.65.129:7005
       slots: (0 slots) slave
       replicates b40d90617e8269632403e51753309e28f8209267
    S: 15044ef928cce6ced4774477bc83a430765f0d0f 192.168.65.128:7003
       slots: (0 slots) slave
       replicates 78941d9a55b406b4d88935cac50661972ff3ff5e
    S: 1a2942513aa21d00610938d9372329e0d3f4e069 192.168.65.129:8004
       slots: (0 slots) slave
       replicates 78941d9a55b406b4d88935cac50661972ff3ff5e
    S: 73d08918a9b88e5d7b2582a1603d47a9edd08c31 192.168.65.129:8005
       slots: (0 slots) slave
       replicates 95cb7c567dca107d98dc3da90623d5e53f742962
    S: bf7e1cc93311439e6eaa5657322055fa9a4c84c9 192.168.65.129:7006
       slots: (0 slots) slave
       replicates b40d90617e8269632403e51753309e28f8209267
    S: 28f6ba0997a3e83f36af7a6b0056c65d6f594fc8 192.168.65.129:8006
       slots: (0 slots) slave
       replicates 95cb7c567dca107d98dc3da90623d5e53f742962
    M: 78941d9a55b406b4d88935cac50661972ff3ff5e 192.168.65.129:7004
       slots:5461-10922 (5462 slots) master
       2 additional replica(s)
    M: 95cb7c567dca107d98dc3da90623d5e53f742962 192.168.65.128:7002
       slots:10923-16383 (5461 slots) master
       2 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.  

    进入集群内任意节点可以使用cluster nodes查看节点状态

    问题

    #当创建集群的时候,出现无法连接节点
    [root@bogon js]# /usr/nacp/redis-3.2.7/src/redis-trib.rb create --replicas 1  192.168.1.225:7031 192.168.1.225:7032  192.168.1.225:7033 192.168.1.225:7034 192.168.1.225:7035
    >>> Creating cluster
    [ERR] Sorry, can't connect to node 192.168.1.225:7031
    
    
    #问题可能出现在下面几个点
    1.服务没有开启----开启服务
    2.服务绑定的地址和创建集群给的地址不一致------查看配置文件中的bind
    3.集群带有密码-----查看配置文件中的requirepass
    4.之前的集群带有密码,下面这个文件你更改过下面文件的:password => nil,
    /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.0/lib/redis/client.rb
    或者在这/usr/local/share/gems/gems/redis-3.2.2/lib/redis/client.rb
    可以用find / -name client.rb找这个文件在哪
    过了很久之后,集群的密码你不需要了,集群的配置文件你给改了,但是ruby的配置文件没改 

    扩容

    #需要先准备好扩容的节点
    ]# ./redis-trib.rb add-node 新节点IP:端口 已经存在的节点IP:端口
    ]# ./redis-trib.rb reshard 集群节点IP:端口
    #打印出进群每个节点信息后,reshard命令需要确认迁移的槽数量,这里我们输入4096个
    #(一共有16384个节点0-16383,这里需要写16383/主节点个数取整):
    How many slots do you want to move (from 1 to 16384)? 4096
    #输入新加节点的ID作为目标节点,也就是要扩容的节点,目标节点只能指定一个
    What is the receiving node ID? xxxxxxxxx
    #直接输入all,之后输入yes
    Source node #1:all
    #迁移完成后命令会自动退出,这时候我们查看一下集群的状态
    ./redis-trib.rb rebalance 10.0.0.51:6380  

    收缩

    #收缩节点之前需要将节点上的数据移动到其他节点
    ./redis-trib.rb reshard 10.0.0.51:6380
    
    #这个数字需要看要被下线节点上有多少个槽位(命令上会有显示出来)
    #假如有4096个槽位,剩下三个主节点留在集群中
    #也就是需要执行三次这个命令,将本槽位的数据分三次移动到留下的三个节点
    #数字就是4096/3取整(1365,1365,1366)
    How many slots do you want to move (from 1 to 16384)? 1365
    输入接收槽位的节点ID
    输入发送槽位的节点ID
    done
    
    #之后在忘记节点
    ./redis-trib.rb del-node 10.0.0.51:6391 节点ID
    ./redis-trib.rb del-node 10.0.0.51:6390 节点ID
    

      


      5.0配置

    #masterauth nsv123
    #requirepass nsv123
    bind 192.168.1.236
    protected-mode yes
    port 7010
    timeout 0
    tcp-keepalive 300
    daemonize yes
    pidfile /var/run/redis_7010.pid
    loglevel notice
    logfile "/usr/nacp/cluster/nagp/7010/redis7010.log"
    databases 16
    dbfilename dump7010.rdb
    dir /usr/nacp/cluster/nagp/7010/
    cluster-enabled yes
    cluster-config-file nodes-7010.conf
    

      5.0创建集群

    redis-cli --cluster create --cluster-replicas 1 192.168.1.236:7010 192.168.1.236:7011 192.168.1.236:7012 192.168.1.236:7013  192.168.1.236:7014 192.168.1.236:7015
    

      5.0集群命令获取帮助

     redis-cli --cluster help
    

      

      

    初学linux,每学到一点东西就写一点,如有不对的地方,恳请包涵!
  • 相关阅读:
    Python3 isidentifier() 方法
    Python partition() 方法
    Python format() 函数
    Python isdecimal() 方法
    Python zfill() 方法
    Python upper() 方法
    Python translate()方法
    windows上安装db2 spatial extender和ArcSDE的问题
    spring mvc上传、下载的实现
    spring mvc国际化(Local)和动态皮肤(Theme)功能
  • 原文地址:https://www.cnblogs.com/forlive/p/10730726.html
Copyright © 2020-2023  润新知