redis集群搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis集群搭建相关的知识,希望对你有一定的参考价值。

系统环境

[[email protected] ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[[email protected] ~]# uname -a
Linux node 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# ifconfig eno16777736
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.72  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::20c:29ff:fec2:e84b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c2:e8:4b  txqueuelen 1000  (Ethernet)
        RX packets 115015  bytes 88316137 (84.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35683  bytes 4729443 (4.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[[email protected] ~]# 

Redis版本:

[[email protected] tools]# ls -l redis-3.2.12.tar.gz 
-rw-r--r-- 1 root root 1551468 Jun 14 00:51 redis-3.2.12.tar.gz
[[email protected] tools]# 

Redis集群介绍:

Redis 是一个开源的 key-value 存储系统,由于出众的性能,大部分互联网企业都用来做服务器端缓存。Redis 在3.0版本前只支持单实例模式,虽然支持主从模式、哨兵模式部署来解决单点故障,但是现在互联网企业动辄大几百G的数据,可完全是没法满足业务的需求,所以,
Redis 在 3.0 版本以后就推出了集群模式。Redis 集群采用了P2P的模式,完全去中心化。Redis 把所有的 Key 分成了 16384 个 slot,每个 Redis 实例负责其中一部分 slot 。集群中的所有信息(节点、端口、slot等),都通过节点之间定期的数据交换而更新。
Redis 客户端可以在任意一个 Redis 实例发出请求,如果所需数据不在该实例中,通过重定向命令引导客户端访问所需的实例。下面说一下 Redis 集群搭建规划,由于集群至少需要6个节点(3主3从模式),所以,没有这么多机器给我玩,我本地也起不了那么多虚拟机(电脑太烂),现在计划是在一台机器上模拟一个集群,当然,这和生产环境的集群搭建没本质区别。
我现在就要在已经有安装了 Redis 的一个 CentOS 下开始进行集群搭建,如果你还不是很清楚 Linux 下如何安装 Redis 。请注意,下面所有集群搭建环境都基于已安装好的 Redis 做的。

部署过程:

[[email protected] tools]# tar xf redis-3.2.12.tar.gz 
[[email protected] tools]# ll
total 1520
drwxrwxr-x 6 root root    4096 Jun 13 18:47 redis-3.2.12
-rw-r--r-- 1 root root 1551468 Jun 14 00:51 redis-3.2.12.tar.gz
[[email protected] tools]#

这里我们以端口进行区分实例,分别是7001~7006,创建对应项目目录

[[email protected] tools]# mkdir -p /application/redis/700{1,2,3,4,5,6}/{conf,data,log,run}
[[email protected] tools]# tree /application/redis/
/application/redis/
├── 7001
│   ├── conf
│   ├── data
│   ├── log
│   └── run
├── 7002
│   ├── conf
│   ├── data
│   ├── log
│   └── run
├── 7003
│   ├── conf
│   ├── data
│   ├── log
│   └── run
├── 7004
│   ├── conf
│   ├── data
│   ├── log
│   └── run
├── 7005
│   ├── conf
│   ├── data
│   ├── log
│   └── run
└── 7006
    ├── conf
    ├── data
    ├── log
    └── run

30 directories, 0 files
[[email protected] tools]# 
[[email protected] tools]# tar xf redis-3.2.12.tar.gz 
[[email protected] tools]# cd redis-3.2.12
[[email protected] redis-3.2.12]# make MALLOC=jemalloc
[[email protected] redis-3.2.12]# make PREFIX=/application/redis/7001/  install
[[email protected] redis-3.2.12]# tree /application/redis/7001/
/application/redis/7001/
├── bin
│   ├── redis-benchmark
│   ├── redis-check-aof
│   ├── redis-check-rdb
│   ├── redis-cli
│   ├── redis-sentinel -> redis-server
│   └── redis-server
├── conf
├── data
├── log
└── run

5 directories, 6 files
[[email protected] redis-3.2.12]# 
[[email protected] redis-3.2.12]# pwd
/root/tools/redis-3.2.12
[[email protected] redis-3.2.12]# cp redis.conf /application/redis/7001/conf/
[[email protected] redis-3.2.12]# 

拷贝/application/redis/7001目录下的内容到 7002、7003、7004、7005、7006目录下;

[[email protected] redis-3.2.12]# cp -rf  /application/redis/7001/* /application/redis/7002/
[[email protected] redis-3.2.12]# cp -rf  /application/redis/7001/* /application/redis/7003/
[[email protected] redis-3.2.12]# cp -rf  /application/redis/7001/* /application/redis/7004/
[[email protected] redis-3.2.12]# cp -rf  /application/redis/7001/* /application/redis/7005/
[[email protected] redis-3.2.12]# cp -rf  /application/redis/7001/* /application/redis/7006/
[[email protected] redis-3.2.12]# 

配置文件redis.conf中对应的配置项根据实际路径修改如下所示:

daemonize yes (以守护进程模式启动)
bind 127.0.0.1 192.168.0.72 (绑定本地127.0.0.1和对外网卡192。168.0.72两个ip上)
pidfile /application/redis/7001/run/redis_7001.pid (定义启动redis的pid进程文件存放路径)
logfile "/application/redis/7001/log/redis_7001.log" (定义日志存放路径)
dir /application/redis/7001/data/ (定义数据存放路径)
cluster-enabled yes (启动集群功能支持集群)
cluster-config-file /application/redis/7001/conf/nodes-7001.conf (集群配置文件存放路径)
cluster-node-timeout 15000 (集群节点之间通信超时时间)

启动Redis实例

[[email protected] ~]# /application/redis/7001/bin/redis-server /application/redis/7001/conf/redis.conf
[[email protected] ~]# /application/redis/7002/bin/redis-server /application/redis/7002/conf/redis.conf
[[email protected] ~]# /application/redis/7003/bin/redis-server /application/redis/7003/conf/redis.conf
[[email protected] ~]# /application/redis/7004/bin/redis-server /application/redis/7004/conf/redis.conf
[[email protected] ~]# /application/redis/7005/bin/redis-server /application/redis/7005/conf/redis.conf
[[email protected] ~]# /application/redis/7006/bin/redis-server /application/redis/7006/conf/redis.conf
[[email protected] ~]# 
[[email protected] ~]# ps xua|grep redis
root      13802  0.2  0.2 138964  9816 ?        Ssl  09:12   0:12 /application/redis/7001/bin/redis-server 127.0.0.1:7001 [cluster]
root      13806  0.2  0.2 138964  9820 ?        Ssl  09:12   0:12 /application/redis/7002/bin/redis-server 127.0.0.1:7002 [cluster]
root      13810  0.2  0.2 138964  9812 ?        Ssl  09:12   0:12 /application/redis/7003/bin/redis-server 127.0.0.1:7003 [cluster]
root      13814  0.2  0.2 136916  7768 ?        Ssl  09:12   0:12 /application/redis/7004/bin/redis-server 127.0.0.1:7004 [cluster]
root      13818  0.2  0.2 136916  7772 ?        Ssl  09:12   0:12 /application/redis/7005/bin/redis-server 127.0.0.1:7005 [cluster]
root      13822  0.2  0.2 136916  7760 ?        Ssl  09:13   0:12 /application/redis/7006/bin/redis-server 127.0.0.1:7006 [cluster]
root      35592  0.0  0.0 112648   952 pts/0    S+   10:49   0:00 grep --color=auto redis
[[email protected] ~]# 
[[email protected] ~]# netstat -lnupt|grep redis|grep 192.168.0.72
tcp        0      0 192.168.0.72:17001      0.0.0.0:*               LISTEN      13802/redis-server  
tcp        0      0 192.168.0.72:17002      0.0.0.0:*               LISTEN      13806/redis-server  
tcp        0      0 192.168.0.72:17003      0.0.0.0:*               LISTEN      13810/redis-server  
tcp        0      0 192.168.0.72:17004      0.0.0.0:*               LISTEN      13814/redis-server  
tcp        0      0 192.168.0.72:17005      0.0.0.0:*               LISTEN      13818/redis-server  
tcp        0      0 192.168.0.72:17006      0.0.0.0:*               LISTEN      13822/redis-server  
tcp        0      0 192.168.0.72:7001       0.0.0.0:*               LISTEN      13802/redis-server  
tcp        0      0 192.168.0.72:7002       0.0.0.0:*               LISTEN      13806/redis-server  
tcp        0      0 192.168.0.72:7003       0.0.0.0:*               LISTEN      13810/redis-server  
tcp        0      0 192.168.0.72:7004       0.0.0.0:*               LISTEN      13814/redis-server  
tcp        0      0 192.168.0.72:7005       0.0.0.0:*               LISTEN      13818/redis-server  
tcp        0      0 192.168.0.72:7006       0.0.0.0:*               LISTEN      13822/redis-server  
[[email protected] ~]# 

 

 验证是否可以进行写入:

[[email protected] src]# /root/tools/redis-3.2.12/src/redis-cli -h 127.0.0.1 -p 7006
127.0.0.1:7006> keys *
(empty list or set)
127.0.0.1:7006> set name redis
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:7006> 
127.0.0.1:7006> set name redis
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:7006> 

连接成功了,但是写入报错,集群处于down状态,Hash slot not served,没有存储的位置,其实就是没有slot(也就是前面说的slot槽)

所以我们解决这个问题

yum install ruby
yum install rubygems
gem install redis 
[[email protected] redis]# yum install ruby rubygems
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.cn99.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package ruby.x86_64 0:2.0.0.648-33.el7_4 will be installed
--> Processing Dependency: ruby-libs(x86-64) = 2.0.0.648-33.el7_4 for package: ruby-2.0.0.648-33.el7_4.x86_64
--> Processing Dependency: rubygem(bigdecimal) >= 1.2.0 for package: ruby-2.0.0.648-33.el7_4.x86_64
--> Processing Dependency: libruby.so.2.0()(64bit) for package: ruby-2.0.0.648-33.el7_4.x86_64
---> Package rubygems.noarch 0:2.0.14.1-33.el7_4 will be installed
--> Processing Dependency: rubygem(rdoc) >= 4.0.0 for package: rubygems-2.0.14.1-33.el7_4.noarch
--> Processing Dependency: rubygem(psych) >= 2.0.0 for package: rubygems-2.0.14.1-33.el7_4.noarch
--> Processing Dependency: rubygem(io-console) >= 0.4.2 for package: rubygems-2.0.14.1-33.el7_4.noarch
--> Running transaction check
---> Package ruby-libs.x86_64 0:2.0.0.648-33.el7_4 will be installed
--> Processing Dependency: libcrypto.so.10(OPENSSL_1.0.2)(64bit) for package: ruby-libs-2.0.0.648-33.el7_4.x86_64
---> Package rubygem-bigdecimal.x86_64 0:1.2.0-33.el7_4 will be installed
---> Package rubygem-io-console.x86_64 0:0.4.2-33.el7_4 will be installed
---> Package rubygem-psych.x86_64 0:2.0.0-33.el7_4 will be installed
--> Processing Dependency: libyaml-0.so.2()(64bit) for package: rubygem-psych-2.0.0-33.el7_4.x86_64
---> Package rubygem-rdoc.noarch 0:4.0.0-33.el7_4 will be installed
--> Processing Dependency: ruby(irb) = 2.0.0.648 for package: rubygem-rdoc-4.0.0-33.el7_4.noarch
--> Processing Dependency: rubygem(json) >= 1.7.7 for package: rubygem-rdoc-4.0.0-33.el7_4.noarch
--> Running transaction check
---> Package libyaml.x86_64 0:0.1.4-11.el7_0 will be installed
---> Package openssl-libs.x86_64 1:1.0.1e-42.el7.9 will be updated
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.1e-42.el7.9 for package: 1:openssl-1.0.1e-42.el7.9.x86_64
---> Package openssl-libs.x86_64 1:1.0.2k-12.el7 will be an update
---> Package ruby-irb.noarch 0:2.0.0.648-33.el7_4 will be installed
---> Package rubygem-json.x86_64 0:1.7.7-33.el7_4 will be installed
--> Running transaction check
---> Package openssl.x86_64 1:1.0.1e-42.el7.9 will be updated
---> Package openssl.x86_64 1:1.0.2k-12.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================================================================================================================================
 Package                                                            Arch                                                   Version                                                            Repository                                            Size
=========================================================================================================================================================================================================================================================
Installing:
 ruby                                                               x86_64                                                 2.0.0.648-33.el7_4                                                 base                                                  71 k
 rubygems                                                           noarch                                                 2.0.14.1-33.el7_4                                                  base                                                 219 k
Installing for dependencies:
 libyaml                                                            x86_64                                                 0.1.4-11.el7_0                                                     base                                                  55 k
 ruby-irb                                                           noarch                                                 2.0.0.648-33.el7_4                                                 base                                                  92 k
 ruby-libs                                                          x86_64                                                 2.0.0.648-33.el7_4                                                 base                                                 2.8 M
 rubygem-bigdecimal                                                 x86_64                                                 1.2.0-33.el7_4                                                     base                                                  83 k
 rubygem-io-console                                                 x86_64                                                 0.4.2-33.el7_4                                                     base                                                  54 k
 rubygem-json                                                       x86_64                                                 1.7.7-33.el7_4                                                     base                                                  79 k
 rubygem-psych                                                      x86_64                                                 2.0.0-33.el7_4                                                     base                                                  82 k
 rubygem-rdoc                                                       noarch                                                 4.0.0-33.el7_4                                                     base                                                 322 k
Updating for dependencies:
 openssl                                                            x86_64                                                 1:1.0.2k-12.el7                                                    base                                                 492 k
 openssl-libs                                                       x86_64                                                 1:1.0.2k-12.el7                                                    base                                                 1.2 M

Transaction Summary
=========================================================================================================================================================================================================================================================
Install  2 Packages (+8 Dependent packages)
Upgrade             ( 2 Dependent packages)

Total download size: 5.5 M
Is this ok [y/d/N]: y
Installed:
  ruby.x86_64 0:2.0.0.648-33.el7_4                                                                                          rubygems.noarch 0:2.0.14.1-33.el7_4                                                                                         

Dependency Installed:
  libyaml.x86_64 0:0.1.4-11.el7_0         ruby-irb.noarch 0:2.0.0.648-33.el7_4   ruby-libs.x86_64 0:2.0.0.648-33.el7_4   rubygem-bigdecimal.x86_64 0:1.2.0-33.el7_4   rubygem-io-console.x86_64 0:0.4.2-33.el7_4   rubygem-json.x86_64 0:1.7.7-33.el7_4  
  rubygem-psych.x86_64 0:2.0.0-33.el7_4   rubygem-rdoc.noarch 0:4.0.0-33.el7_4  

Dependency Updated:
  openssl.x86_64 1:1.0.2k-12.el7                                                                                           openssl-libs.x86_64 1:1.0.2k-12.el7                                                                                          

Complete!
[[email protected] redis]# 
[[email protected] redis]# gem install redis
Fetching: redis-4.0.1.gem (100%)
ERROR:  Error installing redis:
    redis requires Ruby version >= 2.2.2.
[[email protected] redis]# 

提示Ruby版本过低,至少要求2.2.2版本,解决版本问题

[[email protected] redis]# ruby --version
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
[[email protected] redis]# rvm remove 2.0.0
awk: fatal: cannot open file `/usr/local/rvm/config/alias for reading (No such file or directory)
ruby-2.0.0-p648 - #already gone
Now using system ruby.
[[email protected] redis]# rvm remove 2.0.0p648
awk: fatal: cannot open file `/usr/local/rvm/config/alias for reading (No such file or directory)
ruby-2.0.0p648 - #already gone
Now using system ruby.
[[email protected] redis]# 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[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
ruby-head

# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
jruby-1.7[.27]
jruby-9.1[.17.0]
jruby[-9.2.0.0]
jruby-head

# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx-3[.100]
rbx-head

# TruffleRuby
truffleruby[-1.0.0-rc2]

# Opal
opal

# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1.2.0
mruby-1.3.0
mruby-1[.4.0]
mruby[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Topaz
topaz

# MagLev
maglev-1.0.0
maglev-1.1[RC1]
maglev[-1.2Alpha4]
maglev-head

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby
ironruby[-1.1.3]
ironruby-head
[[email protected] redis]# 
[[email protected] redis]# rvm install ruby-2.3.3 
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/7/x86_64/ruby-2.3.3.tar.bz2
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: libffi-devel, readline-devel, sqlite-devel, zlib-devel, openssl-devel..................................
Requirements installation successful.
ruby-2.3.3 - #configure
ruby-2.3.3 - #download
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 25.2M  100 25.2M    0     0   338k      0  0:01:16  0:01:16 --:--:--  332k
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.3.3 - #validate archive
ruby-2.3.3 - #extract
ruby-2.3.3 - #validate binary
ruby-2.3.3 - #setup
ruby-2.3.3 - #gemset created /usr/local/rvm/gems/ruby-2.3.3@global
ruby-2.3.3 - #importing gemset /usr/local/rvm/gemsets/global.gems.............................................
ruby-2.3.3 - #generating global wrappers.......
ruby-2.3.3 - #gemset created /usr/local/rvm/gems/ruby-2.3.3
ruby-2.3.3 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.3.3 - #generating default wrappers.......
[[email protected] redis]# 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[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
ruby-head

# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
jruby-1.7[.27]
jruby-9.1[.17.0]
jruby[-9.2.0.0]
jruby-head

# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx-3[.100]
rbx-head

# TruffleRuby
truffleruby[-1.0.0-rc2]

# Opal
opal

# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1.2.0
mruby-1.3.0
mruby-1[.4.0]
mruby[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Topaz
topaz

# MagLev
maglev-1.0.0
maglev-1.1[RC1]
maglev[-1.2Alpha4]
maglev-head

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby
ironruby[-1.1.3]
ironruby-head
[[email protected] redis]# rvm use 2.3.3 --default
Using /usr/local/rvm/gems/ruby-2.3.3
[[email protected] 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
[[email protected] redis]# 
[[email protected] redis]# ruby --version
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
[[email protected] redis]# gem install redis
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Done installing documentation for redis after 0 seconds
1 gem installed
[[email protected] redis]# 

启动redis实例:

[[email protected] redis]# /application/redis/7002/bin/redis-server /application/redis/7002/conf/redis.conf
[[email protected] redis]# /application/redis/7003/bin/redis-server /application/redis/7003/conf/redis.conf
[[email protected] redis]# /application/redis/7004/bin/redis-server /application/redis/7004/conf/redis.conf
[[email protected] redis]# /application/redis/7005/bin/redis-server /application/redis/7005/conf/redis.conf
[[email protected] redis]# /application/redis/7006/bin/redis-server /application/redis/7006/conf/redis.conf
[[email protected] redis]# ps xua|grep redis
root      35684  0.2  0.7 136916  7572 ?        Ssl  22:31   0:00 /application/redis/7001/bin/redis-server 127.0.0.1:7001 [cluster]
root      35688  0.1  0.7 136916  7572 ?        Ssl  22:31   0:00 /application/redis/7002/bin/redis-server 127.0.0.1:7002 [cluster]
root      35692  0.2  0.7 136916  7572 ?        Ssl  22:31   0:00 /application/redis/7003/bin/redis-server 127.0.0.1:7003 [cluster]
root      35696  0.2  0.7 136916  7568 ?        Ssl  22:31   0:00 /application/redis/7004/bin/redis-server 127.0.0.1:7004 [cluster]
root      35700  0.2  0.7 136916  7568 ?        Ssl  22:31   0:00 /application/redis/7005/bin/redis-server 127.0.0.1:7005 [cluster]
root      35704  0.1  0.7 136916  7572 ?        Ssl  22:31   0:00 /application/redis/7006/bin/redis-server 127.0.0.1:7006 [cluster]
root      35708  0.0  0.0 112648   952 pts/0    S+   22:31   0:00 grep --color=auto redis
[[email protected] redis]# 

通过如下命令创建集群:

S: aec9e99f007c811cb45bce171dcc0352beae2d55 192.168.0.198:7006
   replicates 66ecedcca2644618bac596cce8ecdb930970dc11
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.0.198:7001)
M: c29de69492d3555afe0438ed5ed7cfa30d7cd360 192.168.0.198:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 7d8dc86edf72436a64c63e296c96663993aa4add 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 907d8a3ed18d1365035063908266ac6ea49c765e
M: 907d8a3ed18d1365035063908266ac6ea49c765e 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 9a586eb27ffdc6d5a50ff1810c0b24684baf2dbb 127.0.0.1:7004
   slots: (0 slots) slave
   replicates c29de69492d3555afe0438ed5ed7cfa30d7cd360
S: aec9e99f007c811cb45bce171dcc0352beae2d55 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 66ecedcca2644618bac596cce8ecdb930970dc11
M: 66ecedcca2644618bac596cce8ecdb930970dc11 127.0.0.1:7003
   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.
[[email protected] redis]# 

看到没有报错,提示ALL 16384个slotscoverd表示集群创建完成,可以查看集群状态:

简要说明:

从以上可以看到7001~7003是主节点,也就是master节点,7004~7006是从节点,其中通过字母M和S来表示

M:表示master节点,其中后面长串字母 “c29de69492d3555afe0438ed5ed7cfa30d7cd360” 表示id,

S:表示slave节点

创建集群,通过调用 ruby 命令来进行创建,--replicas 1 表示主从复制比例为 1:1,即一个主节点对应一个从节点;然后,默认给我们分配好了每个主节点和对应从节点服务,以及 solt 的大小,因为在 Redis 集群中有且仅有 16383 个 solt ,默认情况会给我们平均分配,当然你可以指定,后续的增减节点也可以重新分配。

登录集群查看集群信息并验证是否以写入和读取数据:

[[email protected] redis]# /root/tools/redis-3.2.12/src/redis-cli -c -h 127.0.0.1 -p 7001
127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:879
cluster_stats_messages_received:879
127.0.0.1:7001> cluster nodes
7d8dc86edf72436a64c63e296c96663993aa4add 127.0.0.1:7005 slave 907d8a3ed18d1365035063908266ac6ea49c765e 0 1532788809741 5 connected
907d8a3ed18d1365035063908266ac6ea49c765e 127.0.0.1:7002 master - 0 1532788809237 2 connected 5461-10922
9a586eb27ffdc6d5a50ff1810c0b24684baf2dbb 127.0.0.1:7004 slave c29de69492d3555afe0438ed5ed7cfa30d7cd360 0 1532788807724 4 connected
aec9e99f007c811cb45bce171dcc0352beae2d55 127.0.0.1:7006 slave 66ecedcca2644618bac596cce8ecdb930970dc11 0 1532788806714 6 connected
c29de69492d3555afe0438ed5ed7cfa30d7cd360 192.168.0.198:7001 myself,master - 0 0 1 connected 0-5460
66ecedcca2644618bac596cce8ecdb930970dc11 127.0.0.1:7003 master - 0 1532788808734 3 connected 10923-16383
127.0.0.1:7001> 
127.0.0.1:7001> set name keys
-> Redirected to slot [5798] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get name
"keys"
127.0.0.1:7001> exit
[[email protected] redis]# /root/tools/redis-3.2.12/src/redis-cli -c -h 127.0.0.1 -p 7002
127.0.0.1:7002> get name
"keys"
127.0.0.1:7002> 

发现可以正常读写数据,说明redis集群搭建成功,可以正常读写。

 











以上是关于redis集群搭建的主要内容,如果未能解决你的问题,请参考以下文章

基于docker环境搭建redis-cluster集群(多台机器)

如何利用redis来进行分布式集群系统的限流设计

redis搭建集群并用TreeSoft管理

redis集群简单codis搭建

宝塔搭建redis集群

redis 多主多从集群搭建