Redis的集群(搭建)

分布式数据库是把整个数据集按照分区规则映射到多个节点,每个节点负责一部分数据。

Redis Cluster采用虚拟槽分区(引入虚拟槽改进的一致性哈希算法),所有的键根据哈希函数映射到0~16383整数槽内,计算公式:slot=CRC16(key)&16383,找到槽,再找到槽所在的节点。槽是集群内数据管理和迁移的基本单位。

Redis Cluster搭建需要3个步骤

1. 准备节点

Redis Cluster节点数量至少6个才能保证组成完整高可用的集群,每个节点需开启集群模式,配置如下:

# cluster

cluster-enabled yes

cluster-node-timeout 15000

cluster-config-file "nodes-${port}.conf"

cluster-slave-validity-factor 10

cluster-migration-barrier 1

cluster-require-full-coverage yes

启动6个节点,集群模式的Redis除了原有的配置文件redis.conf,又加了一份集群配置文件nodes-${port}.conf,该文件记录了集群内节点信息的变化,如添加/下线节点,故障转移等,由Redis自动维护。

~/6879/data $ cat nodes-6879.conf

51a3c0a30f397cf28d9f36330cb21df1edda25af :0 myself,master - 0 0 0 connected

vars currentEpoch 0 lastVoteEpoch 0

127.0.0.1:6879> info cluster

# Cluster

cluster_enabled:1

127.0.0.1:6879> cluster info

cluster_state:fail

cluster_slots_assigned:0

cluster_slots_ok:0

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:1

cluster_size:0

cluster_current_epoch:0

cluster_my_epoch:0

cluster_stats_messages_sent:0

cluster_stats_messages_received:0

127.0.0.1:6879> cluster nodes

51a3c0a30f397cf28d9f36330cb21df1edda25af :6879 myself,master - 0 0 0 connected

目前每个节点只能识别出自己的信息,并不知道对方的存在,下面通过握手让6个节点彼此建立联系组成一个集群。

2. 节点握手

节点握手是指一批运行在集群模式下的节点通过Gossip协议彼此通信,达到感知对方的过程。在集群内任意节点上执行cluster meet命令加入新节点,握手状态会通过消息在集群内传播,这样其它节点会自动发现新节点并发起握手流程。

127.0.0.1:6879> cluster meet 127.0.0.1 6880

127.0.0.1:6879> cluster meet 127.0.0.1 6881

127.0.0.1:6879> cluster meet 127.0.0.1 6882

127.0.0.1:6879> cluster meet 127.0.0.1 6883

127.0.0.1:6879> cluster meet 127.0.0.1 6884

127.0.0.1:6879> cluster nodes

51a3c0a30f397cf28d9f36330cb21df1edda25af 127.0.0.1:6879 myself,master - 0 0 1 connected

aa94bcbe1f7ebcf850e1d75cad712a0bbc044d97 127.0.0.1:6884 master - 0 1532681718583 5 connected

8c0e31b4cadc12c784eaa63a200fbb9b86e49a72 127.0.0.1:6880 master - 0 1532681713007 2 connected

ae5c6df67610f399491c174a6ee97345e03fd610 127.0.0.1:6882 master - 0 1532681716555 3 connected

b298f797f42b2fe1269f83e9fadd6cbb2af2fd04 127.0.0.1:6881 master - 0 1532681717567 4 connected

ff0092bfdd407ea73f036564e48b277898494ac1 127.0.0.1:6883 master - 0 1532681719596 0 connected

节点握手后还不能正常工作,这时集群处于下线状态,是由于槽没有分配到节点,集群无法完成槽到节点的映射。

127.0.0.1:6879> set hello redis

(error) CLUSTERDOWN Hash slot not served

3. 分配槽

Redis cluster把所有数据映射到16384个槽中,每个key会映射为一个固定的槽,只有当节点分配了槽,才能响应和这些槽关联的键命令,下面通过cluster addslots命令为节点分配槽。

$ redis-cli -p 6879 cluster addslots {0..5641}

$ redis-cli -p 6880 cluster addslots {5642..10922}

$ redis-cli -p 6881 cluster addslots {10923..16383}

当前集群状态是ok,进入在线状态,所有槽都已经分配给节点,cluster nodes命令可看到槽和节点的对应关系。

127.0.0.1:6879> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:5

cluster_my_epoch:1

cluster_stats_messages_sent:2864

cluster_stats_messages_received:2864

127.0.0.1:6879> cluster nodes

51a3c0a30f397cf28d9f36330cb21df1edda25af 127.0.0.1:6879 myself,master - 0 0 1 connected 0-5641

aa94bcbe1f7ebcf850e1d75cad712a0bbc044d97 127.0.0.1:6884 master - 0 1532683047361 5 connected

8c0e31b4cadc12c784eaa63a200fbb9b86e49a72 127.0.0.1:6880 master - 0 1532683048374 2 connected 5642-10922

ae5c6df67610f399491c174a6ee97345e03fd610 127.0.0.1:6882 master - 0 1532683046350 3 connected

b298f797f42b2fe1269f83e9fadd6cbb2af2fd04 127.0.0.1:6881 master - 0 1532683044329 4 connected 10923-16383

ff0092bfdd407ea73f036564e48b277898494ac1 127.0.0.1:6883 master - 0 1532683045341 0 connected

目前还有3个节点没有使用,作为一个完整的集群,每个负责处理槽的节点应该具有从节点,保证出现故障时,可以进行自动故障转移,使用cluster replicate命令让一个节点成为从节点。

127.0.0.1:6882> cluster replicate 51a3c0a30f397cf28d9f36330cb21df1edda25af

127.0.0.1:6883> cluster replicate 8c0e31b4cadc12c784eaa63a200fbb9b86e49a72

127.0.0.1:6884> cluster replicate b298f797f42b2fe1269f83e9fadd6cbb2af2fd04

127.0.0.1:6884> cluster nodes

8c0e31b4cadc12c784eaa63a200fbb9b86e49a72 127.0.0.1:6880 master - 0 1532683770612 2 connected 5642-10922

ae5c6df67610f399491c174a6ee97345e03fd610 127.0.0.1:6882 slave 51a3c0a30f397cf28d9f36330cb21df1edda25af 0 1532683773646 3 connected

b298f797f42b2fe1269f83e9fadd6cbb2af2fd04 127.0.0.1:6881 master - 0 1532683772637 4 connected 10923-16383

ff0092bfdd407ea73f036564e48b277898494ac1 127.0.0.1:6883 slave 8c0e31b4cadc12c784eaa63a200fbb9b86e49a72 0 1532683774150 2 connected

51a3c0a30f397cf28d9f36330cb21df1edda25af 127.0.0.1:6879 master - 0 1532683769602 1 connected 0-5641

aa94bcbe1f7ebcf850e1d75cad712a0bbc044d97 127.0.0.1:6884 myself,slave b298f797f42b2fe1269f83e9fadd6cbb2af2fd04 0 0 5 connected

目前为止,手动建立了一个6个节点的集群,3个主节点负责处理槽和相关数据,3个从节点负责故障转移。

手动搭建集群,步骤比较繁琐,Redis官方提供了redis-trib.rb工具方便快速搭建,该工具采用Ruby实现,使用需先准备Ruby环境。

准备Ruby环境

# wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz

# tar zxf ruby-2.3.1.tar.gz

# ./configure --prefix=/usr/local/ruby

# make

# make install

# wget http://rubygems.org/downloads/redis-3.3.0.gem

# gem install --local redis-3.3.0.gem

Successfully installed redis-3.3.0

Parsing documentation for redis-3.3.0

Installing ri documentation for redis-3.3.0

Done installing documentation for redis after 0 seconds

1 gem installed

安装Ruby环境后,执行redis-trib.rb命令确认环境是否正确。

$ redis-trib.rb

Usage: redis-trib <command> <options> <arguments ...>

...

创建集群

$ redis-trib.rb create --replicas 1 127.0.0.1:6879 127.0.0.1:6880 127.0.0.1:6881 127.0.0.1:6882 127.0.0.1:6883 127.0.0.1:6884

>>> Creating cluster

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

Using 3 masters:

127.0.0.1:6879

127.0.0.1:6880

127.0.0.1:6881

Adding replica 127.0.0.1:6882 to 127.0.0.1:6879

Adding replica 127.0.0.1:6883 to 127.0.0.1:6880

Adding replica 127.0.0.1:6884 to 127.0.0.1:6881

M: 93aec643effa795f33ab2b151c6a2b273eeb5462 127.0.0.1:6879

slots:0-5460 (5461 slots) master

M: 22b6e422aed074e42d295c061f0b4c102304b5bb 127.0.0.1:6880

slots:5461-10922 (5462 slots) master

M: 1835a6b9b3e0c0c08b09a6798c0359996be7ba7b 127.0.0.1:6881

slots:10923-16383 (5461 slots) master

S: 3ae8e6622d37e15701a757139a51c0006a6df664 127.0.0.1:6882

replicates 93aec643effa795f33ab2b151c6a2b273eeb5462

S: 324228642e6fc87a0c367a6b8e7a47e2879aa7d5 127.0.0.1:6883

replicates 22b6e422aed074e42d295c061f0b4c102304b5bb

S: 178525484b9e865522a0cfa2fef7f207413272d3 127.0.0.1:6884

replicates 1835a6b9b3e0c0c08b09a6798c0359996be7ba7b

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 127.0.0.1:6879)

M: 93aec643effa795f33ab2b151c6a2b273eeb5462 127.0.0.1:6879

slots:0-5460 (5461 slots) master

1 additional replica(s)

M: 22b6e422aed074e42d295c061f0b4c102304b5bb 127.0.0.1:6880

slots:5461-10922 (5462 slots) master

1 additional replica(s)

S: 3ae8e6622d37e15701a757139a51c0006a6df664 127.0.0.1:6882

slots: (0 slots) slave

replicates 93aec643effa795f33ab2b151c6a2b273eeb5462

M: 1835a6b9b3e0c0c08b09a6798c0359996be7ba7b 127.0.0.1:6881

slots:10923-16383 (5461 slots) master

1 additional replica(s)

S: 324228642e6fc87a0c367a6b8e7a47e2879aa7d5 127.0.0.1:6883

slots: (0 slots) slave

replicates 22b6e422aed074e42d295c061f0b4c102304b5bb

S: 178525484b9e865522a0cfa2fef7f207413272d3 127.0.0.1:6884

slots: (0 slots) slave

replicates 1835a6b9b3e0c0c08b09a6798c0359996be7ba7b

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

查看集群信息

$ redis-trib.rb info 127.0.0.1:6879

127.0.0.1:6879 (93aec643...) -> 0 keys | 5461 slots | 1 slaves.

127.0.0.1:6880 (22b6e422...) -> 0 keys | 5462 slots | 1 slaves.

127.0.0.1:6881 (1835a6b9...) -> 0 keys | 5461 slots | 1 slaves.

[OK] 0 keys in 3 masters.

0.00 keys per slot on average.

检查集群完整性

redis-trib.rb check 127.0.0.1:6879

>>> Performing Cluster Check (using node 127.0.0.1:6879)

M: 93aec643effa795f33ab2b151c6a2b273eeb5462 127.0.0.1:6879

slots:0-5460 (5461 slots) master

1 additional replica(s)

M: 22b6e422aed074e42d295c061f0b4c102304b5bb 127.0.0.1:6880

slots:5461-10922 (5462 slots) master

1 additional replica(s)

S: 3ae8e6622d37e15701a757139a51c0006a6df664 127.0.0.1:6882

slots: (0 slots) slave

replicates 93aec643effa795f33ab2b151c6a2b273eeb5462

M: 1835a6b9b3e0c0c08b09a6798c0359996be7ba7b 127.0.0.1:6881

slots:10923-16383 (5461 slots) master

1 additional replica(s)

S: 324228642e6fc87a0c367a6b8e7a47e2879aa7d5 127.0.0.1:6883

slots: (0 slots) slave

replicates 22b6e422aed074e42d295c061f0b4c102304b5bb

S: 178525484b9e865522a0cfa2fef7f207413272d3 127.0.0.1:6884

slots: (0 slots) slave

replicates 1835a6b9b3e0c0c08b09a6798c0359996be7ba7b

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

若感兴趣可关注订阅号”数据库最佳实践”(DBBestPractice).

原文地址:http://blog.51cto.com/coveringindex/2151249

时间: 2024-10-09 16:39:17

Redis的集群(搭建)的相关文章

Redis本地集群搭建(5版本以上)

Redis本地集群搭建(5版本以上) 2019年11月3日10:05:48 步骤 1.下载安装Redis的安装包 2.复制5份,一共6份Redis的解压安装版,修改每个Redis节点的端口并开启节点 3.修改每个Redis节点的端口,以及开启集群模式 3.使用redis-cli --cluster create ip:port给集群的节点分配哈希槽(如果要使用主从,只需要添加master节点的ip:port即可) 4.使用redis-cli --cluster check ip:port查看节点

Redis Cluster集群搭建测试

# Redis Clutser # ## 一.Redis Cluster集群 ## 参考资料: http://www.cnblogs.com/lykxqhh/p/5690923.html Redis集群搭建的方式有多种,例如使用zookper等,但从redis3.0之后版本支持redis cluster集群,Redis Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接.其redis cluster架构图如下: 其结构特点: 1.所有的redis节点彼此互

redis主从集群搭建及容灾部署(哨兵sentinel)

Redis也用了一段时间了,记录一下相关集群搭建及配置详解,方便后续使用查阅. 提纲 l  Redis安装 l  整体架构 l  Redis主从结构搭建 l  Redis容灾部署(哨兵sentinel) l  Redis常见问题 Redis安装 发行版:CentOS-6.6 64bit 内核:2.6.32-504.el6.x86_64 CPU:intel-i7 3.6G 内存:2G 下载redis,选择合适的版本 [[email protected] software]# wget http:/

Redis Cluster集群搭建与应用

1.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper,但从redis 3.0之后版本支持redis-cluster集群,redis-cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接.其redis-cluster架构图如下: 其结构特点 所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. 节点的fail是通过集群中超过半数的节点检测失效时才生效. 客户端与redis节点直

redis centos集群搭建和java应用

1. 首先要ssh免密登录 redis集群,3台虚拟机,6个节点,每台机器2个节点一主一从. 192.168.132.154 c0192.168.132.156 c1192.168.132.155 c2 下载并解压文件,请到官网下载.文末有网盘下载地址. tar -zxvf redis-5.0.4.tar.gz 安装或更新C语言环境 yum install gcc-c++安装Redis,默认安装在当前目录下,可以使用PREFIX参数更改安装位置make install PREFIX=/home/

Redis Cluster集群搭建

1.服务器 192.168.1.201 192.168.1.204 192.168.1.205 192.168.1.206 192.168.1.207 192.168.1.208 因为Redis Cluster如果数据冗余是1的话,至少要3个Master和3个Slave. 2.安装步骤 (1)准备工作 mkdir /usr/local/redis_cluster mkdir /usr/local/redis_cluster (安装所需的文件) cd /usr/local/redis_cluste

redis(9)集群搭建

一.搭建流程 redis集群至少需要3个redis实例,不过官方推荐采用6个实例:3个master,3个slave. 那么我们需要做以下几件事: 1)编译一个redis-server 2)创建6个redis.conf配置文件 3)启动6个redis实例 4)redis-trib.rb创建集群 二.创建集群 1)编译一个redis-server $ wget http://download.redis.io/releases/redis-4.0.10.tar.gz $ tar xzf redis-

redis主从集群搭建

一.安装redis 首先登陆官网下载压缩包,我安装的是最新版本5.X,下载地址http://download.redis.io/releases/redis-5.0.2.tar.gz. 进入文件所在目录解压 tar -zxf redis-5.0.2.tar.gz 进入src目录,cd redis-5.0.2/src/ 编译,make && make install 这里因为只有一台服务器所以通过修改端口号的方式搭建伪集群 二.修改配置文件 cp redis.conf redis-6380.

redis伪集群搭建(亲测无坑)

一.单机版安装部署,伪集群只需要操作前1-8步即可,再往下浏览找到:二.单机版-伪集群 ?安装部署继续搭建,如需设置密码参照第10步 1.安装基本工具 yum install -y?gcc-c++ vim lrzsz wget? 2.创建.进入目录 mkdir /opt/redis cd /opt/redis 3.下载 wget http://download.redis.io/releases/redis-5.0.5.tar.gz 4.解压.查看文件 tar zxvf redis-5.0.5.