Redis集群搭建node
redis cluster介绍redis
节点自动发现、集群容错slave选举、Cluster管理、集群配置管理。vim
集群中的每一个Redis节点须要2个TCP链接端口,如6379端口用于Client链接,16379端口用于集群数据通讯ruby
集群采用Hash Slot方案,而不是一致性哈希,共16384个Hashslot。若是有3台机器,那么NodeA在0-5500,NodeB 在5501-11000,NodeC在11001-16384.这种设计下,添加,删除新Node比较方便。app
因为HashSlot在节点间的迁移无需中止操做,集群新增或者删除节点,改变集群内部节点占用的Slot比例等均可在线完成。tcp
工做方式:ide
内部使用二进制协议优化传输速度和带宽,全部的redis节点彼此互联(PING-PONG机制)。优化
集群中超过半数的节点检测失效时集群进入fail状态。ui
客户端不须要链接集群全部节点,链接集群中任何一个可用节点便可lua
状态和选举:
(1)领着选举过程是集群中全部master参与,若是半数以上master节点与master节点通讯超过(cluster-node-timeout),认为当前master节点挂掉.
(2):何时整个集群不可用(cluster_state:fail),当集群不可用时,全部对集群的操做作都不可用,收到((error)CLUSTERDOWN The cluster is down)错误
若是集群任意master挂掉,且当前master没有slave.集群进入fail状态,也能够理解成进群的slot映射[0-16383]不完成时进入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 ID?03ccad2ba5dd1e062464bc7590400441fafb63f2 //新节点
node id Please enter all the sourcenode IDs.
Type 'all' to use all the nodes assource nodes for the hash slots.
Type 'done' once you entered all thesource nodes IDs.
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 ID?304f069a63299bf4b20d2f018a3b2c3bba650a53 //接收6381节点slot的master
Please enter all the source node IDs.
Type 'all' to use all the nodes as sourcenodes for the hash slots.
Type 'done' once you entered all the sourcenodes IDs.
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'