haishi 发表于 2018-11-5 07:42:14

redis部署集群分片

#修改redis的配置文件(redis.6379为例)  
vim /etc/redis6379.conf
  
cluster-enabled yes
  
cluster-config-file nodes-6379.conf    #设置各集群节点的配置文件
  
cluster-node-timeout 5000
  
appendonly yes
  
dir /data/redisAll      #设置redis持久化和备份文件的默认存放地
  

  

  
#安装ruby等支持模块
  
yum install -y zlib ruby rubygems
  
gem install redis
  

  
redis-trib.rb create --replicas 1 172.18.100.103:6379 172.18.100.103:6380 172.18.100.104:6379 172.18.100.104:6380 172.18.100.123:6379 172.18.100.123:6380
  
#--repicas 1 表示希望为集群的每个主节点创建一个从节点 输入yes后当显示” All 16384 slots covered.”时,集群运行正常
  

  
# redis-trib.rb create --replicas 1 172.18.100.103:6379 172.18.100.103:6380 172.18.100.104:6379 172.18.100.104:6380 172.18.100.123:6379 172.18.100.123:6380
  
>>> Creating cluster
  
>>> Performing hash slots allocation on 6 nodes...
  
Using 3 masters:
  
172.18.100.104:6379
  
172.18.100.103:6379
  
172.18.100.123:6379
  
Adding replica 172.18.100.103:6380 to 172.18.100.104:6379
  
Adding replica 172.18.100.104:6380 to 172.18.100.103:6379
  
Adding replica 172.18.100.123:6380 to 172.18.100.123:6379
  
M: 9a64fb747b3e264fdc1118247477299d9ba18839 172.18.100.103:6379
  
   slots:5461-10922 (5462 slots) master
  
S: 67b02bc4a70daf25adec2b2003779e1e1d9d742f 172.18.100.103:6380
  
   replicates d5cbd79d097d8379a3454c3db55a151f6c3204c7
  
M: d5cbd79d097d8379a3454c3db55a151f6c3204c7 172.18.100.104:6379
  
   slots:0-5460 (5461 slots) master
  
S: 1c4d08531bfecf1a0f0b343c701a898fe382b76a 172.18.100.104:6380
  
   replicates 9a64fb747b3e264fdc1118247477299d9ba18839
  
M: 16592e56dbc4d195864d5f8a7a14e3b876c1a314 172.18.100.123:6379
  
   slots:10923-16383 (5461 slots) master
  
S: 7eb0680fc9eb8efc1eb9b2a612f2d15d9c58f0af 172.18.100.123:6380
  
   replicates 16592e56dbc4d195864d5f8a7a14e3b876c1a314
  
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 172.18.100.103:6379)
  
M: 9a64fb747b3e264fdc1118247477299d9ba18839 172.18.100.103:6379
  
   slots:5461-10922 (5462 slots) master
  
M: 67b02bc4a70daf25adec2b2003779e1e1d9d742f 172.18.100.103:6380
  
   slots: (0 slots) master
  
   replicates d5cbd79d097d8379a3454c3db55a151f6c3204c7
  
M: d5cbd79d097d8379a3454c3db55a151f6c3204c7 172.18.100.104:6379
  
   slots:0-5460 (5461 slots) master
  
M: 1c4d08531bfecf1a0f0b343c701a898fe382b76a 172.18.100.104:6380
  
   slots: (0 slots) master
  
   replicates 9a64fb747b3e264fdc1118247477299d9ba18839
  
M: 16592e56dbc4d195864d5f8a7a14e3b876c1a314 172.18.100.123:6379
  
   slots:10923-16383 (5461 slots) master
  
M: 7eb0680fc9eb8efc1eb9b2a612f2d15d9c58f0af 172.18.100.123:6380
  
   slots: (0 slots) master
  
   replicates 16592e56dbc4d195864d5f8a7a14e3b876c1a314
  
All nodes agree about slots configuration.
  
>>> Check for open slots...
  
>>> Check slots coverage...
  
All 16384 slots covered.
  

  

  
#查看集群节点
  
# redis-cli -c -p 6379 cluster nodes
  
NOAUTH Authentication required.
  
# redis-cli -c -p 6379
  
127.0.0.1:6379> auth xxx
  
OK
  
127.0.0.1:6379> cluster nodes
  
d5cbd79d097d8379a3454c3db55a151f6c3204c7 172.18.100.104:6379 master - 0 1468488926113 3 connected 0-5460
  
9a64fb747b3e264fdc1118247477299d9ba18839 172.18.100.103:6379 myself,master - 0 0 1 connected 5461-10922
  
16592e56dbc4d195864d5f8a7a14e3b876c1a314 172.18.100.123:6379 master - 0 1468488925612 5 connected 10923-16383
  
7eb0680fc9eb8efc1eb9b2a612f2d15d9c58f0af 172.18.100.123:6380 slave 16592e56dbc4d195864d5f8a7a14e3b876c1a314 0 1468488925613 6 connected
  
1c4d08531bfecf1a0f0b343c701a898fe382b76a 172.18.100.104:6380 slave 9a64fb747b3e264fdc1118247477299d9ba18839 0 1468488924611 4 connected
  
67b02bc4a70daf25adec2b2003779e1e1d9d742f 172.18.100.103:6380 slave d5cbd79d097d8379a3454c3db55a151f6c3204c7 0 1468488926113 3 connected
  
127.0.0.1:6379>
  

  
#---------
  
# Sorry, can't connect to node 172.18.100.103:6379
  
#解决办法:
  
#/usr/lib/ruby/gems/1.8/gems/redis-3.3.0/lib/redis/client.rb
  
#修改成
  
#:password => 'xxx'
  
#---------
  

  

  
####集群添加删除节点
  
#添加主节点
  
172.18.100.124:6379   #要添加的新主节点
  
172.18.100.103:6379    #可以是集群已有的任一节点
  
redis-trib.rb add-node 172.18.100.124:6379 172.18.100.103:6379
  

  
#给刚添加的新节点添加从节点
  
redis-cli -c -p cluster nodes |grep 172.18.100.124
  

  
#添加从节点
  
redis-trib.rb add-node --slave --master-id 4947227ad677a99becf636d951d1f397631d565c 172.18.100.124:6380 172.18.100.103:6379
  

  
--slave    #表示添加从节点
  
--master-id    #主节点的node-id
  
172.18.100.124:6380    #从节点
  
172.18.100.103:6379    #可以是集群已有的任一节点
  

  
#填加新的节点后需重新分配slot
  
redis-trib.rb reshard 172.18.100.103:6379
  
How many slots do you want to move (from 1 to 16384)? 1000 #设置slot数1000
  
What is the receiving node ID? 4947227ad677a99becf636d951d1f397631d565c #新添加的节点node id
  
Please enter all the source node IDs.
  
Type 'all' to use all the nodes as source nodes for the hash slots.
  
Type 'done' once you entered all the source nodes IDs.
  
Source node #1:all #表示全部节点重新分配
  
Do you want to proceed with the proposed reshard plan (yes/no)? yes #确认重新分
  

  
#说明:新添加的主节点如果没有slot,存储的数据就不会被选中
  

  

  

  

  
#删除从节点
  
redis-trib.rb del-node 172.18.100.124:6380 '5add38375571dbbb20f311553f4399e641e98f60'
  
#'5add38375571dbbb20f311553f4399e641e98f60'要删除的从节点的node-id
  

  
#删除主节点
  
#主节点有从节点的先删从节点,参考删除从节点步骤
  
#主节点有slot的,去掉分配的slot后才能删除主节点
  

  
# redis-trib.rb reshard 172.18.100.124:6379 #取消分配的slot,下面是主要过程
  

  
How many slots do you want to move (from 1 to 16384)? 1000 #被删除master的所有slot数量,根据实际情况
  
What is the receiving node ID? 089275dc4d883c693b81ca11867dc7d94caf1159 #接收要删除主节点slot的其他主节点
  
Please enter all the source node IDs.
  
Type 'all' to use all the nodes as source nodes for the hash slots.
  
Type 'done' once you entered all the source nodes IDs.
  
Source node #1:4947227ad677a99becf636d951d1f397631d565c #被删除主节点的node-id
  
Source node #2:done
  

  
Do you want to proceed with the proposed reshard plan (yes/no)? yes #取消slot后,重新分配
  

  
#删除主节点的slot信息后可删除该节点
  

  
redis-trib.rb del-node 172.18.100.124:6379 '4947227ad677a99becf636d951d1f397631d565c'
  

  
具体可参考http://www.redis.cn/topics/cluster-tutorial.html


页: [1]
查看完整版本: redis部署集群分片