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.
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
/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 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.
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
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
/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster add‐node 192.168.0.61:8007 192.168.0.61:8001
/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
Use the redis-cli command to allocate a hash slot for 8007, find any master node in the cluster, and re-shard itNote: 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
/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster reshard 192.168.0.61:8001
/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster add‐node 192.168.0.61:8008 192.168.0.61:8001
##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的节点idCheck the cluster status, node 8008 has been successfully added as the slave node of node 8007
用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服务也已被停止
因为主节点的里面是有分配了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!