Before we begin, let's ask a few questions:
What is redis cluster?
keyHashSlot The function is used to calculate which slot a given key should be allocated to. The node allocation and node data storage are realized by using the fragment modulus algorithm.
/* --------------cluster.c--------------------------------------------------------------- * Key space handling * -------------------------------------------------------------------------- */ /* We have 16384 hash slots. The hash slot of a given key is obtained * as the least significant 14 bits of the crc16 of the key. * * However if the key contains the {...} pattern, only the part between * { and } is hashed. This may be useful in the future to force certain * keys to be in the same node (assuming no resharding is in progress). */ //We have 16384 hash slots. We will use the least significant 14 bits obtained by crc16 as the hash slot for a given key. //However, if the key contains fields such as' {...} ', only those between' {'and'} 'will be hashed. This may help to force some keys into the same node in the future (assuming no re fragmentation) // Calculates which slot a given key should be assigned to unsigned int keyHashSlot(char *key, int keylen) { int s, e; /* start-end indexes of { and } */ for (s = 0; s < keylen; s++) if (key[s] == '{') break; /* No '{' ? Hash the whole key. This is the base case. */ //0x3FFF converted to decimal is 16383 if (s == keylen) return crc16(key,keylen) & 0x3FFF; /* '{' found? Check if we have the corresponding '}'. */ for (e = s+1; e < keylen; e++) if (key[e] == '}') break; /* No '}' or nothing betweeen {} ? Hash the whole key. */ if (e == keylen || e == s+1) return crc16(key,keylen) & 0x3FFF; /* If we are here there is both a { and a } on its right. Hash * what is in the middle between { and }. */ return crc16(key+s+1,e-s-1) & 0x3FFF;
Why redis cluster cluster?
1. It can realize dynamic node capacity expansion and reduction.
2. High availability, automatic election after the node hangs up, and data migration to the redis node of the surviving node.
What should I pay attention to when setting up a redis cluster?
1. Node data is not synchronized.
2. Port ip communication connection failed.
Three servers are configured with 6381 as the master and 6382 as the slave:
The server | 192.168.192.138 | 192.168.192.137 | 192.168.192.140 |
port | 6381 | 6381 | 6381 |
6382 | 6382 | 6382 |
Corresponding data storage path: / data/6381/ / data/6382/
The data directory is automatically generated data for file mapping volumes.
/data/6381/ redis .conf
#port port 6381 #Set up external network connection redis service protected-mode no #Enable the daemon mode. In this mode, redis will run in the background daemonize yes #pid identification pidfile /var/run/redis_6381.pid #Enable persistence appendonly yes #Start cluster cluster-enabled yes #Cluster node configuration cluster-config-file nodes-6381.conf #Node timeout cluster-node-timeout 15000
/data/6382/ redis .conf
#port port 6382 #Set up external network connection redis service protected-mode no #Enable the daemon mode. In this mode, redis will run in the background daemonize yes #pid identification pidfile /var/run/redis_6382.pid #Enable persistence appendonly yes #Start cluster cluster-enabled yes #Cluster node configuration cluster-config-file nodes-6382.conf #Node timeout cluster-node-timeout 15000
The docker command can be configured to docker-compose.yml:
docker run -v /data/6382/data/:/data -v /data/6382/redis.conf:/etc/redis/redis.conf --privileged=true --net host --name redis-6382 -d redis:latest redis-server /etc/redis/redis.conf docker run -v /data/6381/data/:/data -v /data/6381/redis.conf:/etc/redis/redis.conf --privileged=true --net host --name redis-6381 -d redis:latest redis-server /etc/redis/redis.conf
Enter any container and start the redis command.
docker exec -it redis-6381 /bin/bash
Execute command:
redis-cli --cluster create 192.168.192.140:6382 192.168.192.140:6381 192.168.192.138:6382 192.168.192.138:6381 192.168.192.137:6382 192.168.192.137:6381 --cluster-replicas 1
An error was reported: Could not connect to Redis at 192.168.192.139:6381: No route to host. Generally, the node is not connected. Check the master-slave of the corresponding server node.
The error is that the host port is not open. Close the firewall
Could not connect to Redis at 192.168.192.140:6381: No route to host
[ERR] Node 192.168.192.140:6381 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
If the node data is out of sync, delete the node data and restore it from the new flushdb redis library.
Clear the configuration of docker mapping volume and restart redis.
/data/6381/data
/data/6382/data
Summary:
After seeing this, it indicates that your cluster has been started successfully. We write the default crc64 fragment modulus algorithm. There are 16384 card slots and 3 servers The data is stored according to the card slot. The data write cache can be synchronized to the specified node, so as to realize the high availability of redis.
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
I wrote one in 140 xy_cesi Key is retrieved through other server queries, and the written cache data will be automatically synchronized to other nodes. If you find no data, it is actually normal.
The following is a redis cluster with real production cache.
docker The establishment of redis cluster high availability cluster is over. Later, we will share micro services and integrate redis high availability cluster. It is not easy to create. Thank you for your support!!!