Overview of Cluster Construction
Redis generally requires 6 nodes to form a complete high availability cluster. Here we use Docker-compose to quickly build the cluster. Cluster building is generally divided into three steps:
- Prepare the node
- Node to shake hands
- Distribution of tank
Step 1: Prepare a node
Download the code from GitHub
GitHub address: github.com/willcat/red…
Pay attention to
Due to code update, the container name is manually set in the configuration file, so the container name in the article is different from that in the code, for example, redis-cluster_redis-cluster-6380_1 in the article, node-80 in the code and in the actual operation. In this article, redis-cluster_redis-cluster-6381_1 is Node-81 in code and in practice
Start background services and all nodes
Go to the redis-cluster-docker directory and run the docker-compose up -d command
Check the current cluster status
Enter a container such as redis-cluster_redis-cluster-6380_1[1],docker execit redis-cluster_redis-cluster-6380_1 redis-cli -p 6380, then run the Cluster Nodes command as follows:
$ docker exec -it redis-cluster_redis-cluster-6380_1 redis-cli -p 6380
127.0.0.1:6380> cluster nodes
3914fe7597f9ad9e9c485cf473bcaa461973baaa :6380@16380 myself,master - 0 0 0 connected
Copy the code
It can be seen that at present each node can only return its own information, and each node is not aware of each other.
[1] Docker-compose automatically generates docker-compose based on the docker-comemage. yml configuration. If you don’t want to use the default name, you can specify docker exec using container_name in the configuration file
Step 2 Shake hands
Node handshake refers to the process in which a group of nodes running in cluster mode communicate with each other through the Gossip protocol.
- Execute on a node
cluster meet {ip} {port}
Command to reach a handshake between the two nodes, and the two nodes form a true mutually aware cluster, which then passes between the two nodes periodicallyping/pong
The message carries on normal node communication - Execute on any node in the cluster
cluster meet {ip} {port}
Command to add a new node that has not been added to the cluster - After all nodes are added to the cluster, you can view information about all nodes in the cluster
127.0.0.1:6380 > cluster nodes c6db83c252a072407707917474001c70da649407 172.26.0.6: master 6385 @ 16385-0 1565199856348 3 Connected e356c336482f7a8a3f786674b96ac06030b0dcb4 172.26.0.3:6381 @ 16381 master - 0, 1565199855000 connected Dae83c485b9fb1357947944b36007c3371a750f2 172.26.0.7: master 6383 @ 16383-0 1565199857362 5 connected C87aba899473356f25a919dc2d477340f5222ba4 172.26.0.2: master 6382 @ 16382 1565199855000-0 4 connected 5 ebbd85dfbe1e4e01bae5f4954418a436453876c 172.26.0.5:6384 @ 16384 master - 0 1565199855339 2 connected 3914 fe7597f9ad9e9c485cf473bcaa461973baaa 172.26.0.4:6380 @ 16380 myself, master 1565199855000-0 1 connectedCopy the code
Docker-compose default networking
- These containers will be added when docker-compose up is used to start the container
{app_name}_default
In the network - use
docker network ls
You can look at the network list,docker network inspect <network_name>
You can view the network configuration and IP address of each container. - You can also go through
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-cluster_redis-cluster-6385_1
Command to view the IP address of a container.
Step 3 Allocate slots
After the node establishes a handshake, the cluster is still offline and cannot be written.
127.0.0.1:6380 >set hello world
(error) CLUSTERDOWN Hash slot not served
Copy the code
You can view the cluster information and see that the cluster is in the Fail state
127.0.0.1:6380> cluster info Cluster_state :fail Cluster_SLOts_assigned :0 Cluster_SLOts_OK :0 Cluster_SLOts_pFAIL :0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:0 cluster_current_epoch:5 cluster_my_epoch:1 cluster_stats_messages_ping_sent:718 cluster_stats_messages_pong_sent:283 cluster_stats_messages_meet_sent:8 cluster_stats_messages_sent:1009 cluster_stats_messages_ping_received:283 cluster_stats_messages_pong_received:285 cluster_stats_messages_received:568Copy the code
Run the slot allocation command
Execute something like cluster addslots {0… 5461} to evenly allocate 0 to 16,383 slots to all nodes. Here we allocate only three nodes, and the other three serve as secondary nodes for the first three nodes so that failover can occur automatically if the primary node fails.
Redis -cli -p 6380 cluster addslots {0.. 5461} This mode $dockerexec-it redis-cluster_redis-cluster-6380_1 redis-cli -p 6380 cluster addslots {0.. 5461} $ dockerexec-it redis-cluster_redis-cluster-6381_1 redis-cli -p 6381 cluster addslots {5462.. 10922} $ dockerexec-it redis-cluster_redis-cluster-6382_1 redis-cli -p 6382 cluster addslots {10923.. 16282} / / set three node unallocated slot to 127.0.0.1 from node: 6383 > CLUSTER REPLICATE fe7597f9ad9e9c485cf473bcaa461973baaa OK 127.0.0.1 3914:6384 > CLUSTER REPLICATE e356c336482f7a8a3f786674b96ac06030b0dcb4 OK 127.0.0.1:6385 > CLUSTER REPLICATE c87aba899473356f25a919dc2d477340f5222ba4 OKCopy the code
Official cluster quick setup tool
In this way, the cluster of 3 master and 3 slave is set up, which can be felt to be quite troublesome. Redis3. x and redis4.x provide redis-trib.rb tools to facilitate quick cluster construction. In Redis5. x, you can directly use redis-CLI command to complete cluster construction directly, without redis-Trib. rb rely on Ruby The environment. In the code, readme.md provides commands for quick build up.
Redis5.x redis-cli mode can be used to set up redis-4.x cluster. Redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001\127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \ --cluster-replicas 1Copy the code
Cluster USES
Request redirection
‘MOVED 5798 172.20.0.6:6381’ Redis -cli -p 6380 -c ‘redis-cli -p 6380 -c’ In this way, future operations across nodes will automatically jump to the corresponding node
The Smart client
Redis clients in most development languages use Smart clients to support the cluster protocol. For client selection, refer to Clients.
- The Smart client maintains the slot-> Node mapping in the content, so that the local key-to-node search can be implemented, maximizing I/O efficiency
- while
Moved
Redirection helps Smart clients update slot-> Node mappings.
ASK a redirect
(Error) ASK {slot} {targetIP}:{targetPort} is returned, and the client extracts the target node information from the ASK redirection exception. Send the asking command to the target node to open the client connection flag, and then run the key command. Execute if it exists, and return a nonexistent message if it does not.
The cluster tolerance
TODO