redis常用的架构有三种,单例、哨兵、集群,其他的都说过了,这里只简单介绍集群搭建。

单例最简单没什么好说的。

哨兵之前说过,该模式下有哨兵节点监视master和slave,若master宕机可自动将slave转为master,但它也有一个问题,就是不能动态扩充,并且存储大小受每个节点的内存大小限制。

集群模式Redis-Cluster,采用无中心结构,每个节点都和集群内其他节点有连接,数据可以跨主机分布式存储,解决了存储大小受主机限制的问题,Redis集群预分好16384个插槽(slot),每个节点分配一部分slot,当需要在 Redis 集群中放置一个 key-value 时,根据哈希算法决定将key放到哪个slot中,进而找到对应存放数据的主机,查询数据也一样。

集群模式内部同样可以配置主从,例如集群有六个数据节点,可以设置三个主节点,每个主节点对应一个从节点,当一个主节点宕机,可以自动将从节点变成主节点,保证整个集群还能用。但是一个主节点和对应的从节点都宕机后集群将不可用。每个主节点可以配置多个从节点。

集群搭建步骤

1、创建几台虚机,(集群模式最少要三个主节点)例如我们搭建一个三主三从的集群,创建六台虚机,当然要测试的话也可以部署在同一台主机上,使用不同的端口模拟不同主机。

2、在每台主机上安装redis,注意redis配置文件需要注意cluster相关的配置,例如修改cluster-enabled  的值是yes,表示开启集群,其他的超时时间等配置根据实际修改或默认。

3、redis安装完之后不会自己变成集群,还需要执行创建集群的命令,这个每个版本不太一样,redis5使用redis-cli --cluster ,redis3使用redis-trib.rb,下面具体说。

redis5集群创建

redis5的cluster相关命令,此处只写创建,当然还支持添加节点、删除、对solt进行操作等

[root@fvb9wbpl-clustermanager-e0hhllme ~]# redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

创建主从集群命令,后面是集群的ip地址和端口,cluster-replicas 1表示一个主节点对应1个从节点,创建成功后会自动分配主从关系

redis-cli --cluster create --cluster-replicas  10.110.30.136: 10.110.30.137: 10.110.30.138: 10.110.30.144: 10.110.30.145: 10.110.30.146:

出现提示后再输入yes,然后开始创建集群

创建成功后查看集群,后面的ip可以是集群中的任意一个节点,结果中的M代表主节点,当M节点宕机后集群会自动把S节点变成M节点,这个可以自行测试

[root@fvb9wbpl-clustermanager-e0hhllme ~]# redis-cli --cluster check 10.110.30.68:
10.110.30.70: (5e49929d...) -> keys | slots | slaves.
10.110.30.69: (6dc594c5...) -> keys | slots | slaves.
10.110.30.112: (2b9a0210...) -> keys | slots | slaves.
[OK] keys in masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.110.30.68:)
S: 15a969da394187e239baab1cc57bc6ec810996fe 10.110.30.68:
slots: ( slots) slave
replicates 2b9a0210b3223a1d865324ec32cc70f8ebcbaf65
S: 095777fb0d8ad380e420b9ef465d202a745a14e6 10.110.30.114:
slots: ( slots) slave
replicates 6dc594c5180f655603504ab68591b2e7492c7963
S: 20a33128f5353d36cb084d6ae2bf24c5b4806538 10.110.30.113:
slots: ( slots) slave
replicates 5e49929d47e7bd438ea2e25c3731179f460eb387
M: 5e49929d47e7bd438ea2e25c3731179f460eb387 10.110.30.70:
slots:[-] ( slots) master
additional replica(s)
M: 6dc594c5180f655603504ab68591b2e7492c7963 10.110.30.69:
slots:[-] ( slots) master
additional replica(s)
M: 2b9a0210b3223a1d865324ec32cc70f8ebcbaf65 10.110.30.112:
slots:[-] ( slots) master
additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.

redis3集群创建

redis3不支持cluster,3版本创建集群使用redis-trib.rb,当然还需要ruby等不在这里介绍。

redis-trib.rb的参数跟cluster类似,了解了上面的下面这个一看就懂,基本一样的

[root@mucuijwb-clustermanager-o9sprluh bin]# /usr/local/bin/redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...> create host1:port1 ... hostN:portN
--replicas <arg>
check host:port
info host:port
fix host:port
--timeout <arg>
reshard host:port
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port
--weight <arg>
--auto-weights
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
del-node host:port node_id
set-timeout host:port milliseconds
call host:port command arg arg .. arg
import host:port
--from <arg>
--copy
--replace
help (show this help) For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

创建主从集群命令,参数含义跟cluster的一样

/usr/local/bin/redis-trib.rb create --replicas  10.110.30.74: 10.110.30.137: 10.110.30.138: 10.110.30.144: 10.110.30.145: 10.110.30.146:

检查集群

[root@mucuijwb-clustermanager-o9sprluh bin]# /usr/local/bin/redis-trib.rb check 10.110.30.74:
>>> Performing Cluster Check (using node 10.110.30.74:)
M: 7483d09dadda2ecebd9fdbd6b1f186769e00904d 10.110.30.74:
slots:- ( slots) master
additional replica(s)
M: 76ecc2cf2ddc07fe7f2fda53f2f5318198321761 10.110.30.72:
slots:- ( slots) master
additional replica(s)
M: 0f60c9c6595726d391805f5acfaf5be13496346a 10.110.30.73:
slots:- ( slots) master
additional replica(s)
S: 6f56bbe811be2ff97ef654777ae48c3ae5d83824 10.110.30.111:
slots: ( slots) slave
replicates 76ecc2cf2ddc07fe7f2fda53f2f5318198321761
S: 8c3bb1ed5e531665dfedf126df5121b76dc604d4 10.110.30.110:
slots: ( slots) slave
replicates 7483d09dadda2ecebd9fdbd6b1f186769e00904d
S: 543c3b42c93ccac1728eaa5d5c9a43938f922890 10.110.30.108:
slots: ( slots) slave
replicates 0f60c9c6595726d391805f5acfaf5be13496346a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.

集群连接

客户端连接集群模式可以直接连接redis节点,不需要再经过中间节点转发,客户端连接集群,需要加 -c 参数,否则插入数据会报错

[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c
127.0.0.1:> dbsize
(integer)
127.0.0.1:>

连接其他主机

[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c -h 10.110.30.113 -p
10.110.30.113:> dbsize
(integer)
10.110.30.113:>

插入数据和查询数据,注意对插槽slot的分配操作,可以看出来是分布式存储的

[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c -h 10.110.30.113 -p
10.110.30.113:> set foo bar
-> Redirected to slot [] located at 10.110.30.112:
OK10.110.30.112:> set c d
-> Redirected to slot [] located at 10.110.30.69:
OK
10.110.30.69:>
10.110.30.69:> get foo
-> Redirected to slot [] located at 10.110.30.112:
"bar"
10.110.30.112:> get c
-> Redirected to slot [] located at 10.110.30.69:
"d"
10.110.30.69:>

redis cluster(集群)模式的创建方式的更多相关文章

  1. Redis cluster集群模式的原理

    redis cluster redis cluster是Redis的分布式解决方案,在3.0版本推出后有效地解决了redis分布式方面的需求 自动将数据进行分片,每个master上放一部分数据 提供内 ...

  2. Azure Redis Cache (5) Redis Cache Cluster集群模式

    <Windows Azure Platform 系列文章目录> Redis Cluster 3.0之后的版本,已经支持Redis Cluster集群模式,Redis Cluster采用无中 ...

  3. centos6下redis cluster集群部署过程

    一般来说,redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口,比如:slaveof 192.168.10.10 6379,然后启动主从,主从 ...

  4. Ubuntu 16.04下Redis Cluster集群搭建(官方原始方案)

    前提:先安装好Redis,参考:http://www.cnblogs.com/EasonJim/p/7599941.html 说明:Redis Cluster集群模式可以做到动态增加节点和下线节点,使 ...

  5. Docker 搭建 Redis Cluster 集群环境

    使用 Docker 搭建 Redis Cluster,最重要的环节就是容器通信的问题,这一块我们在之前的文章中已经给大家解决了<Docker 网络模式详解及容器间网络通信>,本篇文章主要练 ...

  6. 搭建分布式 Redis Cluster 集群与 Redis 入门

    目录 Redis 集群搭建 Redis 是啥 集群(Cluster) Redis Cluster 说明 Redis Cluster 节点 Redis Cluster 集群模式 不能保证一致性 创建和使 ...

  7. Redis系列5:深入分析Cluster 集群模式

    Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) 1 背景 前面我们 ...

  8. K8S部署Redis Cluster集群(三主三从模式) - 部署笔记

    一.Redis 介绍 Redis代表REmote DIctionary Server是一种开源的内存中数据存储,通常用作数据库,缓存或消息代理.它可以存储和操作高级数据类型,例如列表,地图,集合和排序 ...

  9. docker下创建redis cluster集群

    概述 在Redis中,集群的解决方案有三种 主从复制 哨兵机制 Cluster Redis Cluster是Redis的分布式解决方案,在 3.0 版本正式推出. 准备工作 1.确定本机IP地址 2. ...

随机推荐

  1. mysql一条语句实现插入或更新的操作

    ,),(,) ON DUPLICATE KEY UPDATE c=VALUES(c); 或者 INSERT INTO table (id,a,b,c) select id,a,b,c from xxx ...

  2. Spring Security(2):过滤器链(filter chain)的介绍

    上一节中,主要讲了Spring Security认证和授权的核心组件及核心方法.但是,什么时候调用这些方法呢?答案就是Filter和AOP.Spring Security在我们进行用户认证以及授予权限 ...

  3. 通过route指令指定笔记本同时连接外网和内网

    假如你的外网网关是:X.X.X.X 内网网关:192.168.1.1 则在命令窗口输入以下两条命令: route add 0.0.0.0 mask 0.0.0.0 X.X.X.X route add ...

  4. C# json提取多层嵌套到数组-- C# json 数组

    json比一般格式再复杂点的就像数组一样,有多层嵌套,研究了一下,记录代码如下: string jsonText = "{'name':'test','phone':'18888888888 ...

  5. Java中处理OPC寄存器数据类型

    1. 在milo中,处理WORD等数据类型 例子如下: VariableNode node = client.getAddressSpace().createVariableNode( new Nod ...

  6. 牛客竞赛(gcd,快速幂)

    一.最大公约数和最小公倍数问题 题目描述: 输入2个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数. 条件:1.P, ...

  7. VC.VS版本&VC版本&OpenCV版本

    1.VS版本 与 VC版本 的对应关系,以及opencv 对 VC版本 的支持情况 - 魔法学徒 - CSDN博客.html(https://blog.csdn.net/yefcion/article ...

  8. linux 抓包工具tcpdump和tshark

    yum install tcpdump tcpdump -nn -c 100 抓一百条 tcpdump -nn -i eth0 指定网卡 tcpdump -nn port 22 指定端口 tcpdum ...

  9. .Net WebApi接口之Swagger集成详解

    本文详细的介绍了.net从一个新的项目中创建api后集成swagger调试接口的流程! 1.首先我们创建一个MVC项目(VS2012): 2.然后在项目中的Controllers文件夹中添加API接口 ...

  10. JavaSE基础(五)--Java运算符

    Java 运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运算符 ...