Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add and delete nodes in Redis cluster

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to add and delete nodes in Redis cluster. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Check the cluster startup status: ps-ef | grep redis

View the slots allocation of the cluster and the master-slave relationship between nodes:

First login node 7000:redis-cli-p 7000-h 192.168.182.132-c / / be careful not to lose-c

Cluster nodes View Cluster Node Information

In this example, we first add two nodes: master node 7006 and slave node 7007, assign 4096 slots to 7006, set 7007 to 7006 slave node, and then delete these two nodes from the cluster. Be sure to delete the master node and then delete the slave node, otherwise the failover will take effect.

I. expansion of clusters

1. Prepare a new node

Add redis7006 and redis7007 directories under the cluster directory redis_cluster directory

Mkdir redis7006

Mkdir redis7007

Increase the completed catalog

Copy the redis.conf configuration file for port 7000 to the redis7006 and redis7007 directories, and modify the port in the configuration file to the port number of the corresponding directory.

For example, the content of the redis.conf file under redis7006 is:

Port 7006bind 192.168.182.132 / / Native IPdaemonize yes / / set to run pidfile / var/run/redis-7006.pidcluster-enabled yes / / enable cluster cluster-config-file node-7006.confcluster-node-timeout 15000appendonly yes in the background

When you are ready, start two new redis nodes:

Redis-server redis7006/redis.confredis-server redis7007/redis.confps-ef | grep redis / / check whether the new redis node starts successfully

Log in to 7006 after startup to view the node status:

Redis-cli-p 7006-h 192.168.182.132-ccluster nodes

two。 Add Primary Node

(1) add node 7006 to the cluster. Make sure that no data has been added to the node, otherwise an error will be reported.

Cd / usr/local/redis/redis/src./redis-trib.rb add-node 192.168.182.132 7006 192.168.182.132 7000 / / the first node is a new node and the second node is a node in the cluster

Added successfully:

You can see that using the addnode command to add a node, the first parameter is the address of the new node, and the second parameter is the IP and port of any existing node. We can see that the new node has been added to the cluster:

The new node 7006 is now connected to the cluster, part of the cluster, and can be redirected to client command requests, but there are two differences between the new node and the other primary nodes:

The new node does not contain any data because it does not contain any hash slots. Although the new node does not contain any hash slots, it is still a master node, so when the cluster needs to upgrade a slave node to a new master node, the new node will not be selected.

Next, just use the redis-trib program to move some hash buckets in the cluster to the new node, and the new node will become the real master node.

(2) assign virtual slots to the primary node 7006

Cd / usr/local/redis/redis/src./redis-trib.rb reshard 192.168.182.132 cd / / 7001 of which can be logged in here for any node is accessed only as a client

After execution:

Because after we add 7006 primary nodes, there are altogether four primary nodes. In order to distribute 7006 equally, we need to allocate 16384 divided by 4 equals 4096 nodes, so we enter 4096 and press enter to continue:

Enter the node ID of 7006 and press enter to continue:

Which master node to extract slots into the new node: all is all master nodes, done: specify the node, here we enter all, press enter to continue:

After entering yes, press enter to start assigning virtual slots to 7006. After the assignment is completed:

Log in to the cluster to check the status of the cluster:

Redis-cli-p 7000-h 192.168.182.132-ccluster nodes

At this point, the master node has been added, and our cluster has changed from three masters and three slaves to four masters and three slaves.

3. Add slave node 7007

(1) add a new node using add-node

Cd / usr/local/redis/redis/src./redis-trib.rb add-node 192.168.182.132 7007 192.168.182.132VR 7000 / / the first node is a new node and the second node is a node in the cluster

After joining the cluster successfully, log in to the cluster to check the cluster status:

7007 is also a Master node and does not have its own slot slot. So we're going to make it a slave node.

(2) change 7007 to 7006 slave node

Use the CLUSTER REPLICATE command to change the master node of a slave node.

The string after redis-cli-p 7007-h 192.168.182.132cluster replicate 52d169e7011ccdf10f99c1d83f92409dcc37ab55 / / is the node ID of node 7006.

Check it out after the setting is successful:

Slave node 7007 of the cluster was added successfully.

Second, the capacity reduction of the cluster

Simply use the del-node command:

The first parameter is the address of any node, and the second node is the address of the node you want to remove. / redis-trib del-node 127.0.0.1V7000.

Use the same method to remove the primary node, but make sure that the primary node is empty before removing it. If it is not empty, you need to refragment the data of this node to other master nodes.

The alternative to removing the master node is to perform a failure recovery manually, and the removed master node will exist as a slave node, but in this case the number of cluster nodes will not be reduced and the data will need to be refragmented. [/ code]

1. Delete slave node

Delete a node with the del-node command. This command needs to specify the ip and port of the delete node, as well as the id of the node.

Cd / usr/local/redis/redis/src./redis-trib.rb del-node 192.168.182.132 ID 7007 7007 node

After successful deletion:

After deletion, we look at the node information of the cluster again. As shown below, 7007 of the slave nodes have been removed.

two。 Delete the primary node

(1) assign the slots of the primary node 7006 to other primary nodes

Cd / usr/local/redis/redis/src. / redis-trib.rb reshard 192.168.182.132:7006

After selecting these items, press enter to continue:

Enter yes to accept the plan, then enter to complete the removal of the 7006-node slot.

Log in to the cluster to view the current cluster situation:

There are no slots on node 7006.

(2) use the del-node command to delete the 7006 primary node.

Cd / usr/local/redis/redis/src./redis-trib.rb del-node 192.168.182.132:7006 52d169e7011ccdf10f99c1d83f92409dcc37ab55

Deleted successfully:

Finally, log in to check that the cluster has returned to the three-master and three-slave structure, but from the initial uniform distribution to the 7000 port master node with 4096 more slots.

This is the end of the article on "how to add and delete nodes in Redis cluster". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report