redis - redis cluster setup

1, Create profile

1. Create a myredis folder:

mkdir /myredis

2. Copy a native redis.conf configuration file to the myredis folder

cp /etc/redis.conf /myredis/redis.conf

3. Create 9 redis profiles in myredis folder:

redis + port.conf indicates the configuration files of different ports


4. Edit profile

redis6379.conf :

include /myredis/redis.conf
port 6379
pidfile "/var/run/"
dbfilename "dump6379.rdb"
cluster-enabled yes		--Open cluster mode
cluster-config-file nodes-6379.conf		--Set node profile name
cluster-node-timeout  15000  --Set node loss time(ms),After this time, the cluster will automatically switch between master and slave nodes

The configuration files of other ports are similar to redis6379.conf. You only need to replace 6379 with the corresponding port

Replace command:

%s/6379/6380 --Replace 6379 with 6380. Other configuration files are similar

2, Create cluster

1. Start all redis services

redis-server /myredis/redis6379.conf
redis-server /myredis/redis6380.conf
redis-server /myredis/redis6381.conf
redis-server /myredis/redis6382.conf
redis-server /myredis/redis6383.conf
redis-server /myredis/redis6384.conf
redis-server /myredis/redis6385.conf
redis-server /myredis/redis6386.conf
redis-server /myredis/redis6387.conf

View the files in myredis and check whether all node configuration files have been generated; If there is no corresponding node configuration file, restart the redis service of the node

2. Merge all nodes to create a cluster

2.1. Enter the src folder of redis

This folder is the src folder under redis-x.x.x obtained when extracting the redis installation package

cd /opt/redis-6.2.6//src

2.2. Execute the merge command

Check whether there is a redis-trib.rb file in the src directory. If not, install it yourself (redis6 is used here for the advanced version of redis with its own RB environment)

redis-cli --cluster create --cluster-replicas 2 ip:port [ip:port...]

Where 2 indicates that a node contains 1 server and 2 slave servers; Therefore, nine servers have three nodes, and each node contains one server and two slave servers

redis-cli --cluster create --cluster-replicas 2

2.3. Enter yes when a query appears

Can I set the above configuration? (type 'yes' to accept): yes

3, Connection cluster

1. Connect any port to enter the cluster

redis-cli -c -p 6379

2. View cluster node information

cluster nodes

4, slots

  • A redis cluster contains 16384 hash slot s, and each key in the database belongs to one of 16384.

  • Node A handles slots 0 - 5460, node B handles slots 5461 - 10922, and node C handles slots 10923 - 16383

  • When we save data to the redis cluster, the cluster calculates which slot the key should belong to through the formula CRC16 (key)% 16384, and then stores the key in the node of the corresponding slot

You can view the slot corresponding to each node through the cluster nodes command

  • As can be seen from the above figure, 6379, 6380 and 6381 are the main nodes

  • (6383 + 6385), (6386 + 6387) and (6382 + 6384) are the slave nodes of 6379, 6380 and 6381 respectively

  • 6379 handles slots 0 - 5460

  • The 6380 handles slots 5461 - 10922

  • 6381 handles slots 10923 ~ 16383

Cluster operation

1. Insert a value in the cluster at the same time

As shown in the figure below, when set test mytest is executed, test is calculated. Its corresponding slot is 6918, and 6918 is in the charge of node 80. Therefore, the cluster gives this operation to node 80 and stores the key in node 80

2. Insert multiple values simultaneously in the cluster

redis cluster cannot assign values through conventional commands

You can add multiple values by grouping

Assignment: mset key{group} value [key{group} value...]

Value: mget key{group} [key{group}]

3. Calculate the slot value of the key

cluster keyslot key

Tags: Redis

Posted on Sun, 21 Nov 2021 20:10:53 -0500 by nishmgopal