Home >Database >Redis >Detailed explanation of Redis cluster operation examples

Detailed explanation of Redis cluster operation examples

WBOY
WBOYforward
2022-06-23 12:24:122281browse

This article brings you relevant knowledge about Redis, which mainly organizes issues related to cluster operations, including adding redis instances, configuring 8007 as the master node, and configuring 8008 as 8007 Let’s take a look at the nodes and so on. I hope it will be helpful to everyone.

Detailed explanation of Redis cluster operation examples

Recommended learning: Redis video tutorial

Based on the existing foundation, here is a basic version of three main and three From, the architecture is as follows

##1. Start the cluster
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8001/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8002/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8003/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8004/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8005/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8006/redis.conf

View cluster status: cluster nodes

As can be seen from the above figure, the entire cluster is running normally, with three master nodes and three slave nodes,

  • The instance node of port 8001 stores hash slots 0-5460,
  • The instance node of port 8002 stores hash slots 5461-10922,
  • The instance node of port 8003 stores these hash slots 10923-16383,

The three master nodes store all the hash slots The storage slots that make up the redis cluster. The slave point is the backup slave node of each master node. The storage slots are not displayed.

2. Cluster operation

We add one more master (8007) and one slave (8008) based on the original cluster. After adding nodes See the following figure for the cluster. New nodes are represented by dotted boxes

2.1. Add a redis instance

## Create 8007 and 8008 folders under /usr/local/redis-cluster, and copy the redis.conf file under the 8001 folder to the 8007 and 8008 folders Next

mkdir 8007 8008 
cd 8001
cp redis.conf /usr/local/redis‐cluster/8007/ 
cp redis.conf /usr/local/redis‐cluster/8008/
 
# 修改8007文件夹下的redis.conf配置文件
vim /usr/local/redis‐cluster/8007/redis.conf 
# 修改如下内容:
port:8007 
dir /usr/local/redis‐cluster/8007/
cluster‐config‐file nodes‐8007.conf 

# 修改8008文件夹下的redis.conf配置文件
vim /usr/local/redis‐cluster/8008/redis.conf
# 修改内容如下:
port:8008
dir /usr/local/redis‐cluster/8008/
cluster‐config‐file nodes‐8008.conf

# 启动8007和8008俩个服务并查看服务状态
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8007/redis.conf
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8008/redis.conf

ps ‐el | grep redis

2.2. Configure 8007 as the master node

Use the add-node command to add a new master node 8007 (master), the previous ip:port is a new node, and the latter ip:port is a known existing node. If you see the "[OK] New node added correctly" prompt at the end of the log, it means that the new node has been added successfully

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster add‐node 192.168.0.61:8007 192.168.0.61:8001

View the cluster status

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐c ‐h 192.168.0.61 ‐p 8001

192.168.0.61:8001> cluster nodes

is as follows:

Note: When the node is added successfully, the newly added node will not have any data because it has not been allocated any slot (hash slot). We need to manually allocate hash for the new node. Slot

Use the redis-cli command to allocate a hash slot for 8007, find any master node in the cluster, and re-shard it

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster reshard 192.168.0.61:8001

2.3. Configure 8008 as the slave node of 8007

Add slave node 8008 to the cluster and check the cluster status

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster add‐node 192.168.0.61:8008 192.168.0.61:8001

is as follows:

##As shown in the figure, it is still a master node. No hash slots have been allocated.

Reason: We need to execute the replicate command to specify the master node ID of the current node (slave node). First, we need to connect the client of the newly added 8008 node, and then Use the cluster command to operate and assign the current 8008 (slave) node to a master node (the 8007 master node created before is used here)

Execute the command

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐c ‐h 192.168.0.61 ‐p 8008 

192.168.0.61:8008> cluster replicate 2728a594a0498e98e4b83a537e19f9a0a3790f38 #后面这串id为8007的节点id
Check the cluster status, node 8008 has been successfully added as the slave node of node 8007

 2.4、删除8080从节点[不删除四主四从]

用del-node删除从节点8008,指定删除节点ip和端口,以及节点id(红色为8008节点id)

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster del‐node 192.168.0.61:8008 a1cfe35722d151cf70585cee212755653 93c0956

再次查看集群状态,如下图所示,8008这个slave节点已经移除,并且该节点的redis服务也已被停止

 2.5、删除8007主节点[不删除四主四从]

因为主节点的里面是有分配了hash槽的,所以我们这里必须先把8007里的hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题(目前只能把master的数据迁移到一个节点上,暂时做不了平均分配功能),执行命令如下:

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster reshard 192.168.0.61:8007

迁移验证:会发现8007下面已经没有任何hash槽了,证明迁移成功!

 用del-node命令删除8007主节点即可

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster del‐node 192.168.0.61:8007 2728a594a0498e98e4b83a537e19f9a0a 3790f38

查看最终集群状态,发现一切恢复如初,至此水平扩展结束

推荐学习:Redis视频教程

The above is the detailed content of Detailed explanation of Redis cluster operation examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete