Construction of Redis Cluster
I. Redis Download, Installation, Startup (Single Example)
We installed Redis in the / opt directory and executed the following commands:
$ cd /opt $ wget http://download.redis.io/releases/redis-4.0.9.tar.gz $ tar zxvf redis-4.0.9.tar.gz $ cd redis-4.0.9 $ make
Now that the installation is complete, let's start the Redis service:
$ cd /opt/redis-4.0.9 $ ./src/redis-server
Redis started successfully and will be configured by default. You can also modify the redis.conf file in the Redis root directory and specify the configuration file when Redis starts, as follows:
$ ./src/redis-server redis.conf
Note that due to Redis's protection mode, only 127.0.0.1 of the local machine is bound, which cannot be accessed from other machines. So we need to add native ip
192.168.xxx.xxx.
II. Redis Cluster (Cluster) Construction
Because our machines are limited, we will build our Redis cluster in a machine with multiple ports.
First, we create the Redis configuration file directory as follows:
$ cd /opt $ mkdir redis-cluster
In the redis-cluster directory, the configuration files of six nodes are created. They are:
- redis-7000.conf
- redis-7001.conf
- redis-7002.conf
- redis-7003.conf
- redis-7004.conf
- redis-7005.conf
The following 7000,7001 is the port number of redis startup. Next edit the contents of the file:
#Ports for this cluster phase port 7000 #Specify a pid_file for each cluster node pidfile /var/run/redis_7000.pid #Add native ip after bind instruction bind 127.0.0.1 149.28.37.147 #Find the code snippet for Cluster configuration to enable Redis to support clustering cluster-enabled yes #Each cluster node has a configuration file that cannot be edited manually. Ensure that the configuration files for each cluster node are not accessible cluster-config-file nodes-7000.conf #Cluster node's timeout time, unit: ms, after the timeout, the cluster will think that the node failed cluster-node-timeout 5000 #Finally, change appendonly to yes appendonly yes
The configuration of such a node is completed, and the other five nodes do the same. Redis instances of six nodes are started:
$ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7000.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7001.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7002.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7003.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7004.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7005.conf &
Use these six nodes to create clusters:
$ /opt/redis-4.0.9/src/redis-trib.rb create --replicas 1 149.28.37.147:7000 149.28.37.147:7001 149.28.37.147:7002 149.28.37.147:7003 149.28.37.147:7004 149.28.37.147:7005
--replicas 1 indicates that we want to create a slave node for each master node in the cluster.
When the command is executed, it will show:
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 149.28.37.147:7000 149.28.37.147:7001 149.28.37.147:7002 Adding replica 149.28.37.147:7004 to 149.28.37.147:7000 Adding replica 149.28.37.147:7005 to 149.28.37.147:7001 Adding replica 149.28.37.147:7003 to 149.28.37.147:7002 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 65625091304b0fa2dd75a00f62b6aceac1701094 149.28.37.147:7000 slots:0-5460 (5461 slots) master M: 4da569bf8402e4f75ab6e0fe7076919c22e3f900 149.28.37.147:7001 slots:5461-10922 (5462 slots) master M: b977680e24f23f8fec96876d9014803ca752e2e2 149.28.37.147:7002 slots:10923-16383 (5461 slots) master S: 7183e47a64bca23157140229352455d1a1407dc2 149.28.37.147:7003 replicates b977680e24f23f8fec96876d9014803ca752e2e2 S: b2f916a643fefef1d43dbd1ef5d22f72c0ee43d6 149.28.37.147:7004 replicates 65625091304b0fa2dd75a00f62b6aceac1701094 S: e362d9aae5fe3e9c343d266a5ab952272e0e37b0 149.28.37.147:7005 replicates 4da569bf8402e4f75ab6e0fe7076919c22e3f900 Can I set the above configuration? (type 'yes' to accept):
We enter yes and return:
>>> 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 149.28.37.147:7000) M: 65625091304b0fa2dd75a00f62b6aceac1701094 149.28.37.147:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) M: b977680e24f23f8fec96876d9014803ca752e2e2 149.28.37.147:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: e362d9aae5fe3e9c343d266a5ab952272e0e37b0 149.28.37.147:7005 slots: (0 slots) slave replicates 4da569bf8402e4f75ab6e0fe7076919c22e3f900 S: b2f916a643fefef1d43dbd1ef5d22f72c0ee43d6 149.28.37.147:7004 slots: (0 slots) slave replicates 65625091304b0fa2dd75a00f62b6aceac1701094 M: 4da569bf8402e4f75ab6e0fe7076919c22e3f900 149.28.37.147:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 7183e47a64bca23157140229352455d1a1407dc2 149.28.37.147:7003 slots: (0 slots) slave replicates b977680e24f23f8fec96876d9014803ca752e2e2 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
The cluster has been built. Spring-Boot is a very convenient way to access the Redis cluster.
3. Ruby version is too low
When using redis-trib.rb, you need to install ruby:
$ yum -y install ruby ruby-devel rubygems rpm-build $ gem install redis
This is an error that redis requires Ruby version >= 2.2.2. Let's install rvm first:
$ curl -L get.rvm.io | bash -s stable $ source /usr/local/rvm/scripts/rvm
View version
rvm list known
Install Version 2.4.1
rvm install 2.4.1
Use version 2.4.1
rvm use 2.4.1
Remove version 2.0.0
rvm remove 2.0.0
View the current ruby Version
ruby --version
Just install redis again.
gem install redis