现在项目上用redis的话,很少说不用集群的情况,毕竟如果生产上只有一台redis会有极大的风险,比如机器挂掉,或者内存爆掉,就比如我们生产环境

曾今也遭遇到这种情况,导致redis内存不够挂掉的情况,当然这些都是我们及其不能容忍的,第一个必须要做到高可靠,其次才是高性能,好了,下面我来

逐一搭建一下。

一:Redis集群搭建

1. 下载

首先去官网下载较新的3.2.0版本,下载方式还是非常简单的,比如官网介绍的这样。

$ wget http://download.redis.io/releases/redis-3.2.0.tar.gz
$ tar xzf redis-3.2.0.tar.gz
$ cd redis-3.2.0
$ make

2. redis配置

 由于我们要做集群,而且还要redis自带的redis-trib.rb 能正常运行,我们需要在集群中开启三台master,三台slave,所以这里我需要建立6个文件

夹,而且文件夹的名称就使用端口地址的名字,比如:6389. 6380....6384。

3. config配置。

现在directory的分布情况大致如上图,接下来要做的事情就是配置redis.conf了,在这里需要配置四个选项。。。

<1> port  端口地址,比如6380文件夹下面的port就是6380,

# Accept connections on the specified port, default is  (IANA #).
# If port is specified Redis will not listen on a TCP socket.
port

<2> cluster-enabled 和 cluster-config-file

这个顾名思义,首先需要开启redis的cluster模式,然后配置一个cluster-config-file文件,这个文件用于存放redis的实时信息,redis会动态追加和修

改这个conf下面的内容信息,不过要记住,这个nodes-6379.conf 可以根据 端口文件夹依次配置,比如6380文件夹可以改成nodes-6380.conf这样。。。

# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes # Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
#
cluster-config-file nodes-6379.conf

<3> directory

为了方便管理,我这里配置的root目录取决于在哪个文件夹,比如6380下面我的dir就是: dir ./6380/

# Note that you must specify a directory here, not a file name.
dir ./6379/

<4> protected-mode

这个是redis 3.2 才追加的一个功能,从功能注释中,我们就可以发现,这个默认就是不让外界可以访问redis,所以这里我们就改为no,可以远程访问。

# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no

ok,到现在为止,我们的config就修改完毕了,其他端口的文件夹也可以依次配置之~

二:开启redis

到现在为止,各个端口文件夹都配置成功了,接下来准备开启了,真的好么么哒~~~,窗口太多,有点萌萌的。

接下来我们可以看一下,在6379下面是不是有生成node-6379.conf文件,比如下面:

三:配置redis-trib.rb

因为redis-trib.rb是ruby写的,而我们的电脑肯定是没有ruby和一些配置依赖项,不过没关系,有强大的yum安装,一切都不是问题。

1. 执行replicas命令

[jack@localhost ~]$ cluster/redis-trib.rb  create --replicas  192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133:
/usr/bin/env: ruby: No such file or directory
[jack@localhost ~]$

可以看到ruby是没有安装的,所以下一步我们要安装ruby了。。。

2. 安装ruby 【一定要是管理员权限哦】

[jack@localhost ~]$ sudo
usage: sudo [-D level] -h | -K | -k | -V
usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user
name|#uid]
usage: sudo -l[l] [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-U user
name] [-u user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C fd] [-D level] [-g
groupname|#gid] [-p prompt] [-u user name|#uid] [-g groupname|#gid]
[VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C fd] [-D level] [-g
groupname|#gid] [-p prompt] [-u user name|#uid] file ...
[jack@localhost ~]$ su
Password:
jacsu: incorrect password
[jack@localhost ~]$ yum install ruby
Loaded plugins: fastestmirror, refresh-packagekit, security
You need to be root to perform this command.
[jack@localhost ~]$ jack
bash: jack: command not found
[jack@localhost ~]$ su
Password:
[root@localhost jack]# yum install ruby
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ruby.x86_64 :1.8.7.374-.el6_6 will be installed
--> Processing Dependency: ruby-libs = 1.8.7.374-.el6_6 for package: ruby-1.8.7.374-.el6_6.x86_64
--> Processing Dependency: libruby.so.1.8()(64bit) for package: ruby-1.8.7.374-.el6_6.x86_64
--> Running transaction check
---> Package ruby-libs.x86_64 :1.8.7.374-.el6_6 will be installed
--> Processing Dependency: libreadline.so.()(64bit) for package: ruby-libs-1.8.7.374-.el6_6.x86_64
--> Running transaction check
---> Package compat-readline5.x86_64 :5.2-17.1.el6 will be installed
--> Finished Dependency Resolution Dependencies Resolved ================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
ruby x86_64 1.8.7.374-.el6_6 base k
Installing for dependencies:
compat-readline5 x86_64 5.2-17.1.el6 base k
ruby-libs x86_64 1.8.7.374-.el6_6 base 1.7 M Transaction Summary
================================================================================
Install Package(s) Total download size: 2.3 M
Installed size: 7.8 M
Is this ok [y/N]: y
Downloading Packages:
(/): compat-readline5-5.2-17.1.el6.x86_64.rpm | kB :
(/): ruby-1.8.7.374-.el6_6.x86_64.rpm | kB :
(/): ruby-libs-1.8.7.374-.el6_6.x86_64.rpm | 1.7 MB :
--------------------------------------------------------------------------------
Total kB/s | 2.3 MB :
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS- Key (CentOS Official Signing Key) <centos--key@centos.org>
Package: centos-release--.el6.centos.11.1.x86_64 (@anaconda-CentOS-.x86_64/6.5)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : compat-readline5-5.2-17.1.el6.x86_64 /
Installing : ruby-libs-1.8.7.374-.el6_6.x86_64 /
Installing : ruby-1.8.7.374-.el6_6.x86_64 /
Verifying : compat-readline5-5.2-17.1.el6.x86_64 /
Verifying : ruby-libs-1.8.7.374-.el6_6.x86_64 /
Verifying : ruby-1.8.7.374-.el6_6.x86_64 / Installed:
ruby.x86_64 :1.8.7.374-.el6_6 Dependency Installed:
compat-readline5.x86_64 :5.2-17.1.el6 ruby-libs.x86_64 :1.8.7.374-.el6_6 Complete!
[root@localhost jack]#

3. 安装rubygems

接着我们再次运行 replicas命令,看看会怎么样。。。

[root@localhost jack]# cluster/redis-trib.rb  create --replicas  192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133:
cluster/redis-trib.rb::in `require': no such file to load -- rubygems (LoadError)
from cluster/redis-trib.rb:
[root@localhost jack]#

结果苦逼了,还是报错,看提示貌似是少了一个rubygems,这次我们还是通过yum安装。

[root@localhost jack]# yum install -y rubygems
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package rubygems.noarch :1.3.-.el6 will be installed
--> Processing Dependency: ruby-rdoc for package: rubygems-1.3.-.el6.noarch
--> Running transaction check
---> Package ruby-rdoc.x86_64 :1.8.7.374-.el6_6 will be installed
--> Processing Dependency: ruby-irb = 1.8.7.374-.el6_6 for package: ruby-rdoc-1.8.7.374-.el6_6.x86_64
--> Running transaction check
---> Package ruby-irb.x86_64 :1.8.7.374-.el6_6 will be installed
--> Finished Dependency Resolution Dependencies Resolved ================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
rubygems noarch 1.3.-.el6 base k
Installing for dependencies:
ruby-irb x86_64 1.8.7.374-.el6_6 base k
ruby-rdoc x86_64 1.8.7.374-.el6_6 base k Transaction Summary
================================================================================
Install Package(s) Total download size: k
Installed size: 3.0 M
Downloading Packages:
(/): ruby-irb-1.8.7.374-.el6_6.x86_64.rpm | kB :
(/): ruby-rdoc-1.8.7.374-.el6_6.x86_64.rpm | kB :
(/): rubygems-1.3.-.el6.noarch.rpm | kB :
--------------------------------------------------------------------------------
Total kB/s | kB :
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : ruby-irb-1.8.7.374-.el6_6.x86_64 /
Installing : ruby-rdoc-1.8.7.374-.el6_6.x86_64 /
Installing : rubygems-1.3.-.el6.noarch /
Verifying : ruby-rdoc-1.8.7.374-.el6_6.x86_64 /
Verifying : ruby-irb-1.8.7.374-.el6_6.x86_64 /
Verifying : rubygems-1.3.-.el6.noarch / Installed:
rubygems.noarch :1.3.-.el6 Dependency Installed:
ruby-irb.x86_64 :1.8.7.374-.el6_6 ruby-rdoc.x86_64 :1.8.7.374-.el6_6 Complete!
[root@localhost jack]#

4. 安装redis驱动

还是继续不死心,我们继续运行replicas命令命令。

[root@localhost jack]# cluster/redis-trib.rb  create --replicas  192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb::in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb::in `require'
from cluster/redis-trib.rb:
[root@localhost jack]#

卧槽,还是有问题,这次貌似是一个gem_original_require没有安装,当然这个一般是说ruby版本太老了,所以现在亲要么升级ruby版本,要么直接安装

ruby的redis驱动。

[root@localhost jack]# gem install redis
Successfully installed redis-3.3.
gem installed
Installing ri documentation for redis-3.3....
Installing RDoc documentation for redis-3.3....

6. 运行replicas命令

终于貌似所有的依赖我们都解决了,接下来就可以真的执行了,真是一把辛酸泪啊。。。操。。。

[root@localhost jack]# cluster/redis-trib.rb  create --replicas  192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133: 192.168.161.133:
>>> Creating cluster
>>> Performing hash slots allocation on nodes...
Using masters:
192.168.161.133:
192.168.161.133:
192.168.161.133:
Adding replica 192.168.161.133: to 192.168.161.133:
Adding replica 192.168.161.133: to 192.168.161.133:
Adding replica 192.168.161.133: to 192.168.161.133:
M: fdae457e803e2e04a7c549c69b44a2beefdae3bc 192.168.161.133:
slots:- ( slots) master
M: 74285cee0fa65e64b443ecc630e447a8a65ee9f8 192.168.161.133:
slots:- ( slots) master
M: 14ed067a1b85044325d5800fa8479a6b4e41a10c 192.168.161.133:
slots:- ( slots) master
S: c50b1ff1fe3a00b16703f3b5705d2dafe046240b 192.168.161.133:
replicates fdae457e803e2e04a7c549c69b44a2beefdae3bc
S: 8f7bd92f7ffb48e327820693a5820b5be7ea5556 192.168.161.133:
replicates 74285cee0fa65e64b443ecc630e447a8a65ee9f8
S: 4a85ed078b4c99afad7f3a9a8df09082c681a649 192.168.161.133:
replicates 14ed067a1b85044325d5800fa8479a6b4e41a10c
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 192.168.161.133:)
M: fdae457e803e2e04a7c549c69b44a2beefdae3bc 192.168.161.133:
slots:- ( slots) master
M: 74285cee0fa65e64b443ecc630e447a8a65ee9f8 192.168.161.133:
slots:- ( slots) master
M: 14ed067a1b85044325d5800fa8479a6b4e41a10c 192.168.161.133:
slots:- ( slots) master
M: c50b1ff1fe3a00b16703f3b5705d2dafe046240b 192.168.161.133:
slots: ( slots) master
replicates fdae457e803e2e04a7c549c69b44a2beefdae3bc
M: 8f7bd92f7ffb48e327820693a5820b5be7ea5556 192.168.161.133:
slots: ( slots) master
replicates 74285cee0fa65e64b443ecc630e447a8a65ee9f8
M: 4a85ed078b4c99afad7f3a9a8df09082c681a649 192.168.161.133:
slots: ( slots) master
replicates 14ed067a1b85044325d5800fa8479a6b4e41a10c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.
[root@localhost jack]#

从上面可以看到,trib程序告知我们是否用 6379,6380,6381 作为主, 6382,6383,6384作为从。。然后我就恩准了。。就这样我们的集群

就创建好了,好了,这篇就讲到这里,也不是特别复杂吧~~~

搭建高可用的redis集群,避免standalone模式带给你的苦难的更多相关文章

  1. Redis总结(八)如何搭建高可用的Redis集群

    以前总结Redis 的一些基本的安装和使用,大家可以这这里查看Redis 系列文章:https://www.cnblogs.com/zhangweizhong/category/771056.html ...

  2. keepalived工作原理和配置说明 腾讯云VPC内通过keepalived搭建高可用主备集群

    keepalived工作原理和配置说明 腾讯云VPC内通过keepalived搭建高可用主备集群 内网路由都用mac地址 一个mac地址绑定多个ip一个网卡只能一个mac地址,而且mac地址无法改,但 ...

  3. 三分钟快速搭建分布式高可用的Redis集群

    这里的Redis集群指的是Redis Cluster,它是Redis在3.0版本正式推出的专用集群方案,有效地解决了Redis分布式方面的需求.当单机内存.并发.流量等遇到瓶颈的时候,可以采用这种Re ...

  4. 快速掌握RabbitMQ(五)——搭建高可用的RabbitMQ集群

    RabbitMQ的集群是依赖erlang集群的,而erlang集群是通过.erlang.cookie文件进行通信认证的,所以我们使用RabbitMQ集群时只需要配置一下.erlang.cookie文件 ...

  5. 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接

    我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...

  6. 搭建高可用的MongoDB集群

    http://www.csdn.net/article/2014-04-09/2819221-build-high-avialable-mongodb-cluster-part-1/1 在大数据的时代 ...

  7. 手动搭建高可用的kubernetes 集群

    之前按照和我一步步部署 kubernetes 集群的步骤一步一步的成功的使用二进制的方式安装了kubernetes集群,在该文档的基础上重新部署了最新的v1.8.2版本,实现了kube-apiserv ...

  8. MyCAT+MySQL搭建高可用企业级数据库集群视频课程

    原文地址:https://www.guangboyuan.cn/mycatmysql%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8%E4%BC%81%E4% ...

  9. MyCAT+MySQL 搭建高可用企业级数据库集群——第2章 MyCat入门

    2-1 章节综述 2-2 什么是MyCat 2-3 什么是数据库中间层 2-4 MyCat的主要作用 2-5 MyCat基本元素 2-6 MyCat的安装 2-1 章节综述 1.掌握Mycat的基础概 ...

随机推荐

  1. php案列分享

    <?php function GetfourStr($len) { $chars_array = array( "0", "1", "2&quo ...

  2. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  3. (C#) Lock - 将对象上锁,互斥多个线程,使同步。

    C# Lock 原文:http://www.dotnetperls.com/lock Locking is essential in threaded programs. It restricts c ...

  4. 菜鸟-手把手教你把Acegi应用到实际项目中(11)-切换用户

    在某些应用场合中,我们可能需要用到切换用户的功能,从而以另一用户的身份进行相关操作.这一点类似于在Linux系统中,用su命令切换到另一用户进行相关操作.      既然实际应用中有这种场合,那么我们 ...

  5. 序列数据挖掘[ZZ]

    一.时间序列数据挖掘 时间序列是数据存在的特殊形式,序列的过去值会影响到将来值,这种影响的大小以及影响的方式可由时间序列中的趋势周期及非平稳等行为来刻画.一般来讲,时间序列数据都具有躁声.不稳定.随机 ...

  6. Gson转Map

    使用google的Gson包.把json字符串转成Map<String,Object>以及List<Object>对象,记得下载Gson包, 我使用的是gson-2.1.jar ...

  7. 剑指Offer_编程题_20

    题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印.   /* struct TreeNode { int val; struct TreeNode *left; struct TreeN ...

  8. PYthon3:简单几步实现冒泡排序

    1.概念理解: 冒泡排序:可以简单的理解为是列表中相近的元素,两两比较,小的在前面.最多需要len()-1次排序. 2.例子:a=[11,7,4,56,35,0] 3.代码实现: 4.输出结果: 第1 ...

  9. Django 编写自定义的 404 / 500 报错界面

    Django 编写自定义的 404 / 500 报错界面 1. 首先 setting.py 文件中的 debug 参数设置成 false ,不启用调试. DEBUG = False 2. 在 temp ...

  10. 文档类型、DOCTYPE切换和浏览器模式

    DTD(文档类型定义)是一种机器可读的规则,它们定义XML或HTML的特定版本中允许有什么.不允许有什么.在解析网页时,浏览器将使用这些规则检查页面的有效性并且采取相应的措施.浏览器通过分析页面的DO ...