Install Redis on Linux and set up a cluster

Second, set up Redis cluster

1. Install Ruby related environments and packages (Ruby installation has a lot of holes, so install ruby first)

(1). Install ruby environment

yum install ruby
yum install rubygems
Copy the code

(2). Install ruby packages

The gem install redis - 5.0.3. GemCopy the code
2. Create a cluster node file directory

Create the 6001 6002 6003 6004 6005 6006 directory in the /usr/local/redis-cluster directory

mkdir 6001 6002 6003 6004 6005 6006
Copy the code
3. Copy the configuration file

From the installed redis, copy the redis. Conf file to the six directories above

 cp  redis.conf  /usr/local/redis-cluster/6001/6001.conf 
Copy the code

Modify the Redis configuration file

Port 6001 # redis daemonize yes # pid pidfile Pid # enable cluster cluster-enabled yes # Automatically generate cluster-config-file when the configuration file is started for the first time /user.local/redis-cluster/6001/nodes_6001.conf # cluster timeout cluster-node-timeout 15000 Appendonly yes appendfilename "appendonly-6001.aof" loglevel notice logfile ""Copy the code

For the same reason, modify node configuration files in other folders.

[root@localhost local]# CD redis-cluster/ [root@localhost redis-cluster]# ll total amount 24 drwxr-xr-x. 2 root root 4096 2月 1 日 06:55 6001 DRWXr-Xr-x. 2 Root root 4096 Feb 1 06:56 6002 DRWXr-xr-x. 2 root root 4096 Feb 1 06:56 6003 DRWXr-Xr-x. 2 root Root 4096 February 1 06:56 6004 drWxr-Xr-x. 2 Root root 4096 February 1 06:56 6005 DRWxr-xr-x. 2 root root 4096 February 1 06:57 6006 [root@localhost redis-cluster]# CD 6001 [root@localhost 6001]# ll total usage 64-rw-r --r--. 1 root root 62156 2月 1 06:55 6001.conf [root@localhost 6001]# vim 6001.conf [root@localhost 6001]# vim 6001.conf [root@localhost 6001]# cp 6001.conf Conf cp: Check whether /usr/local/redis-cluster/6002/6002.conf is overwritten. Y [root@localhost 6001]# cp 6001.conf /usr/local/redis-cluster/6003/6003.conf cp: Is /usr/local/redis-cluster/6003/6003.conf overwritten? Y [root@localhost 6001]# cp 6001.conf /usr/local/redis-cluster/6004/6004.conf Is /usr/local/redis-cluster/6004/6004.conf overwritten? Y [root@localhost 6001]# cp 6001.conf /usr/local/redis-cluster/6005/6005.conf Is /usr/local/redis-cluster/6005/6005.conf overwritten? Y [root@localhost 6001]# cp 6001.conf /usr/local/redis-cluster/6006/6006.conf Is /usr/local/redis-cluster/6006/6006.conf overwritten? y [root@localhost 6001]# cd .. [root@localhost redis-cluster]# cd 6002 [root@localhost 6002]# vim 6002.confCopy the code
4. The nodes are configured and services start
[root@localhost local]# CD Redis-5.0.3 [root@localhost redis-5.0.3]# ls 00-releasenotes CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf Tests [root@localhost redis-5.0.3]# CD SRC [root@localhost SRC]#./redis-server /usr/local/redis-cluster/6001/6001.conf 6719:C 01 Feb 2019 07:47:41.749 # oO0OoO0OoO0Oo Redis is a starting point for oO0OoO0OoO0Oo Redis Version =5.0.3, bits=32, commit=00000000, Modified =0, PID =6719, Just started 6719:C 01 Feb 2019 07:47:41.749 # Configuration Loaded [root@localhost SRC]#./ Redis-server /usr/local/redis-cluster/6002/6002.conf 6724:C 01 Feb 2019 07:47:55.045 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 6724:C 01 Feb 2019 07:47:55 # Redis version=5.0.3, bits=32, commit=00000000, modified=0, PID =6724, Just started 6724:C 01 Feb 2019 07:47:55.045 # Configuration Loaded [root@localhost SRC]#./ Redis-server /usr/local/redis-cluster/6003/6003.conf 6729:C 01 Feb 2019 07:48:08.772 # oO0OoO0OoO0Oo 6729:C 01 Feb 2019 07:48:08.772 # Redis version=5.0.3, bits=32, commit=00000000, modified=0, PID =6729, Just started 6729:C 01 Feb 2019 07:48:08.772 # Configuration loaded [root@localhost SRC]#./ Redis-server /usr/local/redis-cluster/6004/6004.conf 6734:C 01 Feb 2019 07:48:21.169 # oO0OoO0OoO0Oo 6734:C 01 Feb 2019 07:48:21 # Redis version=5.0.3, bits=32, commit=00000000, modified=0, PID =6734, Just started 6734:C 01 Feb 2019 07:48:21.169 # Configuration loaded [root@localhost SRC]#./ Redis-server /usr/local/redis-cluster/6005/6005.conf 6740:C 01 Feb 2019 07:48:33.366 # oO0OoO0OoO0Oo 6740:C 01 Feb 2019 07:48:33 # Redis version=5.0.3, bits=32, commit=00000000, modified=0, PID =6740, Just started 6740:C 01 Feb 2019 07:48:33.366 # Configuration Loaded [root@localhost SRC]#./ Redis-server /usr/local/redis-cluster/6006/6006.conf 6745:C 01 Feb 2019 07:48:52.011 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 6745:C 01 Feb 2019 07:48:52.2 # Redis version=5.0.3, bits=32, commit=00000000, modified=0, PID =6745, Just started 6745:C 01 Feb 2019 07:48:52.011 # Configuration loadedCopy the code

The service is started with redis-server in the SRC directory under the redis decompression package

[root@localhost src]# ps -ef | grep redis root 6018 1 0 06:45 ? 00:00:07./bin/redis-server 127.0.0.1:6379 root 6720 1 0 07:47? 00:00:00./redis-server 192.168.0.105:6001 [cluster] root 6725 10 07:47? 00:00:00./redis-server 192.168.0.105:6002 [cluster] root 6730 10 07:48? 00:00:00./redis-server 192.168.0.105:6003 [cluster] root 6735 10 07:48? 00:00:00./redis-server 192.168.0.105:6004 [cluster] root 6741 10 07:48? 00:00:00./redis-server 192.168.0.105:6005 [cluster] root 6746 10 07:48? 00:00:00./redis-server 192.168.0.105:6006 [cluster] root 6756 6039 0 07:50 PTS /0 00:00:00 grep redisCopy the code
Redis create cluster (note IP address and port number)
[root@localhost SRC]#./redis-trib.rb create -- replicas 1 192.168.0.105:6001 192.168.0.105:6002 192.168.0.105:6003 192.168.0.105 192.168.0.105:6004 192.168.0.105:6005:6006Copy the code

Issue the following warnings:

WARNING: redis-trib.rb is not longer available! You should use redis-cli instead. All commands and features belonging to redis-trib.rb have been moved to redis-cli. In order to use them you should call redis-cli with the --cluster option followed by the subcommand name, arguments and options. Use the following syntax: redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS] Example: Redis -cli --cluster create replicas 1 192.168.0.105:6001 192.168.0.105:6002 192.168.0.105:6003 192.168.0.105:6004 192.168.0.105:6005 192.168.0.105:6006 To get help about all subcommands, type: redis-cli --cluster helpCopy the code

Cause: This command to create a cluster is before redis5, my version is redis5.0.3, after 5 changed to the following command to create a cluster.

[root@localhost SRC]#./redis-cli --cluster create 192.168.0.105:6001 192.168.0.105:6002 192.168.0.105:6003 192.168.0.105:6004 192.168.0.105:6005 192.168.0.105:6006 --cluster-replicas 1Copy the code

If the following command appears, your cluster has been set up successfully!

>>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 192.168.0.105:6004 to 192.168.0.105:6001 Adding Replica 192.168.0.105:6005 to 192.168.0.105:6002 Adding Replica >>> Trying to optimize Slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: Caf410b20574f7b714d252df872e5bf8b4a61a74 192.168.0.105:6001 slots: [0-5460] (5461 slots) master M: 138 c98e4d1d1fe9c604fe777e8a22573b93d69cd 192.168.0.105:6002 slots: [5461-10922] (5462 slots) master M: A2cccb5d178a1135e0002929c4a8795567003164 192.168.0.105:6003 slots: [10923-16383] (5461 slots) master S: 50037 c3a79a91c5255119d2fdf47c4c7f73b396d 192.168.0.105:6004 replicates a2cccb5d178a1135e0002929c4a8795567003164 S: 5 d681dcdf6d9bb4a8e5df6252bb50b07040a273a 192.168.0.105:6005 replicates caf410b20574f7b714d252df872e5bf8b4a61a74 S: 138 c98e4d1d1fe9c604fe777e8a22573b93d69cd dc334ec3adec10a6c7cf85c84aaf5011e751e20f 192.168.0.105:6006 replicates 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 192.168.0.105:6001) M: Caf410b20574f7b714d252df872e5bf8b4a61a74 192.168.0.105:6001 slots: [0-5460] (5461 slots) master 13814765030593265665 additional replica(s) M: 138 c98e4d1d1fe9c604fe777e8a22573b93d69cd 192.168.0.105:6002 slots: [5461-10922] (5462 slots) master 685037151884148737 Additional up (s) s: 5 d681dcdf6d9bb4a8e5df6252bb50b07040a273a 192.168.0.105:6005 slots: (0 slots) slave replicates caf410b20574f7b714d252df872e5bf8b4a61a74 M: A2cccb5d178a1135e0002929c4a8795567003164 192.168.0.105:6003 slots: [10923-16383] (5461 slots) master 685038113956823041 Additional up (s) s: 50037 c3a79a91c5255119d2fdf47c4c7f73b396d 192.168.0.105:6004 slots: (0 slots) slave replicates a2cccb5d178a1135e0002929c4a8795567003164 S: Dc334ec3adec10a6c7cf85c84aaf5011e751e20f 192.168.0.105:6006 slots: (0 slots) slave replicates 138c98e4d1d1fe9c604fe777e8a22573b93d69cd [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.Copy the code

In the –replicas 1,1 command, it represents a ratio of primary to secondary nodes. Slot For a Redis cluster, a place to store data corresponds to a slot. There is a slot range for each Master, but not for the Slave. In the Redis cluster, the Master can still read and write, while the Slave is read-only. Data writes are actually distributed and stored in slots.

6. Data testing

Log in to a cluster client. -c indicates the login in cluster mode

/redis-cli -h 192.168.0.105 -p 6001 -cCopy the code
[root@localhost SRC]#./redis-cli -h 192.168.0.105 -p 6001 -c 192.168.0.105:6001> set redis cluster-test OK 192.168.0.105:6001> quit [root@localhost SRC]#./redis-cli -h 192.168.0.105 -p 6003 -c 192.168.0.105:6003> get redis- > Redirected to slot [1151] located at 192.168.0.0.5:6001 "cluster-test" 192.168.0.105:6001>Copy the code

In the design of Redis Cluster, decentralization and de-middleware have been taken into account. That is to say, each node in the cluster is equal and peer, and each node stores its own data and the status of the whole cluster. Each node is connected to all the other nodes, and these connections remain active, ensuring that we only need to connect to any node in the cluster to get data from the other nodes.

The Redis cluster does not allocate data using traditional consistent hashes. Instead, it allocates data using hash slots. Consistent hashes work well for adding and removing instances to and from the cluster, but hash slots require additional operations. Slots need to be manually re-evenly allocated to the new cluster instances.

The Redis cluster stores data on a master node and synchronizes data between the master and its corresponding Salve. When data is read, data is also obtained from the corresponding master node according to the consistent hash algorithm. Only when a master fails does a corresponding Salve node start to act as the master.

Note that there must be 3 or more primary nodes, otherwise the cluster will fail to be created, and when the number of surviving primary nodes is less than half of the total, the cluster will become unserviceable.

Redis Cluster has a concept of 16384 length slots, they are numbered 0, 1, 2, 3… In 16382, 16383. This slot is a virtual slot, not a real one. Each Master node in the Redis Cluster is responsible for a number of slots. When a key is mapped to a slot for which the Master is responsible, the Master is responsible for providing services for the key. Which Master node is responsible for which slot can be specified by the user. It can also be generated automatically at initialization (the redis-trib.rb script). It is worth mentioning that in the Redis Cluster, only the Master has the ownership of the slot. If it is the slave of a Master, the slave is only responsible for the use of the slot, but has no ownership.

7. Add a node to the cluster
  • Adding a Primary Node
# ./redis-cli --cluster add-node new_host:new_port existing_host:existing_port
# ./redis-cli --cluster add-node 192.168.0.105:6007 192.168.0.105:6001
Copy the code

192.168.0.105:6007 is a new node. 192.168.0.105:6001 Is an old node in the cluster

  • Added cluster node
# ./redis-cli --cluster add-node new_host:new_port existing_host:existing_port \ --cluster-slave [--cluster-master-id < master_id >] #. / redis - cli add - node, slave, master - id e35403b9171c00c92935a7ba15cf90000f511b88 192.168.0.105:6008 192.168.0.105:6001Copy the code

– slave, said add – master – from node id e35403b9171c00c92935a7ba15cf90000f511b88, the master node node id, The node ID of the newly added 6008 is 192.168.0.105:6008. The new node 192.168.0.105:6001 is an old node in the cluster

Here I demonstrate the celestial master node: copy the 6001 file to 6007, and then change the 6007.conf file to 6007 as well. Before adding a redis instance to the cluster, make sure that the redis instance does not store data and cannot persist data files. Otherwise, it will cause an error when adding a Redis instance.

/redis-cli --cluster add-node 192.168.0.105:6007 192.168.0.105:6001Copy the code
  • Reassign slot

// How many slots do you want to move (from 1 to 16384)? 1000 // Set the number of slots 1000 What is the receiving node ID? E35403b9171c00c92935a7ba15cf90000f511b88 / / new node node id both Please enter all the source node IDs. The Type to use all the ‘all’ Nodes as source nodes for the Hash slots. Type ‘done’ once you enter all the source nodes IDs. Source node #1:all Do you want to proceed with the proposed reshard plan (yes/no)? Yes // Confirm rescheduling

  • Check the cluster status
[root@localhost SRC]#./redis-cli -h 192.168.0.105 -p 6001 -c 192.168.0.105:6001> Cluster Nodes E35403b9171c00c92935a7ba15cf90000f511b88 192.168.0.105:6007 @ 16007 master - 0, 1549056838000 connected 0-332. 5461-5794 10923-11255-5 d681dcdf6d9bb4a8e5df6252bb50b07040a273a 192.168.0.105:6005 @ 16005 slave caf410b20574f7b714d252df872e5bf8b4a61a74 0 1549056836851 5 connected dc334ec3adec10a6c7cf85c84aaf5011e751e20f 192.168.0.105:6006 @ 16006 slave c98e4d1d1fe9c604fe777e8a22573b93d69cd 0 138 1549056838868 6 connected Caf410b20574f7b714d252df872e5bf8b4a61a74 192.168.0.105:6001 @ 16001 myself, master 1549056835000-0 1 connected. 333-5460 50037 c3a79a91c5255119d2fdf47c4c7f73b396d 192.168.0.105:6004 @ 16004 slave a2cccb5d178a1135e0002929c4a8795567003164 0 1549056838000 4 connected 138 c98e4d1d1fe9c604fe777e8a22573b93d69cd 192.168.0.105:6002 @ 16002 master - 0 1549056839877 2 Connected to 5795-10922 a2cccb5d178a1135e0002929c4a8795567003164 192.168.0.105:6003 @ 16003 master 1549056838000 3-0 connected 11256-16383Copy the code

Description Slot 192.168.0.105:6007 was successfully allocated

7. Delete a node from the cluster
  • Deleting a secondary Node
/redis-cli --cluster del-node host:port <node-id>./redis-cli --cluster del-node 192.168.0.105:6007 'e35403b9171c00c92935a7ba15cf90000f511b88'Copy the code
  • Deleting the primary Node

If the master node has slave nodes, move the slave node to another master node. If the master node has slots, remove the allocated slots, and then delete the master node

#./redis-cli reshard 192.168.0.105:6007 // Unassign slot

How many slots do you want to move (from 1 to 16384)? 1000 // Number of slots to be removed from the receiving node. E35403b9171c00c92935a7ba15cf90000f511b88 / / receiving 6007 node slot master both 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 enter all the source nodes IDs. Source node # 1: e35403b9171c00c92935a7ba15cf90000f511b88 / / deleted the master node – id the Source node # 2: done

Do you want to proceed with the proposed reshard plan (yes/no)? Yes // Reshard does the same for the new master node after removing slot. The opposite.

# redis – trib. Rb del -node 192.168.0.105:6007 ‘e35403b9171c00c92935a7ba15cf90000f511b88’ new master node is deleted, so back to, is the beginning, this article The status of the node is not added