当前位置:网站首页>Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
2022-08-02 06:18:00 【Have_MonkeyG】
One, one master multi-slave mode (master-slave mode)
1. Concept of master-slave mode
One master node and multiple slave nodes, then the master node can be responsible for: read operations, write operations.The slave node can only be responsible for read operations, not write operations.In this way, the reading pressure can be distributed from the master node to the slave node to reduce the pressure on the master node.
When the master node finishes executing the write command, it will synchronize the data to the slave node.
2. Build a master-slave mode
Principle: The match never matches the master.
(1) Preparation conditions:
Three virtual machines, one master and two slaves
In order to save memory, we can open three redis on a virtual machine
(2) Create a new folder in the redis directory to place the configuration files of 3 machines
(3) Copy 3 copies of the redis.conf configuration file into the created folder, and modify the configuration file
(4) Modify the port number and the name of the rdb file. We use 7001 as the main node, 7002 and 7003 as the child nodes
The other two are modified according to their respective port numbers
(5) Three start redis services at the same time
Start from your own configuration file
(6) Enter the client and view your own master-slave relationship
7. Configure the master-slave relationship with 7001 as the master, 7002 as the slave, and 7003 as the slave
port of the IP master node where the slaveof master node is located
8. Check the master-slave relationship again
9. Test
Through the test, we can see that the data is also synchronized among the three nodes, and the master node can read and write.But slave nodes can only read.
Summary:
1. If there is a problem with the master node, the child node will not replace it and become the master node.
2. If a new slave node is added, the new slave node will synchronize the previous data.
Second, sentry mode
Due to the master-slave mode, after the master node is single, the slave node will not automatically go up.
Add a sentinel service, which monitors the master at all times. If the master hangs up, the sentinel will elect a master node from the slave nodes [sentry voting mechanism].
1. Modify the configuration
(1) Find the sentinel.conf configuration file in the redis directory
(2) Modify the configuration file
#sentinel montor Name of the master node IP of the master node Port number of the master node Number of sentinel approvalssentinel monitor mymaster 127.0.0.1 6379 2
(3) Start Sentinel
redis-sentinel sentinel.conf
(4) At this point we close the master node
You will find that it selects one of the child nodes to be the parent node through a voting mechanism.
(5) Restart the previous master node
We found it became a child node
Three, clustering mode (decentralization)
Regardless of the above master-slave mode or sentinel mode, the problem of single-node write operations cannot be solved.If the concurrency of write operations is relatively high at this time.This is an experimental clustering mode [decentralized mode]
Principle:
There are 16384 hash slots built in the redis cluster. When a key-value needs to be placed in the Redis cluster, redis first uses the crc16 algorithm to calculate an integer result for the key, and then calculates the remainder of the result to 16384, so that eachThe key will correspond to a hash slot numbered between 0-16383, and redis will map the hash slot to different nodes roughly equally according to the number of nodes.
When you add a Key to Redis Cluster, it will calculate which hash slot the key should be distributed to according to crc16(key) mod 16384. There will be many keys and values in a hash slot.You can understand it as a table partition. When using redis on a single node, there is only one table, and all keys are placed in this table; after switching to Redis Cluster, 16384 partition tables will be automatically generated for you, and you will insert data according toSimple algorithm above to decide which partition your key should exist in, each partition has many keys.
Building a cluster:
1. Creation of virtual server
Three masters and three slaves:
6001,6002,6003 Masternodes
6004,6005,6006 child nodes
2. Create a cluster folder under redis to store the configuration file of the cluster
3. Modify the redis.conf configuration file
Because we are opening 6 redis services on a virtual machine, we need to modify a lot. If we only need to modify the bolded ones in future work
Redis-1 Firewall --6379
bind 0.0.0.0 line 69
port 6001 92 lines
daemonize yes 136 lines
dbfilename dump6001.rdb
# Turn on aof persistence
appendonly yes 700 lines
appendonly appendonly6001.aof
# Start the cluster
cluster-enabled yes line 833
# The configuration file of the cluster, which is automatically generated
cluster-config-file nodes-6001.conf line 841
# Cluster timeout
cluster-node-timeout 15000 847 lines
4. Put the configuration files of 6 modification numbers into the newly created folder
5. Start the server
Start all six servers
6. Allocation slot
redis-cli --cluster create --cluster-replicas 1 192.168.118.110:7000 192.168.118.110:7001 192.168.118.110:7002 192.168.118.111:7003 192.168.118.111:7004 192.168.118.111:7005
Just change the ip and port to your own settings
Note: Make sure that each node has no data.
7.Test
redis-cli ==-c== -h 192.168.40.199 -p 6001
Write operations can be evenly distributed to different nodes, reducing the pressure on a single master node.
边栏推荐
猜你喜欢
浏览器的onload事件
Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
Three methods of importing sql files in MySQL
Navicat new database
pg数据库报错问题,有懂的吗
[email protected](使用passwordYES)"/>
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
MySQL 8.0.29 设置和修改默认密码
ORA-04044:此处不允许过程、函数、程序包或类型,系统分析与解决
Detailed explanation of mysql stored procedure
Navicat如何连接MySQL
随机推荐
"Digital reconstruction of the system, getting the CEO is the first step"
ORA-04044:此处不允许过程、函数、程序包或类型,系统分析与解决
MySql copies data from one table to another table
21天学习挑战赛安排
Redis-集群模式(主从复制模式,哨兵模式,集群化模式)
51单片机外设篇:点阵式LCD
力扣 2127. 参加会议的最多员工数 拓扑剪枝与2360补充
classSR论文阅读笔记
navicat无法连接mysql超详细处理方法
Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
Browser onload event
coredns介绍
自动化运维工具——ansible、概述、安装、模块介绍
Android Studio 实现登录注册-源代码 (连接MySql数据库)
MySQL安装教程
PSQL function, predicate, CASE expression and set operations
MySQL 8.0.29 设置和修改默认密码
牛客-TOP101-BM41
mysql 8.0.28版本安装配置方法图文教程
MYSQL unique constraint