---
title: "redis集群搭建"
author: "e_e"
date: "2017-02-28T23:44:44Z"
tags: []
categories: ["数据库"]
slug: "redis"
draft: false
lastmod: "2017-02-28T23:44:44Z"
id: 2359581
url: "https://e-e.iteye.com/blog/2359581"
---

cluster 模式

 

第一步创建文件夹

mkdir cluster-redis

cd cluster-redis

mkdir 6001 6002 6003 7001 7002 7003

 

第二步  配置文件修改

(1)

Bind 192.168.99.171

Demonize yes

Port  7001

(2) redis 实例的pid文件：pidfile “/var/run/redis\_700\*.pid”

(3) 日志的配置文件： logfile “/usr/local/redis-cluster/700\*/logs/edis.log”

(4)dir /usr/local/redis-cluster/700\*/(指定数据文件存放位置，必须要指定不同的目录位置，不然会丢失数据)

(5）cluster-enabled yes （启动集群模式开启）

(6）cluster-config-file nodes700\*.conf (这里700\*最好和port对应上)

(7） cluster-node-timeout 5000

(8）appendonly yes 并修改aof模式为：appendfsync always

 

第三步

拷贝redis安装文件到各个集群文件夹下

cp /usr/local/redis-3.2.8 /usr/local/cluster-reids/600*

拷贝配置文件redis.conf到每个文件夹下

（vi全局修改命令     :%s/6001/6002/g    意思是将文件中所有6001改成6002，g的意思是全局）

 

第四步

安装ruby

yum install ruby

yum install rubygems

gem install redis    //ruby 与redis集成

 

第五步

分别启动每个节点

／/usr/local/bin/redis-server /usr/local/cluster-redis/600\*/redis.conf

ps -ef | grep redis查看是否都启动了

查看结果如下：

    0 36834     1   0 10:59下午 ??         0:00.26 /usr/local/bin/redis-server 192.168.99.171:6001 \[cluster] 

    0 36939     1   0 11:01下午 ??         0:00.18 /usr/local/bin/redis-server 192.168.99.171:6002 \[cluster] 

    0 37013     1   0 11:03下午 ??         0:00.11 /usr/local/bin/redis-server 192.168.99.171:6003 \[cluster] 

    0 37053     1   0 11:04下午 ??         0:00.08 /usr/local/bin/redis-server 192.168.99.171:7001 \[cluster] 

    0 37097     1   0 11:04下午 ??         0:00.05 /usr/local/bin/redis-server 192.168.99.171:7002 \[cluster] 

    0 37143     1   0 11:05下午 ??         0:00.01 /usr/local/bin/redis-server 192.168.99.171:7003 \[cluster] 

此时每个节点都是单独地节点，还不是集群

第六步

redis目录下src下 执行 redis-trib.rb 命令，—replicas 1 的意思是 主节点和从节点的比例是1

cd /usr/local/redis-3.2.8/src/

 ./redis-trib.rb create --replicas 1 192.168.99.171:6001 192.168.99.171:6002 192.168.99.171:6003 192.168.99.171:7001 192.168.99.171:7002 192.168.99.171:7003

 

下面是执行结果

&gt;&gt;&gt; Creating cluster

&gt;&gt;&gt; Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.99.171:6001

192.168.99.171:6002

192.168.99.171:6003

Adding replica 192.168.99.171:7001 to 192.168.99.171:6001

Adding replica 192.168.99.171:7002 to 192.168.99.171:6002

Adding replica 192.168.99.171:7003 to 192.168.99.171:6003

M: 41040ebe0a355446e8aacb1ece9363ae1cd52a2a 192.168.99.171:6001

   slots:0-5460 (5461 slots) master

M: 7df9adb8cb22702a37adf78cf638b2237dbb295d 192.168.99.171:6002

   slots:5461-10922 (5462 slots) master

M: 4ac4444bbdbe31270be1c162233ef663899047a6 192.168.99.171:6003

   slots:10923-16383 (5461 slots) master

S: bc472066eebfec7751ff0feaad4afc81e84c4d9c 192.168.99.171:7001

   replicates 41040ebe0a355446e8aacb1ece9363ae1cd52a2a

S: 21d0cde48313ca062ac059b94230665c1e27924a 192.168.99.171:7002

   replicates 7df9adb8cb22702a37adf78cf638b2237dbb295d

S: ec6b05010a22dc4002ddee7c7a11d8f408c8a471 192.168.99.171:7003

   replicates 4ac4444bbdbe31270be1c162233ef663899047a6

Can I set the above configuration? (type 'yes' to accept): yes           // yes 对执行计划进行确认

&gt;&gt;&gt; Nodes configuration updated

&gt;&gt;&gt; Assign a different config epoch to each node

&gt;&gt;&gt; Sending CLUSTER MEET messages to join the cluster

Waiting for the cluster to join...

&gt;&gt;&gt; Performing Cluster Check (using node 192.168.99.171:6001)

M: 41040ebe0a355446e8aacb1ece9363ae1cd52a2a 192.168.99.171:6001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: ec6b05010a22dc4002ddee7c7a11d8f408c8a471 192.168.99.171:7003

   slots: (0 slots) slave

   replicates 4ac4444bbdbe31270be1c162233ef663899047a6

S: bc472066eebfec7751ff0feaad4afc81e84c4d9c 192.168.99.171:7001

   slots: (0 slots) slave

   replicates 41040ebe0a355446e8aacb1ece9363ae1cd52a2a

S: 21d0cde48313ca062ac059b94230665c1e27924a 192.168.99.171:7002

   slots: (0 slots) slave

   replicates 7df9adb8cb22702a37adf78cf638b2237dbb295d

M: 4ac4444bbdbe31270be1c162233ef663899047a6 192.168.99.171:6003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

M: 7df9adb8cb22702a37adf78cf638b2237dbb295d 192.168.99.171:6002

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

\[OK] All nodes agree about slots configuration.

&gt;&gt;&gt; Check for open slots...

&gt;&gt;&gt; Check slots coverage...

\[OK] All 16384 slots covered.

 

到这里集群就搭建成功了

 

第七步验证

/usr/local/bin/redis-cli -c -h 192.168.99.171 -p 6001

-c 表示以集群的模式启动

 

info

cluster-info

cluster-node

 

 

可以查看集群相关信息