This is the fifth day of my participation in the August Wen Challenge.More challenges in August
1. Download redis
Please use brew Install Redis or download redis 4.0.11 is the latest Stable version from the official website
2. Create a directory and configuration file
We are going to create six nodes and, for convenience, six working directories.
cd ~
mkdir redisCluter
cd redisCluster
mkdir 7000 7001 7002 7003 7004
Copy the code
The directory is created, and the directory name is the port number. Next, you need to configure a “profile” for each node.
In cluster mode, you need to modify the following configurations.
Port 700X # Cluster mode cluster-enabled yes # Node timeout actual, in milliseconds cluster-node-timeout5000Conf file (default nodes.conf) cluster-config-file nodes.conf # start AOF appendonly yesCopy the code
Copy the configuration files you downloaded from the redis directory (usually in /usr/local/redis-xxx/redis.conf) to each directory and modify them one by one.
Then, enter each directory one by one and execute the command:
redis-server redis.conf
Copy the code
At this point, several files appear under each directory:This is similar to the figure, but there may be no RDB file, because this is generated when shutdown redis.
3. Associate all nodes
We have just started all the nodes, but at this point they are all single cluster nodes independent of each other. To implement the cluster, you must associate them and go to the redis-CLI of any node.
Execute the following command:
➜ 7002 redis-cli -p 7000
127.0. 01.:7000> cluster meet 127.0. 01. 7001
OK
127.0. 01.:7000> cluster meet 127.0. 01. 7002
OK
127.0. 01.:7000> cluster meet 127.0. 01. 7003
OK
127.0. 01.:7000> cluster meet 127.0. 01. 7004
OK
127.0. 01.:7000> cluster meet 127.0. 01. 7005
OK
Copy the code
At this point, all the nodes are associated.
4. Distribution of slot
As we know, the Redis Cluster is composed of 16384 slots, so we need to spread these slots among the three nodes (3 master nodes and 3 slave nodes).
Execute command:
➜ 7002 redis-cli -p 7000 cluster addslots {0.5461.}
➜ 7002 redis-cli -p 7001 cluster addslots {546.10922.}
➜ 7002 redis-cli -p 7002 cluster addslots {10923.16383.}
Copy the code
At this point, the node is already allocated. Verify this by running the following command:
redis-cli -p 7000 cluster nodes
Copy the code
7004,7000, 7005, 7005, 7004, 7005, 7001, 7002, 7001, 7002 I can skip it.
5. Master/slave replication
The master node already has slots, so the last step is to associate the master node with the slave node to form a master-slave replication relationship. The command is as follows:
Note: The primary node needs to be associated in the CLI window of the secondary node. You can’t do it the other way around.
redis-cli -p 7003 cluster replicate 7000The NodeID of redis - cli - p7004 cluster replicate 7001The NodeID of redis - cli - p7005 cluster replicate 7002The NodeIDCopy the code
this
7000The NodeIDCopy the code
7001The NodeIDCopy the code
7002The NodeIDCopy the code
It’s really execution
redis-cli -p 7000 cluster nodes
Copy the code
The hexadecimal string in which the command appears.As shown above.
If all goes well, do it again
redis-cli -p 7000 cluster nodes
Copy the code
Command, you will see:Note the red box: master slave, followed by the master NodeId.
conclusion
Manually setting up a Redis Cluster is cumbersome, requiring downloading, configuration files, starting nodes, managing nodes, allocating slots, managing master/slave replicates, etc. Redis provides a small tool: Redis -trib.rb, written in Ruby, internal through these commands, direct use of commands to help us understand the principle of Redis Cluster.
To quickly build a cluster using redis-trib.rb, first you need to configure redis and start it
127.0.0.1:6400
127.0.0.1:6401
127.0.0.1:6402
127.0.0.1:6403
127.0.0.1:6404
127.0.0.1:6405
Redis-trib.rb is in the SRC file in the redis installation directoryThe installation depends on sudo gem install redis