cluster meet 目标节点IP 目标节点端口
把6个节点加到一个集群中:
127.0.0.1:6380> cluster meet 127.0.0.1 6381OK127.0.0.1:6380> cluster meet 127.0.0.1 6382OK127.0.0.1:6380> cluster meet 127.0.0.1 6383OK127.0.0.1:6380> cluster meet 127.0.0.1 6384OK127.0.0.1:6380> cluster meet 127.0.0.1 6385OK
只需要在集群中任意节点上执行cluster meet命令加入新的节点,握手状态会通过消息在集群中传播,其他节点也会自动发现新节点并与之发起握手流程 。
我们再执行一下cluster nodes命令,检查一下6个节点是否已经组成集群:
127.0.0.1:6380> cluster nodes1e1f45677d7b9b0130d03193f0bcec34578ac47d 127.0.0.1:6385@16385 master - 0 1586617919021 5 connecteddf1ac987f47dea35f1d0a83c3b405f0ef86892ab 127.0.0.1:6380@16380 myself,master - 0 1586617916000 2 connected5846b66ebe4fb4a5dcfd035652cc471f7e412752 127.0.0.1:6381@16381 master - 0 1586617917005 1 connecteda435cf98c3444b0b110a224401e397a107c453ef 127.0.0.1:6384@16384 master - 0 1586617914988 4 connected71e0e9e9a6f0c7c85dbe0d396846a9072625c5e8 127.0.0.1:6383@16383 master - 0 1586617918013 3 connectede25590603c7a254cce43aa8437861c5c425d753d 127.0.0.1:6382@16382 master - 0 1586617916000 0 connected
可以看到,6个节点都在集群中了 。不过,此时因为还没有为集群中的节点分配槽,集群还处于下线状态,所有的数据读写都是被禁止的 。比如:
127.0.0.1:6380> set onemore study(error) CLUSTERDOWN Hash slot not served
接下来,我们为集群中的节点分配槽 。
分配槽我们把6380、6382、6384端口的节点作为主节点,负责处理槽和相关数据;6381、6383、6385端口的节点分别作为从节点,负责故障转移 。先把16384个槽平均分配给6380、6382、6384端口的节点,为节点分配槽是通过cluster addslots命令实现:
# ./redis-cli -h 127.0.0.1 -p 6380 cluster addslots {0..5461}OK# ./redis-cli -h 127.0.0.1 -p 6382 cluster addslots {5462..10922}OK# ./redis-cli -h 127.0.0.1 -p 6384 cluster addslots {10923..16383}OK
我们再执行一下cluster nodes命令,检查一下槽是否已经分配:
127.0.0.1:6380> cluster nodes1e1f45677d7b9b0130d03193f0bcec34578ac47d 127.0.0.1:6385@16385 master - 0 1586619468000 5 connecteddf1ac987f47dea35f1d0a83c3b405f0ef86892ab 127.0.0.1:6380@16380 myself,master - 0 1586619464000 2 connected 0-54615846b66ebe4fb4a5dcfd035652cc471f7e412752 127.0.0.1:6381@16381 master - 0 1586619467000 1 connecteda435cf98c3444b0b110a224401e397a107c453ef 127.0.0.1:6384@16384 master - 0 1586619467000 4 connected 10923-1638371e0e9e9a6f0c7c85dbe0d396846a9072625c5e8 127.0.0.1:6383@16383 master - 0 1586619467348 3 connectede25590603c7a254cce43aa8437861c5c425d753d 127.0.0.1:6382@16382 master - 0 1586619468355 0 connected 5462-10922
再使用cluster replicate命令把一个节点变成从节点.,这个命令必须在从节点上运行,它的语法是:
cluster replicate 主节点ID
把6381、6383、6385端口的节点变成对应6380、6382、6384端口的节点的从节点:
# ./redis-cli -h 127.0.0.1 -p 6381127.0.0.1:6381> cluster replicate df1ac987f47dea35f1d0a83c3b405f0ef86892abOK127.0.0.1:6381> exit# ./redis-cli -h 127.0.0.1 -p 6383127.0.0.1:6383> cluster replicate e25590603c7a254cce43aa8437861c5c425d753dOK127.0.0.1:6383> exit# ./redis-cli -h 127.0.0.1 -p 6385127.0.0.1:6385> cluster replicate a435cf98c3444b0b110a224401e397a107c453efOK127.0.0.1:6385> exit
我们再执行一下cluster nodes命令,检查一下集群状态和主从关系:
127.0.0.1:6380> cluster nodesdf1ac987f47dea35f1d0a83c3b405f0ef86892ab 127.0.0.1:6380@16380 myself,master - 0 1586620148000 2 connected 0-54615846b66ebe4fb4a5dcfd035652cc471f7e412752 127.0.0.1:6381@16381 slave df1ac987f47dea35f1d0a83c3b405f0ef86892ab 0 1586620150000 2 connectede25590603c7a254cce43aa8437861c5c425d753d 127.0.0.1:6382@16382 master - 0 1586620151000 0 connected 5462-1092271e0e9e9a6f0c7c85dbe0d396846a9072625c5e8 127.0.0.1:6383@16383 slave e25590603c7a254cce43aa8437861c5c425d753d 0 1586620152220 3 connecteda435cf98c3444b0b110a224401e397a107c453ef 127.0.0.1:6384@16384 master - 0 1586620150000 4 connected 10923-163831e1f45677d7b9b0130d03193f0bcec34578ac47d 127.0.0.1:6385@16385 slave a435cf98c3444b0b110a224401e397a107c453ef 0 1586620149000 5 connected
推荐阅读
- 软件测试基础——Linux系统搭建MySQL数据库
- Java 分布式任务调度平台:PowerJob 快速开始+配置详解
- 淘宝活动中的商品可以改库存吗? 淘宝库存怎么快速修改
- excel中快速输入√与×并自动标注颜色,你不会,同事1分钟就搞定
- 怎么样才能快速健身?
- 教你如何快速开发一个Dubbo应用
- 网络工程师快速入门---网络架构及传输介质详解
- 运动快速减肥有哪些方法?
- 怎么快速减肥不运动呢?
- 怎么快速运动减肥呢?