Redis primary/secondary replication
1. Master/slave replication
The master-slave replication architecture is only used for redundant backup of data, and slave nodes are only used for synchronization of data.
If the fault persists: 1. Automatic failover occurs on the master node
2 Master/slave replication architecture diagram
3. Set up primary/secondary replication
# 1. Prepare 3 machines and modify the configuration
- master
Replicaof 192.168.19.147 6379 (masterip) replicaof 192.168.19.147 6379 (masterip) Replicaof 192.168.19.147 6379 (masterip masterport) -slave2 port 6381 replicaof 192.168.19.147 6379 (masterip masterport)Copy the code
# 2. Start 3 machines for testing
- cd /usr/redis/bind
- ./redis-server /root/master/redis.conf
- ./redis-server /root/slave1/redis.conf
- ./redis-server /root/slave2/redis.conf
Copy the code
12. Redis Sentinel mechanism
1. Sentinel mechanism
Sentinel is Redis high availability solution: The Sentinel system, which consists of one or more Sentinel instances, can monitor any number of master servers and all slave servers under these master servers, and automatically upgrade a slave server under the offline master server to a new master server when the monitored master server goes offline. Sentinel is simply a master-slave architecture with automatic failover
The following problems cannot be solved: 1. The concurrent pressure of a single node 2
2. Sentry architecture principle
3. Build sentinel architecture
# 1. Create the sentinel configuration on the primary node
-Create sentinel.conf file in the same directory as Master redis# 2. Configure sentinels by filling in the sentinel.conf file
-The name of the sentinel monitor monitored database is IP port 1# 3. Start sentinel mode for testing
- redis-sentinel /root/sentinel/sentinel.conf
Copy the code
4. Use SpringBoot to operate sentry
# Redis Sentinel configuration
The master script is the name that the sentry listens for
spring.redis.sentinel.master=mymaster
# Instead of connecting to a specific Redis host, write multiple sentinel nodes
# 26379 is the port number for sentinel service
spring.redis.sentinel.nodes=192.168.19.147:26379
Copy the code
Redis cluster
1, the cluster
Redis began to support Cluster mode after 3.0. At present, Redis Cluster supports automatic node discovery, slave-master election and fault tolerance, online sharding shard and other features.
2. Cluster architecture diagram
3. Cluster details
-All redis nodes are ping-pong with each other and use binary protocols internally to optimize transmission speed and bandwidth.-Nodes fail only when more than half of the nodes in the cluster have detected failure-The client point is directly connected to the Redis node, without the intermediate proxy layer. The client does not need to connect all nodes of the cluster, but any available node in the cluster.-Redis-cluster maps all physical nodes to slot [0-16383] and maintains node<->slot<->valueCopy the code
4. Cluster construction
Judge whether one is the nodes in the cluster is available, used the master node in the cluster is the election process, if more than half of the node that hang up the current node, then the current node is to hang out, so set up advice when redis cluster node number is an odd number of best, need at least three main building cluster nodes, three from the node, will take at least six nodes.
# 1. Prepare the environment to install Ruby and Redis cluster dependencies
- yum install -y ruby rubygems
- gem install redis-xxx.gem
Copy the code
# 2. Create 7 directories on one machine
Copy the code
# 3. Make a copy of the configuration file per directory
[root@localhost ~]# cp redis-4.0.10/redis.conf 7000/
[root@localhost ~]# cp redis-4.0.10/redis.conf 7001/
[root@localhost ~]# cp redis-4.0.10/redis.conf 7002/
[root@localhost ~]# cp redis-4.0.10/redis.conf 7003/
[root@localhost ~]# cp redis-4.0.10/redis.conf 7004/
[root@localhost ~]# cp redis-4.0.10/redis.conf 7005/
[root@localhost ~]# cp redis-4.0.10/redis.conf 7006/
Copy the code
# 4. Modify different directory configuration files
-port 6379 ..... // Modify the port- bind bind \* - : : \ *Get rid of \ here (I added \ here* Need to escape) // Enable remote connection -cluster-enabled yes // Enable cluster mode -cluster-config -file nodes-port.conf // Cluster node configuration file -cluster-node-timeout Appendonly Yes // Enable AOF persistence - daemonize yes // Start background # 5. Start seven nodes with different directory configuration files - [root@localhost bin]#./redis-server /root/7000/redis.conf - [root@localhost bin]#./redis-server /root/7001/redis.conf - [root@localhost bin]# ./redis-server /root/7002/redis.conf - [root@localhost bin]# ./redis-server /root/7003/redis.conf - [root@localhost bin]# ./redis-server /root/7004/redis.conf - [root@localhost bin]# ./redis-server /root/7005/redis.conf - [root@localhost bin]# ./redis-server /root/7006/redis.confCopy the code
# 6. View the process
- [root@localhost bin]# ps aux|grep redis
Copy the code
1. Create a cluster
# 1. Create cluster
-/redis-cli --cluster create 192.168.19.147:7000 192.168.19.147:7001 192.168.19.147:7002 192.168.19.147:7003 192.168.19.147 192.168.19.147:7004:7005 - cluster - replicas of 1Copy the code
2. Check the cluster status
Check [any node in the original cluster] [None]
-. / redis - cli - cluster check 192.168.19.147:7000# 2. Description of cluster node status
-The master nodeThe primary node has Hash slots, and the Hash slots of the primary node are not crossed. The primary node cannot be deleted. A primary node can have multiple secondary nodes
-From the nodeSlave nodes do not have Hash slots. Slave nodes can be removed. Slave nodes are not responsible for writing data, only for synchronizing dataCopy the code
3. Add the primary node
# 1. Add primary node add-node [new node] [any node in the original cluster]
-/redis-cli --cluster add-node 192.168.19.147:7006 192.168.19.147:7005-Note:The node must be started in cluster mode. 2. By default, the node is added as a master nodeCopy the code
4. Add a secondary node
# 1. Add slave node add-node --slave [newly added node] [any node in cluster]
-/redis-cli --cluster add-node --slave 192.168.19.147:7006 192.168.19.147:7000-Note:When no primary node is specified when adding replica nodes, Redis randomly adds the current replica node # 2 to the primary node with fewer replica nodes. Add master node --slave --master-id Master node ID [newly added node] [Any node in the cluster] -./redis-cli --cluster add-node --slave - master - id 3 c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 192.168.19.147:7006 192.168.19.147:7000Copy the code
5. Delete the replica node
Delete node del-node [delete node ID]
-. / redis - cli - cluster del -node 192.168.19.147:7002 0 ca3f102ecf0c888fc7a7ce43a13e9be9f6d3dd1-Note: 1. The nodes to be removed must be from nodes or nodes not assigned Hash SlotsCopy the code
6. Online cluster fragmentation
# 1. Online shard reshard [any node in cluster] [none]
-. / redis - cli - cluster reshard 192.168.19.147:7000Copy the code