Redis Cluster
Calling cluster
Why call?
- 1. concurrency
redis official data the number of concurrent 100,000 / sec
If a higher number of concurrent need another program, and clusters
- 2. The amount of data
Distributed simple to understand: adding machine
Response: a large amount of concurrency, large amount of data
Data distribution
Order of the partitions:
Sharing of data sets stored in the respective nodes
Hash partitioning
Calculated hash value is then take the remainder
hash (key) / nodes (e.g., node modulo embodiment)
- Node modulo
If you need to add a node to perform this migration of more consumption of space, double the recommended expansion mode.
For example three node migration, by expansion to six nodes, data after expansion migration was only 50%.
Consistent hashing
The token (0-2 ^ 32) node along time sharing.
If there is a node is inserted, it will only affect the adjacent nodes, the other nodes are not affected.
Virtual slot partition
Redis Cluster Partitioning
The contrast between the two
Build clusters
Mutual communication between the nodes
Redis Cluster Architecture
- node
cluster-enable:yes
- meet
Messages between nodes share
- Assigned slot
Redis designated data slots 16384.
When key access, will do hash calculations, and then take the remainder, find the corresponding slot in which range of
Find the corresponding node.
- copy
installation
The following installation code is not very strict, mainly in order to understand the structure of Redis Cluster. Note that node setup and port parameters need.
1. Configure the open node
redis-server redis-7000.conf
Open node, but this time each node are mutually isolated.
2.meet
message is done to meet shared between nodes
redis-cli -h 127.0.0.1 -p 7000 cluster meet 127.0.0.1 7001
The main cluster node configuration:
3. Assign slot:
Total number of slots: 16384
redis-cli -h 127.0.0.1 -p 7000 cluster addslots{0...5461}
redis-cli -h 127.0.0.1 -p 7001 cluster addslots{5462...10922}
redis-cli -h 127.0.0.1 -p 7002 cluster addslots{10923...16383}
4. From the master (auto-transfer failure)
cluster replicate node-id
Three from three main
redis-cli -h 127.0.0.1 -p 7003 cluster replicate ${node-id-7000}
redis-cli -h 127.0.0.1 -p 7004 cluster replicate ${node-id-7001}
redis-cli -h 127.0.0.1 -p 7005 cluster replicate ${node-id-7002}
sed 's/7000/7001/g' redis-7000.conf > redis-7001.conf
sed 's/7000/7002/g' redis-7000.conf > redis-7002.conf
sed 's/7000/7003/g' redis-7000.conf > redis-7003.conf
start up
redis-server redis-7000.conf
redis-server redis-7001.conf
redis-server redis-7002.conf
redis-server redis-7003.conf
redis-server redis-7004.conf
redis-server redis-7005.conf
A connection node
redis-cli -p 7000
redis-cli -p 7000 cluster node
redis-cli -p 7000 cluster info
redis-cli -p 7000 cluster slots
Clusters can be used is 16384 slots are allocated good
meet
redis-cli -p 7000 cluster meet 127.0.0.1 7001
redis-cli -p 7000 cluster meet 127.0.0.1 7002
redis-cli -p 7000 cluster meet 127.0.0.1 7003
redis-cli -p 7000 cluster meet 127.0.0.1 7004
redis-cli -p 7000 cluster meet 127.0.0.1 7005
Distribution groove
redis-cli -p 7000 cluster addslots 0
### shell命令分配槽 addslots.sh
start =$1
end =$2
port =$3
for slot in `seq ${start} ${end}`
do
echo "slot:${slot}"
redis-cli -p ${port} cluster addslots ${slot}
done
Execute scripts distribution groove
sh addslots.sh 0 5461 7000
From the main distribution
redis-cli -p 7004 cluster replicate “主节点的id”
Above the main way to understand this process clusters, there is an easier way to official behind.
If you want to delete a node, first turning off the current task
pkill -f redis-server
Before rdb also you need to remove files and log files, nodes -. * Conf file
My Folders directory
/usr/local/var/db/redis
Remove All on the line
Then re-create the service,
redis-server /Users/chennan/redis_cluster_demo/7000/redis.conf &
View
cluster nodes
It can now be found in the isolated state.
比较省事的创建方式mac使用
最后就是redis5.0为例的集群方式
1.首先创建6个实例(三主三从)
我这的架构是这样
先创建一个redis_cluster_demo的文件夹,然后在这个文件下面再创建6个文件夹
7000 7001 7002 8001 8002 8003
然后再每个文件夹里放入redis.conf,将系统安装redis之后的redis.conf拷过来就行。我的redis.conf目录在
/usr/local/etc/redis.conf
2.修改配置文件
修改以下7000文件下的redis.conf几个属性:
daemonize yes #配置redis作为守护进程运行,默认情况下,redis不是作为守护进程运行的
cluster-enabled yes #是否开启集群
cluster-node-timeout 5000 #集群超时时间
appendonly yes #aop日志开启,会每次进行写操作都记录一条日志
protected-mode no #3.2以后加的参数,关闭protected-mode模式,此时外部网络可以直接访问
#下面这些需要根据不同的端口进行修改
port 7000 #节点端口
pidfile /var/run/redis_7000.pid
dbfilename dump_7000.rdb
appendfilename "appendonly_7000.aof"
cluster-config-file nodes-7000.conf #该节点的对应的节点配置文件
然后将文件拷配到各个实例文件夹下,按照文件名进行修改例如7001.
port 7001
pidfile /var/run/redis_7001.pid
dbfilename dump_7001.rdb
appendfilename "appendonly_7001.aof"
cluster-config-file nodes-7001.conf
3.创建实例
redis-server /Users/chennan/redis_cluster_demo/7000/redis.conf &
redis-server /Users/chennan/redis_cluster_demo/7001/redis.conf &
redis-server /Users/chennan/redis_cluster_demo/7002/redis.conf &
redis-server /Users/chennan/redis_cluster_demo/8001/redis.conf &
redis-server /Users/chennan/redis_cluster_demo/8002/redis.conf &
redis-server /Users/chennan/redis_cluster_demo/8003/redis.conf &
4.一键开启集群
cluster-replicas的1表示每个主键点只有一个从节点。
前三个是主节点7000-7002
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 --cluster-replicas 1
出来的提示框输入yes。
输出结果如下即可。
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:8001 to 127.0.0.1:7000
Adding replica 127.0.0.1:8002 to 127.0.0.1:7001
Adding replica 127.0.0.1:8003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: d93815dea7a9466820106adbed8eba8d6979a756 127.0.0.1:7000
slots:[0-5460] (5461 slots) master
M: 0a6af15df11eb3a4736c42ee37ba107789d91150 127.0.0.1:7001
slots:[5461-10922] (5462 slots) master
M: 5964daefe5b38e1390e1ce4bdf6bbd3b1b889273 127.0.0.1:7002
slots:[10923-16383] (5461 slots) master
S: 98844eb247c216f787b72c640060621e895dc794 127.0.0.1:8001
replicates 5964daefe5b38e1390e1ce4bdf6bbd3b1b889273
S: 599ac0b1557745323c3e04e0e4f71abcc5cdc80d 127.0.0.1:8002
replicates d93815dea7a9466820106adbed8eba8d6979a756
S: 5e7ef2702a0a3568482d9c00de2bb5def3cbaf14 127.0.0.1:8003
replicates 0a6af15df11eb3a4736c42ee37ba107789d91150
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: d93815dea7a9466820106adbed8eba8d6979a756 127.0.0.1:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 5964daefe5b38e1390e1ce4bdf6bbd3b1b889273 127.0.0.1:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 0a6af15df11eb3a4736c42ee37ba107789d91150 127.0.0.1:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 98844eb247c216f787b72c640060621e895dc794 127.0.0.1:8001
slots: (0 slots) slave
replicates 5964daefe5b38e1390e1ce4bdf6bbd3b1b889273
S: 599ac0b1557745323c3e04e0e4f71abcc5cdc80d 127.0.0.1:8002
slots: (0 slots) slave
replicates d93815dea7a9466820106adbed8eba8d6979a756
S: 5e7ef2702a0a3568482d9c00de2bb5def3cbaf14 127.0.0.1:8003
slots: (0 slots) slave
replicates 0a6af15df11eb3a4736c42ee37ba107789d91150
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
该过程涉及到分配槽,节点之间共享消息以及主从节点的分配,比上面一步步配置省了很多时间,上面的过程,主要是为了了解Redis Cluster的架构。
其他:
redis密码设置最好的集群完之后再设置