Redis 3.2 集群搭建

                                         Redis  3.2 集群搭建

  Redis3.0版本之后支持Cluster. 这里安装 3.2 版本

1、下载安装包

cd /usr/local/
wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar -zxvf /redis-3.2.1.tar.gz

2 编译安装

cd /usr/local/redis-3.2.1
make && make install

3. 创建 redis节点  在192.168.145.135上

cd /usr/local/
mkdir redis_cluster  //创建集群目录
cd redis_cluster
mkdir 7000 7001 7002  //分别代表三个节点    其对应端口 7000 7001 7002
//创建7000节点为例,拷贝到7000目录
cp /usr/local/redis-3.2.1/redis.conf  ./redis_cluster/7000/   
//拷贝到7001目录
cp /usr/local/redis-3.2.1/redis.conf  ./redis_cluster/7001/   
//拷贝到7002目录
cp /usr/local/redis-3.2.1/redis.conf  ./redis_cluster/7002/

分别对7001,7002、7003文件夹中的3个文件修改对应的配置 修改

daemonize    yes                          //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7002,7003
port  7000                                //端口7000,7002,7003
cluster-enabled  yes                      //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf      //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  5000                //请求超时  设置5秒够了
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志
bind 192.168.145.135
protected-mode no

在192.168.145.136 执行 135上操作,下载安装redis,创建redis节点 7003 7004 7005,然后修改配置.

在两台机器上启动

cd /usr/local
redis-server  redis_cluster/7000/redis.conf
redis-server  redis_cluster/7001/redis.conf
redis-server  redis_cluster/7002/redis.conf
redis-server  redis_cluster/7003/redis.conf
redis-server  redis_cluster/7004/redis.conf
redis-server  redis_cluster/7005/redis.conf

查看是否启动

ps -ef | grep redis   #查看是否启动成功

netstat -tnlp | grep redis #可以看到redis监听端口
扫描二维码关注公众号,回复: 2333504 查看本文章

安装集群,官方提供了一个工具

redis-trib.rb(/usr/local/redis-3.2.1/src/redis-trib.rb)

需要安装ruby

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

然后gem 安装 redis 端口

gem install redis

如果报错

redis requires Ruby version >= 2.2.2问题

解决办法

先安装rvm,再把ruby版本提升至2.3.3

1.安装curl

sudo yum install curl

2. 安装RVM

curl -L get.rvm.io | bash -s stable 

3. 

source /usr/local/rvm/scripts/rvm

4. 查看rvm库中已知的ruby版本

rvm list known

5. 安装一个ruby版本

rvm install 2.3.3

6. 使用一个ruby版本

rvm use 2.3.3

7. 卸载默认版本

rvm remove 2.0.0

8. 卸载一个已知版本

ruby --version

9. 再安装redis就可以了

gem install redis

如果

curl -L get.rvm.io | bash -s stable  

报错如下:

$ curl -L get.rvm.io | bash -s stable --ruby  
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current  
                                 Dload  Upload   Total   Spent    Left  Speed  
100   184  100   184    0     0     30      0  0:00:06  0:00:06 --:--:--    40  
100 22721  100 22721    0     0   1845      0  0:00:12  0:00:12 --:--:--  6170  
Downloading https://github.com/rvm/rvm/archive/1.26.11.tar.gz  
Downloading https://github.com/rvm/rvm/releases/download/1.26.11/1.26.11.tar.gz.asc  
gpg: Signature made Tue 31 Mar 2015 05:52:13 CST using RSA key ID BF04FF17  
gpg: Can't check signature: public key not found  
Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found.  
Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).  
  
GPG signature verification failed for '/home/charles/.rvm/archives/rvm-1.26.11.tgz' - 'https://github.com/rvm/rvm/releases/download/1.26.11/1.26.11.tar.gz.asc'!  
try downloading the signatures:  
  
    gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3  
  
or if it fails:  
  
    command curl -sSL https://rvm.io/mpapis.asc | gpg --import -  
  
the key can be compared with:  
  
    https://rvm.io/mpapis.asc  
    https://keybase.io/mpapis  

解决办法:

try downloading the signatures:  

下面让你执行啥就执行啥

这里执行

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

然后再重新安装 rvm

curl -L get.rvm.io | bash -s stable  

创建集群

--replicas  1  表示 自动为每一个master节点分配一个slave节点    上面有6个节点,程序会按照一定规则生成 3个master(主)3个slave(从)

提示Can I set the above configuration? (type 'yes' to accept): yes    //输入yes

/usr/local/redis-3.2.1/src/redis-trib.rb  create --replicas 1 192.168.145.135:7000  192.168.145.135:7001 192.168.145.135:7002 192.168.145.136:7003 192.168.145.136:7004 192.168.145.136:7005 

如果报错,出现

redis创建集群——[ERR] Sorry, can't connect to node 192.168.X.X

检查redis.conf  的两个参数

bind 127.0.0.1
protected-mode yes  

是否修改

解决办法 

sed -i 's/bind 127.0.0.1/bind 192.168.145.136/' redis.conf
sed -i 's/protected-mode yes/protected-mode no/' redis.conf

 

 

 

参考:http://redis.io/commands/cluster-addslots

redis常用命令

CLUSTER INFO 打印集群的信息
CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。 
//节点
CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。
CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。
CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。
CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。
CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。
CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点。
CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。
CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。
CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。 
//键
CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。
CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。 
//新增
CLUSTER SLAVES node-id 返回一个master节点的slaves 列表

 

登录集群
 

redis-cli -h 192.168.145.136 -c -p 7003

感觉不错,您打赏,一分也是爱,点击下面链接

https://img-blog.csdn.net/20180322135423539

猜你喜欢

转载自blog.csdn.net/wangming520liwei/article/details/81130002
今日推荐