Ext.: http://www.jianshu.com/p/bd7bf38d9b87
A preliminary study on Redis Cluster (1)-Cluster construction and expansion words 2278 read 154 reviews 0 Likes 2
Redis cluster is the official Redis cluster implementation, before which there have been some private third-party Redis cluster solutions, such as Twitter's twenproxy, pea pod codis, unlike the Redis Cluster not use the PORXY mode to connect the cluster nodes, but use the mode without the central node to build the cluster, there are some performance advantages and shortcomings, this article is my research on redis cluster Some knowledge of the collation and experience of the summary.
First, let's try to build a Redis cluster cluster pre -Setup
Redis cluster requires Redis 3.0 and later, and when this article is released, the highest version of Redis is 3.0.5.
wget http://download.redis.io/releases/redis-3.0.5.tar.gz
tar-xvf redis-3.0.5.tar.gz
cd redis-3.0.5
Make
After compiling the Redis, the generated executables are in redis-3.0.5/src, and for ease of use, we add the executable directory to path.
Vim ~/.BASHRC
# Add the following content
redis_home=/home/xiezefan/sofeware/redis-3.0.5/src
path= $REDIS _home: $PATH
Export PATH
# Save for changes to take effect after
source ~/.BASHRC
To create Redis Cluster, we also need to install Ruby and RubyGems.
Yum install ruby
yum install gcc g++ make automake autoconf curl-devel openssl-devel zlib-devel httpd-devel apr-devel Apr-util-devel sqlite-devel
yum install ruby-rdoc ruby-devel
yum install rubygems
gem install Redis
Create a cluster
This time we need to create 8 nodes, port number 7000~7007
Copy redis-3.0.5/redis.conf, modify the content
mkdir cluster
cp ~/sofeware/redis-3.0.5/redis.conf cluster/
vim cluster/redis.conf
# Modify the following
port 7000
cluster-enabled Yes
cluster-config-file nodes.conf
cluster-node-timeout
Yes
Bulk Copy 7 copies, modify the configuration file port number. Once the modifications are complete, the Redis is launched into the respective node directory
CD 7000
redis-server redis.conf
cd 7001
redis-server redis.conf
# start 7000-7007 in turn
...
At this point, 8 Redis are all started, but they are still in a phase where they do not know each other.
xiezefan@ubuntu:~$ Ps-ef | grep redis
xiezefan 13372 1 0 20:09? 00:00:08 redis-server *:7000 [cluster]
xiezefan 13376 1 0 20:09? 00:00:08 redis-server *:7001 [cluster]
xiezefan 13380 1 0 20:09? 00:00:08 redis-server *:7002 [cluster]
xiezefan 13382 1 0 20:09? 00:00:08 redis-server *:7003 [cluster]
xiezefan 13386 1 0 20:09? 00:00:08 redis-server *:7004 [cluster]
xiezefan 13390 1 0 20:09? 00:00:08 redis-server *:7005 [cluster]
xiezefan 13394 1 0 20:09? 00:00:08 redis-server *:7006 [cluster]
xiezefan 13400 1 0 20:09? 00:00:08 redis-server *:7007 [cluster]
Next, we will connect 7000-7005 of these six nodes into a single cluster.
REDIS-TRIB.RB Create--replicas 1 10.211.55.4:7000 10.211.55.4:7001 10.211.55.4:7002 10.211.55.4:7003 10.211.55.4:7004 10.211.55.4:7005
The command indicates that a 7000-7006 node is created with a cluster with redundancy of 1, which is 3 master 3 from. Remember that the IP specified here will be returned when the client sends a move command, so be sure to specify the IP that clients can access, such as the following IP is not feasible, the client took the IP in place is 127.0.0.1 cause the redirection failed.
REDIS-TRIB.RB Create--replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
After input, Redis-trib automatically assigns a slot allocation scheme
>>> performing hash slots allocation on 6 nodes ... Using 3 masters:10.211.55.4:7000 10.211.55.4:7001 10.211.55.4:7002 Adding replica 10.211.55.4:7003 to 10.211.55.4:7000 A dding replica 10.211.55.4:7004 to 10.211.55.4:7001 Adding replica 10.211.55.4:7005 to 10.211.55.4:7002 m:9dfef549f791779 4cbabaf96781ed0e19957c1f3 10.211.55.4:7000 slots:0-5460 (5461 slots) Master M: c14485e8d7f1f3ec5c505b41fe727b657c951d8d 10.211.55.4:7001 slots:5461-10922 (5462 slots) Master M: 8b308093e99f4299b8c18ab1dd81c5a83a3528c6 10.211.55.4:7002 slots:10923-16383 (5461 slots) Master S: 632409883570eb5cecf6089583fba64a41d1154f 10.211.55.4:7003 replicates 9dfef549f7917794cbabaf96781ed0e19957c1f3 S: edd31196e2980360b0738c57af95e1a69b0f9c9b 10.211.55.4:7004 replicates c14485e8d7f1f3ec5c505b41fe727b657c951d8d S: 95063a99c5cf2cc4abc51ca1e8ff0f3b8d60271c 10.211.55.4:7005 replicates 8b308093e99f4299b8c18ab1dd81c5a83a3528c6 Can I Set the above configuration? (Type ' yes ' to accept):
After you enter Yes, the cluster is created automatically, after creation, through REDIS-CLI to any node, using the cluster nodes command to view the status of each node
xiezefan@ubuntu:~$ redis-cli-p 7000
127.0.0.1:7000>cluster nodes
c14485e8d7f1f3ec5c505b41fe727b657c951d8d 10.211.55.4:7001 master-0 1449060968280 2 connected 5461-10922
95063a99c5cf2cc4abc51ca1e8ff0f3b8d60271c 10.211.55.4:7005 Slave 8b308093e99f4299b8c18ab1dd81c5a83a3528c6 0 1449060968280 6 connected
edd31196e2980360b0738c57af95e1a69b0f9c9b 10.211.55.4:7004 Slave C14485E8D7F1F3EC5C505B41FE727B657C951D8D 0 1449060967274 5 connected
8b308093e99f4299b8c18ab1dd81c5a83a3528c6 10.211.55.4:7002 master-0 1449060966769 3 connected 10923-16383
632409883570eb5cecf6089583fba64a41d1154f 10.211.55.4:7003 slave 9dfef549f7917794cbabaf96781ed0e19957c1f3 0 1449060967274 4 connected
9dfef549f7917794cbabaf96781ed0e19957c1f3 10.211.55.4:7000 myself,master-0 0 1 connected 0-5460
Enter a query command at random get User.1
127.0.0.1:7000> Get User.1
(Error) MOVED 9645 10.211.55.4:7001
Because User.1 is located in the solt-9645 on 7001 nodes, cluster send you moved instructions to let you go to 7001 nodes to find data, when connecting REDIS-CLI, using the-c parameter can be specified when the query received the moved command automatically jump
xiezefan@ubuntu:~$ redis-cli-c-P 7000
127.0.0.1:7000> get User.1 redirected to
slots [9645] located at 1 0.211.55.4:7001
(Nil)
Cluster Expansion
Now that we have a cluster with 6 nodes, I write a snippet of code to write 10W test data to the cluster.
Now simulate the machine expansion scenario, adding a master node 7006 and a Slave node 7007 for the cluster.
REDIS-TRIB.RB Add-node 10.211.55.4:7006 10.211.55.4:7000
The above command connects 7006 nodes to the cluster where 7000 resides. Next, we add a slave node for 7006.
REDIS-TRIB.RB Add-node--slave 10.211.55.4:7007 10.211.55.4:7000
The above command means to increase the slave node, add 7006 nodes to the cluster of 7000 nodes as slave node, randomly attach the least slave node in the existing master node, if you need to specify a special master node, use
REDIS-TRIB.RB add-node--slave--master-id 23b412673af0506df6382353e3a65960d5b7e66d 10.211.55.4:7007 10.211.55.4:7000
The 23b412673af0506df6382353e3a65960d5b7e66d is the ID of the 7006 node, and we can see the ID of the node through the cluster nodes command.
Next we use a sit load balancer, which is the smallest unit of Redis cluster data hosting, and we can specify that a range of slots be transferred to a new node for load balancing.
The step for Redis CLUSTER to transfer a slot is to declare on the target node that it will be moved from the source node to the slot CLUSTER setslot <slot> importing <source_node_id> Declare on the source node to move out of slot to the target node CLUSTER setslot <slot> importing <source_node_id> bulk get key CLUSTER from source node Getkeysinslot <slot> <count> migrating the acquired key to the target node MIGRATE <target_ip> <target_port> <key_name> 0 <timeout > Repeat step 3,4 until all data migrations are sent to both nodes CLUSTER setslot <slot> node <target_node_id>, and the command will be broadcast to other nodes in the cluster. The slot has been transferred to the target node. Wait for the cluster status to become OK CLUSTER INFO in cluster_state = OK
I wrote a script to bulk migrate slots
#!/bin/bash source_host=$1 # source node host Source_port=$2 # source node Port Target_host=$3 # destination Node host Target_port=$4 # destination node Port START_SL OT=$5 # Migration node's actually range End_slot=$6 # Migration node end range for slots in ' seq ${start_slot} ${end_slot} ' do redis-cli-c-H ${tar Get_host}-P ${target_port} cluster Setslot ${slot} importing ' redis-cli-c-H ${source_host}-P ${source_port} cluster No Des | grep ${source_port} | awk ' {print '} ' echo ' Setslot importing ${slot} to ${target_host}:${target_port} success "Redis-cli-c-H ${sour Ce_host}-P ${source_port} cluster Setslot ${slot} migrating ' Redis-cli-c-H ${target_host}-P ${target_port} cluster nod Es | grep ${target_port} |
awk ' {print '} ' echo ' Setslot migrating ${slot} from ${source_host}:${source_port} success "while [1-eq 1] Do allkeys= ' redis-cli-c-H ${source_host}-P ${source_port} cluster Getkeysinslot ${slot} ' if [-Z "${allkeys}"] then Redis-cli-c-H ${source_host}-P ${source_port} CLUster setslot ${slot} node ' redis-cli-c-H ${target_host}-P ${target_port} cluster Nodes | grep ${target_port} | awk ' {print $} ' redis-cli-c-H ${target_host}-P ${target_port} cluster Setslot ${slot} node ' redis-cli-c -H ${source_host}-P ${target_port} cluster Nodes | grep ${target_port} | awk ' {print '} ' echo ' Migrate slot ${slot} finish ' break else for key in ${ AllKeys} do Redis-cli-c-H ${source_host}-P ${source_port} migrate ${target_host} ${target_p ORT} ${key} 0 echo "Migrate slot ${slot} key ${key} success" Done fi do Don E
The
Executes the command bash rebalance-cluster.sh 10.211.55.4 7000 10.211.55.4 7006 0 1000 transfers the slots in the 7000 node to 0-1000 nodes, through cluster Nodes command we can see 0-1000 this interval is the slot has shifted from 7000 to 7006
xiezefan@ubuntu:~/sheel$ redis-cli-c-P 7000 cluster nodes 23b412673af0506df6382353e3a65960d5b7e66d 10.211.55.4:7006 master-0 1449064402389 7 connected 0-1000 0c2954d21d7bcdae333f4fdecf468ce05aa25544 10.211.55.4:7001 master-0 144906440 0372 2 connected 5461-10922 384a3bb5bd9ecb2fc7db75c866abc715d7966f82 10.211.55.4:7002 master-0 1449064401381 3 connecte D 10923-16383 C3E09D286EF2DCE49843268B20832D65A5D516A1 10.211.55.4:7004 slave 0c2954d21d7bcdae333f4fdecf468ce05aa25544 0 1449064401885 5 Connected 50737b4a91443ab1a34eec4ef99d4f6fe5d358f4
10.211.55.4:7005 slave 384a3bb5bd9ecb2fc7db75c866abc715d7966f82 0 1449064402389 6 connected 3c62cc6664bba378cceb8ae8e02f5d727deafe9d 10.211.55.4:7007 Slave 23b412673af0506df6382353e3a65960d5b7e66d 0 1449064400878 7 connected d6441916dcd89cbf431465d92dfc0eb3dd235295 10.211.55.4:7003 Slave 6ee21c5d93a6d2f293a2df1b37e8c9c27cb55ad8 0 1449064402389 4 connected 6ee21c5d93a6d2f293a2df1b37e8c9c27cb55ad8 10.211.55.4:7000 myself,master-0 0 1 connected 1001-5460
Reference ArticlesComprehensive analysis of Redis cluster principle and Application Redis cluster feature preview Redis-cluster combat--8.redis-cluster level expansion (REDIS-CLI implementation) talk about the pit of the field in the database (Redis article)
This article starts with: A preliminary study on http://xiezefan.me/2015/12/03/redis_cluster_research_1/Redis cluster (2)-operation regulation-Pinterest -Previous article we said, Rediscluster uses the SmartClient way, avoids the communication with the node also needs through the layer proxy, achieves the performance to ascend. SmartClient the advantages and disadvantages of the online there are many people in the discussion, we now come to understand the next Redisclient connection mode. Rediscluster is a non-central design, the client can connect to any of the one by one nodes in the cluster, when the operation key is not in the slot of the node, such as access to the SLOT-6433 key, clients will return a (error) moved643310.211.55.4:7001 such an error message. Cl