欢迎阅读!

潇湘夜雨

当前位置: 主页 > 数据库 >

redis cluster集群配置和运维

时间:2017-06-09 17:17来源:潇湘夜雨 作者:华嵩阳 点击:
介绍:redis集群使用数据分片(sharding)而非一致性哈希(consistency hashing)来实现: 一个Redis集群包含16384个哈希槽(hash slot),数据库中的每个键都属于这16384个哈希槽 的其中一个,集
介绍:redis集群使用数据分片(sharding)而非一致性哈希(consistency hashing)来实现:
一个Redis集群包含16384个哈希槽(hash slot),数据库中的每个键都属于这16384个哈希槽
的其中一个,集群使用公式CRC16(key)%16384来计算键key属于那个槽,其中CRC16(key)语句用
于计算键key的CRC16校验和。
 
一、redis-cluster配置
 
1、安装ruby环境
用来执行redis-trib.rb脚本
[root@localhost ~]# yum install ruby rubygems -y
[root@localhost ~]# wget https://rubygems.org/downloads/redis-3.2.2.gem
 
[root@localhost ~]# gem install redis-3.2.2.gem 
Successfully installed redis-3.2.2
1 gem installed
Installing ri documentation for redis-3.2.2...
Installing RDoc documentation for redis-3.2.2...
 
 
2、 准备多个redis节点
 
配置文件:
[root@localhost redis]# cat redis3.conf 
bind 0.0.0.0
protected-mode no
port 6381
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6381.pid" #自定义
loglevel notice
logfile "/var/log/redis/redis3.log" #自定义
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump3.rdb" #自定义
dir "/data/redis"
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 90
appendonly no
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-size -2
list-compress-depth 0
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
cluster-enabled yes #开启集群
cluster-config-file nodes6381.conf  #自定义
注意:自定义的部分,各个节点不能相同。
 
集群配置解析
 
默认情况下不能从slaves读取数据,但建立连接后,执行一次命令READONLY,该slaves即可读取数据。
否则只能以redis-cli -c -h -p命令登录 -c是以集群方式
cluster-enabled yes    集群开关,默认是不开启集群模式。
cluster-config-file nodes-6379.conf  集群配置文件的名称,每个节点都有一个集群相关的配置文件,持久化保存集群的信息。这个文件并不需要手动配置,这个配置文件有 Redis生成并更新,每个Redis集群节点需要一个单独的配置文件,请确保与实例运行的系统中配置文件名称不冲突。
cluster-node-timeout 15000 节点互连超时的阀值。集群节点超时毫秒数。即节点与集群其他节点断开多长时间将被认定为超时。建议稍微大一点
cluster-slave-validity-factor 10 在进行故障转移的时候,全部slave都会请求申请为master,但是有些slave可能与master断开连接一段时间了,导致数据过于陈旧,这样的slave不应该被提升为master。该参数就是用来判断slave节点与master断线的时间是否过长。判断方法是:比较slave断开连接的时间和(node-timeout * slave-validity-factor)+ repl-ping-slave-period如果节点超时时间为三十秒, 并且slave-validity-factor为10,假设默认的repl-ping-slave-period是10秒,即如果超过310秒slave将不会尝试进行故障转移
cluster-migration-barrier 1 master的slave数量大于该值,slave才能迁移到其他孤立master上,如这个参数若被设为2,那么只有当一个主节点拥有2个可工作的从节点时,它的一个从节点才会尝试迁移。
cluster-require-full-coverage yes 默认情况下,集群全部的slot有节点负责,集群状态才为ok,才能提供服务。设置为no,可以在slot没有全部分配的时候提供服务。不建议打开该配置,这样会造成分区的时候,小分区的master一直在接受写请求,而造成很长时间数据不一致。
/usr/local/redis/bin/redis-trib.rb --replicas 1 192.168.198.128:6379 192.168.198.128:6380 192.168.198.130:6379 192.168.198.130:6380
 
 
启动各个节点
 
3、创建集群
 
[root@localhost redis]# /usr/local/redis/bin/redis-trib.rb create --replicas 1 192.168.198.128:6379 192.168.198.128:6380 192.168.198.130:6379 192.168.198.130:6380
>>> Creating cluster
[ERR] Node 192.168.198.130:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决方法:用 FLUSHALL,命令清除已经存在的键值。
[root@localhost redis]# /usr/local/redis/bin/redis-trib.rb create --replicas 1 192.168.198.128:6379 192.168.198.128:6380 192.168.198.130:6379 192.168.198.130:6380
>>> Creating cluster
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 4 nodes and 1 replicas per node.
*** At least 6 nodes are required.
 
错误原因:redis cluster至少需要6个节点,至少3个主节点
 
/usr/local/redis/bin/redis-trib.rb create --replicas 1 192.168.198.128:6379 192.168.198.128:6380 192.168.198.128:6381 192.168.198.130:6379 192.168.198.130:6380 192.168.198.130:6381
 
[root@localhost ~]# /usr/local/redis/bin/redis-trib.rb create --replicas 1 192.168.198.128:6379 192.168.198.128:6380 192.168.198.128:6381 192.168.198.130:6379 192.168.198.130:6380 192.168.198.130:6381
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.198.128:6379
192.168.198.130:6379
192.168.198.128:6380
Adding replica 192.168.198.130:6380 to 192.168.198.128:6379
Adding replica 192.168.198.128:6381 to 192.168.198.130:6379
Adding replica 192.168.198.130:6381 to 192.168.198.128:6380
M: 367f09037eb2aa0d5905bb826ee113458a965267 192.168.198.128:6379
  slots:0-5460,5907,12054 (5463 slots) master
M: 4eda271e009e929e47573efdce97f60b2ae127a3 192.168.198.128:6380
  slots:1559,2960,5907,10923-16383 (5464 slots) master
S: 64f4db57670eb12937aff6b24c7d074417f3a961 192.168.198.128:6381
  replicates 3858fa7dfd1255cddbb0d53320457138885c1790
M: 3858fa7dfd1255cddbb0d53320457138885c1790 192.168.198.130:6379
  slots:1559,2960,5461-10922,12054 (5465 slots) master
S: 3690d1f3e44fc1a331fd3c524ded78d0a078be97 192.168.198.130:6380
  replicates 367f09037eb2aa0d5905bb826ee113458a965267
S: 28132897c4ab835471a868084f123e7878258b05 192.168.198.130:6381
  replicates 4eda271e009e929e47573efdce97f60b2ae127a3
Can I set the above configuration? (type 'yes' to accept): yes
/usr/lib/ruby/gems/1.8/gems/redis-3.2.2/lib/redis/client.rb:114:in `call': ERR Slot 1559 is already busy (Redis::CommandError)
from /usr/lib/ruby/gems/1.8/gems/redis-3.2.2/lib/redis.rb:2646:in `method_missing'
from /usr/lib/ruby/gems/1.8/gems/redis-3.2.2/lib/redis.rb:57:in `synchronize'
from /usr/lib/ruby/1.8/monitor.rb:242:in `mon_synchronize'
from /usr/lib/ruby/gems/1.8/gems/redis-3.2.2/lib/redis.rb:57:in `synchronize'
from /usr/lib/ruby/gems/1.8/gems/redis-3.2.2/lib/redis.rb:2645:in `method_missing'
from /usr/local/redis/bin/redis-trib.rb:212:in `flush_node_config'
from /usr/local/redis/bin/redis-trib.rb:776:in `flush_nodes_config'
from /usr/local/redis/bin/redis-trib.rb:775:in `each'
from /usr/local/redis/bin/redis-trib.rb:775:in `flush_nodes_config'
from /usr/local/redis/bin/redis-trib.rb:1296:in `create_cluster_cmd' (责任编辑:liangzh)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
推荐内容