Redis service master-slave replication, sentinel mode, cluster mode

Redis service master-slave replication, sentinel mode, cluster mode

One, Redis master-slave replication

1. The concept of Redis master-slave replication

Master-slave replication refers to copying the data of one Redis server to other Redis servers.
The former is called the master node (Master), and the latter is called the slave node (Slave);
data replication is one-way, only from the master node to the slave node.
By default, each Redis server is the master node;
a master node can have multiple slave nodes (or no slave nodes), but a slave node can only have one master node.

2. The role of Redis master-slave replication

Data redundancy: Master-slave replication realizes hot backup of data, which is a data redundancy method besides persistence.
Failure recovery: When the master node has a problem, the slave node can provide services to achieve rapid failure recovery; in fact, it is a kind of service redundancy.
Load balancing: On the basis of master-slave replication, with the separation of read and write, the master node can provide the write service, and the slave node can provide the read service (that is, the application connects to the master node when writing Redis data, and the application connects to the slave node when reading Redis data) , Share the server load; especially in the scenario of writing less and reading more, sharing the read load by multiple slave nodes can greatly increase the concurrency of the Redis server.
The cornerstone of high availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinels and clusters. Therefore, master-slave replication is the basis for Redis high availability.

3. Redis master-slave replication process

Step 1: If a slave machine process is started, it will send a "sync command" command to the Master machine to request a synchronous connection.
Step 2: Whether it is the first connection or reconnection, the Master machine will start a background process to save the data snapshot to the data file (execute the RDB operation), and the Master will also record all the commands to modify the data and cache them in the data File.
Step 3: After the background process completes the cache operation, the Maste machine will send the data file to the Slave machine, and the Slave machine will save the data file to the hard disk, and then load it into the memory, and then the Master machine will modify the data. All operations are sent to the slave machine. If the Slave fails and causes a downtime, it will automatically reconnect after returning to normal.
Step 4: After the Master machine receives the connection from the Slave side machine, it sends its complete data file to the Slave side machine. If Mater receives synchronization requests from multiple Slaves at the same time, the Master will start a process in the background. Save the data file, and then send it to all slave-side machines to ensure that all slave-side machines are normal.

4. Redis master-slave replication construction

Redis-5.0.7.tar.gz
related installation package
Extraction code: 37z7
environment configuration

Host operating system IP address Installation package
Master CentOS7 192.168.2.9 redis-5.0.7.tar.gz
Slave1 CentOS7 192.168.2.10 redis-5.0.7.tar.gz
Slave2 CentOS7 192.168.2.11 redis-5.0.7.tar.gz

Per host

hostnamectl set-hostname master  #修改主机名,每台主机要改的不一样
su
systemctl stop firewalld
setenforce 0

Insert picture description here

(1) Install Redis (all hosts)

Master:192.168.2.9
Slave1:192.168.2.10
Slave2:192.168.2.11

yum install -y gcc gcc-c++ make
cd /opt
tar zxvf redis-5.0.7.tar.gz

cd /opt/redis-5.0.7/
make && make PREFIX=/usr/local/redis install

cd /opt/redis-5.0.7/utils
./install_server.sh
......
#回车四次后,手动输入,需要一次性输入正确
Please select the redis executable path [] /usr/local/redis/bin/redis-server  	

ln -s /usr/local/redis/bin/* /usr/local/bin/

Insert picture description here
Insert picture description here

(2) Modify the Redis configuration file of the Master node

Master:192.168.2.9

vim /etc/redis/6379.conf
#70行,修改bind 项,0.0.0.0监听所有网段
bind 0.0.0.0
#137行,开启守护进程
daemonize yes
#172行,指定日志文件目录
logfile /var/log/redis_6379.log
#264行,指定工作目录
dir /var/lib/redis/6379
#700行,开启AOF持久化功能
appendonly yes

/etc/init.d/redis_6379 restart

Insert picture description here
Insert picture description here

(3) Modify the Redis configuration file of the slave node

Slave1:192.168.2.10
Slave2:192.168.2.11

vim /etc/redis/6379.conf
#70行,修改bind 项,0.0.0.0监听所有网卡
bind 0.0.0.0
#137行,开启守护进程
daemonize yes
#172行,指定日志文件目录
logfile /var/log/redis_6379.log
#264行,指定工作目录
dir /var/lib/redis/6379
#288行,指定要同步的Master节点IP和端口
replicaof 192.168.163.10 6379
#700行,开启AOF持久化功能
appendonly yes

/etc/init.d/redis_6379 restart

Insert picture description here
Insert picture description here

(4) Verify the master-slave effect

Master: 192.168.2.9
view logs on the Master node

tail -f /var/log/redis_6379.log

Insert picture description here
Verify the slave node on the master node

redis-cli info replication

Insert picture description here
Insert picture description here

Two, Redis sentinel mode

  • The core function of Sentinel: On the basis of master-slave replication, Sentinel introduces automatic failover of the master node

1. The principle of sentinel mode

Sentinel: It is a distributed system used to monitor each server in the master-slave structure. When a failure occurs, a new Master is selected through a voting mechanism and all slaves are connected to the new Master. Therefore, the number of the entire cluster running sentinels must not be less than 3 nodes.

2. The role of sentinel mode

  • Monitoring: The sentinel will constantly check whether the master node and slave node are operating normally.
  • Automatic failover: When the master node fails to work normally, the sentinel will start an automatic failover operation. It will upgrade one of the slave nodes of the failed master node to a new master node and make other slave nodes copy the new master node.
  • Notification (reminder): The sentry can send the result of the failover to the client.

3. The structure of sentinel mode

The sentinel structure consists of two parts, the sentinel node and the data node:

  • Sentinel node: The sentinel system consists of one or more sentinel nodes. The sentinel node is a special redis node and does not store data.
  • Data node: Both the master node and the slave node are data nodes.

The start of the sentinel depends on the master-slave mode, so you must install the master-slave mode before doing the sentry mode. All nodes need to deploy the sentry mode. The sentry mode will monitor whether all Redis working nodes are normal. When the Master appears When there is a problem, because other nodes have lost contact with the master node, they will vote. More than half of the vote is considered that there is a problem with this Master, and then the sentry room will be notified, and then one of the Slaves will be selected as the new Master.

Note: Objective offline is a concept only available to the master node; if the slave node and sentinel node fail, after the sentinel is subjectively offline, there will be no subsequent objective offline and failover operations.

4. Construction of sentinel mode

Environment configuration
Master: based on the established master-slave replication

Host operating system IP address Installation package
Master CentOS7 192.168.2.9 redis-5.0.7.tar.gz
Slave1 CentOS7 192.168.2.10 redis-5.0.7.tar.gz
Slave2 CentOS7 192.168.2.11 redis-5.0.7.tar.gz

(1) Modify Redis configuration file (all nodes operation)

Master:192.168.2.9
Slave1:192.168.2.10
Slave2:192.168.2.11

vim /opt/redis-5.0.7/sentinel.conf
#17行,关闭保护模式
protected-mode no
#21行,Redis哨兵默认的监听端口
port 26379
#26行,指定sentinel为后台启动
daemonize yes
#36行,指定日志存放路径
logfile "/var/log/sentinel.log"
#65行,指定数据库存放路径
dir "/var/lib/redis/6379"
#84行,修改 指定该哨兵节点监控192.168.2.9:6379这个主节点,该主节点的名称是mymaster,最后的2的含义与主节点的故障判定有关:至少需要2个哨兵节点同意,才能判定主节点故障并进行故障转移
sentinel monitor mymaster 192.168.2.9 6379 2
#113行,判定服务器down掉的时间周期,默认30000毫秒(30秒)
sentinel down-after-milliseconds mymaster 30000
#146行,故障节点的最大超时时间为180000(180秒)
sentinel failover-timeout mymaster 180000

Insert picture description here

(2) Start sentinel mode

Master: 192.168.2.9
Slave1: 192.168.2.10
Slave2: 192.168.2.11
Note: start master first, then slave

cd /opt/redis-5.0.7/
redis-sentinel sentinel.conf &

Start the master node first,
Insert picture description here
then start the slave node
Insert picture description here

(3) View the sentinel mode information

Master:192.168.2.9

redis-cli -p 26379 info Sentinel

Insert picture description here

(4) Fault simulation

Master:192.168.2.9

#查看redis-server进程号
ps aux | grep redis
root      56039  0.0  0.4 162696  7820 ?        Ssl  3月09   0:02 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root      56626  0.1  0.1 153992  2668 ?        Ssl  00:30   0:01 redis-sentinel *:26379 [sentinel]
root      56745  0.0  0.0 112824   980 pts/3    S+   00:42   0:00 grep --color=auto redis

#杀死 Master 节点上redis-server的进程号,模拟故障
kill -9 56039			#Master节点上redis-server的进程号

Insert picture description here

(5) Verification result

tail -f /var/log/sentinel.log
redis-cli -p 26379 INFO Sentinel

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_35456705/article/details/114594621