Redis master-slave, sentinel, cluster

Master-slave replication

principle

slave启动后会给master发送sync同步命令,master接到命令后,启动后台的存盘进程,同时收集所有接收到的
用于修改数据命令集命令,在后台执行进程完毕之后,master将所有数据文件发送到slave,完成一次完全同步。

Full copy : When the slave starts.
Incremental replication : After full replication, the master's subsequent command updates use incremental replication.

Icon

Insert picture description here

Configuration:

Master does not need to be configured, just configure the slave.

修改配置文件:
#redis_6380.conf
port 6380
pidfile /var/run/redis_6380.pid
dbfilename dump_6380.rdb

#redis_6381.conf
port 6381
pidfile /var/run/redis_6381.pid
dbfilename dump_6381.rdb

Start the slave service

./redis-server ../etc/redis_6380.conf
./redis-server ../etc/redis_6381.conf

Log in to the client:

127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> set k2 v2
OK

127.0.0.1:6380> get k1
"v1"
127.0.0.1:6380> get k2
"v2"

127.0.0.1:6381> get k1
"v1"
127.0.0.1:6381> get k2
"v2"

Read and write mode

master write, slave read

Disadvantages

After the master is down, it can only be read, not written

Sentinel mode

principle

Solve the problem that the master can not write after the master down in the replication.
The master dies, and the sentry will elect one of the slaves to be the master. If the master starts again, it is converted into a slave by the sentry.

Icon

Insert picture description here

Configuration

#修改配置文件sentinel_6379.conf
port 26379
sentinel monitor mymaster 127.0.0.1 6379 1

start up

#方法一:
 ./redis-sentinel ../etc/sentinel_6379.conf
#方法二:
 ./redis-server ../etc/sentinel_6379.conf --sentinel

test

#关闭master
127.0.0.1:6379> shutdown

View sentinel logs

Insert picture description here
Get 6381 was selected as the master

Log in 6381 and set the key, you can get the corresponding key in 6380. Realize automatic conversion of master.

advantage

1. Sentinel cluster, based on the master-slave replication model, it has all the advantages of master-slave configuration
2. Master-slave can be converted, the system availability is better

Disadvantages

1. Online expansion is difficult
2. Sentinel mode configuration is very troublesome

cluster mode

Origin

为了解决哨兵模式不能在线扩容而产生的

Features

  • All redis nodes are interconnected with each other (PING-PONG mechanism), and a binary protocol is used internally to optimize the transmission speed and bandwidth.
  • The fail of a node takes effect when more than half of the nodes in the cluster detect failure.
  • The client is directly connected to the redis node and does not require an intermediate proxy layer. The client does not need to connect to all nodes in the cluster, just connect to any available node in the cluster.

Way of working

On each node of redis, there are two key points, one isSlot, Its value range is: 0-16383. Another one iscluster, Can be understood as a plug-in for cluster management.
When our access key arrives, redis will get a result according to the crc16 algorithm, and then find the remainder of the result to 16384, so that each key will correspond to a number between 0-16383 (2 ^ 14) Hash slot, through this value, to find the node corresponding to the corresponding slot, and then automatically jump directly to the corresponding node for access operations.

In order to ensure high availability, the redis-cluster cluster introduces a master-slave mode. One master node corresponds to one or more slave nodes. When the master node is down, the slave node will be enabled. When other master nodes ping a master node A, if more than half of the master nodes communicate with A, the master node A is considered to be down.
If both the master node A and its slave node A1 are down, the cluster can no longer provide services.

Creating a cluster sounds difficult, but in fact redis has done most of the work for us, and only requires a small amount of configuration.

Process

1. Open at least 6 instances
2. Execute the create cluster command

Configuration

Write redis _ *. Conf configuration file

#这里只说一个配置,其他配置都一样,就是改下端口号而已
#重新从源码包中将配置文件移动到新创建的目录cluster中,并复制6个,命名redis_7001.conf--redis_7006.conf
#这里只说redis_7001.conf,其他类似
port 7001    #不同实例
pidfile /var/run/redis_7001.pid  #记录每个实例的pid
dbfilename dump_7001.rdb          #如果使用RDB就改这个
appendfilename "appendonly_7001.aof" #如果使用aof就改这个
daemonize yes	#开启后台运行
protected-mode no    #集群模式要关闭保护模式
appendonly yes      #如果使用aof还要开启这个
cluster-enabled yes   #启用集群
cluster-config-file nodes-7001.conf  #和端口一起改变,Redis群集节点每次发生更改时自动保留群集配置
cluster-node-timeout 5000 #Redis群集节点可以不可用的最长时间,在这个时间段内而不会将其视为失败

Quickly generate other configuration files The
7001 configuration files are generated above. Copy 5 copies and enter 7001 into the file to replace the full text of 7001 with the corresponding port.

:%s/7001/7003/g

Write a startup script, a shutdown script, and add execution permissions
vi cluster_start.sh

#!/bin/sh
set -e

./redis-server ../cluster/redis_7001.conf
./redis-server ../cluster/redis_7002.conf
./redis-server ../cluster/redis_7003.conf
./redis-server ../cluster/redis_7004.conf
./redis-server ../cluster/redis_7005.conf
./redis-server ../cluster/redis_7006.conf
#!/bin/sh
set -e

./redis-cli -p 7001 shutdown
./redis-cli -p 7002 shutdown
./redis-cli -p 7003 shutdown
./redis-cli -p 7004 shutdown
./redis-cli -p 7005 shutdown
./redis-cli -p 7006 shutdown

chmod +x cluster_start.sh
chmod +x cluster_stop.sh

But for closing the service, I still prefer a more violent method

killall redis-server

Hey! !

Start an instance

./cluster_start.sh

View

ps -ef| grep redis

Insert picture description here

Create a cluster

./redis-cli --cluster create 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:7006 --cluster-replicas 1

Insert picture description here
It will automatically create a master-slave node and assign different ranges of slots. If you want more slave nodes, when creating

	--cluster-replicas 1

Specify. The premise is that there are enough open instances.

Open or close the cluster individually

After it is created, it only needs to be turned on or off in the future, and does not need to be created again.
Find the management script in the source code:

redis-5.0.8/utils/create-cluster/create-cluster

Modify the port (setting 7000) and nodes (6) in the script, execute the script, and the cluster will be turned on or off.
I suggest to take a look at how to play this script. Don't be pitted

./create-cluster start/stop

note

The command to start an instance after creating a cluster is different from the general one. I posted the script file start process:

 ./redis-server --port $PORT --cluster-enabled yes --cluster-config-file nodes_${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly_${PORT}.aof --dbfilename dump_${PORT}.rdb --logfile ${PORT}.log --daemonize yes

Login test

./redis-cli -c -p 7001

-c: means cluster

Redis cluster command description

command Explanation
// cluster
CLUSTER INFO Print cluster information
CLUSTER NODES List all the nodes currently known in the cluster (node), and the relevant information of these nodes.
// node
CLUSTER MEET <ip> <port> Add the nodes specified by ip and port to the cluster and make it a part of the cluster.
CLUSTER FORGET <node_id> Remove the node specified by node_id from the cluster.
CLUSTER REPLICATE <node_id> Set the current node as the slave node of the node specified by node_id.
CLUSTER SAVECONFIG Save the configuration file of the node to the hard disk.
// slot
CLUSTER ADDSLOTS <slot> [slot …] Assign one or more slots to the current node.
CLUSTER DELSLOTS <slot> [slot …] Remove the assignment of one or more slots to the current node.
CLUSTER FLUSHSLOTS Remove all slots assigned to the current node and make the current node a node without any slots assigned.
CLUSTER SETSLOT <slot> NODE <node_id> Assign the slot to the node specified by node_id. If the slot is already assigned to another node, let the other node delete the slot first, and then assign it.
CLUSTER SETSLOT <slot> MIGRATING <node_id> Migrate the slot of this node to the node specified by node_id.
CLUSTER SETSLOT <slot> IMPORTING\ <node_id> Import the slot from the node specified by node_id to this node.
CLUSTER SETSLOT <slot> STABLE Cancel the import or migration of slots.
// 键 (key)
CLUSTER KEYSLOT <key> Calculate the slot where the key should be placed.
CLUSTER COUNTKEYSINSLOT <slot> Returns the number of key-value pairs currently contained in the slot.
CLUSTER GETKEYSINSLOT <slot> Returns the keys in count slots.

These commands are unique to the cluster. You must log in before executing the above command

View cluster information

127.0.0.1:7001> CLUSTER info    ##查看cluster信息
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:2242
cluster_stats_messages_pong_sent:2286
cluster_stats_messages_sent:4528
cluster_stats_messages_ping_received:2281
cluster_stats_messages_pong_received:2242
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:4528
127.0.0.1:7001> CLUSTER NODES      ##查看节点信息, node-id 就是第一列字符
ba5de67fdb5786436a85af94b756e222e460782e 127.0.0.1:7006@17006 slave 587bde2531213f68721a47629ffa73f02aa4a901 0 1587219713720 6 connected
66503dd0a778be6219b72523e7067701ef195d2e 127.0.0.1:7002@17002 master - 0 1587219713515 2 connected 5461-10922
adff8ad98fa736c0dec59504c9d327a6e8c6aa96 127.0.0.1:7004@17004 slave 6c73487c7ba28b701538663640161a7ea8ae5a5e 0 1587219713515 4 connected
6c73487c7ba28b701538663640161a7ea8ae5a5e 127.0.0.1:7001@17001 myself,master - 0 1587219714000 1 connected 0-5460
383c0994ab130e32b34dca84ae9b69cd771c782b 127.0.0.1:7005@17005 slave 66503dd0a778be6219b72523e7067701ef195d2e 0 1587219713515 5 connected
587bde2531213f68721a47629ffa73f02aa4a901 127.0.0.1:7003@17003 master - 0 1587219714771 3 connected 10923-16383

##查看当前服务的主从关系
127.0.0.1:7001> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7004,state=online,offset=1106,lag=1
master_replid:74a6e347a1ae2ba4d8cd3f17d678fa924dc53166
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1106
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1106

##查看slots范围
127.0.0.1:7001> CLUSTER SLOTS
1) 1) (integer) 0
   2) (integer) 5460
   3) 1) "127.0.0.1"
      2) (integer) 7001
      3) "6c73487c7ba28b701538663640161a7ea8ae5a5e"
   4) 1) "127.0.0.1"
      2) (integer) 7004
      3) "adff8ad98fa736c0dec59504c9d327a6e8c6aa96"
2) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "127.0.0.1"
      2) (integer) 7005
      3) "383c0994ab130e32b34dca84ae9b69cd771c782b"
   4) 1) "127.0.0.1"
      2) (integer) 7002
      3) "66503dd0a778be6219b72523e7067701ef195d2e"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "127.0.0.1"
      2) (integer) 7003
      3) "587bde2531213f68721a47629ffa73f02aa4a901"
   4) 1) "127.0.0.1"
      2) (integer) 7006
      3) "ba5de67fdb5786436a85af94b756e222e460782e"


##设置键值,可以发现存储在不同地方,自动切换到不同的节点
127.0.0.1:7001> set k1 v1
-> Redirected to slot [12706] located at 127.0.0.1:7003
OK
127.0.0.1:7003> set k2 v2
-> Redirected to slot [449] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get k1
-> Redirected to slot [12706] located at 127.0.0.1:7003
"v1"
127.0.0.1:7003> get k2
-> Redirected to slot [449] located at 127.0.0.1:7001
"v2"

Test failover

./redis-cli -p 7002 debug segfault

Before failure:
Insert picture description here
after failure:
Insert picture description here
7002 is finished, 7005 becomes master

Restart 7002 service

./redis-server ../cluster/redis_7002.conf

Insert picture description here
After restarting 7002, it was found that it became a slave node.

Manual failover

Use cluster failover for manual failover,Must be a slave node of the master server to be transferredExecute the command.
This command tells the master node to get off work, and the slave node becomes the master node. There are two options force and takeover. The specific difference between Baidu.

127.0.0.1:7004> CLUSTER FAILOVER 

The above slots are automatically allocated by redis. If you want to set each node yourself, then use the following command

127.0.0.1:7001> CLUSTER ADDSLOTS {0..m}

The maximum value of m is 16383

Add Node

a. Add master node

./redis-cli --cluster add-node 127.0.0.1:7007 127.0.0.1:7001

The first parameter is the node to be added, and the second parameter is a random node that already exists in the cluster.

The newly added node has no slots, so it needs to be taken from other nodes

Assign slots

./redis-cli --cluster reshard 127.0.0.1:7001

Just specify a node and the others will be found automatically. Next, you will enter the number of slots that need to be moved and the target ID and from which nodes to get the slots.
Insert picture description here
The allocation process can be understood as playing cards, all means that everyone reshuffles; enter the node id of a master node, and then enter done, it is like drawing cards from a node.

Check the running status of the test cluster

./redis-cli --cluster check 127.0.0.1:7002

b. Add slave node

If you do not specify a master node, the program will automatically add it to the master node with fewer slave nodes.

./redis-cli --cluster add-node 127.0.0.1:7008 127.0.0.1:7001 --cluster-slave --cluster-master-id 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e

c. Change the master from the node

127.0.0.1:7008> CLUSTER REPLICATE 6c73487c7ba28b701538663640161a7ea8ae5a5e   //新master的node-id
OK

Delete the node:

a. Delete the master node

If the master node has a slave node, transfer the slave node to other master nodes (see method c above).
If the master node has a slot, remove the allocated slot, and then delete the master node

Remove allocated slots

./redis-cli --cluster reshard 127.0.0.1:7007    #想要删除的主节点ip:port

Insert picture description here
You can also use the command line parameters to move the slots directly

redis-cli --cluster  reshard 10.150.134.92:7001 --cluster-to 21c7c13ed399a466e7011d8037fad7bc05c9aecc --cluster-from 8fc5771cff9b54aad06541aded5253e804b2432b --cluster-slots [0-60] --cluster-yes

Delete the master node

./redis-cli --cluster del-node 127.0.0.1:7001 'b39f84c35cfc0c11e9730b20d34b8d0766b181ce'

The first parameter is the cluster random node, the second parameter is the node ID to be deleted

b. Delete slave node

./redis-cli --cluster del-node 127.0.0.1:7001 '3328859bf6d03844d12c7c94fe1a679b039652e9'

The first parameter is the cluster random node, the second parameter is the node ID to be deleted

Node repair

./redis-cli --cluster fix ip:port

Disaster recovery

Program

The master node does not do RDB and AOF, the slave node opens two at the same time.
If the master node hangs, copy dump.rdb and appendonly.aof from the slave node to the master node and restart the master node. File reading order, read AOF file first.

The actual situation

Use AOF less and recover slowly. The slave node directly uses RDB, increases the write frequency or writes a scheduled task to periodically store bgsave's snapshot.

Actual redis monitoring tool

treeNMS

Redis data write-back mechanism

Synchronize: Save command (easy system crash, not recommended)
asynchronous: Bgsave , the main process fork a process responsible for writing, and then close.

System setting parameters: (direct release of memory application)

sysctl vm.overcommit_memory=1

Save data to the same slot (on a node)

Use {} tag:

127.0.0.1:7005> set foo1{my_tag} 333
OK
127.0.0.1:7005> set foo{my_tag} 222
OK

The cluster cannot select other DBs, only DB0 can be used

Ie only select 0

Published 193 original articles · Like 13 · Visitors 40,000+

Guess you like

Origin blog.csdn.net/u013919153/article/details/105604334