In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "introduction of Cluster cluster in Redis". In daily operation, I believe many people have doubts about the introduction of Cluster cluster in Redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "introduction to Cluster cluster in Redis". Next, please follow the editor to study!
Redis Cluster (Redis Cluster)
Redis Cluster is a distributed implementation of Redis. When we transfer the data to the Redis Cluster, the data is automatically sliced and stored on each Redis node.
As opposed to a single point of Redis, Redis Cluster can continue to run when some nodes fail or are unable to communicate. However, if there is a major failure of the server (for example, more than half of the servers are unavailable), the cluster will stop running.
Redis Cluster TCP port
Each Redis cluster node needs to open two TCP connections. One is the regular TCP port for service clients, which defaults to 6379. The second port is used for the cluster bus, with a default setting of 16379, the node-to-node communication channel of the binary protocol. Nodes use cluster bus for fault detection, configuration update, failover authorization and so on.
Clients should never try to communicate with cluster bus ports, but should always communicate with normal Redis command ports, but be sure to open both ports in the firewall at the same time, otherwise Redis cluster nodes will not be able to communicate. The offset of the command port and the cluster bus port is fixed and is always 10000.
If both TCP ports are not open at the same time, the cluster will not function properly.
Cluster bus uses different binary protocols for node-to-node data exchange, which is more suitable for exchanging information between nodes with less bandwidth and processing time.
Redis Cluster data fragmentation
Instead of using consistent hashes, Redis Cluster uses something called a hash slot hash_slot.
There are 16384 hash slots in the Redis cluster, and when we store a pair of Key-Value in Redis, we calculate the hash slot for a given Key. The method is to first calculate the CRC16 of Key, and then take the module 16384 for the calculated result:
Hash_slot = CRC16 (key) mod 16384
Each Redis node in the Redis cluster is responsible for a subset of the hash slot, so if you have a cluster of 3 nodes, where:
Node A contains hash slots from 0 to 5500.
Node B contains hash slots from 5501 to 11000.
Node C contains hash slots from 11001 to 16383.
This makes it easy to add and remove nodes from the cluster. For example, if I want to add a new node D, I need to move some hash slots from node A _ Magi B ~ C to D. Similarly, if I want to remove node A from the cluster, I just need to move the hash slot served by A to B and C. When node An is empty, I can remove it completely from the cluster.
Because there is no need to stop moving hash slots from one node to another, adding or removing or changing the proportion of hash slots held by nodes does not require any downtime.
Next, we use docker to build a Redis cluster of three masters and three slaves.
Using Docker to build RedisCluster
Redis configuration
Port ${PORT} # # Node port protected-mode no # # enable cluster mode cluster-enabled yes # # cluster cluster mode cluster-config-file nodes.conf # # Cluster configuration name cluster-node-timeout 5000 # # timeout cluster-announce-ip 192.168.1.XX # # actually assign ip to each node Nic first use an ip instead of cluster-announce-port ${PORT} # # Node Mapping Port cluster-announce-bus-port 1 ${PORT} # # Node bus Port appendonly yes # # persistence mode
Create a custom network
Docker network create redis-net
Custom path
Mkdir-p / usr/redis_clustercd / 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.confdone
The IP in cluster-announce-ip 192.168.1.XX must be the ip for communication between containers, which can be viewed later in the previously added network.
A total of 6 folders are generated, from 6001 to 6006, each containing the data and conf folders, and the redis.conf configuration file in the conf.
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
# enter any Redis container docker exec-it redis-6001 / bin/bash# to initialize the Redis cluster command redis-cli-- cluster create 172.19.0.2VR 6601 172.19.0.3 it redis-6001 6602 172.19.0.4 it redis-6001 6603 172.19.0.5 it redis-6001 6604 172.19.0.6 VR 6605 172.19.0.7 it redis-6001 6606-- cluster-replicas 1
After successful creation, we can use the redis-cli command to connect to one of the Redis services.
# start redis-cli-h 127.0.0.1-p 600 cluster mode start redis-cli-c-h 127.0.0.1-p 6001 in stand-alone mode
After that, you can view the node information through the cluster nodes command, and find that it meets the expectation of 3 masters and 3 slaves.
At this point, the study on "introduction to Cluster clusters in Redis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.