目录

安装配置redis的cluster集群

1. Redis cluster架构

2. 部署redis集群

2.1 环境准备

2.2 redis安装配置

2.3 创建redis cluster集群的前提

2.4 创建集群

2.5 验证Redis集群状态:

2.6 验证集群写入key

附 Redis 5 版本集群创建及验证命令

创建集群

查看集群状态


安装配置redis的cluster集群

Redis 分布式集群部署方案:
1) 客户端分区:由客户端程序决定key写分配和写入的redis node,但是需要客户端自己处理写入分配、高可用管理和故障转移等
2) 代理方案:基于三方软件实现redis proxy(codis等),客户端先连接之代理层,由代理层实现key的写入分配,对客户端来说是有比较简单,但是对于集群管节点增减相对比较麻烦,而且代理本身也是单点和性能瓶颈。

redis官方在redis 3.0版本之后推出了无中心架构的redis cluster机制,在无中心的redis集群当中,其每个节点保存当前节点数据和整个集群状态,每个节点都和其他所有节点连接,特点如下:
1)所有Redis节点使用(PING机制)互联
2)集群中某个节点的失效,是整个集群中超过半数的节点监测都失效才算真正的失效
3)客户端不需要proxy即可直接连接redis,应用程序需要写全部的redis服务器IP。
4)redis cluster把所有的redis node映射到 0-16383个槽位(slot)上,读写需要到指定的redis node上进行操作,因此有多少个reids node相当于redis 并发扩展了多少倍。
5)Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点上,从而有效解决单机瓶颈。

1. Redis cluster架构

假如三个主节点分别是:A, B, C 三个节点,采用哈希槽 (hash slot)的方式来分配16384个slot 的话,它们三个节点分别承担的slot 区间是:
节点A覆盖 0-5460
节点B覆盖 5461-10922
节点C覆盖 10923-16383
同时每个Redis master对应一个slave实现高可用

安装redis linux 安装redis cluster集群_运维

2. 部署redis集群

2.1 环境准备

服务器规划

IP地址                    redis版本          主从类型
172.18.200.101     redis-4.0.14      主
172.18.200.102     redis-4.0.14      主
172.18.200.103     redis-4.0.14      主
172.18.200.104     redis-4.0.14      从
172.18.200.105     redis-4.0.14      从
172.18.200.106     redis-4.0.14      从

2.2 redis安装配置

官方的安装命令:
https://redis.io/download

# pwd
/usr/local/src
# tar xf redis-4.0.14.tar.gz
# cd redis-4.0.14
# make PREFIX=/apps/redis install #指定redis安装目录
# mkdir /apps/redis/{etc,logs,data,run} #创建配置文件、日志、数据等目录
# cp redis.conf /apps/redis/etc/

编辑redis服务启动脚本:
cat /usr/lib/systemd/system/redis.service

[Unit]
 Description=Redis persistent key-value database
 After=network.target
 After=network-online.target
 Wants=network-online.target
 [Service]
 ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
 ExecReload=/bin/kill -s HUP $MAINPID
 ExecStop=/bin/kill -s QUIT $MAINPID
 Type=notify
 User=redis
 Group=redis
 RuntimeDirectory=redis
 RuntimeDirectoryMode=0755
 [Install]
 WantedBy=multi-user.target

 

redis部分配置

bind 0.0.0.0
protected-mode yes
port 6379
requirepass 123456
masterauth 123456  --设置masterauth,创建的集群主从关系才能建立,否则集群运行后主从建立失败,需要在每个slave控制台使用config set设置
pidfile /apps/redis/run/redis_6379.pid
logfile "/apps/redis/logs"
cluster-enabled yes
cluster-config-file nodes-6379.conf
......

配置完成后,压缩打包redis安装目录,连同启动脚本一起scp至其他服务器,完成其他服务器的部署

2.3 创建redis cluster集群的前提

1)每个redis node节点采用相同的硬件配置、相同的密码、相同的redis版本。
2)每个节点必须开启的参数
cluster-enabled yes #必须开启集群状态,开启后redis 进程会有cluster显示
cluster-config-file nodes-6380.conf #此文件有redis cluster集群自动创建和维护,不需要手动操作
3)所有redis服务器必须没有任何数据
4)先启动为单机redis且没有任何key value

2.4 创建集群

需要使用到集群管理工具redis-trib.rb,这个工具是redis官方推出的管理redis集群的工具,集成在redis的源码src目录下,redis-trib.rb是redis作者用ruby开发完成的,centos 系统yum安装的ruby存在版本较低问题,如下:

[root@s1 ~]# yum install ruby rubygems -y
 [root@s1 ~]# find / -name redis-trib.rb
 /usr/local/src/redis-4.0.14/src/redis-trib.rb
 [root@s1 ~]# cp /usr/local/src/redis-4.0.14/src/redis-trib.rb /usr/bin/
 [root@s1 src]# gem install redis    --ruby创建redis集群需安装redis模块
 Fetching: redis-4.1.2.gem (100%)
 ERROR: Error installing redis:
 redis requires Ruby version >= 2.3.0.

#解决ruby版本较低问题:

[root@s1 src]# yum remove ruby rubygems -y
 [root@s1 src]# wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
 [root@s1 src]# tar xf ruby-2.5.5.tar.gz
 [root@s1 src]# cd ruby-2.5.5
 [root@s1 ruby-2.5.5]# ./configure
 [root@s1 ruby-2.5.5]# make -j 2
 [root@s1 ruby-2.5.5]# make install
 [root@s1 ruby-2.5.5]# gem install redis

#如果无法在线安装,可以下载reids模块安装包离线安装
#https://rubygems.org/gems/redis #先下载redis模块安装包
#gem install -l redis-3.3.0.gem #安装redis模块

#修改创建集群时redis 登录密码

[root@s1ruby-2.5.4]# vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.0/lib/redis/client.rb

# 开始创建集群

[root@s1 ~]# redis-trib.rb create --replicas 1 172.18.200.101:6379 172.18.200.102:6379
172.18.200.103:6379 172.18.200.104:6379 172.18.200.105:6379 172.18.200.106:6379

安装redis linux 安装redis cluster集群_redis_02

如果有之前的操作导致Redis集群创建报错,则执行清空数据和集群命令:

127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> cluster reset
OK
127.0.0.1:6379> FLUSHALL

2.5 验证Redis集群状态:

查看master状态:

[root@redis-s1 ~]# redis-cli -h 192.168.7.101 -p 6379 -a 123456
 Warning: Using a password with '-a' or '-u' option on the command line interface may not
 be safe.
 192.168.7.101:6379> INFO Replication
 # Replication
 role:master
 connected_slaves:1
 slave0:ip=192.168.7.105,port=6379,state=online,offset=840,lag=0
 master_replid:0aa3281030eb29bf268f3317d4afe401f661a917
 master_replid2:0000000000000000000000000000000000000000
 master_repl_offset:840
 second_repl_offset:-1
 repl_backlog_active:1
 repl_backlog_size:4026531840
 repl_backlog_first_byte_offset:1
 repl_backlog_histlen:840
 192.168.7.101:6379>

查看集群状态:

192.168.7.101:6379> 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:6
 cluster_my_epoch:1
 cluster_stats_messages_ping_sent:1474
 cluster_stats_messages_pong_sent:1507
 cluster_stats_messages_sent:2981
 cluster_stats_messages_ping_received:1502
 cluster_stats_messages_pong_received:1474
 cluster_stats_messages_meet_received:5
 cluster_stats_messages_received:2981

查看集群node对应关系:

192.168.7.105:6379> cluster nodes
 7186c6d03dd9a5e3be658f2d08e800bc55b04a09 192.168.7.104:6380@16380 slave
 f4cfc5cf821c0d855016488d6fbfb62c03a14fda 0 1545659135000 4 connected
 7eda0bcf0c01bb7343313b81267c42dd1b26c8a6 192.168.7.105:6379@16380 myself,slave
 116c4c6de036fdbac5aaad25eb1a61ea262b64af 0 1545659135000 6 conne
 ctedf4cfc5cf821c0d855016488d6fbfb62c03a14fda 192.168.7.101:6379@16379 master - 0
 1545659135000 1 connected 0-5460
 116c4c6de036fdbac5aaad25eb1a61ea262b64af 192.168.7.102:6379@16379 master - 0
 1545659136000 3 connected 5461-10922
 70de3821dde4701c647bd6c23b9dd3c5c9f24a62 192.168.7.103:6379@16379 master - 0
 1545659134000 5 connected 10923-16383
 2b6e5d9c3944d79a5b64a19e54e52f83d48438d6 192.168.7.106:6379@16380 slave
 70de3821dde4701c647bd6c23b9dd3c5c9f24a62 0 1545659135946 5 connected

集群状态验证与监控

[root@redis-s1 ~]# redis-trib.rb check 172.18.200.105:6379
 >>> Performing Cluster Check (using node 172.18.200.105:6379)
 S: dfa53d634b3bd798e256ef9861579d5e637fc4b0 172.18.200.105:6379
    slots: (0 slots) slave
    replicates 99e09216d4ca5739788791da81a816bd2322802d
 M: 3ed26459bcdf4bbd3004c9a7506ba1f6e87dd55a 172.18.200.102:6379
    slots:5461-10922 (5462 slots) master
    1 additional replica(s)
 M: 99e09216d4ca5739788791da81a816bd2322802d 172.18.200.101:6379
    slots:0-5460 (5461 slots) master
    1 additional replica(s)
 S: 45e28f36573eb5123c27b359ae870e55a7d73017 172.18.200.104:6379
    slots: (0 slots) slave
    replicates 4d6357633a82f4ab48910d9c26dec8a2ef5b757c
 S: adc943d76aa9ef1b123319f0772e4322756f34a2 172.18.200.106:6379
    slots: (0 slots) slave
    replicates 3ed26459bcdf4bbd3004c9a7506ba1f6e87dd55a
 M: 4d6357633a82f4ab48910d9c26dec8a2ef5b757c 172.18.200.103:6379
    slots:10923-16383 (5461 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.[root@redis-s1 ~]# redis-trib.rb info 172.18.200.105:6379
 172.18.200.102:6379 (3ed26459...) -> 1 keys | 5462 slots | 1 slaves.
 172.18.200.101:6379 (99e09216...) -> 0 keys | 5461 slots | 1 slaves.
 172.18.200.103:6379 (4d635763...) -> 0 keys | 5461 slots | 1 slaves.
 [OK] 1 keys in 3 masters.
 0.00 keys per slot on average.

2.6 验证集群写入key

192.168.7.101:6379> SET key1 value1 #经过算法计算,当前key的槽位需要写入指定的node
 (error) MOVED 9189 192.168.7.102:6379 #槽位不在当前node所以无法写入
 192.168.7.103:6379> SET key1 value1
 (error) MOVED 9189 192.168.7.102:6379
 192.168.7.102:6379> SET key1 value1 #指定的node就可以写入
 OK
 192.168.7.102:6379> KEYS *
 1) "key1"192.168.7.101:6379> KEYS *
 (empty list or set)
 192.168.7.103:6379> KEYS *
 (empty list or set)

附 Redis 5 版本集群创建及验证命令

创建集群

[root@redis-s1 ~]# redis-cli -a 123456 --cluster create 192.168.7.101:6379
 192.168.7.101:6380 192.168.7.102:6379 192.168.7.102:6380 1
 92.168.7.103:6379 192.168.7.103:6380 --cluster-replicas 1
 Warning: Using a password with '-a' or '-u' option on the command line interface may not
 be safe.
 >>> Performing hash slots allocation on 6 nodes...
 Master[0] -> Slots 0 - 5460
 Master[1] -> Slots 5461 - 10922
 Master[2] -> Slots 10923 - 16383
 Adding replica 192.168.7.102:6380 to 192.168.7.101:6379
 Adding replica 192.168.7.101:6380 to 192.168.7.102:6379
 Adding replica 192.168.7.103:6380 to 192.168.7.103:6379
 >>> Trying to optimize slaves allocation for anti-affinity
 [OK] Perfect anti-affinity obtained!
 M: f4cfc5cf821c0d855016488d6fbfb62c03a14fda 192.168.7.101:6379 #带M的为master
 slots:[0-5460] (5461 slots) master #当前master的槽位起始和结束位
 S: 2b6e5d9c3944d79a5b64a19e54e52f83d48438d6 192.168.7.101:6380 #带S的slave
 replicates 70de3821dde4701c647bd6c23b9dd3c5c9f24a62
 M: 116c4c6de036fdbac5aaad25eb1a61ea262b64af 192.168.7.102:6379
 slots:[5461-10922] (5462 slots) master #当前master的槽位起始和结束位
 S: 7186c6d03dd9a5e3be658f2d08e800bc55b04a09 192.168.7.102:6380
 replicates f4cfc5cf821c0d855016488d6fbfb62c03a14fda
 M: 70de3821dde4701c647bd6c23b9dd3c5c9f24a62 192.168.7.103:6379
 slots:[10923-16383] (5461 slots) master #当前master的槽位起始和结束位
 S: 7eda0bcf0c01bb7343313b81267c42dd1b26c8a6 192.168.7.103:6380
 replicates 116c4c6de036fdbac5aaad25eb1a61ea262b64af
 Can I set the above configuration? (type 'yes' to accept): yes #输入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 192.168.7.101:6379)
 M: f4cfc5cf821c0d855016488d6fbfb62c03a14fda 192.168.7.101:6379 #master的ID及端口
    slots:[0-5460] (5461 slots) master #已经分配的槽位
    1 additional replica(s) #分配了一个slave
 S: 7186c6d03dd9a5e3be658f2d08e800bc55b04a09 192.168.7.102:6380
    slots: (0 slots) slave #slave没有分配槽位
    replicates f4cfc5cf821c0d855016488d6fbfb62c03a14fda
 M: 70de3821dde4701c647bd6c23b9dd3c5c9f24a62 192.168.7.103:6379
    slots:[10923-16383] (5461 slots) master
    1 additional replica(s)
 M: 116c4c6de036fdbac5aaad25eb1a61ea262b64af 192.168.7.102:6379
    slots:[5461-10922] (5462 slots) master
    1 additional replica(s)
 S: 2b6e5d9c3944d79a5b64a19e54e52f83d48438d6 192.168.7.101:6380
    slots: (0 slots) slave
    replicates 70de3821dde4701c647bd6c23b9dd3c5c9f24a62
 S: 7eda0bcf0c01bb7343313b81267c42dd1b26c8a6 192.168.7.103:6380
    slots: (0 slots) slave
    replicates 116c4c6de036fdbac5aaad25eb1a61ea262b64af
 [OK] All nodes agree about slots configuration. #所有节点槽位分配完成
 >>> Check for open slots... #检查打开的槽位
 >>> Check slots coverage... #检查插槽覆盖范围
 [OK] All 16384 slots covered. #所有槽位(16384个)分配完成

查看集群状态

# redis-cli -a 123456 --cluster check 192.168.7.101:6379

安装redis linux 安装redis cluster集群_redis_03