别忘记关闭防火墙
hash算法一致性
三台虚拟机,一台虚拟机搭建两个redis 且两个不同的端口
第一台ip和分配两个端口 6381 6382 --- 192.168.154.128 6381 6382
第二台ip和分配两个端口 6383 6384 --- 192.168.154.129 6383 6384
第三台ip和分配两个端口 6385 6386 --- 192.168.154.130 6385 6386
命令 mkdir cluster 不要用 -->mkdir /cluster 加这 斜杠 / 就会报错,因为 斜杠 是在根目录下创建目录
或者用 mkdir -p /myredis/cluster 创建树形目录
bind 0.0.0.0
daemonize yes
protected-mode no
port 6381
logfile "/myredis/cluster/cluster6381.log"
pidfile /myredis/cluster6381.pid
dir /myredis/cluster
dbfilename dump6381.rdb
appendonly yes
appendfilename "appendonly6381.aof"
requirepass 111111
masterauth 111111
bind 0.0.0.0
daemonize yes
protected-mode no
port 6382
logfile "/myredis/cluster/cluster6382.log"
pidfile /myredis/cluster6382.pid
dir /myredis/cluster
dbfilename dump6382.rdb
appendonly yes
appendfilename "appendonly6382.aof"
requirepass 111111
masterauth 111111
cluster-enabled yes
cluster-config-file nodes-6382.conf
cluster-node-timeout 5000
bind 0.0.0.0
daemonize yes
protected-mode no
port 6383
logfile "/myredis/cluster/cluster6383.log"
pidfile /myredis/cluster6383.pid
dir /myredis/cluster
dbfilename dump6383.rdb
appendonly yes
appendfilename "appendonly6383.aof"
requirepass 111111
masterauth 111111
cluster-enabled yes
cluster-config-file nodes-6383.conf
cluster-node-timeout 5000
bind 0.0.0.0
daemonize yes
protected-mode no
port 6384
logfile "/myredis/cluster/cluster6384.log"
pidfile /myredis/cluster6384.pid
dir /myredis/cluster
dbfilename dump6384.rdb
appendonly yes
appendfilename "appendonly6384.aof"
requirepass 111111
masterauth 111111
cluster-enabled yes
cluster-config-file nodes-6384.conf
cluster-node-timeout 5000
bind 0.0.0.0
daemonize yes
protected-mode no
port 6385
logfile "/myredis/cluster/cluster6385.log"
pidfile /myredis/cluster6385.pid
dir /myredis/cluster
dbfilename dump6385.rdb
appendonly yes
appendfilename "appendonly6385.aof"
requirepass 111111
masterauth 111111
cluster-enabled yes
cluster-config-file nodes-6385.conf
cluster-node-timeout 5000
bind 0.0.0.0
daemonize yes
protected-mode no
port 6386
logfile "/myredis/cluster/cluster6386.log"
pidfile /myredis/cluster6386.pid
dir /myredis/cluster
dbfilename dump6386.rdb
appendonly yes
appendfilename "appendonly6386.aof"
requirepass 111111
masterauth 111111
cluster-enabled yes
cluster-config-file nodes-6386.conf
cluster-node-timeout 5000
第一台(192.168.154.128)
[root@localhost cluster]# redis-server redisCluster6381.conf
[root@localhost cluster]# redis-server redisCluster6382.conf
第二台(192.168.154.129)
[root@localhost cluster]# redis-server redisCluster6383.conf
[root@localhost cluster]# redis-server redisCluster6384.conf
第三台(192.168.154.130)
[root@localhost cluster]# redis-server redisCluster6385.conf
[root@localhost cluster]# redis-server redisCluster6386.conf
replicas 1 为每个主机分配一个从机 slave
-a 111111 这个是客户端登录的密码 具体密码是根据信息配置的来确定客户端密码
redis-cli -a 111111 --cluster create --cluster-replicas 1 192.168.154.128:6381 192.168.154.128:6382 192.168.154.129:6383 192.168.154.129:6384 192.168.154.130:6385 192.168.154.130:6386
[ERR] Node 192.168.154.129:6384 is not empty
一定注意槽位的范围区间,需要路由到位,路由到位,路由到位,路由到位
就是说槽位之间要实现能跳转,才能达到集群的目的即数据之间共享的作用
之前 redis-cli -a 111111 -p 6381
正确 redis-cli -a 111111 -p 6381 -c
原因:hash算法算出的 hash_slot 槽位不在此主机上,需要路由跳转重定向到另一台主机提供的槽位上,达到集群的效果即共享数据集的意义
keyslot key
bind 0.0.0.0
daemonize yes
protected-mode no
port 6387
logfile "/myredis/cluster/cluster6387.log"
pidfile /myredis/cluster6387.pid
dir /myredis/cluster
dbfilename dump6387.rdb
appendonly yes
appendfilename "appendonly6387.aof"
requirepass 111111
masterauth 111111
cluster-enabled yes
cluster-config-file nodes-6387.conf
cluster-node-timeout 5000
bind 0.0.0.0
daemonize yes
protected-mode no
port 6388
logfile "/myredis/cluster/cluster6388.log"
pidfile /myredis/cluster6388.pid
dir /myredis/cluster
dbfilename dump6388.rdb
appendonly yes
appendfilename "appendonly6388.aof"
requirepass 111111
masterauth 111111
cluster-enabled yes
cluster-config-file nodes-6388.conf
cluster-node-timeout 5000
[root@localhost cluster]# redis-server redisCluster6387.conf
[root@localhost cluster]# redis-server redisCluster6388.conf
将新增的6387作为master节点加入原有集群
redis-cli -a 密码 --cluster add-node 自己实际IP地址(新员工):端口号 自己实际IP地址(老员工):端口号
redis-cli -a 111111 --cluster add-node 192.168.154.130:6387 192.168.154.130:6385
6387 就是将要作为master新增节点
6385 就是原来集群节点里面的领路人,相当于6387拜拜6385的码头从而找到组织加入集群
redis-cli -a 111111 --cluster check 192.168.154.130:6385
重新分派槽号
命令:redis-cli -a 密码 --cluster reshard IP地址:端口号
redis-cli -a 111111 --cluster reshard 192.168.154.130:6385
命令:redis-cli -a 密码 --cluster add-node ip:新slave端口 ip:新master端口 --cluster-slave --cluster-master-id 新主机节点ID
redis-cli -a 111111 --cluster add-node 192.168.154.130:6388 192.168.154.130:6387 --cluster-slave --cluster-master-id xxx新主机idxxx-------这个是6387的编号,按照自己实际情况
------------------------------
redis-cli -a 111111 --cluster add-node 192.168.154.130:6388 192.168.154.130:6387 --cluster-slave --cluster-master-id a6f8f63687c2a3e26a1571fe8e1e6cc399e5677d
redis-cli -a 111111 --cluster check 192.168.154.130:6387
查看目前运行状态
可以修改这个默认配置
cluster-require-full -coverage no
1、槽位范围 0 -16384 也就是 0-2^14
2、每个主机master 提供的槽位 是根据 16384 /主机数量 约等于的平摊槽位
3、