标签:
为什么要分片:随着Redis存储的数据越来越庞大,会导致Redis的性能越来越差!
目前分片的方法:
1、安装环境:首先确保安装了Redis
cd /opt/ mkdir `seq 7001 7008` cp /etc/redis/6379.conf ./ 把相关的信息都统一修改为:6379 (端口、日志文件、存储dir持久化) sed ‘s/6379/7001/g‘ 6379.conf > 7001/redis.conf sed ‘s/6379/7002/g‘ 6379.conf > 7002/redis.conf sed ‘s/6379/7003/g‘ 6379.conf > 7003/redis.conf sed ‘s/6379/7004/g‘ 6379.conf > 7004/redis.conf sed ‘s/6379/7005/g‘ 6379.conf > 7005/redis.conf sed ‘s/6379/7006/g‘ 6379.conf > 7006/redis.conf sed ‘s/6379/7007/g‘ 6379.conf > 7007/redis.conf sed ‘s/6379/7008/g‘ 6379.conf > 7008/redis.conf for i in `seq 7001 7009`;do cd /opt/$i && /usr/local/bin/redis-server redis.conf ; done
2、安装管理工具,源码自带了一个管理Cluster集群的工具是用ruby写的所以需要安装ruby
yum -y install ruby rubygems 安装ruby的管理工具redis gem install redis
3、复制管理工具
cp /opt/redis-3.0.4/src/redis-trib.rb /usr/local/bin/redis-trib 查看redis-trib帮助 redis-trib help
4、创建集群 7001-7006 6个redis为集群node 7007-7008 “2个redis为back node”
[root@server.tianshuai.com]$ redis-trib create --replicas 1 192.168.0.201:7001 192.168.0.201:7002 192.168.0.201:7003 192.168.0.201:7004 192.168.0.201:7005 192.168.0.201:7006 >>> Creating cluster Connecting to node 192.168.0.201:7001: OK Connecting to node 192.168.0.201:7002: OK Connecting to node 192.168.0.201:7003: OK Connecting to node 192.168.0.201:7004: OK Connecting to node 192.168.0.201:7005: OK Connecting to node 192.168.0.201:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.0.201:7001 192.168.0.201:7002 192.168.0.201:7003 Adding replica 192.168.0.201:7004 to 192.168.0.201:7001 Adding replica 192.168.0.201:7005 to 192.168.0.201:7002 Adding replica 192.168.0.201:7006 to 192.168.0.201:7003 M: 699f318027f87f3c49d48e44116820e673bd306a 192.168.0.201:7001 slots:0-5460 (5461 slots) master M: 96892fd3f51292e922383ddb6e8018e2f772deed 192.168.0.201:7002 slots:5461-10922 (5462 slots) master M: f702fd03c1e3643db7e385915842533ba5aab98d 192.168.0.201:7003 slots:10923-16383 (5461 slots) master S: d0994ce7ef68c0834030334afcd60013773f2e77 192.168.0.201:7004 replicates 699f318027f87f3c49d48e44116820e673bd306a S: d880581504caff4a002242b2b259d5242b8569fc 192.168.0.201:7005 replicates 96892fd3f51292e922383ddb6e8018e2f772deed S: a77b16c4f140c0f5c17c907ce7ee5e42ee2a7b02 192.168.0.201:7006 replicates f702fd03c1e3643db7e385915842533ba5aab98d 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.0.201:7001) M: 699f318027f87f3c49d48e44116820e673bd306a 192.168.0.201:7001 slots:0-5460 (5461 slots) master M: 96892fd3f51292e922383ddb6e8018e2f772deed 192.168.0.201:7002 slots:5461-10922 (5462 slots) master M: f702fd03c1e3643db7e385915842533ba5aab98d 192.168.0.201:7003 slots:10923-16383 (5461 slots) master M: d0994ce7ef68c0834030334afcd60013773f2e77 192.168.0.201:7004 slots: (0 slots) master replicates 699f318027f87f3c49d48e44116820e673bd306a M: d880581504caff4a002242b2b259d5242b8569fc 192.168.0.201:7005 slots: (0 slots) master replicates 96892fd3f51292e922383ddb6e8018e2f772deed M: a77b16c4f140c0f5c17c907ce7ee5e42ee2a7b02 192.168.0.201:7006 slots: (0 slots) master replicates f702fd03c1e3643db7e385915842533ba5aab98d [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. #create --replicas 1 这里--replicas 1 是指定复制几份,相当于每个master有几个从 #redis cluaster最低要求有3个master #master的定义 host1:port host2:port host3:port如果--replicas 1 那么: #host1:port == master host2:port 是:host1:port 是host1:port 从 #如果--replicas 2 那么: #host1:port == master host2:port & host3:port 是host1:port 的从 M: 这个是cluaster 自动生成的ID 集群在通信的时候是使用这个ID来区分的
4、连接cluster,(连接任意的Cluster集群中的服务器即可)
redis-cli -c -h 192.168.0.201 -p 7001 的需要加-c的参数 可以连接集群的任意节点! 192.168.0.201:7001> cluster nodes 查看cluster节点 f702fd03c1e3643db7e385915842533ba5aab98d 192.168.0.201:7003 master - 0 1444813870405 3 connected 10923-16383 699f318027f87f3c49d48e44116820e673bd306a 192.168.0.201:7001 myself,master - 0 0 1 connected 0-5460 d0994ce7ef68c0834030334afcd60013773f2e77 192.168.0.201:7004 slave 699f318027f87f3c49d48e44116820e673bd306a 0 1444813870105 4 connected a77b16c4f140c0f5c17c907ce7ee5e42ee2a7b02 192.168.0.201:7006 slave f702fd03c1e3643db7e385915842533ba5aab98d 0 1444813868605 6 connected 96892fd3f51292e922383ddb6e8018e2f772deed 192.168.0.201:7002 master - 0 1444813869405 2 connected 5461-10922 d880581504caff4a002242b2b259d5242b8569fc 192.168.0.201:7005 slave 96892fd3f51292e922383ddb6e8018e2f772deed 0 1444813869105 5 connected 192.168.0.201:7001> cluster info 查看cluster信息 cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_sent:1809 cluster_stats_messages_received:1809
5、集群扩容
redis-trib add-node 192.168.0.201:7007 192.168.0.201:7001 命令解释: redis-trib add-node 要加的节点和端口 现有任意节点和端口 加完之后查看结果: 192.168.0.201:7001> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:7 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_sent:2503 cluster_stats_messages_received:2503 192.168.0.201:7001> cluster nodes f702fd03c1e3643db7e385915842533ba5aab98d 192.168.0.201:7003 master - 0 1444814061587 3 connected 10923-16383 699f318027f87f3c49d48e44116820e673bd306a 192.168.0.201:7001 myself,master - 0 0 1 connected 0-5460 d0994ce7ef68c0834030334afcd60013773f2e77 192.168.0.201:7004 slave 699f318027f87f3c49d48e44116820e673bd306a 0 1444814062087 4 connected a77b16c4f140c0f5c17c907ce7ee5e42ee2a7b02 192.168.0.201:7006 slave f702fd03c1e3643db7e385915842533ba5aab98d 0 1444814061087 6 connected a1301a9e1fd24099cd8dc49c47f2263e3124e4d6 192.168.0.201:7007 master - 0 1444814063089 0 connected 96892fd3f51292e922383ddb6e8018e2f772deed 192.168.0.201:7002 master - 0 1444814062589 2 connected 5461-10922 d880581504caff4a002242b2b259d5242b8569fc 192.168.0.201:7005 slave 96892fd3f51292e922383ddb6e8018e2f772deed 0 1444814061587 5 connected 192.168.0.201:7001>
6、新加上来没有数据-及没有槽位,我们可以用命令让他重新分片(分片)
redis-trib reshard 192.168.0.201:7007
7、在添加一个服务器做从
在添加一个7008 让他做7008的从 [root@server.tianshuai.com]$ redis-trib add-node 192.168.0.201:7008 192.168.0.201:7001 加进来之后默认就是mater但是他没有任何的槽位 192.168.0.201:7001> cluster nodes f702fd03c1e3643db7e385915842533ba5aab98d 192.168.0.201:7003 master - 0 1444814915795 3 connected 11089-16383 699f318027f87f3c49d48e44116820e673bd306a 192.168.0.201:7001 myself,master - 0 0 1 connected 166-5460 d0994ce7ef68c0834030334afcd60013773f2e77 192.168.0.201:7004 slave 699f318027f87f3c49d48e44116820e673bd306a 0 1444814917298 4 connected a77b16c4f140c0f5c17c907ce7ee5e42ee2a7b02 192.168.0.201:7006 slave f702fd03c1e3643db7e385915842533ba5aab98d 0 1444814916297 6 connected a02a66e0286ee2f0a9b5380f7584b9b20dc032ff 192.168.0.201:7008 master - 0 1444814915796 0 connected a1301a9e1fd24099cd8dc49c47f2263e3124e4d6 192.168.0.201:7007 master - 0 1444814915295 7 connected 0-165 5461-5627 10923-11088 96892fd3f51292e922383ddb6e8018e2f772deed 192.168.0.201:7002 master - 0 1444814916898 2 connected 5628-10922 d880581504caff4a002242b2b259d5242b8569fc 192.168.0.201:7005 slave 96892fd3f51292e922383ddb6e8018e2f772deed 0 1444814916798 5 connected 然后连接到7008的这个redis实例上,然后复制7007的ID 192.168.0.201:7008> cluster replicate a1301a9e1fd24099cd8dc49c47f2263e3124e4d6 OK 然后看下: 192.168.0.201:7008> cluster nodes 699f318027f87f3c49d48e44116820e673bd306a 192.168.0.201:7001 master - 0 1444815074072 1 connected 166-5460 a1301a9e1fd24099cd8dc49c47f2263e3124e4d6 192.168.0.201:7007 master - 0 1444815073071 7 connected 0-165 5461-5627 10923-11088 96892fd3f51292e922383ddb6e8018e2f772deed 192.168.0.201:7002 master - 0 1444815073671 2 connected 5628-10922 a77b16c4f140c0f5c17c907ce7ee5e42ee2a7b02 192.168.0.201:7006 slave f702fd03c1e3643db7e385915842533ba5aab98d 0 1444815073571 3 connected f702fd03c1e3643db7e385915842533ba5aab98d 192.168.0.201:7003 master - 0 1444815072571 3 connected 11089-16383 d0994ce7ef68c0834030334afcd60013773f2e77 192.168.0.201:7004 slave 699f318027f87f3c49d48e44116820e673bd306a 0 1444815073071 1 connected d880581504caff4a002242b2b259d5242b8569fc 192.168.0.201:7005 slave 96892fd3f51292e922383ddb6e8018e2f772deed 0 1444815073871 2 connected a02a66e0286ee2f0a9b5380f7584b9b20dc032ff 192.168.0.201:7008 myself,slave a1301a9e1fd24099cd8dc49c47f2263e3124e4d6 0 0 0 connected 192.168.0.201:7008>
8、Cluster的主从复制这个操作也是向master发送请求,然后master做个BGSAVE然后拿过来之后重载一下!如果master特别大的所以要起个多个redis实例,每个里面存一部分。
也需要注意,涉及到多key的操作!是不行的因为你不同的key存在不同的地方!
192.168.7.107:7002> set key101 shuaige -> Redirected to slot [1601] located at 192.168.7.107:7001 OK 192.168.7.107:7001> set key102 shuaige -> Redirected to slot [13858] located at 192.168.7.107:7003 OK 192.168.7.107:7003> set key103 shuaige -> Redirected to slot [9731] located at 192.168.7.107:7002 OK 192.168.7.107:7002> set key104 shuaige -> Redirected to slot [5860] located at 192.168.7.107:7007 OK 192.168.7.107:7007> set key105 shuaige -> Redirected to slot [1733] located at 192.168.7.107:7001 OK 192.168.7.107:7001>
Redis-cluster集群【第四篇】:redis-cluster集群配置
标签:
原文地址:http://www.cnblogs.com/luotianshuai/p/4969612.html