ubuntu14.04 redis3.20集群搭

准备工作 (亲测)

要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点并部署到2台物理节点上,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下:
系统:Ubuntu 14.04
集群版本 redis-3.2.0,ruby-2.3.1
10.251.248.39:7000 master 服务器
10.251.248.39:7001
10.251.248.39:7002
10.252.97.106:7000 slave 服务器
10.252.97.106:7001
10.252.97.106:7002

一,安装redis,主从服务器均按照下列一、二步骤安装
1.安装所需依赖包

root@iZ23tsilmb7Z:~# cd /usr/local/src
root@iZ23tsilmb7Z:/usr/local/src# apt-get update
root@iZ23tsilmb7Z:/usr/local/src# apt-get install tcl8.5-dev
2.解压源码包并安装
root@iZ23tsilmb7Z:/usr/local/src# wget http://download.redis.io/releases/redis-3.2.0.tar.gz
root@iZ23tsilmb7Z:/usr/local/src# tar -zxvf redis-3.2.0.tar.gz
root@iZ23tsilmb7Z:/usr/local/src# cd redis-3.2.0  
root@iZ23tsilmb7Z:/usr/local/src# make
root@iZ23tsilmb7Z:/usr/local/src# make test
3.复制并修改配置文件
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0# mkdir -p /usr/local/src/cluster/7000
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0# mkdir -p /usr/local/src/cluster/7001
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0# mkdir -p /usr/local/src/cluster/7002
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0# cp /usr/local/src/redis-3.2.0/redis.conf /usr/local/src/cluster
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0# cd /usr/local/src/cluster
root@iZ23tsilmb7Z:/usr/local/src/cluster# vim redis.conf
    bind 10.251.248.39 127.0.0.1 
    port 7000
    daemonize yes
    cluster-enabled yes
    cluster-config-file nodes.conf
    cluster-node-timeout 5000
    appendonly yes

//需绑定为本机IP,不绑定的话会出现连接不上,见下面的说明
将修改后的配置文件分别拷贝到7000/7001/7002目录下面,完成之后要修改7001/7002目录下面redis.conf文件中的port参数,分别改为对应的文件夹的名称

root@iZ23yrv1t36Z:/usr/local/src/cluster# cp /usr/local/src/cluster/redis.conf /usr/local/src/cluster/7000
root@iZ23yrv1t36Z:/usr/local/src/cluster# cp /usr/local/src/cluster/redis.conf /usr/local/src/cluster/7001
root@iZ23yrv1t36Z:/usr/local/src/cluster# cp /usr/local/src/cluster/redis.conf /usr/local/src/cluster/7002
4.启动redis实例

root@iZ23yrv1t36Z:/usr/local/src/cluster# cd /usr/local/src/cluster/7000
root@iZ23yrv1t36Z:/usr/local/src/cluster/7000# /usr/local/src/redis-3.2.0/src/redis-server redis.conf
root@iZ23yrv1t36Z:/usr/local/src/cluster/7000# cd ../7001
root@iZ23yrv1t36Z:/usr/local/src/cluster/7001# /usr/local/src/redis-3.2.0/src/redis-server redis.conf
root@iZ23yrv1t36Z:/usr/local/src/cluster/7001# cd ../7002
root@iZ23yrv1t36Z:/usr/local/src/cluster/7002# /usr/local/src/redis-3.2.0/src/redis-server redis.conf
root@iZ23yrv1t36Z:/usr/local/src/cluster/7002# cd ..
5.查看redis的启动情况
root@iZ23yrv1t36Z:/usr/local/src/cluster# ps -ef|grep redis
root     11098     1  0 15:33 ?        00:00:00 /usr/local/src/redis-3.2.0/src/redis-server *:7000 [cluster]
root     11102     1  0 15:33 ?        00:00:00 /usr/local/src/redis-3.2.0/src/redis-server *:7001 [cluster]
root     11106     1  0 15:33 ?        00:00:00 /usr/local/src/redis-3.2.0/src/redis-server *:7002 [cluster]
root     11120   840  0 15:34 pts/9    00:00:00 grep --color=auto redis
root@iZ23yrv1t36Z:/usr/local/src/cluster#
,在master服务器上安装redis集群
1.安装ruby和rubygem,为了方便起见这里推荐安装ruby 1.9.2及之后的版本,这些版本已经包含了rubygem
root@iZ23yrv1t36Z:/usr/local/src/cluster# apt-get install -y libssl-dev libreadline-dev zlib1g-dev
root@iZ23yrv1t36Z:/usr/local/src/cluster# wget https://ruby.taobao.org/mirrors/ruby/ruby-2.3.1.tar.gz
root@iZ23yrv1t36Z:/usr/local/src/cluster# tar -zxvf ruby-2.3.1.tar.gz
root@iZ23yrv1t36Z:/usr/local/src/cluster# cd ruby-2.3.1/
root@iZ23yrv1t36Z:/usr/local/src/cluster/ruby-2.3.1# ./configure
root@iZ23yrv1t36Z:/usr/local/src/cluster/ruby-2.3.1# make
root@iZ23yrv1t36Z:/usr/local/src/cluster/ruby-2.3.1# make install
root@iZ23yrv1t36Z:/usr/local/src/cluster/ruby-2.3.1#  cd ..
2. 更新gem(ruby的redis模块)
root@iZ23yrv1t36Z:/usr/local/src/cluster# gem install redis  //安装ruby的redis模块
root@iZ23yrv1t36Z:/usr/local/src/cluster#
3.执行redis的创建集群命令创建集群
root@iZ23yrv1t36Z:/usr/local/src/cluster# cd ../redis-3.2.0/src
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0/src# ./redis-trib.rb create --replicas 1 10.251.248.39:7000 10.251.248.39:7001 10.251.248.39:7002 10.252.97.106:7000 10.252.97.106:7001 10.252.97.106:7002
>>> Creating cluster
[ERR] Sorry, can't connect to node 10.251.248.39:7000
注意:出现连接不上节点,并不是网上说的ruby和gem版本太低,即便更新到最新版本也是同样的问题,因为这个问题,导致我重装了好几次集成,一度怀疑是系统的问题,但后面发现通过127.0.0.1 的这个IP地址可以连接上,但内网IP不行,发现这个现象后,问题就好解决了
//解决方法,绑定IP地址,6个实例的都要绑定为本地的IP
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0/src# vim redis.conf
    bind 10.251.248.39 127.0.0.1
见证奇迹的时刻来了
//查看redis进程(ps -ef|grep redis),并用kill 杀死这些进程 ,重新启动redis,见上面的启动方法
//启动完成后,启动redis集群
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0/src# ./redis-trib.rb create --replicas 1 10.251.248.39:7000 10.251.248.39:7001 10.251.248.39:7002 10.252.97.106:7000 10.252.97.106:7001 10.252.97.106:7002
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.251.248.39:7000
10.252.97.106:7000
10.251.248.39:7001
Adding replica 10.252.97.106:7001 to 10.251.248.39:7000
Adding replica 10.251.248.39:7002 to 10.252.97.106:7000
Adding replica 10.252.97.106:7002 to 10.251.248.39:7001
M: 29dc0828009818a44527dcedfbe94d0d39e51993 10.251.248.39:7000
   slots:0-5460 (5461 slots) master
M: 7a575ab3bffa7dcb55daafc045ea3e6f31a352b0 10.251.248.39:7001
   slots:10923-16383 (5461 slots) master
S: f304c0304c21def47d69457c2db5fc80a3231d34 10.251.248.39:7002
   replicates ce8df12fc1172ce27e69b8f2eacd441a41b95d7c
M: ce8df12fc1172ce27e69b8f2eacd441a41b95d7c 10.252.97.106:7000
   slots:5461-10922 (5462 slots) master
S: 445c2f6db60fc5c9f7b2f408169d48252b350e3c 10.252.97.106:7001
   replicates 29dc0828009818a44527dcedfbe94d0d39e51993
S: 3fa5d7b4890ea4a41e3f65b1410d9400c4ef1d6e 10.252.97.106:7002
   replicates 7a575ab3bffa7dcb55daafc045ea3e6f31a352b0
Can I set the above configuration? (type 'yes' to accept):   yes
*** Aborting...
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0/src#
哈哈,当你看到这些时,说明已经启动成功了
--replicas表示为每个master节点创建多少个slave节点
查看集群状态 redis-trib.rb check 10.251.248.39:7000
4.使用redis-cli命令进入集群环境
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0/src# ./redis-cli -c -p 7000 -h 10.251.248.39
10.251.248.39:7000>
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0/src# ./redis-cli -c -p 7000 -h 10.251.248.39
10.251.248.39:7000> set name xiaoming
-> Redirected to slot [5798] located at 10.252.97.106:7000
OK
10.251.248.39:7000> get name
-> Redirected to slot [5798] located at 10.252.97.106:7000
"xiaoming"
10.252.97.106:7000> 
//登录另一台redis,读取刚才设置的name
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0/src# ./redis-cli -c -p 7000 -h 10.252.97.106:7001
Could not connect to Redis at 10.252.97.106:7001:7000: Name or service not known
not connected> 
root@iZ23tsilmb7Z:/usr/local/src/redis-3.2.0/src# ./redis-cli -c -p 7001 -h 10.252.97.106 
10.252.97.106:7001> get name
-> Redirected to slot [5798] located at 10.252.97.106:7000
"xiaoming"
10.252.97.106:7000>
至此大功告成,心中不免有些窃喜!
  • 发布时间:2019-04-05