redis系列(五):搭建redis-cluster集群
1、为什么要用redis-cluster
a、并发要求
redis官方声称可以达到10万每秒,但是如果业务需要每秒100万条呢?
b、数据量太大
一台服务器的内存正常是16-256G,如果业务需要500G内存怎么办?
2、搭建redis-cluster
针对上述问题,redis-cluster集群就提供了很好的解决方案。
(1)、先准备环境,开启多个redis实例
[root@localhost redis_conf]# ls
redis-.conf redis-.conf redis-.conf
redis-.conf redis-.conf redis-.conf
[root@localhost redis_conf]#
暂时准备了6个配置文件,
daemonize yes
port
logfile ./data//redis.log #日志存放位置
dir ./data/ # 数据存放位置
dbfilename dbmp.rdb # 数据文件名称
cluster-enabled yes # 开启集群模式
cluster-config-file nodes-.conf # 集群内部的配置文件
cluster-require-full-coverage no # redis cluster需要16384个slot都正常的时候才能对外提供服务,换句话说,只要任何一个slot异常那么整个cluster不对外提供服务。 因此生产环境一般为no
上面的配置为redis-7000.conf的配置文件内容,其他配置文件内容相同,只是将7000全部改成对应的端口。
可以使用如下命令快速生成:
sed "s/7000/7001/g" redis-.conf > redis-.conf
通过sed命令将redis-7000.conf中的7000修改成7001,然后写入redis-7001.conf文件中
每个节点仅仅是端口的不同。
注意:还要确保配置中的日志以及数据存放文件夹存在。
比如我的:
[root@localhost redis_conf]# mkdir -p data/{,,,,,}
[root@localhost redis_conf]# tree
.
├── data
│ ├──
│ ├──
│ ├──
│ ├──
│ ├──
│ └──
├── redis-.conf
├── redis-.conf
├── redis-.conf
├── redis-.conf
├── redis-.conf
└── redis-.conf directories, files
(2)、运行redis实例
[root@localhost redis_conf]# redis-server redis-.conf
[root@localhost redis_conf]# redis-server redis-.conf
[root@localhost redis_conf]# redis-server redis-.conf
[root@localhost redis_conf]# redis-server redis-.conf
[root@localhost redis_conf]# redis-server redis-.conf
[root@localhost redis_conf]# redis-server redis-.conf
查看是否已经启动
[root@localhost redis_conf]# ps -ef | grep redis
root : ? :: redis-server *: [cluster]
root : ? :: redis-server *: [cluster]
root : ? :: redis-server *: [cluster]
root : ? :: redis-server *: [cluster]
root : ? :: redis-server *: [cluster]
root : ? :: redis-server *: [cluster]
root : pts/ :: grep --color=auto redis
[root@localhost redis_conf]#
此时集群还用不了,可以登录redis查看
[root@localhost redis_conf]# redis-cli -p
127.0.0.1:> set name felixi
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:>
(3)、创建redis-cluster
a、准备ruby环境
下载,编译,安装ruby (ruby官网地址)
、下载(个人用的当前的最新版本2.6.0)
wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.0.tar.gz
、解压,安装
tar -zxvf ruby-2.6..tar.gz
cd ruby-2.6.
./configure --prefix=/opt/ruby/
make && make install
、添加环境变量
export PATH=/opt/ruby/bin/:$PATH # 将这句添加到./bashrc和/etc/profile文件末尾。
source ./bashrc /etc/profile # 加载一下
b、查看是否已经安装
[root@localhost ~]# gem -v
3.0.
[root@localhost ~]#
c、下载安装ruby操作redis的模块包
[root@localhost ~]# gem install redis
Successfully installed redis-4.1.
Parsing documentation for redis-4.1.
Done installing documentation for redis after seconds
gem installed
[root@localhost ~]#
7、启动集群
我的redis版本是5.0.2,使用如下方式启动
redis-cli --cluster create 127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1: --cluster-replicas
其他旧版本可能需要如下命令:注意(redis-trib.rb可能找不到,可以通过find / -name redis-trib.rb来查找)
/opt/redis-4.0./src/redis-trib.rb create --replicas 127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1:
8、出现如下说明启动成功
[root@localhost ~]# redis-cli --cluster create 127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1: --cluster-replicas
>>> Performing hash slots allocation on nodes...
Master[] -> Slots -
Master[] -> Slots -
Master[] -> Slots -
Adding replica 127.0.0.1: to 127.0.0.1:
Adding replica 127.0.0.1: to 127.0.0.1:
Adding replica 127.0.0.1: to 127.0.0.1:
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 511bc46a1da42e3964ce41f48234076bd5743baf 127.0.0.1:
slots:[-] ( slots) master
M: bb029209525fb9aa2eeaa8c27182065ca1b29457 127.0.0.1:
slots:[-] ( slots) master
M: 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0 127.0.0.1:
slots:[-] ( slots) master
S: 25c00188cc8b58f5442ac674647389ab4f9206e9 127.0.0.1:
replicates 511bc46a1da42e3964ce41f48234076bd5743baf
S: bd093c559ebbc54fead2da9200d6f0c10a90bc87 127.0.0.1:
replicates bb029209525fb9aa2eeaa8c27182065ca1b29457
S: 3c0031255f960fbc15bbf9a78dacd7427ac24115 127.0.0.1:
replicates 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0
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:)
M: 511bc46a1da42e3964ce41f48234076bd5743baf 127.0.0.1:
slots:[-] ( slots) master
additional replica(s)
M: 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0 127.0.0.1:
slots:[-] ( slots) master
additional replica(s)
M: bb029209525fb9aa2eeaa8c27182065ca1b29457 127.0.0.1:
slots:[-] ( slots) master
additional replica(s)
S: bd093c559ebbc54fead2da9200d6f0c10a90bc87 127.0.0.1:
slots: ( slots) slave
replicates bb029209525fb9aa2eeaa8c27182065ca1b29457
S: 25c00188cc8b58f5442ac674647389ab4f9206e9 127.0.0.1:
slots: ( slots) slave
replicates 511bc46a1da42e3964ce41f48234076bd5743baf
S: 3c0031255f960fbc15bbf9a78dacd7427ac24115 127.0.0.1:
slots: ( slots) slave
replicates 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.
[root@localhost ~]#
9、查看主从状态
redis-cli -p info replication
结果如下:
[root@localhost ~]# redis-cli -p info replication
# Replication
role:master
connected_slaves:
slave0:ip=127.0.0.1,port=,state=online,offset=,lag=
master_replid:2b20fa4941542dc58d9fc2c32fe6f3dbb6cce72b
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
[root@localhost ~]# redis-cli -p info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:
master_link_status:up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_replid:ad11aa24aa797d942a54550ee77ab0e185e9d92c
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
[root@localhost ~]#
9、向redis集群写入数据,查看数据流向
redis-cli -p 7000 -c #这里会将key自动的重定向,放到某一个节点的slot槽位中
效果如下:
[root@localhost ~]# redis-cli -p -c
127.0.0.1:> keys *
(empty list or set)
127.0.0.1:>
127.0.0.1:> set name felix
-> Redirected to slot [] located at 127.0.0.1:
OK
127.0.0.1:> keys *
) "name"
127.0.0.1:>
至此,集群就搭建好了
redis系列(五):搭建redis-cluster集群的更多相关文章
- Redis系列5:深入分析Cluster 集群模式
Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) 1 背景 前面我们 ...
- centos7搭建ELK Cluster集群日志分析平台(四):Fliebeat-简单测试
续之前安装好的ELK集群 各主机:es-1 ~ es-3 :192.168.1.21/22/23 logstash: 192.168.1.24 kibana: 192.168.1.25 测试机:cli ...
- centos7搭建ELK Cluster集群日志分析平台(三):Kibana
续 centos7搭建ELK Cluster集群日志分析平台(一) 续 centos7搭建ELK Cluster集群日志分析平台(二) 已经安装好elasticsearch 5.4集群和logst ...
- centos7搭建ELK Cluster集群日志分析平台(二):Logstash
续 centos7搭建ELK Cluster集群日志分析平台(一) 已经安装完Elasticsearch 5.4 集群. 安装Logstash步骤 . 安装Java 8 官方说明:需要安装Java ...
- centos7搭建ELK Cluster集群日志分析平台(一):Elasticsearch
应用场景: ELK实际上是三个工具的集合,ElasticSearch + Logstash + Kibana,这三个工具组合形成了一套实用.易用的监控架构, 很多公司利用它来搭建可视化的海量日志分析平 ...
- centos7搭建ELK Cluster集群日志分析平台
应用场景:ELK实际上是三个工具的集合,ElasticSearch + Logstash + Kibana,这三个工具组合形成了一套实用.易用的监控架构, 很多公司利用它来搭建可视化的海量日志分析平台 ...
- Windows系统搭建Mysql Cluster集群
简单介绍一下MySQL集群涉及的三种节点: 管理节点(也可以称管理服务器)是整个集群环境的核心,类似于集群中起调度作用的枢纽,由它来负责管理其它节点(数据节点和SQL节点)的开启.关闭或重启某 ...
- MySQL集群---②Windows平台搭建MySQL CLUSTER集群
原文:http://blog.csdn.net/mazhaojuan/article/details/42211857 本文将通过两台电脑来简单介绍一下Windows平台如何搭建MySQL集群. My ...
- Redis系列(五):Redis的过期键删除策略
本篇博客是Redis系列的第5篇,主要讲解下Redis的过期键删除策略. 本系列的前4篇可以点击以下链接查看: Redis系列(一):Redis简介及环境安装 Redis系列(二):Redis的5种数 ...
- Redis搭建(五):Cluster集群搭建
一.方案 1. 介绍 redis3.0及以上版本实现,集群中至少应该有奇数个节点,所以至少有三个节点,官方推荐三主三从的配置方式 使用哈希槽的概念,Redis 集群有16384个哈希槽,每个key通过 ...
随机推荐
- 14-3 SQL Server基本操作
SQL Server安装默认新建四个系统数据库 1.master数据库 Master数据库时SQL Server中最重要的数据库.它记录SQL Server实例的所有系统级信息,包括实例范围的元数据. ...
- Java Swing 资料(转载学习)
Swing图像界面简介:https://blog.csdn.net/xietansheng/article/details/72814531 Swing实用经验总结篇:https://blog.csd ...
- 5G能带来什么改变-从鸿蒙OS说起
背景 从5G投票事件开始,开始关注5G.许多文章都说到5G的特点有速度快.时延低,其中,时延低是最重要的特点.然而,时延低能给社会带来什么改变呢? 2G是短信的时代,3G促成了语音视频,4G促成了短视 ...
- 【错误集】类ExcelExport是公共的, 应在名为 ExcelExport.java 的文件中声明
检查类名是否相同 区分大小写,复制代码的时候会连类名也复制了,哈哈哈,总结一下
- js求对象数组的交集/并集/差集/去重
1.求交集 var arr1 = [{name:'name1',id:1},{name:'name2',id:2},{name:'name3',id:3}]; var arr1Id = [1,2,3] ...
- Linux ping:unknown host问题排查
一.检查网卡配置:输入ifconfig可以查看当前网卡配置的IP地址并且查看配置文件中网络的设置: [root@bqh- ~]# ifconfig eth0 Link encap:Ethernet H ...
- Scrapy 框架的使用
Scrapy 框架的介绍 Scrapy 是一个基于Twisted的异步处理框架,是纯Python实现的爬虫框架,其架构清晰模块之间的耦合成都低,可扩展性极强,可以灵活完成各种需求.我们只需要定制开发几 ...
- 设置Layer模态框的 z-index
$.get(url, {}, function(data){ layui.use(['layer'],function () { var layer = layui.layer,$=layui.$; ...
- 计算机网络原理,TCP&UDP
UDP伪首部:计算校验和时会用到,然后实际传输过程中里包含的IP地址没有什么用. UDP校验和计算:求数值之和,如果溢出回卷,最后求出反码;UDP伪首部,UDP首部,应用层数据相加 tcp报文,最短2 ...
- edgex简述
一.概述 Edgex foundry是一个Linux 基金会运营的开源边缘计算物联网软件框架项目,该项目的核心是基于与硬件和操作系统完全无关的参考软件平台建立的互操作框架,使能即插即用的组件生态系统, ...