两台服务器redis 3.0.5分布式集群安装部署

目前redis支持的cluster特性:

1):节点自动发现

2):slave->master 选举,集群容错

3):Hot resharding:在线分片

4):集群管理:cluster xxx

5):基于配置(nodes-port.conf)的集群管理

6):ASK 转向/MOVED 转向机制.


一、redis集群安装

两台电脑:   192.168.0.60  192.168.0.219

1.分别在两台电脑下载redis(redis-3.0.5)

wget  http://download.redis.io/releases/redis-3.0.5.tar.gz

2. 分别在两台电脑解压,编译

tar  -zxvf redis-3.0.5.tar.gz

mv redis-3.0.5 /usr/local/redis3.0

cd  /usr/local/redis3.0

make

make install

3. 分别在两台电脑创建集群需要的目录

1)192.168.0.60

cd /etc/redis

mkdir 9000  9001 9002

2) 192.168.0.219

cd /etc/redis

mkdir 9003  9004 9005

4 . 修改配置文件

cp /usr/local/redis3.0/redis.conf   /etc/redis/9000

cd /etc/redis/9000

mv redis.conf   9000.conf

vim  9000.conf    

## 修改配置文件中的下面选项

port 9000

daemonize yes

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

appendonly yes

bind 192.168.0.60

#若要设置密码,需要把requirepass前面的#号去掉,并设置它后面的密码以及masterauth

##修改完9000.conf配置文件中的这些配置后把这个配置文件分别拷贝到9001/9002/9003/9004/9005目录下面

cp  /etc/redis/9000.conf   /etc/redis/9001

cp  /etc/redis/9000.conf   /etc/redis/9002

scp  /etc/redis/9000.conf   root@192.168.0.219:/etc/redis/9003

scp  /etc/redis/9000.conf   root@192.168.0.219:/etc/redis/9004

scp  /etc/redis/9000.conf   root@192.168.0.219:/etc/redis/9005

#注意:拷贝完成后要修改9001/9002/9003/9004/9005下面redis配置文件中的port分别对应的文件夹的名称和bind参数(需要更新为物理接口IP 0.60或0.219)

同时需要在两台服务器的防火墙放开对应端口

5. 分别启动这6个redis实例

1) 192.168.0.60上

cd /etc/redis/9000

redis-server 9000.conf

cd /etc/redis/9001

redis-server 9001.conf

cd /etc/redis/9002

redis-server 9002.conf

2) 192.168.0.60上

cd /etc/redis/9003

redis-server 9003.conf

cd /etc/redis/9004

redis-server 9004.conf

cd /etc/redis/9005

redis-server 9005.conf

#启动之后用ps -ef | grep redis查看redis的启动情况

如下则说明成功

6.升级ruby安装gem

#yum install ruby ruby-devel rubygems rpm-build

检查ruby版本

#ruby -v

ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]

是否安装rubygems

#rpm -qa | grep ruby

ruby-1.8.7.374-4.el6_6.x86_64

rubygems-1.3.7-5.el6.noarch

ruby-rdoc-1.8.7.374-4.el6_6.x86_64

ruby-devel-1.8.7.374-4.el6_6.x86_64

ruby-libs-1.8.7.374-4.el6_6.x86_64

ruby-irb-1.8.7.374-4.el6_6.x86_64

7. gem安装redis ruby接口

gem install redis

若报错,则可手动下载

wget http://rubygems.org/gems/redis/versions/3.0.0

gem install -l redis-3.0.0

8.执行redis的创建集群命令创建集群

# redis-trib.rd的create子命令创建

# --replicas则指定了为Redis Cluster中的每个Master节点配备了几个Slave节点

# 节点角色由顺序决定,先master之后是slave

创建方式

[root@mysqlslave src]#cd /usr/local/redis3.0/src

[root@mysqlslave src]# ./redis-trib.rb  create --replicas 1 192.168.0.60:9000 192.168.0.60:9001 192.168.0.60:9002 192.168.0.219:9003 192.168.0.219:9004 192.168.0.219:9005

>>> Creating cluster

Connecting to node 192.168.0.60:9000: OK

Connecting to node 192.168.0.60:9001: OK

Connecting to node 192.168.0.60:9002: OK

Connecting to node 192.168.0.219:9003: OK

Connecting to node 192.168.0.219:9004: OK

Connecting to node 192.168.0.219:9005: OK

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.0.60:9000

192.168.0.219:9003

192.168.0.60:9001

Adding replica 192.168.0.219:9004 to 192.168.0.60:9000

Adding replica 192.168.0.60:9002 to 192.168.0.219:9003

Adding replica 192.168.0.219:9005 to 192.168.0.60:9001

M: d7728bfbd286f9387911fdb459fbb580f7189d80 192.168.0.60:9000

   slots:0-5460 (5461 slots) master

M: 9cd3cfcadd4f8f3c3aa9cc5fce21f58fd9d75016 192.168.0.60:9001

   slots:10923-16383 (5461 slots) master

S: 169353ccf9e9162819a937ec7fe248e5357f3d30 192.168.0.60:9002

   replicates 33857a90b7c53dfc30f3e5a2c66e4b220154a972

M: 33857a90b7c53dfc30f3e5a2c66e4b220154a972 192.168.0.219:9003

   slots:5461-10922 (5462 slots) master

S: 78b95af1ff6b6d1873056285274abf472dc82119 192.168.0.219:9004

   replicates d7728bfbd286f9387911fdb459fbb580f7189d80

S: 87b88def2e17278487b53482a51b46279fdd2d5b 192.168.0.219:9005

   replicates 9cd3cfcadd4f8f3c3aa9cc5fce21f58fd9d75016

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.60:9000)

M: d7728bfbd286f9387911fdb459fbb580f7189d80 192.168.0.60:9000

   slots:0-5460 (5461 slots) master

M: 9cd3cfcadd4f8f3c3aa9cc5fce21f58fd9d75016 192.168.0.60:9001

   slots:10923-16383 (5461 slots) master

M: 169353ccf9e9162819a937ec7fe248e5357f3d30 192.168.0.60:9002

   slots: (0 slots) master

   replicates 33857a90b7c53dfc30f3e5a2c66e4b220154a972

M: 33857a90b7c53dfc30f3e5a2c66e4b220154a972 192.168.0.219:9003

   slots:5461-10922 (5462 slots) master

M: 78b95af1ff6b6d1873056285274abf472dc82119 192.168.0.219:9004

   slots: (0 slots) master

   replicates d7728bfbd286f9387911fdb459fbb580f7189d80

M: 87b88def2e17278487b53482a51b46279fdd2d5b 192.168.0.219:9005

   slots: (0 slots) master

   replicates 9cd3cfcadd4f8f3c3aa9cc5fce21f58fd9d75016

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

二、测试篇

1)检查集群状态

[root@mysqlslave src]# ./redis-trib.rb check 192.168.0.60:9000

Connecting to node 192.168.0.60:9000: OK

Connecting to node 192.168.0.60:9002: OK

Connecting to node 192.168.0.219:9004: OK

Connecting to node 192.168.0.219:9005: OK

Connecting to node 192.168.0.60:9001: OK

Connecting to node 192.168.0.219:9003: OK

>>> Performing Cluster Check (using node 192.168.0.60:9000)

M: d7728bfbd286f9387911fdb459fbb580f7189d80 192.168.0.60:9000

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: 169353ccf9e9162819a937ec7fe248e5357f3d30 192.168.0.60:9002

   slots: (0 slots) slave

   replicates 33857a90b7c53dfc30f3e5a2c66e4b220154a972

S: 78b95af1ff6b6d1873056285274abf472dc82119 192.168.0.219:9004

   slots: (0 slots) slave

   replicates d7728bfbd286f9387911fdb459fbb580f7189d80

S: 87b88def2e17278487b53482a51b46279fdd2d5b 192.168.0.219:9005

   slots: (0 slots) slave

   replicates 9cd3cfcadd4f8f3c3aa9cc5fce21f58fd9d75016

M: 9cd3cfcadd4f8f3c3aa9cc5fce21f58fd9d75016 192.168.0.60:9001

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

M: 33857a90b7c53dfc30f3e5a2c66e4b220154a972 192.168.0.219:9003

   slots:5461-10922 (5462 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.

 

2)测试redis集群

[root@mysqlslave src]# redis-cli -c -p 9000 -h 192.168.0.60

192.168.0.60:9000> set test ljp20151215

-> Redirected to slot [6918] located at 192.168.0.219:9003

OK

192.168.0.219:9003> get test    //直接转到0.219:9003

"ljp20151215"  //可以取到值

192.168.0.219:9003>

我们也可以到另一台电脑0.219上去。连接到0.219的9005. 可以取到test的值。