redis cluster(集群)模式的创建方式
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(集群)模式的创建方式的更多相关文章
- Redis cluster集群模式的原理
redis cluster redis cluster是Redis的分布式解决方案,在3.0版本推出后有效地解决了redis分布式方面的需求 自动将数据进行分片,每个master上放一部分数据 提供内 ...
- Azure Redis Cache (5) Redis Cache Cluster集群模式
<Windows Azure Platform 系列文章目录> Redis Cluster 3.0之后的版本,已经支持Redis Cluster集群模式,Redis Cluster采用无中 ...
- centos6下redis cluster集群部署过程
一般来说,redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口,比如:slaveof 192.168.10.10 6379,然后启动主从,主从 ...
- Ubuntu 16.04下Redis Cluster集群搭建(官方原始方案)
前提:先安装好Redis,参考:http://www.cnblogs.com/EasonJim/p/7599941.html 说明:Redis Cluster集群模式可以做到动态增加节点和下线节点,使 ...
- Docker 搭建 Redis Cluster 集群环境
使用 Docker 搭建 Redis Cluster,最重要的环节就是容器通信的问题,这一块我们在之前的文章中已经给大家解决了<Docker 网络模式详解及容器间网络通信>,本篇文章主要练 ...
- 搭建分布式 Redis Cluster 集群与 Redis 入门
目录 Redis 集群搭建 Redis 是啥 集群(Cluster) Redis Cluster 说明 Redis Cluster 节点 Redis Cluster 集群模式 不能保证一致性 创建和使 ...
- Redis系列5:深入分析Cluster 集群模式
Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) 1 背景 前面我们 ...
- K8S部署Redis Cluster集群(三主三从模式) - 部署笔记
一.Redis 介绍 Redis代表REmote DIctionary Server是一种开源的内存中数据存储,通常用作数据库,缓存或消息代理.它可以存储和操作高级数据类型,例如列表,地图,集合和排序 ...
- docker下创建redis cluster集群
概述 在Redis中,集群的解决方案有三种 主从复制 哨兵机制 Cluster Redis Cluster是Redis的分布式解决方案,在 3.0 版本正式推出. 准备工作 1.确定本机IP地址 2. ...
随机推荐
- redis watch 加 事务实现秒杀
<?php //redis watch 加 事务实现秒杀 $redis = new redis(); $result = $redis->connect('10.10.10.119 ...
- 安装“Microsoft SQL Server 2014 Management Objects”时报错"Error Writing to file: Microsoft.SqlServer.XEvent.Linq.dll."
问题: 当安装的软件依赖Microsoft SQL Server 2014 Management Objects时,会把这个组件打进安装包里,但是在服务器上安装时却报如下错误: “Error Writ ...
- webdriervAPI(警告框处理)
from selenium import webdriver driver = webdriver.Chorme() driver.get("http://www.baidu.co ...
- ffmpeg 编译集成libfdk-aac
目录 1. 编译libfdk-aac 2. 编译ffmpeg 1. 编译libfdk-aac 下载源码:$ git clone https://github.com/mstorsjo/fdk-aac. ...
- flask数据库连接池DBUtils
数据库连接池 为啥要使用数据库连接池 频繁的连接和断开数据库,消耗大,效率低 DBUtils可以创建多个线程连接数据库,且一直保持连接,不会断开 执行数据库操作时,由数据池分配线程,当数据池空时,可选 ...
- 用JavaScript来实现单例模式
首先,了解一下什么是单例模式,这里我直接把菜鸟教程中的定义给copy过来: 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供 ...
- centos7安装过程中的安装源设置
centos7的安装源设置:http://mirrors.aliyun.com/centos/7/os/x86_64/
- 深度优先dfs与广度bfs优先搜索总结+例题
DFS(Deep First Search)深度优先搜索 深度优先遍历(dfs)是对一个连通图进行遍历的算法.它的思想是从一个顶点开始,沿着一条路一直走到底,如果发现不能到达目标解,那就返回到上一个节 ...
- 【FFMPEG】不要试图用msvc来编译ffmpeg
原文:http://blog.csdn.net/hn756si/article/details/41147497 出于学习目的,想建一个vs2010工程来编译ffmpeg(http://www.ffm ...
- 【计算机视觉】Opencv中的Face Detection using Haar Cascades
[计算机视觉]Opencv中的Face Detection using Haar Cascades 标签(空格分隔): [图像处理] 声明:引用请注明出处http://blog.csdn.net/lg ...