欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

redis集群搭建_动力节点Java学院整理

程序员文章站 2022-03-23 22:52:13
现在项目上用redis的话,很少说不用集群的情况,毕竟如果生产上只有一台redis会有极大的风险,比如机器挂掉,或者内存爆掉,就比如我们生产环境曾今也遭遇到这种情况,导致r...

现在项目上用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。

redis集群搭建_动力节点Java学院整理

3. config配置。

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

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

# accept connections on the specified port, default is 6379 (iana #815344).
# if port 0 is specified redis will not listen on a tcp socket.
port 6379

<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

到现在为止,各个端口文件夹都配置成功了,接下来准备开启了。

redis集群搭建_动力节点Java学院整理

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

redis集群搭建_动力节点Java学院整理

三:配置redis-trib.rb

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

1. 执行replicas命令

[jack@localhost ~]$ cluster/redis-trib.rb create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
/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 0:1.8.7.374-4.el6_6 will be installed
--> processing dependency: ruby-libs = 1.8.7.374-4.el6_6 for package: ruby-1.8.7.374-4.el6_6.x86_64
--> processing dependency: libruby.so.1.8()(64bit) for package: ruby-1.8.7.374-4.el6_6.x86_64
--> running transaction check
---> package ruby-libs.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> processing dependency: libreadline.so.5()(64bit) for package: ruby-libs-1.8.7.374-4.el6_6.x86_64
--> running transaction check
---> package compat-readline5.x86_64 0: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-4.el6_6     base    538 k
installing for dependencies:
 compat-readline5    x86_64    5.2-17.1.el6       base    130 k
 ruby-libs       x86_64    1.8.7.374-4.el6_6     base    1.7 m

transaction summary
================================================================================
install    3 package(s)

total download size: 2.3 m
installed size: 7.8 m
is this ok [y/n]: y
downloading packages:
(1/3): compat-readline5-5.2-17.1.el6.x86_64.rpm     | 130 kb   00:00   
(2/3): ruby-1.8.7.374-4.el6_6.x86_64.rpm         | 538 kb   00:00   
(3/3): ruby-libs-1.8.7.374-4.el6_6.x86_64.rpm      | 1.7 mb   00:02   
--------------------------------------------------------------------------------
total                      747 kb/s | 2.3 mb   00:03   
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-6 key (centos 6 official signing key) <centos-6-key@centos.org>
 package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-centos-201311272149.x86_64/6.5)
 from  : /etc/pki/rpm-gpg/rpm-gpg-key-centos-6
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             1/3 
 installing : ruby-libs-1.8.7.374-4.el6_6.x86_64              2/3 
 installing : ruby-1.8.7.374-4.el6_6.x86_64                3/3 
 verifying : compat-readline5-5.2-17.1.el6.x86_64             1/3 
 verifying : ruby-libs-1.8.7.374-4.el6_6.x86_64              2/3 
 verifying : ruby-1.8.7.374-4.el6_6.x86_64                3/3 

installed:
 ruby.x86_64 0:1.8.7.374-4.el6_6                        

dependency installed:
 compat-readline5.x86_64 0:5.2-17.1.el6 ruby-libs.x86_64 0:1.8.7.374-4.el6_6 

complete!
[root@localhost jack]# 

3. 安装rubygems

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

[root@localhost jack]# cluster/redis-trib.rb create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
cluster/redis-trib.rb:24:in `require': no such file to load -- rubygems (loaderror)
  from cluster/redis-trib.rb:24
[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 0:1.3.7-5.el6 will be installed
--> processing dependency: ruby-rdoc for package: rubygems-1.3.7-5.el6.noarch
--> running transaction check
---> package ruby-rdoc.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> processing dependency: ruby-irb = 1.8.7.374-4.el6_6 for package: ruby-rdoc-1.8.7.374-4.el6_6.x86_64
--> running transaction check
---> package ruby-irb.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> finished dependency resolution

dependencies resolved

================================================================================
 package      arch      version          repository  size
================================================================================
installing:
 rubygems     noarch     1.3.7-5.el6        base     207 k
installing for dependencies:
 ruby-irb     x86_64     1.8.7.374-4.el6_6     base     317 k
 ruby-rdoc     x86_64     1.8.7.374-4.el6_6     base     381 k

transaction summary
================================================================================
install    3 package(s)

total download size: 905 k
installed size: 3.0 m
downloading packages:
(1/3): ruby-irb-1.8.7.374-4.el6_6.x86_64.rpm       | 317 kb   00:00   
(2/3): ruby-rdoc-1.8.7.374-4.el6_6.x86_64.rpm      | 381 kb   00:00   
(3/3): rubygems-1.3.7-5.el6.noarch.rpm          | 207 kb   00:00   
--------------------------------------------------------------------------------
total                      625 kb/s | 905 kb   00:01   
running rpm_check_debug
running transaction test
transaction test succeeded
running transaction
 installing : ruby-irb-1.8.7.374-4.el6_6.x86_64              1/3 
 installing : ruby-rdoc-1.8.7.374-4.el6_6.x86_64              2/3 
 installing : rubygems-1.3.7-5.el6.noarch                 3/3 
 verifying : ruby-rdoc-1.8.7.374-4.el6_6.x86_64              1/3 
 verifying : ruby-irb-1.8.7.374-4.el6_6.x86_64              2/3 
 verifying : rubygems-1.3.7-5.el6.noarch                 3/3 

installed:
 rubygems.noarch 0:1.3.7-5.el6                         

dependency installed:
 ruby-irb.x86_64 0:1.8.7.374-4.el6_6  ruby-rdoc.x86_64 0:1.8.7.374-4.el6_6  

complete!
[root@localhost jack]# 

 

4. 安装redis驱动

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

[root@localhost jack]# cluster/redis-trib.rb create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (loaderror)
  from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
  from cluster/redis-trib.rb:25
[root@localhost jack]# 

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

[root@localhost jack]# gem install redis
successfully installed redis-3.3.0
1 gem installed
installing ri documentation for redis-3.3.0...
installing rdoc documentation for redis-3.3.0...

6. 运行replicas命令

终于貌似所有的依赖我们都解决了,接下来就可以真的执行了。

[root@localhost jack]# cluster/redis-trib.rb create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
>>> creating cluster
>>> performing hash slots allocation on 6 nodes...
using 3 masters:
192.168.161.133:6379
192.168.161.133:6380
192.168.161.133:6381
adding replica 192.168.161.133:6382 to 192.168.161.133:6379
adding replica 192.168.161.133:6383 to 192.168.161.133:6380
adding replica 192.168.161.133:6384 to 192.168.161.133:6381
m: fdae457e803e2e04a7c549c69b44a2beefdae3bc 192.168.161.133:6379
  slots:0-5460 (5461 slots) master
m: 74285cee0fa65e64b443ecc630e447a8a65ee9f8 192.168.161.133:6380
  slots:5461-10922 (5462 slots) master
m: 14ed067a1b85044325d5800fa8479a6b4e41a10c 192.168.161.133:6381
  slots:10923-16383 (5461 slots) master
s: c50b1ff1fe3a00b16703f3b5705d2dafe046240b 192.168.161.133:6382
  replicates fdae457e803e2e04a7c549c69b44a2beefdae3bc
s: 8f7bd92f7ffb48e327820693a5820b5be7ea5556 192.168.161.133:6383
  replicates 74285cee0fa65e64b443ecc630e447a8a65ee9f8
s: 4a85ed078b4c99afad7f3a9a8df09082c681a649 192.168.161.133:6384
  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:6379)
m: fdae457e803e2e04a7c549c69b44a2beefdae3bc 192.168.161.133:6379
  slots:0-5460 (5461 slots) master
m: 74285cee0fa65e64b443ecc630e447a8a65ee9f8 192.168.161.133:6380
  slots:5461-10922 (5462 slots) master
m: 14ed067a1b85044325d5800fa8479a6b4e41a10c 192.168.161.133:6381
  slots:10923-16383 (5461 slots) master
m: c50b1ff1fe3a00b16703f3b5705d2dafe046240b 192.168.161.133:6382
  slots: (0 slots) master
  replicates fdae457e803e2e04a7c549c69b44a2beefdae3bc
m: 8f7bd92f7ffb48e327820693a5820b5be7ea5556 192.168.161.133:6383
  slots: (0 slots) master
  replicates 74285cee0fa65e64b443ecc630e447a8a65ee9f8
m: 4a85ed078b4c99afad7f3a9a8df09082c681a649 192.168.161.133:6384
  slots: (0 slots) master
  replicates 14ed067a1b85044325d5800fa8479a6b4e41a10c
[ok] all nodes agree about slots configuration.
>>> check for open slots...
>>> check slots coverage...
[ok] all 16384 slots covered.
[root@localhost jack]# 

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