redis cluster cluster construction

This time I bring you the redis cluster cluster construction.

 

1. First of all, we first create 6 folders according to the document. The directory cluster-test (the folder name and location is optional) can be established in the compiled redis root directory, then cd into clustert-test, and then create 7000-7005 A total of 6 folders,

mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005

Add a redis.conf file to each folder, and the redis.conf file can be obtained from our redis root directory.

 

2. Then we need to modify the corresponding code in redis.conf to the following form:

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

The port number can be configured in sequence from 7000-7005

 

3. After that, we need to start 6 redis respectively, but first, we need to enter the directory where the redis.conf file is located, such as the 7000 directory, and then execute the startup command.

Why do you do this? In fact, you can see it after entering the 7000-7005 folder after running it. There are some corresponding files under each folder, such as nodes.conf in the third line of the redis.conf configuration above, blogger I was pitted _(:з"∠)_......

Start command:

cd 7000
redis-server ./redis.conf

redis-server is the startup command of redis, you need to add it to the environment variable yourself

 

 

4. Now you need to install the ruby ​​module of redis, because the script written by the author needs

yum install ruby
yum install rubygems
gem install redis

Then enter the src directory of redis and execute the following statement

./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

In this way, the cluster is completed, and the 6 redis are 3 masters and 3 slaves.

redis-cli -p 7000 cluster nodes

Through this statement, the master-slave relationship can be output

 

5. Finally, we can test the following:

>redis-cli -c -h 127.0.0.1 -p 7000

Note that the parameter -c is added here, which means to enter cluster mode.

127.0.0.1:7000> set a 10
-> Redirected to slot [15495] located at 127.0.0.1:7002

Here, after the cache a is calculated, it falls on the master whose port is 7002, and the slot is the slot:

HASH_SLOT = CRC16(key) mod 16384

Through cluster nodes, you can see the slot corresponding to each master

127.0.0.1:7002> cluster nodes
b3b19495801d2bf7960ae857ac5acd41884ab20f 127.0.0.1:7000 slave ce09b994443852c51f75a82b032ee4a0c0a203c5 0 1458577108602 7 connected
3dff391695f54477b4290afe434b8bda4bfec4e2 127.0.0.1:7005 slave 3fcd607421bfe9be0f277653cfd924c2c6d1c3c7 0 1458577109102 6 connected
ce09b994443852c51f75a82b032ee4a0c0a203c5 127.0.0.1:7003 master - 0 1458577108100 7 connected 0-5460
56d52eb49e6f9bcd2652b7f4cc00028e28fb346c 127.0.0.1:7004 slave 5f29d9b84350df536d60a1b32d93196d2be1b338 0 1458577110103 5 connected
3fcd607421bfe9be0f277653cfd924c2c6d1c3c7 127.0.0.1:7002 myself,master - 0 0 3 connected 10923-16383
5f29d9b84350df536d60a1b32d93196d2be1b338 127.0.0.1:7001 master - 0 1458577108601 2 connected 5461-1092

Careful readers can find that my 7000 is the slave and the 7003 is the master. This is because I manually killed the 7000 and started it again.

 

6. About cluster expansion:

Then create two services 7006 and 7007, the process is the same as above, and then execute:

./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000

The first address is the node we need to add, the second is any valid node in the cluster, output

>>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
S: b3b19495801d2bf7960ae857ac5acd41884ab20f 127.0.0.1:7000
   slots: (0 slots) slave
   replicates ce09b994443852c51f75a82b032ee4a0c0a203c5
M: 5f29d9b84350df536d60a1b32d93196d2be1b338 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 56d52eb49e6f9bcd2652b7f4cc00028e28fb346c 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 5f29d9b84350df536d60a1b32d93196d2be1b338
M: 3fcd607421bfe9be0f277653cfd924c2c6d1c3c7 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 3dff391695f54477b4290afe434b8bda4bfec4e2 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 3fcd607421bfe9be0f277653cfd924c2c6d1c3c7
M: ce09b994443852c51f75a82b032ee4a0c0a203c5 127.0.0.1:7003
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster.
[OK] New node added correctly.

Check the master-slave status again:

127.0.0.1:7000> cluster nodes
b3b19495801d2bf7960ae857ac5acd41884ab20f 127.0.0.1:7000 myself,slave ce09b994443852c51f75a82b032ee4a0c0a203c5 0 0 1 connected
5f29d9b84350df536d60a1b32d93196d2be1b338 127.0.0.1:7001 master - 0 1458578163681 2 connected 5461-10922
56d52eb49e6f9bcd2652b7f4cc00028e28fb346c 127.0.0.1:7004 slave 5f29d9b84350df536d60a1b32d93196d2be1b338 0 1458578164182 5 connected
3fcd607421bfe9be0f277653cfd924c2c6d1c3c7 127.0.0.1:7002 master - 0 1458578163180 3 connected 10923-16383
3dff391695f54477b4290afe434b8bda4bfec4e2 127.0.0.1:7005 slave 3fcd607421bfe9be0f277653cfd924c2c6d1c3c7 0 1458578163681 6 connected
ce09b994443852c51f75a82b032ee4a0c0a203c5 127.0.0.1:7003 master - 0 1458578164182 7 connected 0-5460
88645a7e5d89fb66de5ae9d7a4b90324a8acb7d9 127.0.0.1:7006 master - 0 1458578164684 0 connecte

It can be seen that 7006 has become the master, and then 7007 is added to the cluster as the slave of 7006:

./redis-trib.rb add-node --slave --master-id 88645a7e5d89fb66de5ae9d7a4b90324a8acb7d9 127.0.0.1:7007 127.0.0.1:7000

-slave indicates that the current node is the slave, --master-id indicates who is the slave, and then confirm:

127.0.0.1:7000> cluster nodes
b3b19495801d2bf7960ae857ac5acd41884ab20f 127.0.0.1:7000 myself,slave ce09b994443852c51f75a82b032ee4a0c0a203c5 0 0 1 connected
5f29d9b84350df536d60a1b32d93196d2be1b338 127.0.0.1:7001 master - 0 1458578725767 2 connected 5461-10922
56d52eb49e6f9bcd2652b7f4cc00028e28fb346c 127.0.0.1:7004 slave 5f29d9b84350df536d60a1b32d93196d2be1b338 0 1458578725265 5 connected
3fcd607421bfe9be0f277653cfd924c2c6d1c3c7 127.0.0.1:7002 master - 0 1458578725767 3 connected 10923-16383
7a4d3410f9b4f06733a08b79c2aa10a3762e565b 127.0.0.1:7007 slave 88645a7e5d89fb66de5ae9d7a4b90324a8acb7d9 0 1458578724264 0 connected
3dff391695f54477b4290afe434b8bda4bfec4e2 127.0.0.1:7005 slave 3fcd607421bfe9be0f277653cfd924c2c6d1c3c7 0 1458578724764 6 connected
ce09b994443852c51f75a82b032ee4a0c0a203c5 127.0.0.1:7003 master - 0 1458578724764 7 connected 0-5460
88645a7e5d89fb66de5ae9d7a4b90324a8acb7d9 127.0.0.1:7006 master - 0 1458578724264 0 connected

 This is 7007 has become the slave of 7006.

 

7. At this time, there is another problem, that is, 7006 does not allocate a slot

So, we can execute the following statement to re-divide the slot:

> ./redis-trib.rb reshard 127.0.0.1:7000

There will be several options in the middle:

How many slots do you want to move (from 1 to 16384)? 1000 // =========1
What is the receiving node ID? 88645a7e5d89fb66de5ae9d7a4b90324a8acb7d9 // =====2
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 // ==========3
...
Do you want to proceed with the proposed reshard plan (yes/no)? yes // =======4

1. 1000 means to take 1000 slots from the existing slot,

2. The node ID represents the node to which the slot in 1 is transferred (7006 here),

3. The source node indicates from which node the 1000 slots are taken out and allocated to the target node. verify:

127.0.0.1:7000> cluster nodes
b3b19495801d2bf7960ae857ac5acd41884ab20f 127.0.0.1:7000 myself,slave ce09b994443852c51f75a82b032ee4a0c0a203c5 0 0 1 connected
5f29d9b84350df536d60a1b32d93196d2be1b338 127.0.0.1:7001 master - 0 1458578948666 2 connected 5795-10922
56d52eb49e6f9bcd2652b7f4cc00028e28fb346c 127.0.0.1:7004 slave 5f29d9b84350df536d60a1b32d93196d2be1b338 0 1458578947665 5 connected
3fcd607421bfe9be0f277653cfd924c2c6d1c3c7 127.0.0.1:7002 master - 0 1458578949669 3 connected 11256-16383
7a4d3410f9b4f06733a08b79c2aa10a3762e565b 127.0.0.1:7007 slave 88645a7e5d89fb66de5ae9d7a4b90324a8acb7d9 0 1458578948165 9 connected
3dff391695f54477b4290afe434b8bda4bfec4e2 127.0.0.1:7005 slave 3fcd607421bfe9be0f277653cfd924c2c6d1c3c7 0 1458578949167 6 connected
ce09b994443852c51f75a82b032ee4a0c0a203c5 127.0.0.1:7003 master - 0 1458578948165 7 connected 333-5460
88645a7e5d89fb66de5ae9d7a4b90324a8acb7d9 127.0.0.1:7006 master - 0 1458578949167 9 connected 0-332 5461-5794 10923-11255

 As you can see, we have allocated 1000 slots to 7006 nodes.

 

8. Finally, delete the node:

./redis-trib.rb del-node 127.0.0.1:7000 7a4d3410f9b4f06733a08b79c2aa10a3762e565b

del-node means to delete a node, the first parameter is any valid node, and the second parameter is the ID of the target node to be deleted

Only empty master nodes and slave nodes can be deleted. Otherwise, it is necessary to reshard first to transfer all the slots of the target master, and the slave of the node will look for other masters at this time.

 

Deleting a node will also stop the redis service of the target node, which needs attention.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041427&siteId=291194637