Home > Database > Mysql Tutorial > body text

Detailed introduction to redis cluster configuration and management (with code)

不言
Release: 2019-04-13 10:15:24
forward
3314 people have browsed it

This article brings you a detailed introduction to redis cluster configuration and management (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Redis began to support clusters after version 3.0. After continuous updates and optimizations in the middle versions, the cluster function of the latest version has been very complete. This article briefly introduces the process and configuration method of building a Redis cluster. The redis version is 5.0.4, and the operating system is Kirin (basically the same as the Centos kernel).

1. Principle of Redis Cluster

Redis cluster is an assembly that provides data sharing between multiple Redis nodes. The cluster nodes share the same A decentralized network is built. Each node in the cluster has an equal identity, and each node saves its own data and cluster status. Nodes use the Gossip protocol to communicate , ensuring the synchronization of node status information.

Redis cluster data is managed through partitions, and each node saves a subset of the cluster data. Data is allocated using a method called hash slot, which is different from traditional consistent hashing. The Redis cluster has 16384 hash slots. Each key passes the CRC16 check and modulo 16384 to determine which slot to place.

In order to make the cluster still available when some nodes fail or most nodes cannot communicate, the cluster uses a master-slave replication model. When reading data, the data is obtained from the corresponding master node according to the consistent hashing algorithm. If the master hangs up, a corresponding salve node will be started to act as the master.

2. Environment preparation

Here we are going to build a redis cluster with 3 masters and 3 slaves on a PC.

Create a new folder rediscluster in the /opt/ directory to store the cluster node directory.

Then create 6 folders of server10, server11, server20, server21, server30, and server31 respectively to prepare 6 redis nodes. These nodes use 6379, 6380, 6381, 6382, 6383, and 6384 ports respectively, with server10 as the An example configuration is as follows:

port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file  nodes-6379.conf
Copy after login

Other nodes only need to modify the port and file name, and configure them in sequence. After the configuration is completed, start these nodes.

[root@localhost rediscluster]# ./server10/redis-server ./server10/redis.conf &
[root@localhost rediscluster]# ./server11/redis-server ./server11/redis.conf &
[root@localhost rediscluster]# ./server20/redis-server ./server20/redis.conf &
[root@localhost rediscluster]# ./server21/redis-server ./server21/redis.conf &
[root@localhost rediscluster]# ./server30/redis-server ./server30/redis.conf &
[root@localhost rediscluster]# ./server31/redis-server ./server31/redis.conf &
Copy after login

Check the startup status:

[root@localhost rediscluster]# ps -ef|grep redis
root     11842     1  0 15:03 ?        00:00:12 ./server10/redis-server 127.0.0.1:6379 [cluster]
root     11950     1  0 15:03 ?        00:00:13 ./server11/redis-server 127.0.0.1:6380 [cluster]
root     12074     1  0 15:04 ?        00:00:13 ./server20/redis-server 127.0.0.1:6381 [cluster]
root     12181     1  0 15:04 ?        00:00:12 ./server21/redis-server 127.0.0.1:6382 [cluster]
root     12297     1  0 15:04 ?        00:00:12 ./server30/redis-server 127.0.0.1:6383 [cluster]
root     12404     1  0 15:04 ?        00:00:12 ./server31/redis-server 127.0.0.1:6384 [cluster]
Copy after login

3. Cluster configuration

is very simple: redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster -replicas 1

##Where -replicas 1 means 1 slave node per master node#

[root@localhost rediscluster]# ./server10/redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:6383 to 127.0.0.1:6379
Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
Adding replica 127.0.0.1:6382 to 127.0.0.1:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382
   replicates 63e20c75984e493892265ddd2a441c81bcdc575c
S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383
   replicates d9a79ed6204e558b2fcee78ea05218b4de006acd
S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384
   replicates efa84a74525749b8ea20585074dda81b852e9c29
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
....
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
additional replica(s)
M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
additional replica(s)
S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382
   slots: (0 slots) slave
   replicates 63e20c75984e493892265ddd2a441c81bcdc575c
S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384
   slots: (0 slots) slave
   replicates efa84a74525749b8ea20585074dda81b852e9c29
M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
additional replica(s)
S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383
   slots: (0 slots) slave
   replicates d9a79ed6204e558b2fcee78ea05218b4de006acd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Copy after login
Creation completed, master and slave nodes assigned As follows:
Adding replica 127.0.0.1:6383 to 127.0.0.1:6379
Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
Adding replica 127.0.0.1:6382 to 127.0.0.1:6381
Copy after login

4. Cluster test

was tested after connecting through the 6379 client, and it was found that it was redirected to 6381:

[root@localhost rediscluster]# ./server10/redis-cli -h 127.0.0.1 -c -p 6379
127.0.0.1:6379> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:6381
OK
127.0.0.1:6381> get foo
"bar"
Copy after login

Connected on 6381 Test:

[root@localhost rediscluster]# ./server10/redis-cli -h 127.0.0.1 -c -p 6381
127.0.0.1:6381> get foo
"bar"
Copy after login

The results are the same, indicating that the cluster configuration is normal.


5. Cluster node expansion

In the

rediscluster

directory, add two new directories server40 and server41, add two new redis nodes and configure two ports 6385 and 6386. Use 6385 as the new master node and 6386 as the slave node, and then start the node:

[root@localhost server41]# ps -ef|grep redis
root     11842     1  0 15:03 ?        00:00:18 ./server10/redis-server 127.0.0.1:6379 [cluster]
root     11950     1  0 15:03 ?        00:00:19 ./server11/redis-server 127.0.0.1:6380 [cluster]
root     12074     1  0 15:04 ?        00:00:18 ./server20/redis-server 127.0.0.1:6381 [cluster]
root     12181     1  0 15:04 ?        00:00:18 ./server21/redis-server 127.0.0.1:6382 [cluster]
root     12297     1  0 15:04 ?        00:00:17 ./server30/redis-server 127.0.0.1:6383 [cluster]
root     12404     1  0 15:04 ?        00:00:18 ./server31/redis-server 127.0.0.1:6384 [cluster]
root     30563     1  0 18:01 ?        00:00:00 ./redis-server 127.0.0.1:6385 [cluster]
root     30582     1  0 18:02 ?        00:00:00 ./redis-server 127.0.0.1:6386 [cluster]
Copy after login
Add the master node:
[root@localhost server41]# ./redis-cli --cluster add-node 127.0.0.1:6385 127.0.0.1:6379
>>> Adding node 127.0.0.1:6385 to cluster 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
additional replica(s)
M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
additional replica(s)
S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382
   slots: (0 slots) slave
   replicates 63e20c75984e493892265ddd2a441c81bcdc575c
S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384
   slots: (0 slots) slave
   replicates efa84a74525749b8ea20585074dda81b852e9c29
M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
additional replica(s)
S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383
   slots: (0 slots) slave
   replicates d9a79ed6204e558b2fcee78ea05218b4de006acd
[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:6385 to make it join the cluster.
[OK] New node added correctly.
Copy after login

View the node list:

[root@localhost server41]# ./redis-cli 
127.0.0.1:6379> cluster nodes
22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385@16385 master - 0 1555064037664 0 connected
efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555064036000 1 connected 0-5460
d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555064038666 3 connected 10923-16383
0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555064035000 4 connected
ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555064037000 6 connected
63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555064037000 2 connected 5461-10922
fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555064037000 5 connected
Copy after login

Add the slave node :

[root@localhost server41]# ./redis-cli --cluster add-node 127.0.0.1:6386 127.0.0.1:6379 --cluster-slave --cluster-master-id 22e8a8e97d6f7cc7d627e577a986384d4d181a4f
>>> Adding node 127.0.0.1:6386 to cluster 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
additional replica(s)
M: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385
   slots: (0 slots) master
M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
additional replica(s)
S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382
   slots: (0 slots) slave
   replicates 63e20c75984e493892265ddd2a441c81bcdc575c
S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384
   slots: (0 slots) slave
   replicates efa84a74525749b8ea20585074dda81b852e9c29
M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
additional replica(s)
S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383
   slots: (0 slots) slave
   replicates d9a79ed6204e558b2fcee78ea05218b4de006acd
[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:6386 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 127.0.0.1:6385.
[OK] New node added correctly.
Copy after login

After the addition is successful, allocate data to the new node:

[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385

How many slots do you want to move (from 1 to 16384)? 1000
What is the receiving node ID? 22e8a8e97d6f7cc7d627e577a986384d4d181a4f
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
Copy after login

The addition is completed. You can check the newly added slot distribution through the cluster nodes command

127.0.0.1:6379> cluster nodes
22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385@16385 master - 0 1555064706000 7 connected 0-332 5461-5794 10923-11255
efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555064707000 1 connected 333-5460
d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555064705000 3 connected 11256-16383
7c24e205301b38caa1ff3cd8b270a1ceb7249a2e 127.0.0.1:6386@16386 slave 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 0 1555064705000 7 connected
0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555064707000 4 connected
ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555064707236 6 connected
63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555064706000 2 connected 5795-10922
fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555064708238 5 connected
Copy after login

6. Cluster node reduction

缩减节点时先缩减从节点:

[root@localhost server41]# ./redis-cli --cluster del-node 127.0.0.1:6386 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e
>>> Removing node 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e from cluster 127.0.0.1:6386
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
Copy after login

然后进行主节点slot转移:

[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385How many slots do you want to move (from 1 to 16384)? 1000What is the receiving node ID? efa84a74525749b8ea20585074dda81b852e9c29  //要移到的节点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: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f   //要删除的主节点Source node #2: done
Copy after login

最后在缩减主节点

[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385

How many slots do you want to move (from 1 to 16384)? 1000
What is the receiving node ID? efa84a74525749b8ea20585074dda81b852e9c29  //要移到的节点
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: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f   //要删除的主节点
Source node #2: done
Copy after login

缩减完成!【相关推荐:Redis教程

The above is the detailed content of Detailed introduction to redis cluster configuration and management (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!