目录
1、什么是集群
2、为什么使用
3、集群连接
4、redis cluster 如何分配这六个节点?
5、集群搭建:
1、什么是集群
- Redis 集群(包括很多小集群)实现了对Redis的水平扩容,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N,即一个小集群存储1/N的数据,每个小集群里面维护好自己的1/N的数据。
- Redis 集群通过分区(partition)来提供一定程度的可用性(availability): 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。
- 该模式的redis集群特点是:分治、分片。
2、为什么使用
容量不够,redis如何进行扩容?
并发写操作, redis如何分摊?
另外,主从模式,薪火相传模式,主机宕机,导致ip地址发生变化,应用程序中配置需要修改对应的主机地址、端口等信息。
之前通过代理主机来解决,但是redis3.0中提供了解决方案。就是无中心化集群配置。
3、集群连接
- 普通方式登录:可能直接进入读主机,存储数据时,会出现MOVED重定向操作,所以,应该以集群方式登录。
- 集群登录:redis-cli -c -p 6379 采用集群策略连接,设置数据会自动切换到相应的写主机。
4、redis cluster 如何分配这六个节点?
一个集群至少要有三个主节点。
选项 –cluster-replicas 1 :表示我们希望为集群中的每个主节点创建一个从节点。
分配原则尽量保证每个主数据库运行在不同的IP地址,每个从库和主库不在一个IP地址上。
5、集群搭建:
1) 通过redis中utils路径下 ./install_server.sh 执行文件创建6个不同的redis节点,端口号分别为6379、6380、6381、8362、6383、6384
2)如果各个节点原本有存储数据,则先将各个节点的数据文件清空。将6个文件夹内的文件全部删除。
清空数据目录。删除 Redis 数据目录下的所有文件。使用命令: rm -rf /path/to/redis/data/*
3)修改6个redis节点的配置文件
在 /etc/redis 文件中,找到每一个端口号对应的配置文件:
例如修改的内容如下:
bind 0.0.0.0
port 6380 # 设置成对应服务专属的端口号
daemonize yes
dbfilename “dump6380.rdb” # 设置成对应服务专属的名字
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6380.conf # 设置成对应服务专属的
4)启动六个节点
[root@bogon src]# ./redis-server /etc/redis/6379.conf[root@bogon src]# ./redis-server /etc/redis/6380.conf[root@bogon src]# ./redis-server /etc/redis/6381.conf[root@bogon src]# ./redis-server /etc/redis/6382.conf[root@bogon src]# ./redis-server /etc/redis/6383.conf[root@bogon src]# ./redis-server /etc/redis/6384.conf
5)查看各个节点是否启动成功
[root@bogon src]# ps -ef | grep redisroot38891 0 09:56 ?00:00:03 ./redis-server 0.0.0.0:6379 [cluster]root38951 0 09:56 ?00:00:03 ./redis-server 0.0.0.0:6380 [cluster]root39011 0 09:57 ?00:00:03 ./redis-server 0.0.0.0:6381 [cluster]root39071 0 09:57 ?00:00:02 ./redis-server *:6382 [cluster]root39131 0 09:57 ?00:00:02 ./redis-server 0.0.0.0:6383 [cluster]root39191 0 09:57 ?00:00:02 ./redis-server 0.0.0.0:6384 [cluster]root42472418 0 10:22 pts/000:00:00 grep --color=auto redis
6)配置集群
命令格式: –cluster-replicas 1 表示为每个master创建一个slave节点
注意:这里的IP为每个节点所在机器的真实IP
[root@localhost src]# ./redis-cli --cluster create 192.168.177.128:6379 192.168.177.128:6380 192.168.177.128:6381 192.168.177.128:6382 192.168.177.128:6383 192.168.177.128:6384 --cluster-replicas 1>>> Performing hash slots allocation on 6 nodes...Master[0] -> Slots 0 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383Adding replica 192.168.177.128:6383 to 192.168.177.128:6379Adding replica 192.168.177.128:6384 to 192.168.177.128:6380Adding replica 192.168.177.128:6382 to 192.168.177.128:6381>>> Trying to optimize slaves allocation for anti-affinity[WARNING] Some slaves are in the same host as their masterM: ae77569d28f01657d9e3e04810e8562abdb3f5dd 192.168.177.128:6379 slots:[0-5460] (5461 slots) masterM: 5ccafb9ee2f223c987c740d3d8282f200e4892dd 192.168.177.128:6380 slots:[5461-10922] (5462 slots) masterM: dcd5d5066cd9aa5e3f2830ce985395acb978d764 192.168.177.128:6381 slots:[10923-16383] (5461 slots) masterS: 8849bb5437931a3fd8510ffa4c0f755f4e26d1eb 192.168.177.128:6382 replicates ae77569d28f01657d9e3e04810e8562abdb3f5ddS: b00800f65775c52519bd6e9f398f916ffe46fed8 192.168.177.128:6383 replicates 5ccafb9ee2f223c987c740d3d8282f200e4892ddS: 6e7a1ae63691267b000363f6528b620a656e03e7 192.168.177.128:6384 replicates dcd5d5066cd9aa5e3f2830ce985395acb978d764Can 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 clusterWaiting for the cluster to join..>>> Performing Cluster Check (using node 192.168.177.128:6379)M: ae77569d28f01657d9e3e04810e8562abdb3f5dd 192.168.177.128:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s)M: dcd5d5066cd9aa5e3f2830ce985395acb978d764 192.168.177.128:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s)M: 5ccafb9ee2f223c987c740d3d8282f200e4892dd 192.168.177.128:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s)S: 8849bb5437931a3fd8510ffa4c0f755f4e26d1eb 192.168.177.128:6382 slots: (0 slots) slave replicates ae77569d28f01657d9e3e04810e8562abdb3f5ddS: b00800f65775c52519bd6e9f398f916ffe46fed8 192.168.177.128:6383 slots: (0 slots) slave replicates 5ccafb9ee2f223c987c740d3d8282f200e4892ddS: 6e7a1ae63691267b000363f6528b620a656e03e7 192.168.177.128:6384 slots: (0 slots) slave replicates dcd5d5066cd9aa5e3f2830ce985395acb978d764[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
7)查看主从关系
命令格式:redis-cli –cluster check 【本台redis自己的IP】:【本台redis自己的端口】
[root@localhost src]# ./redis-cli --cluster check 192.168.177.128:6379192.168.177.128:6379 (ae77569d...) -> 0 keys | 5461 slots | 1 slaves.192.168.177.128:6381 (dcd5d506...) -> 0 keys | 5461 slots | 1 slaves.192.168.177.128:6380 (5ccafb9e...) -> 0 keys | 5462 slots | 1 slaves.[OK] 0 keys in 3 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.177.128:6379)M: ae77569d28f01657d9e3e04810e8562abdb3f5dd 192.168.177.128:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s)M: dcd5d5066cd9aa5e3f2830ce985395acb978d764 192.168.177.128:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s)M: 5ccafb9ee2f223c987c740d3d8282f200e4892dd 192.168.177.128:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s)S: 8849bb5437931a3fd8510ffa4c0f755f4e26d1eb 192.168.177.128:6382 slots: (0 slots) slave replicates ae77569d28f01657d9e3e04810e8562abdb3f5ddS: b00800f65775c52519bd6e9f398f916ffe46fed8 192.168.177.128:6383 slots: (0 slots) slave replicates 5ccafb9ee2f223c987c740d3d8282f200e4892ddS: 6e7a1ae63691267b000363f6528b620a656e03e7 192.168.177.128:6384 slots: (0 slots) slave replicates dcd5d5066cd9aa5e3f2830ce985395acb978d764[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
查看下图即可发现,一主对一从:
8) 主节点数据写测试
加参数 -c ,防止路由失效
[root@bogon src]# ./redis-cli -p 6381 -c127.0.0.1:6381> get name-> Redirected to slot [5798] located at 192.168.109.149:6380(nil)192.168.109.149:6380> get name(nil)192.168.109.149:6380>
9)从节点读数据测试
情况:redis cluster集群中slave节点能成功复制master节点数据槽数据,但是无法get数据,显示只能到对应的master节点读取
原因:Redis Cluster集群中的从节点,官方默认设置的是不分担读请求的、只作备份和故障转移用,当有请求读向从节点时,会被重定向对应的主节点来处理
解决办法:在get数据之前先使用命令readonly,这个readonly告诉 Redis Cluster 从节点客户端愿意读取可能过时的数据并且对写请求不感兴趣
注意:断开连接后readonly就失效了,再次连接需要重新使用该命令。
[root@bogon src]# ./redis-cli -p 6379 -c127.0.0.1:6379> set name zhangsan-> Redirected to slot [5798] located at 192.168.109.149:6380OK192.168.109.149:6380> get name"zhangsan"192.168.109.149:6380>[root@bogon src]# ./redis-cli -p 6383 -c127.0.0.1:6383> get name-> Redirected to slot [5798] located at 192.168.109.149:6380"zhangsan"192.168.109.149:6380>[root@bogon src]# ./redis-cli -p 6383 -c127.0.0.1:6383> readonlyOK127.0.0.1:6383> get name"zhangsan"127.0.0.1:6383>