搭建高可用的redis集群,避免standalone模式带给你的苦难

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

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

逐一搭建一下。

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

$ wget http: $ tar xzf redis-3.2.0-3.2.0

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

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

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

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

# Accept connections on the specified port, (IANA #

<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 # started cluster nodes can. In order to start a Redis instance

Every cluster node has a cluster configuration file. This file <span style="color: #0000ff;">is<span style="color: #000000;"> not

intended to be edited by hand. It <span style="color: #0000ff;">is<span style="color: #000000;"> created and updated by Redis nodes.

Every Redis Cluster node requires a different cluster configuration file.

Make sure that instances running <span style="color: #0000ff;">in the same system <span style="color: #0000ff;">do<span style="color: #000000;"> not have

overlapping cluster configuration file names.

<span style="color: #ff0000;">cluster<span style="color: #ff0000;">-config-file nodes-6379.conf

<3> directory

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

<4> protected-mode

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

# By mode enabled. You should disable it only no authentication configured,nor a specific the -mode no

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

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

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

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

[jack@localhost ~]$ cluster/redis-trib.rb create --replicas .: .: .: .: .: .: /usr/bin/~]$

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

[jack@localhost ~-D level] -h | -K | -k | --v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-|-l[l] [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [--u user name|#uid] [-g groupname|-AbEHknPS] [-r role] [-t type] [-C fd] [-D level] [-|#gid] [-p prompt] [-u user name|#uid] [-g groupname|=value] [-i|-s] [-e [-AknS] [-r role] [-t type] [-C fd] [-D level] [-|#gid] [-p prompt] [-u user name|~~-~~* **-->---> Package ruby.x86_64 :.---> Processing Dependency: ruby-libs = .-.el6_6 package: ruby-.---> Processing Dependency: libruby.so.()(64bit) package: ruby-.--->---> Package ruby-libs.x86_64 :.---> Processing Dependency: libreadline.so.()(64bit) package: ruby-libs-.--->---> Package compat-readline5.x86_64 :--->Dependencies Resolved

================================================================================<span style="color: #000000;">
Package Arch Version Repository Size
================================================================================<span style="color: #000000;">
Installing:
ruby x86_64 <span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6 <span style="color: #0000ff;">base <span style="color: #800080;">538<span style="color: #000000;"> k
Installing <span style="color: #0000ff;">for<span style="color: #000000;"> dependencies:
compat-readline5 x86_64 <span style="color: #800080;">5.2-<span style="color: #800080;">17.1.el6 <span style="color: #0000ff;">base <span style="color: #800080;">130<span style="color: #000000;"> k
ruby-libs x86_64 <span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6 <span style="color: #0000ff;">base <span style="color: #800080;">1.7<span style="color: #000000;"> M

Transaction Summary
================================================================================<span style="color: #000000;">
Install <span style="color: #800080;">3<span style="color: #000000;"> Package(s)

Total download size: <span style="color: #800080;">2.3<span style="color: #000000;"> M
Installed size: <span style="color: #800080;">7.8<span style="color: #000000;"> M
Is <span style="color: #0000ff;">this ok [y/<span style="color: #000000;">N]: y
Downloading Packages:
(<span style="color: #800080;">1/<span style="color: #800080;">3): compat-readline5-<span style="color: #800080;">5.2-<span style="color: #800080;">17.1.el6.x86_64.rpm | <span style="color: #800080;">130 kB <span style="color: #800080;">00:<span style="color: #800080;">00<span style="color: #000000;">
(<span style="color: #800080;">2/<span style="color: #800080;">3): ruby-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64.rpm | <span style="color: #800080;">538 kB <span style="color: #800080;">00:<span style="color: #800080;">00<span style="color: #000000;">
(<span style="color: #800080;">3/<span style="color: #800080;">3): ruby-libs-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64.rpm | <span style="color: #800080;">1.7 MB <span style="color: #800080;">00:<span style="color: #800080;">02
--------------------------------------------------------------------------------<span style="color: #000000;">
Total <span style="color: #800080;">747 kB/s | <span style="color: #800080;">2.3 MB <span style="color: #800080;">00:<span style="color: #800080;">03<span style="color: #000000;">
warning: rpmts_HdrFromFdno: Header V3 RSA/<span style="color: #000000;">SHA256 Signature,key ID c105b9de: NOKEY
Retrieving key <span style="color: #0000ff;">from file:<span style="color: #808080;">///<span style="color: #008000;">etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key <span style="color: #800080;">0xC105B9DE<span style="color: #000000;">:
Userid : CentOS-<span style="color: #800080;">6 Key (CentOS <span style="color: #800080;">6 Official Signing Key) <centos-<span style="color: #800080;">6-key@centos.org><span style="color: #000000;">
Package: centos-release-<span style="color: #800080;">6-<span style="color: #800080;">5.el6.centos.<span style="color: #800080;">11.1.x86_64 (@anaconda-CentOS-<span style="color: #800080;">201311272149.x86_64/<span style="color: #800080;">6.5<span style="color: #000000;">)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-<span style="color: #800080;">6<span style="color: #000000;">
Is <span style="color: #0000ff;">this ok [y/<span style="color: #000000;">N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : compat-readline5-<span style="color: #800080;">5.2-<span style="color: #800080;">17.1.el6.x86_64 <span style="color: #800080;">1/<span style="color: #800080;">3<span style="color: #000000;">
Installing : ruby-libs-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64 <span style="color: #800080;">2/<span style="color: #800080;">3<span style="color: #000000;">
Installing : ruby-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64 <span style="color: #800080;">3/<span style="color: #800080;">3<span style="color: #000000;">
Verifying : compat-readline5-<span style="color: #800080;">5.2-<span style="color: #800080;">17.1.el6.x86_64 <span style="color: #800080;">1/<span style="color: #800080;">3<span style="color: #000000;">
Verifying : ruby-libs-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64 <span style="color: #800080;">2/<span style="color: #800080;">3<span style="color: #000000;">
Verifying : ruby-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64 <span style="color: #800080;">3/<span style="color: #800080;">3<span style="color: #000000;">

Installed:
ruby.x86_64 <span style="color: #800080;">0:<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4<span style="color: #000000;">.el6_6

Dependency Installed:
compat-readline5.x86_64 <span style="color: #800080;">0:<span style="color: #800080;">5.2-<span style="color: #800080;">17.1.el6 ruby-libs.x86_64 <span style="color: #800080;">0:<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4<span style="color: #000000;">.el6_6

Complete!<span style="color: #000000;">
[root@localhost jack]#

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

[root@localhost jack]# cluster/redis-trib.rb create --replicas .: .: .: .: .: .:/redis-trib.rb:: `require cluster/redis-trib.rb:

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

[root@localhost jack]# Loaded plugins: fastestmirror,security Loading mirror speeds * **-->---> Package rubygems.noarch :.---> Processing Dependency: ruby-rdoc package: rubygems-.--->---> Package ruby-rdoc.x86_64 :.---> Processing Dependency: ruby-irb = .-.el6_6 package: ruby-rdoc-.--->---> Package ruby-irb.x86_64 :.--->Dependencies Resolved

================================================================================<span style="color: #000000;">
Package Arch Version Repository Size
================================================================================<span style="color: #000000;">
Installing:
rubygems noarch <span style="color: #800080;">1.3.<span style="color: #800080;">7-<span style="color: #800080;">5.el6 <span style="color: #0000ff;">base <span style="color: #800080;">207<span style="color: #000000;"> k
Installing <span style="color: #0000ff;">for<span style="color: #000000;"> dependencies:
ruby-irb x86_64 <span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6 <span style="color: #0000ff;">base <span style="color: #800080;">317<span style="color: #000000;"> k
ruby-rdoc x86_64 <span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6 <span style="color: #0000ff;">base <span style="color: #800080;">381<span style="color: #000000;"> k

Transaction Summary
================================================================================<span style="color: #000000;">
Install <span style="color: #800080;">3<span style="color: #000000;"> Package(s)

Total download size: <span style="color: #800080;">905<span style="color: #000000;"> k
Installed size: <span style="color: #800080;">3.0<span style="color: #000000;"> M
Downloading Packages:
(<span style="color: #800080;">1/<span style="color: #800080;">3): ruby-irb-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64.rpm | <span style="color: #800080;">317 kB <span style="color: #800080;">00:<span style="color: #800080;">00<span style="color: #000000;">
(<span style="color: #800080;">2/<span style="color: #800080;">3): ruby-rdoc-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64.rpm | <span style="color: #800080;">381 kB <span style="color: #800080;">00:<span style="color: #800080;">00<span style="color: #000000;">
(<span style="color: #800080;">3/<span style="color: #800080;">3): rubygems-<span style="color: #800080;">1.3.<span style="color: #800080;">7-<span style="color: #800080;">5.el6.noarch.rpm | <span style="color: #800080;">207 kB <span style="color: #800080;">00:<span style="color: #800080;">00
--------------------------------------------------------------------------------<span style="color: #000000;">
Total <span style="color: #800080;">625 kB/s | <span style="color: #800080;">905 kB <span style="color: #800080;">00:<span style="color: #800080;">01<span style="color: #000000;">
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : ruby-irb-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64 <span style="color: #800080;">1/<span style="color: #800080;">3<span style="color: #000000;">
Installing : ruby-rdoc-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64 <span style="color: #800080;">2/<span style="color: #800080;">3<span style="color: #000000;">
Installing : rubygems-<span style="color: #800080;">1.3.<span style="color: #800080;">7-<span style="color: #800080;">5.el6.noarch <span style="color: #800080;">3/<span style="color: #800080;">3<span style="color: #000000;">
Verifying : ruby-rdoc-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64 <span style="color: #800080;">1/<span style="color: #800080;">3<span style="color: #000000;">
Verifying : ruby-irb-<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6.x86_64 <span style="color: #800080;">2/<span style="color: #800080;">3<span style="color: #000000;">
Verifying : rubygems-<span style="color: #800080;">1.3.<span style="color: #800080;">7-<span style="color: #800080;">5.el6.noarch <span style="color: #800080;">3/<span style="color: #800080;">3<span style="color: #000000;">

Installed:
rubygems.noarch <span style="color: #800080;">0:<span style="color: #800080;">1.3.<span style="color: #800080;">7-<span style="color: #800080;">5<span style="color: #000000;">.el6

Dependency Installed:
ruby-irb.x86_64 <span style="color: #800080;">0:<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4.el6_6 ruby-rdoc.x86_64 <span style="color: #800080;">0:<span style="color: #800080;">1.8.<span style="color: #800080;">7.374-<span style="color: #800080;">4<span style="color: #000000;">.el6_6

Complete!<span style="color: #000000;">
[root@localhost jack]#

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

[root@localhost jack]# cluster/redis-trib.rb create --replicas .: .: .: .: .: .: /usr/lib/ruby/site_ruby//rubygems/custom_require.rb:: `gem_original_require /usr/lib/ruby/site_ruby//rubygems/custom_require.rb:: `require cluster/redis-trib.rb:

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

ruby的redis驱动。

-. redis-. redis-....

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

[root@localhost jack]# cluster/redis-trib.rb create --replicas .: .: .: .: .: .: >>>>>> Performing hash slots allocation on .: .: .:.: to .:.: to .:.: to .:.:- (.:- (.:- (.:.:.: the above configuration? (type >>>>>>>>>>>> Performing Cluster Check ( node .:.:- (.:- (.:- (.:.:.:>>> Check >>>

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

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

相关文章

文章浏览阅读1.3k次。在 Redis 中,键(Keys)是非常重要的概...
文章浏览阅读3.3k次,点赞44次,收藏88次。本篇是对单节点的...
文章浏览阅读8.4k次,点赞8次,收藏18次。Spring Boot 整合R...
文章浏览阅读978次,点赞25次,收藏21次。在Centos上安装Red...
文章浏览阅读1.2k次,点赞21次,收藏22次。Docker-Compose部...
文章浏览阅读2.2k次,点赞59次,收藏38次。合理的JedisPool资...