A redis3 master-3-slave cluster built based on hash slots using docker containers

1. Construction background

Cluster mode-docker version, hash slot partition for billion-level data storage

1. 100-200 million pieces of data need to be cached. How to design it?

Answer: Using distributed storage, there are generally three solutions in the industry

The first type: hash remainder partition (generally used by small factories)

Disadvantages: The biggest disadvantage of the hash remainder partition is that it is inconvenient for expansion. When expansion is required, the mapping relationship needs to be recalculated.

The second type: Consistent hash algorithm partitioning (generally used by large manufacturers)

Background: To solve the problem of data change and mapping (the above problem), if a certain machine is down and the denominator quantity changes, naturally the remainder will not work.

What it can do: Propose a consistent Hash solution, with the purpose of minimizing the impact on the mapping relationship between the client and the server when the current server changes.

3 major steps: Algorithm to build consistent hash ring

Server IP node mapping

Key drop rules for keys falling to the server

Advantages: fault tolerance, scalability

Disadvantages: Data skew problem of consistent hashing algorithm (too few nodes)

The third type: Hash slot partition (commonly used by large manufacturers)

Background: To solve the above data skew problem, it is essentially an array

2. Redis cluster 3 master 3 slave

1. Close the firewall + start the docker background service

Turn off the firewall: systemctl stop firewalld systemctl
Start the docker background service: systemctl start docker

2. Create 6 new docker container instances

# Start the first node
# --net host uses the host's IP and port, default
# --cluster-enabled yes Open redis cluster
# --appendonly yes Enable redis persistence
# --port 6381 configure redis port number
docker run -d --name redis-node-1 --net host --privileged=true -v /app/redis-cluster/share/redis-node-1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
?

# Start the second node
docker run -d --name redis-node-2 --net host --privileged=true -v /app/redis-cluster/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
?

# Start the third node
docker run -d --name redis-node-3 --net host --privileged=true -v /app/redis-cluster/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
?

# Start the 4th node
docker run -d --name redis-node-4 --net host --privileged=true -v /app/redis-cluster/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
?

# Start the 5th node
docker run -d --name redis-node-5 --net host --privileged=true -v /app/redis-cluster/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
?
# Start the 6th node
docker run -d --name redis-node-6 --net host --privileged=true -v /app/redis-cluster/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386

3. Enter machine 1:

docker exec -it container name /bin/bash

4. Allocate hash slots to 6 machines in order to build a master-slave relationship.

Prerequisite: Enter any machine

–cluster-replicas 1: Build a 1-to-1 relationship

docker exec -it container name /bin/bash

5. Connect to 6381 and change the entry point to check the cluster status.

1) View cluster information: First: redis-cli -p 6381 Then: cluster info

2) Check who has mounted whom in the cluster: first: redis-cli -p 6381 and then: cluster nodes

3. Master-slave fault-tolerant switching migration case

1. Data reading, writing and storage

1) Start a cluster of 6 machines and enter through exec

First enter docker: docker exec -it 2dd08e5aedbc /bin/bash

Then enter redis: redis-cli -p 6381 -c (if you cannot enter in stand-alone mode, add -c)

2) Add two new keys to 6381 (test)

3) To prevent routing failure, add parameter -c and add two new keys (test)

Test: redis-cli -p 6381 -c

4) View cluster information

Check the network settings according to the situation and execute in docker: redis-cli –cluster check 192.168.xxx.xxx:6381

2. Master-slave fault-tolerant switching and migration

1) Stop machine No. 1 in Linux: docker stop redis-node-1

2) Enter redis as machine No. 2: docker exec -it redis-node-2 /bin/bash

redis-cli -p 6382 -c

3) Check whether the cluster has changed from slave to master: cluster nodes

4) Restore machine No. 1: After docker start redis-node-1, it changes from the master machine to the slave machine.

5) First restore the original position of No. 1 as the host, then stop the slave: docker stop redis-node-5

Then start the slave machine: docker start redis-node-5

4. Master-slave expansion case

1. Create two new nodes 6387 and 6388 + start after creation + check whether there are 8 nodes

1) Create new 6387,6388 nodes:

# Start the 7th node
docker run -d --name redis-node-7 --net host --privileged=true -v /app/redis-cluster/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
?
# Start the 8th node
docker run -d --name redis-node-8 --net host --privileged=true -v /app/redis-cluster/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388

2. Enter inside the 6387 container instance: docker exec -it redis-node-7 /bin/bash

3. Add the newly added 6387 node (empty slot) to the original cluster as the master node:

Prerequisite: Enter the 6387 container

redis-cli –cluster add-node 192.168.xxx.xxx:6387 192.168.xxx.xxx:6381

(redis-cli –cluster add-node This node address is one of the node addresses in the cluster to be added)

4. Check the cluster status for the first time: redis-cli –cluster check 192.168.xxx.xxx:6381

5. Reassign the slot number: redis-cli –cluster reshard 192.168.xxx.xxx:6381

Then fill in the slots to be allocated and who to allocate to, and reassign them all: all

6. Check the cluster status for the second time: redis-cli –cluster check 192.168.xxx.xxx:6381

7. Assign the slave node 6388 to the master node 6387: redis-cli –cluster add-node 192.168.xxx.xxx:6388 192.168.xxx.xxx:6381 –cluster-slave –cluster-master-id 6387 node ten Hexadecimal number string

8. Check whether it is connected: redis-cli –cluster check 192.168.xxx.xxx:6381

5. Master-slave reduction case

(Delete the slave first)

1. Check the cluster status for the first time: Obtain the node ID of 6388: redis-cli –cluster check 192.168.xxx.xxx:6381

2. Delete 6388: redis-cli –cluster del-node 192.168.xxx.xxx:6388 6388 node number

(:node id of IP port 6388)

4. Clear the slot number of 6387 and redistribute it. In this case, all the cleared slot numbers will be assigned to 6381.

Reshard: redis-cli –cluster reshard 192.168.xxx.xxx:6381

Slot size to be allocated:

Who will receive: 6381 node id

Who will take out these slots: 6387 node id

5. Check the cluster status for the second time: redis-cli –cluster check 192.168.xxx.xxx:6381

6. Delete 6387: redis-cli –cluster del-node 192.168.xxx.xxx:6387 6387 node number

7. Check the cluster status for the third time: redis-cli –cluster check 192.168.xxx.xxx:6381