Home > other >  Redis cluster
Redis cluster

Time:10-07

1, case overview
Single node Redis server problems
A single point of failure, the service is not available
Unable to process a large number of concurrent data request
Data loss - catastrophe
The solution
Building Redis cluster
2, the case front knowledge
Redis cluster is introduced
Redis cluster is an offer to share data between nodes between multiple Redis assembly
Handle multiple keys Redis cluster does not support the command, because it needs to move data between different nodes, so as to reach the performance like Redis, in the case of high load may cause unpredictable errors
Redis cluster partition to provide a certain amount of availability, in a real environment when a node is down or unreachable cases can continue to process the command
The advantage of Redis cluster
Automatic segmentation data to different nodes
Part of the whole cluster node failure or inaccessible to be able to continue to process the command
Redis cluster method of
Have client subdivision
Agent shard
The server-side shard
Redis - Cluster data fragmentation
Redis cluster does not use hash consistency, but the introduction of the concept of hash slot
Redis cluster has 16384 hash slot
After each key by CRC16 check for 16384 modulus to determine slot
Each node is responsible for part of the cluster hash slot
With three nodes of the cluster as an example
The node has A 0 to 5500 hash slot
Node B contains 5501 to 11000 the hash slot
Node C contains 11001 to 16384 the hash slot
Support to add or delete node
Add or remove nodes do not need to stop the service
For example,
If you want to add A new node D need A mobile node A, B, C on the part of the tank to D
If you want to remove the node A, need to move in A slot to B and C node, then there will be no any slot of A node is removed from the cluster
Redis - Cluster of master-slave replication model
Cluster has A, B, C three nodes, if the node B failed, the whole cluster will be due to the lack of the range of 5501-11000 slots and unavailable
For each node to add a node from A1, B1, C1, and the whole cluster has three master node and slave nodes, in the node B after the failure of the cluster will be B1 election for the new master node to continue service
When B and B1 are failed, the cluster will not be available
3, case environment

4 case implementation,
Set the network parameters, shut off the firewall and selinux (all nodes)
Download and install Redis (all nodes)
The required software package redis - 4.0.11. Tar. Gz
(1) unzip
/root @ node1 ~ # tar ZXVF redis - 4.0.11. Tar. Gz - C/usr/SRC/

(2) compiled
/root @ node1 ~ # CD/usr/SRC/redis - 4.0.11/

Makefile, no additional configuration, can be directly compiled


[root @ node1 redis - 4.0.11] # make

(3) install
[root @ node1 SRC] # make install

Modify the Redis configuration file (all nodes)
/root @ node1 ~ # mkdir -p/data/redis/{7000700}/data
/root @ node1 ~ # mkdir -p/usr/local/redis cluster/bin
/root @ node1 ~ # mkdir -p/usr/local/redis cluster/7000
[root @ node1 redis - 4.0.11] # cp redis. Conf/usr/local/redis cluster/7000/

(1) to modify the main configuration file redis. Conf
Vim/usr/local/redis - cluster/7000/redis. Conf
Bind 192.168.116.152 # binding native IP
Daemonize yes # open background
Dir/data/redis/# 7000/data data storage path
The port # listening on port 7000
Pidfile/var/run/redis_7000 pid # pid file
Logfile/var/log/redis_7000 log #
the log fileCluster - enabled yes # open comments
Cluster -- config file nodes. The conf # open comments
Cluster node - a timeout on 15000 # comments
Appendonly yes # open persistence

(2) start the redis
/root @ node1 ~ #/usr/local/redis cluster/bin/redis server/usr/local/redis - cluster/7000/redis. Conf
/root @ node1 ~ #/usr/local/redis cluster/bin/redis server/usr/local/redis - cluster/7001/redis. Conf

(3) to check the state of the redis
Node1:
/root @ node1 ~ # netstat anpt | grep redis




2:


Node3:

2, Node3 with Node1 deployment redis process is the same, no longer detail,
Create Redis cluster (master1 node)
Install Ruby2.4.9 version
Install depend on environment
/root @ node1 ~ # yum - y install zlib - devel openssl devel -

Install ruby
The required software package: ruby - 2.4.9. Tar. Gz
(1) unzip
/root @ node1 ~ # tar ZXVF ruby - 2.4.9. Tar. Gz - C/usr/SRC/

(2) configuration
/root @ node1 ~ # CD/usr/SRC/ruby - 2.4.9/
[root @ node1 ~] #./configure -- prefix=/usr/local/ruby - 2.4.9

(3) compiled
/root @ node1 ~ # make

(4) install
/root @ node1 ~ # make install

(5) the optimal path
/root @ node1 ~ # ln -s/usr/local/ruby - 2.4.9/bin/ruby/usr/bin/ruby
/root @ node1 ~ # ln -s/usr/local/ruby - 2.4.9/bin/gems/usr/bin/gem
/root @ node1 ~ # gem - v
2.6.14.4
/root @ node1 ~ # ruby -v
Ruby 2.4.9 p362 (67824) the 2019-10-02 revision/x86_64 - Linux

Install redis client
/root @ node1 ~ # gem install redis

Create a redis cluster
/usr/local/redis cluster/bin/redis - trib. Rb create -- replicas 192.168.116.152:1 7000 192.168.116.152:7001 192.168.116.138:7000 192.168.116.138:7001 192.168.116.153:7000 192.168.116.153:7001

Adjust the Redis cluster master-slave relationship
Delete all the slave nodes (master1 node)
Delete node configuration file and persistence (slave1, 2, 3)
Add back from node (master1 node)
Check the new master-slave relationship (master1 node)
Test cluster data read and write
Through the client connected to the command, through cluster command to look at the state and the node information, etc.
/root @ localhost ~ #/usr/local/redis cluster/bin/redis - 192.168.116.138 cli - h - p - 7001 - c
192.168.116.138:7000 & gt; The set of k1 11
-> Redirected to slot [12706] located at 192.168.116.153:7000
OK

Write data to a hash slot 12706 inside, no matter which redis database connection, and can get to the key value,
192.168.116.153:7000 & gt; Cluster nodes

2193 cd6df9d79cd0d95900e8ec3b7a05edd08aae 192.168.116.138:7000 @ 17000 master - 0 1595598603718 3 connected. 5461-10922
C8a068aa04fe79c5bfc8f8f2776cbca76da4abf9 192.168.116.152:7000 @ 17000 master 1595598602000 connected 1 0 0-5460
4 bee60ba1bda511a8f5ddfd8e97d29c5b30176b5 192.168.116.138:7001 @ 17001 slave c8a068aa04fe79c5bfc8f8f2776cbca76da4abf9 0 1595598601000 4 connected
Bf52c192bf23ab200979c419e5fcbaf3f0de9ff7 192.168.116.153:7001 @ 17001 slave cd6df9d79cd0d95900e8ec3b7a05edd08aae 0 2193 1595598601000 6 connected
9 a7940498aee1898b8c72fa04bc13bd9250debae 192.168.116.152:7001 @ 17001 slave db014e1565d78784f0cb6eed875bb79f865b54 0 1595598602709 5 93 connected
93 db014e1565d78784f0cb6eed875bb79f865b54 192.168.116.153:7000 @ 17000 myself, master - 0 1595598603000 5 connected. 10923-16383


  • Related