fumingxia 发表于 2018-11-4 10:26:55

redis3.0集群搭建

  Redis集群搭建
  redis cluster介绍
  节点自动发现、集群容错slave选举、Cluster管理、集群配置管理。
  集群中的每个Redis节点需要2个TCP连接端口,如6379端口用于Client连接,16379端口用于集群数据通信
  集群采用Hash Slot方案,而不是一致性哈希,共16384个Hashslot。如果有3台机器,那么NodeA在0-5500,NodeB 在5501-11000,NodeC在11001-16384.这种设计下,添加,删除新Node比较方便。
  由于HashSlot在节点间的迁移无需停止操作,集群新增或者删除节点,改变集群内部节点占用的Slot比例等都可在线完成。
  工作方式:
  内部使用二进制协议优化传输速度和带宽,所有的redis节点彼此互联(PING-PONG机制)。
  集群中超过半数的节点检测失效时集群进入fail状态。
  客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
  状态和选举:
  (1)领着选举过程是集群中所有master参与,如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉.
  (2):什么时候整个集群不可用(cluster_state:fail),当集群不可用时,所有对集群的操作做都不可用,收到((error)CLUSTERDOWN The cluster is down)错误
  如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成进群的slot映射不完成时进入fail状态.
  如果进群超过半数以上master挂掉,无论是否有slave集群进入fail状态.
  安装
  安装依赖ruby
  yum install ruby rubygems
  安装redis cluster
  tar zxvf redis-3.2.3.tar.gz
  cd redis-3.2.3
  make
  cp redis-3.0.5/src/redis-trib.rb /bin/
  cp redis-3.0.5/src/redis-server /bin/
  cp redis-3.0.5/src/redis-cli    /bin/
  配置文件
vim/etc/redis_6380.conf  daemonize yes
  port 6380
  tcp-backlog 511
  timeout 0
  tcp-keepalive 0
  loglevel notice
  maxmemory 10gb
  databases 16
  dir /data/redis/6380
  slave-serve-stale-data yes
  loglevel notice
  logfile "/data/redis/6380/redis_6380.log"
  #slave只读
  slave-read-only yes
  #not use default
  repl-disable-tcp-nodelay yes
  slave-priority 100
  appendonly yes
  #打开aof持久化
  appendfsync everysec
  #每秒一次aof写
  no-appendfsync-on-rewrite yes
  #关闭在aof rewrite的时候对新的写操作进行fsync
  auto-aof-rewrite-min-size 64mb
  lua-time-limit 5000
  cluster-enabled yes
  #打开redis集群
  cluster-config-file /data/redis/6380/nodes-6380.conf
  cluster-node-timeout 15000
  #节点互连超时的阀值(单位毫秒)
  cluster-migration-barrier 1
  #一个主节点在拥有多少个好的从节点的时候就要割让一个从节点出来给其他没有从节点或者从节点挂掉的主节点
  cluster-require-full-coverage no
  #如果某一些key space没有被集群中任何节点覆盖,最常见的就是一个node挂掉,集群将停止接受写入
  auto-aof-rewrite-percentage 80-100
  #部署在同一机器的redis实例,把auto-aof-rewrite搓开,防止瞬间fork所有redis进程做rewrite,占用大量内存
  slowlog-log-slower-than 10000
  slowlog-max-len 128
  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
  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
  启动服务
  redis-server /etc/redis_6380.conf
  redis-server /etc/redis_6381.conf
  集群配置参数
  所有的集群配置参数都存在于redis.conf中,主要几个如下:
  Cluster-enabled    :是否开启集群模式
  Cluster-config-file:集群配置变更后会自动写入改文件
  Cluster-node-timeout :节点超时时间,超过该时间无法连接主要Master节点后,会停止接受查询服务
  Cluster-slave-validity-factor   :控制从节点FailOver相关的设置
  设为0,从节点会一直尝试启动FailOver.
  设为正数,失联大于一定时间(factor*节点TimeOut),不再进行FailOver
  Cluster-migration-barrier   :最小从节点连接数
  Cluster-require-full-coverage:默认为Yes,丢失一定比例Key后(可能Node无法连接或者挂掉),集群停止接受写操作
  设置为No,集群丢失Key的情况下仍提供查询服务
  创建集群
  /usr/local/redis-3.2.3/bin/redis-trib.rbcreate --replicas 1 192.168.0.131:6380 192.168.0.132:6380 192.168.0.154:6380192.168.0.154:6381 192.168.0.132:6381 192.168.0.131:6381

  输入yes
  查看集群状态
  redis-trib.rb check192.168.0.131:6380      //集群任一节点

  其他
  添加节点
  redis-trib.rb add-node 新节点   旧节点(集群任意节点)
  添加节点为指点节点的从节点
  redis-trib.rb add-node --slave--master-id '304f069a63299b……(master节点id)'新节点    127.0.0.1:6380(集群任意节点)
  重新分配slot
  redis-trib.rb reshard192.168.10.219:6378 //下面是主要过程
  How many slots do you want to move(from 1 to 16384)? 1000 //设置slot数1000

  What is the receiving node>
  node>  Type 'all' to use all the nodes assource nodes for the hash slots.

  Type 'done' once you entered all thesource nodes>  Source node #1:all //表示全部节点重新洗牌
  Do you want to proceed with the proposedreshard plan (yes/no)? yes //确认重新分
  删除节点
  1,删除从节点
  # redis-trib.rb del-node 192.168.0.131:6381 '304f069a63299bf4b20d2f018a3b2c3bba650a53'
  2,删除主节点
  如果主节点有从节点,将从节点转移到其他主节点
  如果主节点有slot,去掉分配的slot,然后在删除主节点
  redis-trib.rb reshard192.168.0.131:6381 //取消分配的slot,下面是主要过程
  How many slots do you want to move (from 1to 16384)? 1000 //被删除master的所有slot数量

  What is the receiving node>
  Please enter all the source node>  Type 'all' to use all the nodes as sourcenodes for the hash slots.

  Type 'done' once you entered all the sourcenodes>  Source node#1:304f069a63299bf4b20d2f018a3b2c3bba650a53 //被删除master的node-id
  Source node #2:done
  Doyou want to proceed with the proposed reshard plan (yes/no)? yes //取消slot后,reshard
  新增master节点后,也进行了这一步操作,当时是分配,现在去掉。反着的。
  # redis-trib.rb del-node192.168.0.131:6381 '304f069a63299bf4b20d2f018a3b2c3bba650a53'
  参考链接:redis知识库

页: [1]
查看完整版本: redis3.0集群搭建