1. Redis master-slave replication
Master-slave replication refers to copying the data of one redis server (master node-master) to other redis servers (slave node-slave). By default, each redis server is a master node, and each master node can have multiple or There are no slave nodes, but a slave node can only have one master node
1.1 The role of master-slave replication
Data redundancy: Master-slave replication implements hot backup of data, which is a data redundancy method other than persistence
Failure recovery: when the master node fails, the slave node can provide services
Load balancing: on the basis of master-slave replication with read-write separation, the master node provides write services, and the slave nodes provide read services to share server load
The cornerstone of high availability: master-slave replication is the basis for the implementation of sentinels and clusters
1.2 Common master-slave structure
One master and one slave: used for failover of the master node from the slave node, mainly for data security and avoiding the impact of persistence on the master node
One master and multiple slaves: mainly for scenarios with a large number of reads, the reads are shared by multiple nodes, but it affects the stability of the master node and affects the bandwidth
Tree-like master-slave: In order to alleviate the problem of high pressure on the master node caused by one master and multiple slaves, a hierarchical structure is formed, master-slave-slave and then master-slave
1.3 Realize master-slave replication
Copy multiple copies of redis.conf and modify the configuration
92 prot 6379
158 pidfile /var/run/redis_6379.pid
171 logfile "redis6379.log"
253 dbfilename dump6379.rdb
Can be replaced in batches with commands
:%s/6379/6380/g
Start different servers and open different terminals to connect to different servers
redis-server redis6379.conf
redis-cli -p 6379
Enter info replication to view information such as the role of the current server
Enter the command under the server to set the current service as the slave server and who is its master server
salveof main server ip port number
2. redis sentry
When the main server is down, in order to maintain the normal implementation of the function, we need a sentry to select a slave server to become the main server; (the sentinel is also a separate redis server, connected to the main server, and connected to the slave through the main)
In order to prevent a single sentry from misjudgment of downtime, we can also use multiple sentries (must be an odd number), set a voting parameter, and only when this number of sentries think that the master server is down, will the master-slave switch
Sentinel's sentinel.conf should be set as follows (remove comments)
protected-mode no # Turn off protected mode, convenient for testing
port 26379 # Sentinel port
sentinel monitor mymaster 192.168.41.226 6379 1# 192.168.41.226: host ip 6379: port 1: failover when at least a few sentinels think the host is offline
Enter redis-sentinel sentinel6379.conf to start the sentinel
3. Redis cluster
Redis-cluster clusters are supported in versions after redis 3.0, which means that multiple redis master servers are connected together to form a cluster.
This is to solve the problem that although the sentinel mode can meet most requirements, it cannot handle a large number of write requests;
This is also a classic P2P (decentralization)
3.1 Implementation of redis cluster
-
Create a redis-cluster directory under /usr/local/redis
-
Copy redis6379 to redis-cluster and name it redis8001.conf
-
92 Modify the port number in redis8001.conf
-
158 modify pidfile
-
171 Modify log files
-
253 Modify rdb file
-
699 Enable aof and set it to yes
-
703 specify aof file name
-
832 Enable the cluster and set it to yes
-
840 Specify nodes configuration file
In order to facilitate the startup of multiple redis servers, we can write a startup script, create a start_all.sh file under /user/local/redis, and add it to the file
redis-server redis-cluster/redis8001.conf
redis-server redis-cluster/redis8002.conf
redis-server redis-cluster/redis8003.conf
redis-server redis-cluster/redis8004.conf
redis-server redis-cluster/redis8005.conf
redis-server redis-cluster/redis8006.conf
ps -ef|grep redis
Save and exit, add execution permission to the start_all.sh file
chmod +x start_all.sh
start redis
./start_all
create cluster
redis-cli --cluster create 192.168.74.130:8001 192.168.74.130:8002 192.168.74.130:8003 192.168.74.130:8004 192.168.74.130:8005 192.168.74.130:8006 --cluster-replicas 1
Enter yes during creation
To connect to the redis cluster, use -c
redis-cli -c -p 8001
Create a cluster shutdown script
#!/bin/bash
PORT=8001
ENDPORT=8006
while [ $((PORT <= ENDPORT)) != "0" ]; do
echo "Stopping Redis $PORT"
redis-cli -p $PORT shutdown
PORT=$((PORT+1))
done
echo "done"
exit 0
Add execute permission
chmod +x stop_all.sh