Redis cluster construction and MOVED error

Why create a Redis cluster

Because a single node may fail
, it will get stuck when processing a large number of concurrent requests.
If a single node is down, data is easily lost

So to create a redis cluster
Insert picture description here

Redis has three cluster modes

Master-slave mode

  • The master database can perform read and write operations. When the read and write operations cause data changes, the data will be automatically synchronized to the slave database
  • The slave database is generally read-only and receives data synchronized from the master database
  • A master can have multiple slaves, but a slave can only correspond to one master
  • The slave hangs up and does not affect the reading and writing of other slaves and the master. After restarting, the data will be synchronized from the master.
  • After the master hangs, it does not affect the slave's reading, but redis no longer provides write services. After the master restarts, redis will provide external write services again
  • After the master hangs up, it will not re-select a master in the slave node

Sentinel mode

  • The sentinel mode is based on the master-slave mode. If there is only one Redis node, sentinel has no meaning
  • When the master hangs up, sentinel will select one of the slaves as the master and modify their configuration files. The configuration files of other slaves will also be modified. For example, the slaveof attribute will point to the new master.
  • When the master restarts, it will no longer be the master but as a slave to receive the synchronization data
    sentinel of the new master. Because it is also a process that may hang, sentinel will also start multiple to form a sentinel cluster
  • When multiple sentinels are configured, sentinels will also be automatically monitored
  • When the password is configured in the master-slave mode, sentinel will also synchronously modify the configuration information into the configuration file, so you don’t need to worry about it.
    A sentinel or sentinel cluster can manage multiple master-slave Redis, and multiple sentinels can also monitor the same redis.
  • Sentinel is best not to be deployed on the same machine as Redis, otherwise after the Redis server hangs, sentinel also hangs

Cluster mode

  • Network interconnection of multiple redis nodes and data sharing
  • All nodes are one master and one slave (or one master and multiple slaves), which never provide services, only serve as backup
  • Does not support processing multiple keys at the same time (such as MSET/MGET), because redis needs to evenly distribute the keys on each node
  • Creating key-values ​​at the same time with high concurrency will reduce performance and cause unpredictable behavior
  • Support online addition and deletion of nodes
  • The client can connect to any master node for reading and writing

Cluster data sharding

  • Did not introduce consistent HASH, but introduced the concept of hash slot (hash slot)
  • 16384 hash slots per cluster
  • After each key is checked by CRC16, the hash slot is used to determine the slot
  • Each node of the cluster is responsible for a part of the hash slot

bring it on! Show! !

We are using cluster mode

  1. The sentinel mode can basically meet the needs of general production and has high availability. But when the amount of data is too large for one server to store, the master-slave mode or sentinel mode cannot meet the demand. At this time, the stored data needs to be fragmented and stored in multiple Redis instances. The emergence of the cluster mode is to solve the problem of the limited capacity of a single Redis, and Redis data is distributed to multiple machines according to certain rules.
  2. The cluster can be said to be a combination of sentinel and master-slave mode. The master-slave and master reselection function can be realized through cluster, so if you configure two copies and three shards, you need six Redis instances. Because Redis data is allocated to different machines in the cluster according to certain rules, when the amount of data is too large, new machines can be added for expansion.
  3. To use a cluster, you only need to open the cluster-enable configuration in the redis configuration file. Each cluster requires at least three main databases to run normally, and it is very convenient to add nodes.

Experimental parameters

The IP of the six servers is
20.0.0.11
20.0.0.12
20.0.0.21
20.0.0.22
20.0.0.23
20.0.0.24

Experimental configuration

All six hosts need to install redis

[root@localhost ~]# yum -y install gcc gcc-c++ make
[root@localhost ~]# mkdir /bao
[root@localhost ~]# cd /bao
[root@localhost bao]# rz -E
rz waiting to receive.
[root@localhost bao]# tar zxvf redis-5.0.7.tar.gz 
[root@localhost bao]# cd redis-5.0.7/
[root@localhost redis-5.0.7]# make
[root@localhost redis-5.0.7]# make install PREFIX=/usr/local/redis
[root@localhost redis-5.0.7]# cd utils/
[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@localhost utils]# ln -s /usr/local/redis/bin/* /usr/local/bin

Modify the configuration file

[root@localhost utils]# vim /etc/redis/6379.conf
70 #bind 127.0.0.1	##注释掉监听地址
89 protected-mode no	##关闭保护模式
93 port 6379	##端口还是6379
137 daemonize yes	##独立启动进程
700 appendonly yes	##开启aof持久化
833 cluster-enabled yes	##开启群集功能,取消注释
841 cluster-config-file nodes-6379.conf	##群集文件名称,取消注释
847 cluster-node-timeout 15000	##群集超时时间设置,取消注释
[root@localhost utils]# vim /etc/redis/6379.conf 
[root@localhost utils]# cd /var/lib/redis/6379/
[root@localhost 6379]# service redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost 6379]# ls
appendonly.aof  dump.rdb  nodes-6379.conf

Install rvm and RUBY on a main server

[root@localhost 6379]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3	##密钥文件导入
[root@localhost 6379]# cd /bao/
[root@localhost bao]# rz -E
rz waiting to receive.
[root@localhost bao]# ls
redis-5.0.7  redis-5.0.7.tar.gz  rvm-installer.sh
[root@localhost bao]# chmod +x rvm-installer.sh 
[root@localhost bao]# ./rvm-installer.sh		##这个下的太慢了,我直接拿的下好的
[root@localhost bao]# ./rvm-installer.sh
[root@localhost bao]# source /etc/profile.d/rvm.sh
[root@localhost bao]# rvm list known		##列出可安装版本
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.10]
[ruby-]2.5[.8]
[ruby-]2.6[.6]
[ruby-]2.7[.1]
ruby-head

# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
jruby-1.7[.27]
jruby-9.1[.17.0]
jruby[-9.2.13.0]
jruby-head

# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx-3[.107]
rbx-4[.20]
rbx-5[.0]
rbx-head

# TruffleRuby
truffleruby[-20.2.0]

# Opal
opal

# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1.2.0
mruby-1.3.0
mruby-1[.4.1]
mruby-2.0.1
mruby-2[.1.1]
mruby[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Topaz
topaz

# MagLev
maglev-1.0.0
maglev-1.1[RC1]
maglev[-1.2Alpha4]
maglev-head

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby
ironruby[-1.1.3]
ironruby-head

[root@localhost bao]# rvm install 2.4.1		##安装 2.4.1版本
[root@localhost bao]# ruby -v	##查看版本信息
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
[root@localhost bao]#  gem install redis	##再次安装 redis

Configure the cluster on the master server.
Here you can see the master and slave nodes.

[root@localhost network-scripts]# redis-cli --cluster create 20.0.0.11:6379 20.0.0.12:6379 20.0.0.21:6379 20.0.0.22:6379 20.0.0.23:6379 20.0.0.24:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 20.0.0.23:6379 to 20.0.0.11:6379
Adding replica 20.0.0.24:6379 to 20.0.0.12:6379
Adding replica 20.0.0.22:6379 to 20.0.0.21:6379
M: 32dfc6ddc0b9b11a5761468cd56be4bc4d932eff 20.0.0.11:6379
   slots:[0-5460] (5461 slots) master
M: da81b568b82ec8d2e65967a4c3ed6550aa59b42d 20.0.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: 32dfc6ddc0b9b11a5761468cd56be4bc4d932eff 20.0.0.21:6379
   slots:[10923-16383] (5461 slots) master
S: 32dfc6ddc0b9b11a5761468cd56be4bc4d932eff 20.0.0.22:6379
   replicates 32dfc6ddc0b9b11a5761468cd56be4bc4d932eff
S: da81b568b82ec8d2e65967a4c3ed6550aa59b42d 20.0.0.23:6379
   replicates 32dfc6ddc0b9b11a5761468cd56be4bc4d932eff
S: da81b568b82ec8d2e65967a4c3ed6550aa59b42d 20.0.0.24:6379
   replicates da81b568b82ec8d2e65967a4c3ed6550aa59b42d
Can I set the above configuration? (type 'yes' to accept): yes		##手敲yes

Experimental verification

Start verification

[root@localhost network-scripts]# redis-cli -h 20.0.0.11 -p 6379 -c
20.0.0.11:6379> set gundam EXIA
OK
20.0.0.11:6379> keys *
1) "gundam"


[root@localhost network-scripts]# redis-cli -h 20.0.0.23 -p 6379 -c	##另一台机器,验证“主从”
20.0.0.23:6379> keys *
1) "gundam"
20.0.0.23:6379> get gundam
"EXIA"

Verify automatic deletion

[root@localhost network-scripts]# redis-cli -h 20.0.0.11 -p 6379 -c
20.0.0.11:6379> EXPIRE gundam 5		##五秒后删除这个键
(integer) 1
##五秒后
20.0.0.11:6379> keys *
(empty list or set)

##另一台机器
20.0.0.23:6379> keys *	##另一台机器也没了
(empty list or set)

Error MOVED

If this error occurs when you create the key, exit, add option -c when entering, -c means to start cluster mode
(error) MOVED 5798 127.0.0.1:7001

redis-cli -h 20.0.0.12 -p 6379 -c

Note: If a master is down, the corresponding slave will become the master on top of the two, and the service will be used normally. If the corresponding master and slave go down, your cluster will be gone...

Guess you like

Origin blog.csdn.net/Ora_G/article/details/108490235