This article will introduce you to the Cluster cluster in Redis, take a look at TCP ports, data sharding, and learn how to use docker to build a Redis cluster with three masters and three slaves.
Redis Cluster is Redis Distributed implementation. When we transfer data to Redis Cluster, the data will be automatically fragmented and stored on each Redis node. [Related recommendations: Redis Video Tutorial]
Compared with single-point Redis, Redis Cluster can continue to run when some nodes fail or cannot communicate. However, if there is a major server failure (for example, more than half of the servers are unavailable), the cluster will stop functioning.
Each Redis cluster node needs to open two TCP connections. One is the regular TCP port used to serve clients, which defaults to 6379. The second port is used for the cluster bus, with the default setting of 16379, a binary protocol node-to-node communication channel. Nodes utilize the cluster bus for failure detection, configuration updates, failover authorization, etc.
Redis Cluster does not use consistent hashing, but uses a hash slot. hash_slot stuff.
There are 16384 hash slots in the Redis cluster. When we store a pair of Key-Value in Redis, we need to calculate the hash slot for a given Key. The method is to first calculate the CRC16 of the Key, and then take the calculated result modulo 16384:
hash_slot = CRC16(key) mod 16384
Everyone in the Redis cluster Redis nodes are each responsible for a subset of hash slots, so if you have a cluster of 3 nodes where:
This makes it easy to add and remove nodes in the cluster. For example, if I want to add a new node D, I need to move some hash slots from nodes A, B, C to D. Similarly, if I want to remove node A from the cluster, I just move the hash slots served by A to B and C. When node A is empty, I can completely remove it from the cluster.
Because moving a hash slot from one node to another does not require downtime, adding or removing or changing the proportion of hash slots held by a node does not require any downtime.
Next we use docker to build a Redis cluster with three masters and three slaves.
Redis configuration
port ${PORT} ##节点端口 protected-mode no ##开启集群模式 cluster-enabled yes ##cluster集群模式 cluster-config-file nodes.conf ##集群配置名 cluster-node-timeout 5000 ##超时时间 cluster-announce-ip 192.168.1.XX ##实际为各节点网卡分配ip 先用一个ip代替 cluster-announce-port ${PORT} ##节点映射端口 cluster-announce-bus-port 1${PORT} ##节点总线端口 appendonly yes ##持久化模式
Create a custom network
docker network create redis-net
Custom path
mkdir -p /usr/redis_cluster cd /usr/redis_cluster
Generate conf and data targets under the custom path, and generate configuration information
for port in `seq 6001 6006`; do mkdir -p ./${port}/conf touch ./${port}/conf/redis.conf mkdir -p ./${port}/data echo "port ${port}" >>./${port}/conf/redis.conf echo "protected-mode no" >>./${port}/conf/redis.conf echo "cluster-enabled yes" >>./${port}/conf/redis.conf echo "cluster-config-file nodes.conf" >>./${port}/conf/redis.conf echo "cluster-node-timeout 5000" >>./${port}/conf/redis.conf echo "cluster-announce-ip 192.168.1.XX" >>./${port}/conf/redis.conf echo "cluster-announce-port ${port}" >>./${port}/conf/redis.conf echo "cluster-announce-bus-port 1${port}" >>./${port}/conf/redis.conf echo "appendonly yes" >>./${port}/conf/redis.conf done
Start the Redis container
for port in `seq 6001 6006`; do \ docker run -d --privileged=true -p ${port}:${port} -p 1${port}:1${port}\ -v $PWD/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \ -v $PWD/${port}/data:/data \ --restart always --name redis-${port} --net redis-net \ redis:5.0.5 redis-server /usr/local/etc/redis/redis.conf; \ done
Start the cluster
# 进入任意Redis容器 docker exec -it redis-6001 /bin/bash # 初始化Redis集群命令 redis-cli --cluster create 172.19.0.2:6601 172.19.0.3:6602 172.19.0.4:6603 172.19.0.5:6604 172.19.0.6:6605 172.19.0.7:6606 --cluster-replicas 1
# 单机模式启动 redis-cli -h 127.0.0.1 -p 6001 # 集群模式启动 redis-cli -c -h 127.0.0.1 -p 6001
More programming For related knowledge, please visit: programming video! !
The above is the detailed content of An article to talk about the Cluster cluster in Redis. For more information, please follow other related articles on the PHP Chinese website!