redis集群安装

1.下载redis源码

2.解压并进入解压后的文件夹redis内

3.make,生成一系列的文件(mkreleasehdr.sh, redis-benchmark, redis-check-aof, redis-check-rdb, redis-cli, redis-sentinel, redis-server, redis-server, redis-trib.rb)

要部署集群,至少需要6个节点,这里使用了六台电脑centos系统,每台电脑上运行一个redis实例,端口为6379 。(如果没有6台,则可以在一台电脑上运行多个redis实例,设置不同的端口即可)

4.设置配置文件 src/redis.conf

port 6379  # 绑定端口,可以改为其他端口

daemon yes   # 后台运行

cluster-enabled yes #集群
cluster-config-file nodes.conf  # 组成集群时自动生成的配置文件,用户不需修改这个文件
cluster-node-timeout 5000    # 超时(认为宕机)
appendonly yes          # 每次更新或插入新数据时写入日志文件

bind

bind <本机ip>  # 绑定ip,目的是为了让其他节点知道,如果在一台电脑上运行6个redis实例,则这里可以设为127.0.0.1(没有实测)

5. 安装ruby,rubygems,可以从网上下载源码安装或者 yum install

6. 执行指令

gem install redis

这是因为集群启动是使用ruby脚本,如果出现安装错误比如说没有zlib或者openssl,则需要yum install zlib zlib-devel openssl openssl-devel,然后需要重新编译安装ruby(否则仍然提示错误)。

7. 将redis文件夹复制到其他5台电脑上,并在6台电脑上依次启动6个redis实例

8. 执行指令

./redis-trib.rb create --replicas  127.0.0.1: 127.0.0.1: \
127.0.0.1: 127.0.0.1: 127.0.0.1: 127.0.0.1:

将127.0.0.1:<port> 替换为实际的ip:port

如果出现redis waiting for the cluster to join.... 一直阻塞,则说明有的节点端口无法通信,可以开放所绑定的端口(以及 绑定端口+10000 得到的另一个端口,这个端口redis也使用)

如果出现 sorry, can't connect to node xxx.xxx.xx.xx,则很可能是防火墙没有开放端口

进行如下设置即可

开启端口
 
firewall-cmd --zone=public --add-port=6379/tcp --permanent
 firewall-cmd --zone=public --add-port=16379/tcp --permanent
命令含义:
 
--zone #作用域
 
--add-port=80/tcp  #添加端口,格式为:端口/通讯协议
 
--permanent   #永久生效,没有此参数重启后失效
 
重启防火墙
 
firewall-cmd --reload

9.如果需要关闭集群,则需要在每台电脑上分别关闭

ps -aux | grep redis

得到redis的pid,然后

kill - <pid>

此时如果再次执行上面 7,8 步骤重启集群,则会出现类似

Node 192.168.10.219:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

的错误,此时需要将redis启动目录中的appendonly.aof,nodes.conf,dump.rdb文件删除后再执行 7,8步骤方可

10. 最后登录集群

./redis-cli -h 192.168.10.10 -c -p 

11. 如果给集群设置密码,则注意在集群搭建完成前不要设置密码,在搭建之后通过客户端设置密码,如下(注意分别登录每个节点并设置)

config set masterauth <p1>
config set requirepass <p2>
auth <p2>
config rewrite

下次登录可使用

./redis-cli -h <ip> -c -p  -a <p2>

或者使用10步骤登录后,再使用 auth <p2>验证身份

redis集群管理工具

(以下内容来源:http://blog.sina.com.cn/s/blog_13ea37b210102wyc7.html

redis-trib.rb是redis官方推出的管理redis集群的工具,集成在redis的源码src目录下,是基于redis提供的集群命令封装成简单、便捷、实用的操作工具。redis-trib.rb是redis作者用ruby完成的。

先从redis-trib.rb的help信息,看下redis-trib.rb提供了哪些功能。
redis-trib
选项:
  create          host1:port1 ... hostN:portN  创建集群,只有大于等于3个节点才能创建集群
                      --replicas                              每个节点需要几个slave
  check           host:port                              检查集群,只需要选择一个集群中的一个节点即可
  info             host:port                               查看集群信息,只需要选择一个集群中的一个节点即可
  fix               host:port                               修复集群,目前fix命令能修复两种异常,一种是集群有处
                                                                  于迁移中的slot的节点,一种是slot未完全分配的异常
                     --timeout                              超时时间
  reshard         host:port                             在线迁移slot
                    --from                   需要从哪些源节点上迁移slot,可从多个源节点完成迁移,
                                                  以逗号隔开,传递的是节点的node id,
                                                  还可以直接传递--from all,这样源节点就是集群的所有节点,
                                                  不传递该参数的话,则会在迁移过程中提示用户输入。
                  --to                    Slot需要迁移的目的节点的node id,目的节点只能填写一个,
                                            不传递该参数的话,则会在迁移过程中提示用户输入。
                  --slots                 需要迁移的slot数量,不传递该参数的话,则会在迁移过程中提示用户输入。
                  --yes                        设置该参数,可以在打印执行reshard计划的时候,提示用户输入yes确认后再执行reshard
                  --timeout               设置migrate命令的超时时间。
                  --pipeline              定义cluster getkeysinslot命令一次取出的key数量,
                                                不传的话使用默认值为10。
  rebalance       host:port         平衡集群节点slot数量,这个是必传参数,
                                                用来从一个节点获取整个集群信息,相当于获取集群信息的入口
                  --weight                节点的权重,格式为node_id=weight,如果需要为多个节点分配权
                                                重的话,需要添加多个--weight 参数,
                                                即--weight b31e3a2e=5 --weight 60b8e3a1=5,node_id可为节点
                                                名称的前缀,只要保证前缀位数能唯一区分该节点即可。
                                                没有传递–weight的节点的权重默认为1。
                  --auto-weights               这个参数在rebalance流程中并未用到
                  --use-empty-masters          rebalance是否考虑没有节点的master,
                                               默认没有分配slot节点的master是不参与rebalance的,
                                               设置--use-empty-masters可以让没有分配slot的节点参与rebalance
                  --timeout              设置migrate命令的超时时间
                  --simulate             设置该参数,可以模拟rebalance操作,提示用户会迁移哪些slots,
                                                而不会真正执行迁移操作
                  --pipeline              与reshar的pipeline参数一样,定义cluster getkeysinslot命令一次
                                                取出的key数量,不传的话使用默认值为10。
                  --threshold             只有节点需要迁移的slot阈值超过threshold,才会执行rebalance操作
  add-node        new_host:new_port existing_host:existing_port 将新节点加入集群,
                                               命令可以将新节点加入集群,节点可以为master,也可
                                               以为某个master节点的slave
                  --slave                  设置该参数,则新节点以slave的角色加入集群
                  --master-id           这个参数需要设置了--slave才能生效,--master-id用来指定新节点的
                                               master节点。如果不设置该参数,则会随机为节点选择master节点。
  del-node        host:port node_id            从集群中删除节点,
  set-timeout     host:port milliseconds       设置集群节点间心跳连接的超时时间,单位是毫秒
  call            host:port command arg arg .. arg  在集群全部节点上执行命令,
                                              call命令也是需要通过集群的一个节点地址,连上整个集群,
                                              然后在集群的每个节点执行该命令。
  import          host:port        将外部redis节点数据导入集群,
                                              import命令更适合离线的把外部redis数据导入,
                                               以slave的方式连接redis节点去同步节点数据应该是更好的方式。
                  --from                  源节点
                  --copy                       拷贝
                  --replace                    替换
  help            打印帮助信息
 
示例:
* 创建Redis集群
$ redis-trib.rb create --replicas 1 172.16.0.7:6379 172.16.0.7:6380 172.16.0.7:6381 172.16.0.7:6382 172.16.0.7:6383 172.16.0.7:6384
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.0.7:6379
172.16.0.7:6380
172.16.0.7:6381
Adding replica 172.16.0.7:6382 to 172.16.0.7:6379
Adding replica 172.16.0.7:6383 to 172.16.0.7:6380
Adding replica 172.16.0.7:6384 to 172.16.0.7:6381
M: 8b3809e6e5591dcfec067216906a25d7cd482598 172.16.0.7:6379
   slots:0-5460 (5461 slots) master
M: a1ac1fc64549ca15eb889395c46d001162bf2aa6 172.16.0.7:6380
   slots:5461-10922 (5462 slots) master
M: 01a0cccbb8b15e00469d1cafd2eedfb6bd8bd2b5 172.16.0.7:6381
   slots:10923-16383 (5461 slots) master
S: 7d033c0df5b7db4f872afa94566fb061c4ebe16a 172.16.0.7:6382
   replicates 8b3809e6e5591dcfec067216906a25d7cd482598
S: 72b5baa2dbdb315fef604ea8b6deb0143372ae1b 172.16.0.7:6383
   replicates a1ac1fc64549ca15eb889395c46d001162bf2aa6
S: 8641d66fce1f5cc982ef7649d53a1eba2fa9b7c5 172.16.0.7:6384
   replicates 01a0cccbb8b15e00469d1cafd2eedfb6bd8bd2b5
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 172.16.0.7:6379)
M: 8b3809e6e5591dcfec067216906a25d7cd482598 172.16.0.7:6379
   slots:0-5460 (5461 slots) master
M: a1ac1fc64549ca15eb889395c46d001162bf2aa6 172.16.0.7:6380
   slots:5461-10922 (5462 slots) master
M: 01a0cccbb8b15e00469d1cafd2eedfb6bd8bd2b5 172.16.0.7:6381
   slots:10923-16383 (5461 slots) master
M: 7d033c0df5b7db4f872afa94566fb061c4ebe16a 172.16.0.7:6382
   slots: (0 slots) master
   replicates 8b3809e6e5591dcfec067216906a25d7cd482598
M: 72b5baa2dbdb315fef604ea8b6deb0143372ae1b 172.16.0.7:6383
   slots: (0 slots) master
   replicates a1ac1fc64549ca15eb889395c46d001162bf2aa6
M: 8641d66fce1f5cc982ef7649d53a1eba2fa9b7c5 172.16.0.7:6384
   slots: (0 slots) master
   replicates 01a0cccbb8b15e00469d1cafd2eedfb6bd8bd2b5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

* 检查Redis集群
$ redis-trib.rb check 172.16.0.7:6379
>>> Performing Cluster Check (using node 172.16.0.7:6379)
M: 8b3809e6e5591dcfec067216906a25d7cd482598 172.16.0.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 8641d66fce1f5cc982ef7649d53a1eba2fa9b7c5 172.16.0.7:6384
   slots: (0 slots) slave
   replicates 01a0cccbb8b15e00469d1cafd2eedfb6bd8bd2b5
S: 7d033c0df5b7db4f872afa94566fb061c4ebe16a 172.16.0.7:6382
   slots: (0 slots) slave
   replicates 8b3809e6e5591dcfec067216906a25d7cd482598
M: a1ac1fc64549ca15eb889395c46d001162bf2aa6 172.16.0.7:6380
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 72b5baa2dbdb315fef604ea8b6deb0143372ae1b 172.16.0.7:6383
   slots: (0 slots) slave
   replicates a1ac1fc64549ca15eb889395c46d001162bf2aa6
M: 01a0cccbb8b15e00469d1cafd2eedfb6bd8bd2b5 172.16.0.7:6381
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.

* 查看Redis集群信息
$ redis-trib.rb info 172.16.0.7:6379
172.16.0.7:6379 (8b3809e6...) -> 0 keys | 5461 slots | 1 slaves.
172.16.0.7:6380 (a1ac1fc6...) -> 0 keys | 5462 slots | 1 slaves.
172.16.0.7:6381 (01a0cccb...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

* 添加Redis节点
$ redis-trib.rb add-node 172.16.0.7:6385 172.16.0.7:6379
>>> Adding node 172.16.0.7:6385 to cluster 172.16.0.7:6379
>>> Performing Cluster Check (using node 172.16.0.7:6379)
M: 328fe71182234df381fb4845e3f4bb4c3288320d 172.16.0.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: da6149e83031f8e1d790784b8650732365134dee 172.16.0.7:6383
   slots: (0 slots) slave
   replicates 237d335822d545e97723224dc965bb986586c98e
M: 6d7ad892ceb964063db08fbb6c1d7b0f6226418c 172.16.0.7:6381
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 237d335822d545e97723224dc965bb986586c98e 172.16.0.7:6380
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 23c093add21934a6b3eeba18897f536947c89f42 172.16.0.7:6384
   slots: (0 slots) slave
   replicates 6d7ad892ceb964063db08fbb6c1d7b0f6226418c
S: 4341c643d2b785019bc940e1ffcf5e44b2343e09 172.16.0.7:6382
   slots: (0 slots) slave
   replicates 328fe71182234df381fb4845e3f4bb4c3288320d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.16.0.7:6385 to make it join the cluster.
[OK] New node added correctly.

* 添加Redis节点
$ redis-trib.rb add-node --slave --master-id 9d6229abfea1fa5a3faa639a7b54ac3a7810f9ff 172.16.0.7:6386 172.16.0.7:6379
>>> Adding node 172.16.0.7:6386 to cluster 172.16.0.7:6379
>>> Performing Cluster Check (using node 172.16.0.7:6379)
M: 328fe71182234df381fb4845e3f4bb4c3288320d 172.16.0.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: da6149e83031f8e1d790784b8650732365134dee 172.16.0.7:6383
   slots: (0 slots) slave
   replicates 237d335822d545e97723224dc965bb986586c98e
M: 6d7ad892ceb964063db08fbb6c1d7b0f6226418c 172.16.0.7:6381
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 9d6229abfea1fa5a3faa639a7b54ac3a7810f9ff 172.16.0.7:6385
   slots: (0 slots) master
   0 additional replica(s)
M: 237d335822d545e97723224dc965bb986586c98e 172.16.0.7:6380
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 23c093add21934a6b3eeba18897f536947c89f42 172.16.0.7:6384
   slots: (0 slots) slave
   replicates 6d7ad892ceb964063db08fbb6c1d7b0f6226418c
S: 4341c643d2b785019bc940e1ffcf5e44b2343e09 172.16.0.7:6382
   slots: (0 slots) slave
   replicates 328fe71182234df381fb4845e3f4bb4c3288320d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.16.0.7:6386 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 172.16.0.7:6385.
[OK] New node added correctly.

2.6 平衡集群节点slot数量
$ redis-trib.rb rebalance --use-empty-masters 172.16.0.7:6379
>>> Performing Cluster Check (using node 172.16.0.7:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 4 nodes. Total weight = 4
Moving 1366 slots from 172.16.0.7:6380 to 172.16.0.7:6385
##########################################################################
Moving 1365 slots from 172.16.0.7:6381 to 172.16.0.7:6385
##########################################################################
Moving 1365 slots from 172.16.0.7:6379 to 172.16.0.7:6385
##########################################################################

* 在线迁移Slot
$ redis-trib.rb reshard --from 9d6229abfea1fa5a3faa639a7b54ac3a7810f9ff --to 328fe71182234df381fb4845e3f4bb4c3288320d --slots 4096 172.16.0.7:6379

* 从集群中删除节点
$ redis-trib.rb del-node 172.16.0.7:6379 9d6229abfea1fa5a3faa639a7b54ac3a7810f9ff
>>> Removing node 9d6229abfea1fa5a3faa639a7b54ac3a7810f9ff from cluster 172.16.0.7:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

注意:必须确保数据迁移后,才能从集群中删除节点,否则报以下错误:
[ERR] Node 172.16.0.7:6385 is not empty! Reshard data away and try again.

redis 集群安装的更多相关文章

  1. CentOS下redis集群安装

    环境: 一台CentOS虚拟机上部署六个节点,创建3个master,3个slave节点 1.下载并解压 cd /root wget http://download.redis.io/releases/ ...

  2. Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置

    Redis简介 Redis是一个基于C语言开发的开源(BSD许可),开源高性能的高级内存数据结构存储,用作数据库.缓存和消息代理.它支持数据结构,如 字符串.散列.列表.集合,带有范围查询的排序集,位 ...

  3. redis集群安装部署

    (要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下) 192.168.1.160:7000 192. ...

  4. 【Redis篇】Redis集群安装与初始

    一.前述   本文将单台节点不同端口模拟集群方式. 二.具体搭建 前提是安装好redis具体可参考http://www.cnblogs.com/LHWorldBlog/p/8463269.html 1 ...

  5. windows下redis集群安装和部署

    1.下载windows版本的Redis 官网只提供linux版本的下载 官网下载地址:http://redis.io/download github下载地址:https://github.com/MS ...

  6. Linux 下Redis集群安装部署及使用详解(在线和离线两种安装+相关错误解决方案)

    一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,其中主要包括在联网的Linux环境和脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网 ...

  7. Windows下Redis集群安装与部署

    1.下载 Redis-x64-3.2.100.zip 安装程序 官网下载地址:http://redis.io/download GitHub下载地址:https://github.com/micros ...

  8. redis集群安装2

      概要:本文主要介绍如何在Centos7中单机搭建redis集群三主三从,按照本文绝对可以实现该需求,至于先搭建单机版主要为了方便理解redis集群,为下一步开发或生产上redis集群做铺垫.同时本 ...

  9. redis集群安装多端口多实例部署

    目标(本文达成的结果,配对关系可能会变): 先在131上进行操作 1.下载redis http://download.redis.io/releases/redis-5.0.2.tar.gz 2.解压 ...

随机推荐

  1. stand up meeting 1/15/2016 && work of weekend 1/16/2016~1/17/2016

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云  组内对生词卡片又重新进行了讨论:准备最后的发布和整个开发的整理 ...

  2. stand up meeting 12-8

    根据计划今天项目组成员和travis老师毕然同学进行了最后一次关于design和feature的确认meeting. 项目design和UI的改动较大,feature改动较小,需对UI进行重新整合,对 ...

  3. stand up meeting 11/30/2015

    part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云   完善了UI的各项功能,弹窗的显示格式等方面的规范:解决logic部分调用该dll的问题:解决鼠标事件的捕捉中~     ...

  4. jmeter 聚合报告参数解释

    label:每个请求的名称 样本:发送给服务器的请求数量 平均值:平均响应时间,默认情况下是单个 Request 的平均响应时间,当使用了 Transaction Controller 时,也可以以T ...

  5. Redis的三大问题

    一般我们对缓存读操作的时候有这么一个固定的套路: 如果我们的数据在缓存里边有,那么就直接取缓存的. 如果缓存里没有我们想要的数据,我们会先去查询数据库,然后将数据库查出来的数据写到缓存中. 最后将数据 ...

  6. ES6让字符串String增加了哪些好玩的特性呢?

    确实因为现在天气变热了,所以一天天的这么写我也很累.所以如果阅读的时候有什么错误还请大家指出来,不好意思.学习永无止境. OK,今天继续讲解ES6系列知识 学过上一节的解构赋值就知道,ES6确实给我们 ...

  7. ES6中不得不说的关键字const

    上一节讲了let关键字,它是用来声明一个变量,只在块级作用域起作用.这一节我们来学习ES6新增的另一个关键字const. const 的作用 const是constant(常量)的缩写,const和 ...

  8. thinkphp5--model数据操作的坑

    最近用thinkphp5开发,经常用到model了来操作数据,但是操作多了,就发现他的坑了. 就好像如果你只是初始化一个model对象,但是你却用这个对象进行多次的数据操作,这时候他的数据就会发生混乱 ...

  9. 创建线程的方式三:实现Callable接口-----JDK5.0 新增

    package com.yhqtv.java2; /* * 创建线程的方式三:实现Callable接口-----JDK5.0 新增 * * 如何理解实现Callable接口的方式创建多线程比实现Run ...

  10. Linux系统管理第三次作业 账号管理 权限及归属管理

    1.创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) [root@localhost ~]# mkdir /guanli [root@localhos ...