1. Cluster Construction in Linux
System environment: Deepin /Ubuntu
Redis version: redis – 3.0.1
Node: Use master-slave to set up the distributed system. At least three nodes are required for the master node and at least three nodes for the slave node.
Note: This paper mainly refers to the pseudo-distributed Redis cluster built under one machine and simulated through ports.
1. Download redis-3.0.1.tar.gz from the official website or run the following command to perform operations
Wget http://download.redis.io/releases/redis-3.0.1.tar.gz tar XVZF redis - 3.0.1. Tar. GzcdRedis - 3.0.1 / make - j#apt-get install tcl
make test
Install redis on /usr/local/redis
#cd src && make PREFIX=/usr/local/redis install
Copy the code
2. Create folder redis-cluster in /usr/local/, and create folders 7001 to 7006 in redis-cluster.
cd /usr/local
mkdir redis-cluster
cd redis-cluster
mkdir 7001 7002 7003 7004 7005 7006
cp /usr/local/redis/ /usr/local/redis-cluster/7001 -r
cp /usr/local/ redis - 3.0.1 / redis. Conf/usr /local/redis-cluster/7001/bin
cp /usr/local/redis/ /usr/local/redis-cluster/7002 -r
cp /usr/local/ redis - 3.0.1 / redis. Conf/usr /local/redis-cluster/7002/bin
cp /usr/local/redis/ /usr/local/redis-cluster/7003 -r
cp /usr/local/ redis - 3.0.1 / redis. Conf/usr /local/redis-cluster/7003/bin
cp /usr/local/redis/ /usr/local/redis-cluster/7004 -r
cp /usr/local/ redis - 3.0.1 / redis. Conf/usr /local/redis-cluster/7004/bin
cp /usr/local/redis/ /usr/local/redis-cluster/7005 -r
cp /usr/local/ redis - 3.0.1 / redis. Conf/usr /local/redis-cluster/7005/bin
cp /usr/local/redis/ /usr/local/redis-cluster/7006 -r
cp /usr/local/ redis - 3.0.1 / redis. Conf/usr /local/redis-cluster/7006/bin
Copy the code
3. Open the bin/redis.conf configuration file in the 7001 to 7006 directories and modify the fields in the file, for example, 7001/bin/redis.config. Here is the configuration file for a cluster with the fewest options: Conf to the corresponding port nodess-xxx. conf and pid to pidfile./redis. Pid in the current directory.
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yesCopy the code
4. In the root directory of the installation, write the startup script, shutdown service script, and Ruby script linked to Cluster-Node.
4.1 Installing Ruby Script support
Because the official native cluster is written in the Ruby scripting language, the executable file is redis-trib.rb in the SRC directory of the downloaded file, which is known as a Ruby file by extension.
sudo apt-get install ruby
sudo apt-get install ruby gem
Copy the code
4.2 Writing startup Scripts
cd /usr/local/redis-cluster
sudo vim redis-cluster-start.sh
##redis-cluster-start.sh press I to enter insert mode
cd /usr/local/redis-cluster/7001/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/7002/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/7003/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/7004/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/7005/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/7006/bin
./redis-server redis.conf
## Press ESC to enter command mode
:wq
# redis-cluster-start.sh grant run permission to redis-cluster-start.sh
chmod u+x redis-cluster-start.sh
Copy the code
4.3 Writing a Service Termination Script
cd /usr/local/redis-cluster
sudo vim redis-cluster-stop.sh
##redis-cluster-start.sh press I to enter insert mode
cd /usr/local/redis-cluster/7001/bin
./redis-cli -p 7001 shutdown
cd /usr/local/redis-cluster/7002/bin
./redis-cli -p 7002 shutdown
cd /usr/local/redis-cluster/7003/bin
./redis-cli -p 7003 shutdown
cd /usr/local/redis-cluster/7004/bin
./redis-cli -p 7004 shutdown
cd /usr/local/redis-cluster/7005/bin
./redis-cli -p 7005 shutdown
cd /usr/local/redis-cluster/7006/bin
./redis-cli -p 7006 shutdown
## Press ESC to enter command mode
:wq
# redis-cluster-start.sh grant run permission to redis-cluster-start.sh
chmod u+x redis-cluster-stop.shCopy the code
5 Start services and establish a cluster-connection
. / redis - cluster - start. Sh ~ / redis - 3.0.1 / SRCRb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006M: 1984 c27297c6ef50bbfcbd35c11b93cc40ba17e4 127.0.0.1:7001 slots: 0-5460 (5461 slots) master M: 481 e256be4c724f5a2c64a761e52b4be61ca45b4 127.0.0.1:7002 slots: 5461-10922 (5462 slots) master M: B5b652fa02d9999861e66c843b01fd2700c02adf 127.0.0.1:7003 slots: 10923-16383 (5461 slots) master S: 821 ec823dc0c2d4f65319e84fe74157fb1014155 127.0.0.1:7004 replicates 1984 c27297c6ef50bbfcbd35c11b93cc40ba17e4 S: B3b8541b9520d707180d56a2fb3cf3ee6895ed10 127.0.0.1:7005 replicates 481 e256be4c724f5a2c64a761e52b4be61ca45b4 S: D2b437ca8b9007dcdb63ac16210f6540860361e3 127.0.0.1:7006 replicates b5b652fa02d9999861e66c843b01fd2700c02adf Can Iset the above configuration? (type 'yes' to accept):
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Copy the code
6 Check and use
The advantage of the native cluster is that the client only needs to connect to a node to use the cluster, as described in the next chapter.
Connect arbitrarily to a node.
cd /usr/local/redis-cluster/7001/bin
./redis-cli -p 7006 -c
If the following content appears, the configuration is complete127.0.0.1:7006 > 127.0.0.1:7006 >setKey1 Value1 OK 127.0.0.1:7006>get KEY1 ->Redirected to slot [2922] located at 127.0.0.1:7001"value1"Copy the code
The principle of Redis-Cluster
Redis – Cluster architecture diagram
Structure Analysis:
- The nodes in Redis are connected to each other using the PING PONG mechanism, and internal use of binary protocols to optimize the transmission speed and framing.
- The node fail is passed through the urgent masses more than half of the node detection failure before failure, through the voting mechanism.
- The client is directly connected to the Redis node, no intermediate proxy layer is required, the client only needs to connect to any available node
- Redis-cluster maps all nodes to 16384 slots (hash slots) in the range 0-16383. The Cluster maintains node<->slot<->value.
Principle Introduction:
The redis cluster has 16384 hash slots. When a key-value needs to be placed in the redis cluster, the redis first uses CRC16 algorithm to calculate a result a for the key, and then calculates the remainder of 16384 for a, so that each key will fall into the hash slot [0,16383]. Redis maps hash slots to different nodes roughly equally based on the number of nodes.
For more information, please refer to the official website