I. Overview
Introduced in the previous article, the main redis from the Sentinel and two kinds of cluster solution, redis Version 3.0 introduces redis-cluster (cluster). From the master-slave - you can see continuous improvement redis clusters - Sentinel; master-slave replication is the simplest node synchronization scheme can not be automatically transferred from the main fault. Sentinel can manage a plurality of master-slave synchronization scheme also can be processed automatically transferred from the main fault, a single point of network failure can be solved by arranging a plurality of problems sentinel nodes, but the performance of a single node pressure can not be solved. Cluster solved all the problems in front of the two programs.
Redis-Cluster architecture diagram:
1.Redis-Cluster-free central structure, each node and the other nodes remain connected by cross-ping, each node stores the state information of the entire cluster, can be read or written by any node connected to the data (or no data empty node).
2. Only when the majority of the cluster nodes simultaneously fail before the entire cluster fail.
3. There are 16,384 slot entire cluster, when to be placed in a key-value Redis cluster, based on the value CRC16 (key) mod 16384, decided to which a key into the tub. Is the same as when reading a key algorithm.
4. When the node will fail the master node from the master node to upgrade, then the fail online automatically become the master node from the node.
Second, the cluster installation
1. Plug Installation
yum -y install ruby rubygems
Next week ruby dependent redis.gem
wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.2.gem
Installation redis.gem
gem install redis-3.2.2.gem
Note: Do not use gem install redis to the default installation, the default installation may install redis-4.0.0 version. Before installing the default version of time because excessive lead reshard has failed. You can refer to: http://www.cnblogs.com/chenmh/p/9221672.html
Install redis-stat
gem install redis-stat
If the operating system 7.0 or later, probably because of the way yum install ruby version is too low, reported " Redis The requires Ruby Version> = 2.2.2 " error, then you can deal with this:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
\curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
View version that can be installed:
rvm list known
Wherein a version of the installation, can be greater than the version 2.2.2
rvm install 2.4.1
View installed gem, ruby version
ruby -v gem -v
Gem install execution
gem install redis
2. Install redis
tar -xvf redis-3.0.7.tar.gz cd redis-3.0.7 make MALLOC=libc cp src/redis-server redis-cli redis-trib.rb redis-benchmark redis-check-aof redis-check-rdb redis-sentinel /usr/local/bin
Create a directory redis
mkdir -p /redis/redis-cluster/ cd /redis/redis-cluster/ mkdir -p 7001/data 7001/log 7001/tmp cp -rp 7002 cp -rp 7003 cp -rp 8001
Create a profile
7001 CD force redis.conf
port 7001 daemonize yes timeout 300 databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes lua-time-limit 5000 bind 192.168.191.11 127.0.0.1
######################################limit####################### maxclients 10000 maxmemory 512mb maxmemory-policy volatile-ttl maxmemory-samples 5 #######################################file####################### pidfile "/redis/redis-cluster/7001/tmp/redis_7001.pid" loglevel notice logfile "/redis/redis-cluster/7001/log/redis_7001.log" dbfilename "dump.rdb" dir "/redis/redis-cluster/7001/data" appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb ######################################replication########################### slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass "123456" masterauth "123456" ######################################slowlog################################ slowlog-log-slower-than 1000 slowlog-max-len 128 latency-monitor-threshold 0 ####################################cluster#################################### cluster-enabled yes cluster-config-file "/redis/redis-cluster/7001/nodes.conf" cluster-node-timeout 5000
Redis.conf Copy profile to several other nodes and modify information corresponding to the port, the path and the like. slowlog unit is subtle
Note: Note the ip bind behind the order, if the configuration 127.0.0.1 in front, then the cluster is enabled by default 127.0.0.1 will cause remote inaccessible.
3. Start redis clusters
Start all of the cluster redis
./redis-server redis.conf
You can be written in bash startup script file
Third, cluster management
1. Create a cluster
redis-trib.rb create 192.168.191.11:7001 192.168.191.11:7002 192.168.191.11:7003
If you add parameters --replicas 1 represents the creation of a node from the master node for each
Note: In fact, not recommended -replicas 1 to automatically create a master-slave relationship, because this is no way to specify which method to create the master node from which the corresponding node, can not control the cross from the master. You can create a good master node, and then specify the master node by node from the creation.
2. cluster configuration cryptographic processing
If the default redis cluster node is configured with password authentication, redis-trib.rb operation fails, we need to modify client.rb file node redis-trib.rb need to perform the operation, as follows:
find / -name 'client.rb'
vim /usr/lib/ruby/gems/1.8/gems/redis-3.2.1/lib/redis/client.rb
Note: Since each person is different so the installed version may be different route, setting up authentication password behind the passwd parameters, note the password for all nodes must be the same.
3. Check the cluster
redis-trib.rb check 192.168.191.11:7001
4. Add Node
1. increase the master node
redis-trib.rb add-node 192.168.191.11:7004 192.168.191.11:7001
Note: after the last ip and port is already present in the cluster nodes, in front of the ip and port is to be increased.
2. Increase from node
---- node increases from redis-trib.rb add-node --slave 192.168.191.11:8001 192.168.191.11:7001 ---- increases and designated master node from redis-trib.rb add-node --slave --master-id 64ee652b75a78fc08253fddab5a920988a870f68 192.168.191.11:8001 192.168.191.11:7001
Note: The last plane ip and port is now a cluster node already exists
The mobile slot
redis-trib.rb reshard 192.168.191.11:7001
iphe specified port just one cluster, and then enter the slot number of the source node and the destination node id to be moved, it can be done last input.
6. The mobile node
You can specify the current master node, or changing the master node currently belongs, to sign in the current node to execute the following command:
cluster replicate 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 ###新master的nodeid
7. Delete Node
Note: Before you delete the master node needs to move first of all slot on the primary node to another master node, and ensure that no, or need to be removed or deleted from the first node from the node
When delete a need to specify the node node id
redis-trib.rb del-node 192.168.191.11:7001 af0f17b501b9f9f5e71ff6be1cf8114c9e11a80b
后面的id即为需要删除的节点id,删除节点和增加节点语法节点的循序正好相反
四、集群相关命令
CLUSTER INFO 打印集群的信息 CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。 CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。 CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。 CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。 CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。 CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。 CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。 CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点。 CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。 CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。 CLUSTER SETSLOT <slot> STABLE cancel the import of the slot grooves (Import) or migration (migrate). CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。 CLUSTER COUNTKEYSINSLOT <slot> return groove slot currently contains the number of key-value. CLUSTER GETKEYSINSLOT <slot> <count> returns a slot count slot key. CLUSTER SLAVES node-id master node returns a list of slaves