Redis Cluster 的安装和配置(2)

51的内容长度限制,这里继续 redis cluster 的简单使用和维护。

7,下面简单测试一下 cluster
  
7.1 测试数据分布

使用 redis-cli 来测试 cluster shard,可以看到 foo->bar 被存储到了 7002 节点上,并且自动跳转到了 7002 上,然后 7002 上 hello-world 是存储在 7000 上,再次重定向到 7000 节点上了。

# -c 选项为进入 cluster mode
$ redis-cli -c -p 7000
127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
127.0.0.1:7002> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"
127.0.0.1:7000>

7.2 reshard

重新分布,这里测试reshard 1000 个 slots 到 7000 节点的情形:

$ ./redis-trib.rb reshard 127.0.0.1:7000
... ...
# 需要迁移的 slots 数目
How many slots do you want to move (from 1 to 16384)? 1000
# 接收这 1000 个 slots 的节点,这里必须填写为 NodeID
What is the receiving node ID? 123ed65d59ff22370f2f09546f410d31207789f6
# 这里需要设置从哪些 源节点 中选择 slots,这里 all 表示从所有节点中选择
Please enter all the source node IDs.
  Type ‘all‘ to use all the nodes as source nodes for the hash slots.
  Type ‘done‘ once you entered all the source nodes IDs.
Source node #1: all
# 下面列出了 Source nodes 以及 Destination node(即 7000 节点)
Ready to move 1000 slots.
  Source nodes:
    M: 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1:7001
   slots:5962-10922 (4961 slots) master
   1 additional replica(s)
    M: f5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1:7002
   slots:11422-16383 (4962 slots) master
   1 additional replica(s)
  Destination node:
    M: 123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000
   slots:0-5961,10923-11421 (6461 slots) master
   1 additional replica(s)
   
# 下面为 redis-trib 列出的 reshard 的 slot 规划:
  Resharding plan:
    Moving slot 11422 from f5bdda1518cd3826100a30f5953ed82a5861ed48
    Moving slot 11423 from f5bdda1518cd3826100a30f5953ed82a5861ed48
    Moving slot 11424 from f5bdda1518cd3826100a30f5953ed82a5861ed48
# 如果接收 redis-trib 的 slot reshard plan,则录入 yes
Do you want to proceed with the proposed reshard plan (yes/no) yes
# 开始执行 resharding
Moving slot 11422 from 127.0.0.1:7002 to 127.0.0.1:7000: 
Moving slot 11423 from 127.0.0.1:7002 to 127.0.0.1:7000: 
Moving slot 11424 from 127.0.0.1:7002 to 127.0.0.1:7000:
# 下面我们校验一下,确认 resharding OK
$ ./redis-trib.rb check 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000
   slots:0-6460,10923-11922 (7461 slots) master
   1 additional replica(s)

7.3 failover 测试(这里没有做一致性检查,只是看一下是否 master-slave 做了切换)

首先通过 debug segfault 命令停止了 7002 节点(从上面的运行日志中,可以看到该节点为 master 节点)

  $ redis-cli -p 7002 debug segfault
  Error: Server closed the connection
  
  # 下面为 7002 节点的日志
=== REDIS BUG REPORT END. Make sure to include from START to END. ===
       Please report the crash opening an issue on github:
           http://github.com/antirez/redis/issues
  Suspect RAM error? Use redis-server --test-memory to verify it.
Segmentation fault
$

然后从 cluster nodes 命令输出可以看到 7002 节点 fail,同时它的 slave 7005 节点升级为了 master 节点

127.0.0.1:7000> cluster nodes
35e0f6fdadbf81a00a1d6d1843698613e653867b 127.0.0.1:7003 slave 123ed65d59ff22370f2f09546f410d31207789f6 0 1410838367042 7 connected
61dfb1055760d5dcf6519e35435d60dc5b207940 127.0.0.1:7004 slave 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 0 1410838368047 5 connected
82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1:7001 master - 0 1410838368547 2 connected 6461-10922
123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000 myself,master - 0 0 7 connected 0-6460 10923-11922
bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7005 master - 0 1410838366541 8 connected 11923-16383
f5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1:7002 master,fail - 1410838288398 1410838286795 3 disconnected
127.0.0.1:7000>

把 7002 节点再次启动后,7002 节点自动成为了 7005 节点的 slave 节点

127.0.0.1:7000> cluster nodes
35e0f6fdadbf81a00a1d6d1843698613e653867b 127.0.0.1:7003 slave 123ed65d59ff22370f2f09546f410d31207789f6 0 1410838468336 7 connected
61dfb1055760d5dcf6519e35435d60dc5b207940 127.0.0.1:7004 slave 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 0 1410838468336 5 connected
82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1:7001 master - 0 1410838468837 2 connected 6461-10922
123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000 myself,master - 0 0 7 connected 0-6460 10923-11922
bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7005 master - 0 1410838467334 8 connected 11923-16383
f5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1:7002 slave bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410838469337 8 connected
127.0.0.1:7000>

7.4 新增节点
 
  7.4.1 新增 master 节点
 
  默认是新增一个 master 节点,同样的,这里测试是先新增 7006/ 目录,再编辑 redis.conf 配置文件

在 7006 目录下启动 7006 节点 redis-server redis.conf

下面通过 redis-trib 工具在当前的 cluster 中新增 7006 节点

  $ ./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000
  >>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000
  ...
  >>> Performing Cluster Check (using node 127.0.0.1:7000)
  ...
  Connecting to node 127.0.0.1:7006: OK
  // 这里发送 CLUSTER MEET 命令给 7006 节点,要求其加入 cluster
  >>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster.
  [OK] New node added correctly.

此时,7006 节点已经加入 cluster,可以接收客户端的指令,并执行 redirection
 
  7.4.2 新增 slave 节点
 
  有两种方式来添加 slave 节点:
 
  1) 通过 --slave 选项添加 slave 节点,这里以新增的 7007 节点作为 7005(当前已经为master节点)的 slave 节点为例

$ ./redis-trib.rb add-node --slave --master-id bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7007 127.0.0.1:7000
>>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
... ...
>>> Performing Cluster Check (using node 127.0.0.1:7000)
S: 35e0f6fdadbf81a00a1d6d1843698613e653867b 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 123ed65d59ff22370f2f09546f410d31207789f6
M: bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7005
   slots:11923-16383 (4461 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.
// 将 7007 加入 cluster,并配置为 7005 的 slave 节点
Connecting to node 127.0.0.1:7007: OK
>>> Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 127.0.0.1:7005.
[OK] New node added correctly.
[[email protected] redis-cluster]# 
    
127.0.0.1:7000> cluster nodes
8868592d98d84b7cf5752cc0b97af4ac807d1a12 127.0.0.1:7007 slave bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410844154906 8 connected
6d1ebedad33bb31ffbaa99bad095eef4a5920857 127.0.0.1:7006 master - 0 1410844155909 0 connected
35e0f6fdadbf81a00a1d6d1843698613e653867b 127.0.0.1:7003 slave 123ed65d59ff22370f2f09546f410d31207789f6 0 1410844154405 7 connected
61dfb1055760d5dcf6519e35435d60dc5b207940 127.0.0.1:7004 slave 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 0 1410844153905 5 connected
82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1:7001 master - 0 1410844153905 2 connected 6461-10922
123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000 myself,master - 0 0 7 connected 0-6460 10923-11922
bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7005 master - 0 1410844155408 8 connected 11923-16383
f5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1:7002 slave bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410844154406 8 connected
127.0.0.1:7000>

2) 通过 cluster replicate 命令来添加 slave 节点,如下:

# 将 7007 节点添加为 NodeID=bfc910f924d772fe03d9fe6a19aabd73d5730d26 的 slave 节点
127.0.0.1:7007> cluster replicate bfc910f924d772fe03d9fe6a19aabd73d5730d26

---------

7.4 删除节点

7.4.1 删除 slave 节点
 
  对于删除 slave 节点,可以直接使用 redis-trib 的 del-node 命令来删除,如:

./redis-trib del-node 127.0.0.1:7000 <node-id>

node-id 为待删除的slave节点的 NodeID。
 
  7.4.2 删除 master 节点
 
  也可以使用 del-node 命令来删除 master 节点,但是必须为空,因此需要在删除 master 节点之前先通过 reshard 将数据迁移至其他的 master 节点上。
  当然也可以使用上面的 CLUSTER FAILOVER 命令来删除 master 节点,并选择一个 slave 作为新的 master 节点。

时间: 2024-08-27 20:26:10

Redis Cluster 的安装和配置(2)的相关文章

redis简介、安装、配置和数据类型

redis简介.安装.配置和数据类型 redis简介 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理. 它支持字符串.哈希表.列表.集合.有序集合,位图,hyperloglogs等数据类型. 内置复制.Lua脚本.LRU收回.事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区. Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redi

redis的单机安装与配置以及生产环境启动方案

简单介绍一下redis的单机安装与配置,方便自己记录安装步骤的同时方便他人获取知识. 首先,从官网下载最新版的(稳定版)的redis安装包.官网地址如下:https://redis.io/download 下载源码包后,redis需要编译安装.需要安装gcc和tcl,gcc用于编译tcl用于测试. 使用命令安装gcc,yum install gcc,一路选择yes,gcc就可以安装成功. 接下来安装tcl,首先获取tcl源码包(见百度云盘)或者使用命令:wget http://downloads

Windows Server2016+SQL Server Cluster 2016安装及配置

我们知道,近期微软正式发布了Windows Server2016和SQL Server Cluster 2016,所以借此机会来尝试一下Windows Server2016+SQL Server Cluster 2016安装及配置,对于相关功能的优化及升级需要在官网上自己查看整理.今天主要介绍一下Windows Server2016+SQL Server Cluster 2016安装及配置的详情介绍.具体见下: 环境介绍: Hostname:DC IP:192.168.5.10 Role:DC.

Redis集群安装及配置步骤

运行环境:CentOS 7(版本号:1503) Redis版本:3.0.5 参考文档:① http://www.redis.cn/topics/cluster-tutorial.html ② http://www.linuxidc.com/Linux/2015-08/121845.htm 注意:redis集群需要至少6个节点,也就是六台服务器.如果服务器数量不足可在每台服务器上建立多个节点,如2台服务器,每台服务器上建立3个节点 一.      为所有服务器安装Redis 1.使用ssh工具连接

elk(Elasticsearch 2.0.0, Logstash 2.0.0, Kibana4.2.0) redis centos6.6安装与配置

本次安装的相关环境:Centos_x64_6.6  redis2.8.23  Elasticsearch 2.0.0, Logstash 2.0.0,  Kibana4.2.0 软件下载地址: redis: http://redis.io/download Elasticsearch: wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticse

Redis简介、安装、配置学习笔记

前一篇文章有介绍关系型数据库和非关系型数据库的差异,现在就来学习一下用的较广的非关系型数据库:Redis数据库 Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储

在Django中使用redis:包括安装、配置、启动。

安装redis: 1.下载: wget http://download.redis.io/releases/redis-3.2.8.tar.gz step2:解压 tar -zxvf redis-3.2.8.tar.gz step3:复制,放到usr/local?录下 sudo mv ./redis-3.2.8 /usr/local/redis step4:进?redis?录 cd /usr/local/redis/ step5:生成 sudo make step6:测试,这段运?时间会较? s

REDIS CLUSTER简单安装配置

1 redis简单的配置文件 [[email protected] 7007]cat redis.conf daemonize yes pidfile /var/run/redis7007.pid port 7007 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "/tmp/redis7007.log" databases 16 save 900 1 save 300 10 save 60 10000

Redis Cluster 简单安装配置

1 先安装ruby-2.3.1.tar.gz 2 解压 redis-3.2.1.zip 得到redis-3.2.1.gem 3 gem install --local redis-3.2.1.gem -V 4 解压 redis-3.2.0.tar.gz 拷贝到/app/redis/redis-3.2.0 5 安装 make / make install 6 创建集群需要的目录mkdir /app/redis/redis-nodescd /app/redis/redis-nodesmkdir 70