搭建redis集群

Linux版本:CentOS 7
Redis版本:3.2.8
Redis已经成功安装,安装路径为/usr/local/redis-3.2.8

在单台机器上搭建Redis集群,方式是通过不同的TCP端口启动多个实例,然后组成集群。

1、启动Redis多个实例

我们在home下创建目录redis,并编写7000.conf~7005.conf 6个配置文件,这6个配置文件用来启动6个实例,后面将使用这6个实例组成集群。
以7000.conf为例,配置文件需要填写如下几项:

port  7000                                        //端口7000,7001,7002,7003,7004 ,7005      
bind 0.0.0.0                                     //默认ip为127.0.0.1 需要改为0.0.0.0所有机器可访问 否则创建集群时无法访问对应的端口,无法创建集群
daemonize    yes                               //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002
cluster-enabled  yes                           //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志 

分别启动6个实例

redis-server cluster/conf/7000.conf
redis-server cluster/conf/7001.conf 
redis-server cluster/conf/7002.conf 
redis-server cluster/conf/7003.conf 
redis-server cluster/conf/7004.conf 
redis-server cluster/conf/7005.conf 
启动成功后,看一下进程

[root@localhost redis]# ps aux|grep redis
root      2541  0.9  0.2 141028  9780 ?        Ssl  18:48   0:00 redis-server 0.0.0.0:7000 [cluster]
root      2545  0.8  0.2 141028  9776 ?        Ssl  18:48   0:00 redis-server 0.0.0.0:7001 [cluster]
root      2549  0.5  0.2 141028  9788 ?        Ssl  18:48   0:00 redis-server 0.0.0.0:7002 [cluster]
root      2553  0.7  0.2 141028  7756 ?        Ssl  18:48   0:00 redis-server 0.0.0.0:7003 [cluster]
root      2559  0.4  0.1 141028  7728 ?        Ssl  18:48   0:00 redis-server 0.0.0.0:7004 [cluster]
root      2565  0.4  0.1 141028  7708 ?        Ssl  18:49   0:00 redis-server 0.0.0.0:7005 [cluster]
root      2571  0.0  0.0 112680   976 pts/0    R+   18:49   0:00 grep --color=auto redis


至此,ip=10.93.84.53机器上创建了6个实例,端口号为port=7000~7005。

2、安装ruby

下面的过程都是在root权限下完成的。

1)yum安装ruby和依赖的包。

# yum -y install ruby ruby-devel rubygems rpm-build 

一般来说,这一步是能正常完成的。

2)使用gem这个命令来安装redis接口

gem是ruby的一个工具包

# gem install redis 

安装过程出错


图片.png

升级Ruby的版本

安装rvm,我不知道这是个什么东西,但是感觉像是Ruby的一个包管理器。

图片.png
# gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <mpapis@gmail.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

然后重新下载rvm安装,成功了。

# curl -L get.rvm.io | bash -s stable 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
24090  100 24090    0     0  18023      0  0:00:01  0:00:01 --:--:--  129k
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: Signature made Mon 11 Sep 2017 04:59:21 AM CST using RSA key ID BF04FF17
gpg: Good signature from "Michal Papis (RVM signing) <mpapis@gmail.com>"
gpg:                 aka "Michal Papis <michal.papis@toptal.com>"
gpg:                 aka "[jpeg image of size 5015]"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3
     Subkey fingerprint: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17
GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.

接着,source环境,让rvm可用。

# source /usr/local/rvm/scripts/rvm

查看Ruby可用版本


# rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]

可以看到最新的版本是2.4.1,那么我们装最新的吧。

# rvm install 2.4.1
Searching for binary rubies, this might take some time.
No binary rubies available for: centos/6/x86_64/ruby-2.4.1.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: libffi-devel, libyaml-devel......
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.4.1, this may take a while depending on your cpu(s)...
ruby-2.4.1 - #downloading ruby-2.4.1, this may take a while depending on your connection...

curl: (35) SSL connect error
There was an error(35).
Checking fallback: https://ftp.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11.9M  100 11.9M    0     0  1753k      0  0:00:07  0:00:07 --:--:-- 2919k
ruby-2.4.1 - #extracting ruby-2.4.1 to /usr/local/rvm/src/ruby-2.4.1....
ruby-2.4.1 - #applying patch /usr/local/rvm/patches/ruby/2.4.1/random_c_using_NR_prefix.patch.
ruby-2.4.1 - #configuring..................................................................
ruby-2.4.1 - #post-configuration..
ruby-2.4.1 - #compiling..............................................................................................
ruby-2.4.1 - #installing.........................
ruby-2.4.1 - #making binaries executable..
ruby-2.4.1 - #downloading rubygems-2.6.14
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  751k  100  751k    0     0   443k      0  0:00:01  0:00:01 --:--:--  628k
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.4.1 - #extracting rubygems-2.6.14....
ruby-2.4.1 - #removing old rubygems.........
ruby-2.4.1 - #installing rubygems-2.6.14...........................
ruby-2.4.1 - #gemset created /usr/local/rvm/gems/ruby-2.4.1@global
ruby-2.4.1 - #importing gemset /usr/local/rvm/gemsets/global.gems...............................................
ruby-2.4.1 - #generating global wrappers........
ruby-2.4.1 - #gemset created /usr/local/rvm/gems/ruby-2.4.1
ruby-2.4.1 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.4.1 - #generating default wrappers........
ruby-2.4.1 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.4.1 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri

至此,我们升级了Ruby的版本。

4安装gem redis接口,成功!

#gem install redis
Fetching: redis-4.0.1.gem (100%)
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 0 seconds
1 gem installed

5 安装rubygems,成功!

# yum install -y rubygems
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
base                                                                                                                                                   | 3.7 kB     00:00     
didi_jenkins_enable                                                                                                                                    | 1.5 kB     00:00     
didi_op_toa_enable                                                                                                                                     | 1.5 kB     00:00     
didi_txjenkins_enable                                                                                                                                  | 1.5 kB     00:00     
didi_update                                                                                                                                            | 1.5 kB     00:00     
epel                                                                                                                                                   | 4.3 kB     00:00     
extras                                                                                                                                                 | 3.4 kB     00:00     
tmprepo                                                                                                                                                | 1.5 kB     00:00     
updates                                                                                                                                                | 3.4 kB     00:00     
Package rubygems-1.3.7-5.el6.noarch already installed and latest version
Nothing to do

至此,我们的Ruby和运行redis-trib.rb需要的环境安装完成了。

3、Redis集群搭建

有了Ruby执行环境,可以开始将之前的6个实例组建成集群了。

在安装redis的src目录下运行下面的命令

命令执行的路径:/usr/local/redis-3.2.8/src

命令方式:

ruby ./bin/redis-trib.rb create --replicas 1 10.93.84.53:7000 10.93.84.53:7001 10.93.84.53:7002 10.93.84.53:7003 10.93.84.53:7004 10.93.84.53:7005

--replicas 1表示为集群的master节点创建1个副本。那么6个实例里,有三个master,有三个是slave。

后面跟上6个实例就好了,形式就是ip:port

执行情况:

#cd /usr/local/redis-3.2.8/src
# ruby ./bin/redis-trib.rb create --replicas 1 10.93.84.53:7000 10.93.84.53:7001 10.93.84.53:7002 10.93.84.53:7003 10.93.84.53:7004 10.93.84.53:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.93.84.53:7000
10.93.84.53:7001
10.93.84.53:7002
Adding replica 10.93.84.53:7003 to 10.93.84.53:7000
Adding replica 10.93.84.53:7004 to 10.93.84.53:7001
Adding replica 10.93.84.53:7005 to 10.93.84.53:7002
M: 6346ae8c7af7949658619fcf4021cc7aca454819 10.93.84.53:7000
   slots:0-5460 (5461 slots) master
M: 5ac973bceab0d486c497345fe884ff54d1bb225a 10.93.84.53:7001
   slots:5461-10922 (5462 slots) master
M: cc46a4a1c0ec3f621b6b5405c6c10b7cffe73932 10.93.84.53:7002
   slots:10923-16383 (5461 slots) master
S: 92f62ec93a0550d962f81213ca7e9b3c9c996afd 10.93.84.53:7003
   replicates 6346ae8c7af7949658619fcf4021cc7aca454819
S: 942c9f97dc68198c39f425d13df0d8e3c40c5a58 10.93.84.53:7004
   replicates 5ac973bceab0d486c497345fe884ff54d1bb225a
S: a92a81532b63652bbd862be6f19a9bd8832e5e05 10.93.84.53:7005
   replicates cc46a4a1c0ec3f621b6b5405c6c10b7cffe73932
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 10.93.84.53:7000)
M: 6346ae8c7af7949658619fcf4021cc7aca454819 10.93.84.53:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: a92a81532b63652bbd862be6f19a9bd8832e5e05 10.93.84.53:7005
   slots: (0 slots) slave
   replicates cc46a4a1c0ec3f621b6b5405c6c10b7cffe73932
M: 5ac973bceab0d486c497345fe884ff54d1bb225a 10.93.84.53:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 942c9f97dc68198c39f425d13df0d8e3c40c5a58 10.93.84.53:7004
   slots: (0 slots) slave
   replicates 5ac973bceab0d486c497345fe884ff54d1bb225a
S: 92f62ec93a0550d962f81213ca7e9b3c9c996afd 10.93.84.53:7003
   slots: (0 slots) slave
   replicates 6346ae8c7af7949658619fcf4021cc7aca454819
M: cc46a4a1c0ec3f621b6b5405c6c10b7cffe73932 10.93.84.53:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

可以看到16384个slot都已经创建完成,并且建立了3个master和对应的replica:

Using 3 masters:
10.93.84.53:7000
10.93.84.53:7001
10.93.84.53:7002
Adding replica 10.93.84.53:7003 to 10.93.84.53:7000
Adding replica 10.93.84.53:7004 to 10.93.84.53:7001
Adding replica 10.93.84.53:7005 to 10.93.84.53:7002

。。。

[OK] All 16384 slots covered.

下次启动集群的时候直接启动6个实例即可

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,999评论 4 368
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,102评论 1 302
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,709评论 0 250
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,439评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,846评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,881评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,062评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,783评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,517评论 1 248
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,762评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,241评论 1 264
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,568评论 3 260
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,236评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,145评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,941评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,965评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,802评论 2 275

推荐阅读更多精彩内容