本文最后更新于 257 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com
Redis的分布式集群(Cluster)架构部署实战
一.Redis Cluster实验架构说明
如下图所示,在搭建集群时,会为每一个分片的主节点,对应一个从节点,实现slaveof的功能,同时当主节点down,实现类似于sentinel的自动failover的功能。
Redis数据分片原理说明:
(1)redis会有多组分片构成,本篇笔记实验环境是3组;
(2)redis cluster使用固定个数的slot存储数据,一共16384是slot;
(3)每组分片分得1/3 slot个数,分别为"0-5460,5461-10921,10922-16383"
(4)基于CRC16(key) % 16384公式会得到一个"槽位号"(该编号范围在"0-16383"之间);
(5)根据计算得出的槽位值,找到相对应的分片节点的主节点,存储到相应槽位上;
(6)如果客户端当时连接的节点不是将来要存储的分片节点,分片集群会将客户端连接切换至真正存储节点进行数据存储;
综上所述,我们来举个例子,在Redis分布式集群中,假设我们执行了"SET name JasonYin"这一条指令,那么数据该如何存储的呢?其大致的存储逻辑如下:
(1)使用"CRC16(KEY_NAME)"进行校验计算一个值,假设"CRC16(name)"的计算结果为"1234567890";
(2)再使用"CRC16(KEY_NAME)%16384"进行计算,从而得出该KEY_NAME被分配到哪个slot上,假设结果为"1234567890%16384=722",就表示数据被落到slot=722的分片上;
(3)而slot=722恰好在第一个分片上,因为我们第一个分片的slot范围就是"0-5460"
二.Redis Cluster部署实战
1.实验环境说明
在生产环境中,如果部署6个redis实例,一般会放到3台硬件服务器。在企业规划中,一个分片被分配到两个不同的物理机,防止硬件主机宕机造成的整个分片数据丢失。
温馨提示:
本案例为了实验方便,就直接将6个Redis实例放在同一个节点上,对应的端口号为:63790-63795。
2.安装ruby,rubygems软件,这个在EPEL软件源中就有支持哟~
[root@redis201.oldboyedu.com ~]# yum -y install ruby rubygems
温馨提示:
总所周知,Redis已经更新到6版本了,但目前为止,Redis 3依旧是主流,原因之一是生产环境中Redis 3基本上能满足需求无需升级版本外,还有一个重要的原因就是Redis 3集群模式有开源的组件支撑。
rubygems是Ruby社区的Gem托管服务。
3.切换"ruby"国内的软件源,目的是为了提速,gem是ruby语言的包管理工具,类似于python语言的pip。
[root@redis201.oldboyedu.com ~]# gem source -l
*** CURRENT SOURCES ***
https://rubygems.org/
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# gem source -a http://mirrors.aliyun.com/rubygems/
http://mirrors.aliyun.com/rubygems/ added to sources
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# gem source -l
*** CURRENT SOURCES ***
https://rubygems.org/
http://mirrors.aliyun.com/rubygems/
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# gem source --remove https://rubygems.org/
https://rubygems.org/ removed from sources
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# gem source -l
*** CURRENT SOURCES ***
http://mirrors.aliyun.com/rubygems/
[root@redis201.oldboyedu.com ~]#
4.ruby语言安装redis驱动插件
[root@redis201.oldboyedu.com ~]# gem install redis -v 3.3.3
Fetching: redis-3.3.3.gem (100%)
Successfully installed redis-3.3.3
Parsing documentation for redis-3.3.3
Installing ri documentation for redis-3.3.3
1 gem installed
[root@redis201.oldboyedu.com ~]#
5.创建多Redis实例使用的配置文件,数据,日志目录
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/softwares/redis1000{0..5}
mkdir: 已创建目录 "/oldboyedu/softwares/redis10000"
mkdir: 已创建目录 "/oldboyedu/softwares/redis10001"
mkdir: 已创建目录 "/oldboyedu/softwares/redis10002"
mkdir: 已创建目录 "/oldboyedu/softwares/redis10003"
mkdir: 已创建目录 "/oldboyedu/softwares/redis10004"
mkdir: 已创建目录 "/oldboyedu/softwares/redis10005"
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/data/redis1000{0..5}
mkdir: 已创建目录 "/oldboyedu/data/redis10000"
mkdir: 已创建目录 "/oldboyedu/data/redis10001"
mkdir: 已创建目录 "/oldboyedu/data/redis10002"
mkdir: 已创建目录 "/oldboyedu/data/redis10003"
mkdir: 已创建目录 "/oldboyedu/data/redis10004"
mkdir: 已创建目录 "/oldboyedu/data/redis10005"
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/logs/redis1000{0..5}
mkdir: 已创建目录 "/oldboyedu/logs/redis10000"
mkdir: 已创建目录 "/oldboyedu/logs/redis10001"
mkdir: 已创建目录 "/oldboyedu/logs/redis10002"
mkdir: 已创建目录 "/oldboyedu/logs/redis10003"
mkdir: 已创建目录 "/oldboyedu/logs/redis10004"
mkdir: 已创建目录 "/oldboyedu/logs/redis10005"
[root@redis201.oldboyedu.com ~]#
6.为各个Redis实例创建配置文件
(1)创建"redis10000"实例的配置文件
[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis10000/redis.conf <<EOF
> port 10000
> daemonize yes
> pidfile /oldboyedu/data/redis10000/redis.pid
> loglevel notice
> logfile /oldboyedu/logs/redis10000/redis.log
> dbfilename dump.rdb
> dir /oldboyedu/data/redis10000
> bind 172.200.1.201 127.0.0.1
> # 配置RDB持久化策略
> save 900 1
> save 300 10
> save 60 10000
> # 配置AOF持久化
> appendonly yes
> appendfsync everysec
> # 配置Redis Cluster模式
> protected-mode no
> cluster-enabled yes
> cluster-config-file nodes.conf
> cluster-node-timeout 5000
> EOF
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis10000/redis.conf
port 10000
daemonize yes
pidfile /oldboyedu/data/redis10000/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10000/redis.log
dbfilename dump.rdb
dir /oldboyedu/data/redis10000
bind 172.200.1.201 127.0.0.1
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
[root@redis201.oldboyedu.com ~]#
(2)创建"redis10001"实例的配置文件
[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis10001/redis.conf <<EOF
> port 10001
> daemonize yes
> pidfile /oldboyedu/data/redis10001/redis.pid
> loglevel notice
> logfile /oldboyedu/logs/redis10001/redis.log
> dbfilename dump.rdb
> dir /oldboyedu/data/redis10001
> bind 172.200.1.201 127.0.0.1
> # 配置RDB持久化策略
> save 900 1
> save 300 10
> save 60 10000
> # 配置AOF持久化
> appendonly yes
> appendfsync everysec
> # 配置Redis Cluster模式
> protected-mode no
> cluster-enabled yes
> cluster-config-file nodes.conf
> cluster-node-timeout 5000
> EOF
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis10001/redis.conf
port 10001
daemonize yes
pidfile /oldboyedu/data/redis10001/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10001/redis.log
dbfilename dump.rdb
dir /oldboyedu/data/redis10001
bind 172.200.1.201 127.0.0.1
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
[root@redis201.oldboyedu.com ~]#
(3)创建"redis10002"实例的配置文件
[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis10002/redis.conf <<EOF
> port 10002
> daemonize yes
> pidfile /oldboyedu/data/redis10002/redis.pid
> loglevel notice
> logfile /oldboyedu/logs/redis10002/redis.log
> dbfilename dump.rdb
> dir /oldboyedu/data/redis10002
> bind 172.200.1.201 127.0.0.1
> # 配置RDB持久化策略
> save 900 1
> save 300 10
> save 60 10000
> # 配置AOF持久化
> appendonly yes
> appendfsync everysec
> # 配置Redis Cluster模式
> protected-mode no
> cluster-enabled yes
> cluster-config-file nodes.conf
> cluster-node-timeout 5000
> EOF
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis10002/redis.conf
port 10002
daemonize yes
pidfile /oldboyedu/data/redis10002/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10002/redis.log
dbfilename dump.rdb
dir /oldboyedu/data/redis10002
bind 172.200.1.201 127.0.0.1
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
[root@redis201.oldboyedu.com ~]#
(4)创建"redis10003"实例的配置文件
[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis10003/redis.conf <<EOF
> port 10003
> daemonize yes
> pidfile /oldboyedu/data/redis10003/redis.pid
> loglevel notice
> logfile /oldboyedu/logs/redis10003/redis.log
> dbfilename dump.rdb
> dir /oldboyedu/data/redis10003
> bind 172.200.1.201 127.0.0.1
> # 配置RDB持久化策略
> save 900 1
> save 300 10
> save 60 10000
> # 配置AOF持久化
> appendonly yes
> appendfsync everysec
> # 配置Redis Cluster模式
> protected-mode no
> cluster-enabled yes
> cluster-config-file nodes.conf
> cluster-node-timeout 5000
> EOF
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis10003/redis.conf
port 10003
daemonize yes
pidfile /oldboyedu/data/redis10003/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10003/redis.log
dbfilename dump.rdb
dir /oldboyedu/data/redis10003
bind 172.200.1.201 127.0.0.1
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
[root@redis201.oldboyedu.com ~]#
(5)创建"redis10004"实例的配置文件
[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis10004/redis.conf <<EOF
> port 10004
> daemonize yes
> pidfile /oldboyedu/data/redis10004/redis.pid
> loglevel notice
> logfile /oldboyedu/logs/redis10004/redis.log
> dbfilename dump.rdb
> dir /oldboyedu/data/redis10004
> bind 172.200.1.201 127.0.0.1
> # 配置RDB持久化策略
> save 900 1
> save 300 10
> save 60 10000
> # 配置AOF持久化
> appendonly yes
> appendfsync everysec
> # 配置Redis Cluster模式
> protected-mode no
> cluster-enabled yes
> cluster-config-file nodes.conf
> cluster-node-timeout 5000
> EOF
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis10004/redis.conf
port 10004
daemonize yes
pidfile /oldboyedu/data/redis10004/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10004/redis.log
dbfilename dump.rdb
dir /oldboyedu/data/redis10004
bind 172.200.1.201 127.0.0.1
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
[root@redis201.oldboyedu.com ~]#
(6)创建"redis10005"实例的配置文件
[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis10005/redis.conf <<EOF
> port 10005
> daemonize yes
> pidfile /oldboyedu/data/redis10005/redis.pid
> loglevel notice
> logfile /oldboyedu/logs/redis10005/redis.log
> dbfilename dump.rdb
> dir /oldboyedu/data/redis10005
> bind 172.200.1.201 127.0.0.1
> # 配置RDB持久化策略
> save 900 1
> save 300 10
> save 60 10000
> # 配置AOF持久化
> appendonly yes
> appendfsync everysec
> # 配置Redis Cluster模式
> protected-mode no
> cluster-enabled yes
> cluster-config-file nodes.conf
> cluster-node-timeout 5000
> EOF
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis10005/redis.conf
port 10005
daemonize yes
pidfile /oldboyedu/data/redis10005/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10005/redis.log
dbfilename dump.rdb
dir /oldboyedu/data/redis10005
bind 172.200.1.201 127.0.0.1
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
[root@redis201.oldboyedu.com ~]#
配置参数说明:
cluster-enabled yes:
开启集群功能。
cluster-config-file nodes.conf:
指定集群的配置文件,无需修改,这是由redis自行维护。
cluster-node-timeout 5000:
指定集群节点的超时时间为5000毫秒,即5秒钟。
温馨提示:
(1)Redis的端口号范围在10000~55535之间。这是因为当我们启动各个redis实例后,会监听在对应redis实例的端口上加10000;
(2)由于上面我使用了"protected-mode no"指令,表示不开启安全模式,如果设置为"YES",尽管配置了"requirepass"和"masterauth"这两个配置项,我们在将redis节点加入到集群时依旧会报错哟!
(3)Redis cluster模式无需像redis sentinel模式那样有一个专门的监控模式;
7.启动各个Redis实例
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis10000/redis.conf
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis10001/redis.conf
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis10002/redis.conf
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis10003/redis.conf
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis10004/redis.conf
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis10005/redis.conf
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# netstat -untalp | grep redis
tcp 0 0 127.0.0.1:10000 0.0.0.0:* LISTEN 48987/redis-server
tcp 0 0 172.200.1.201:10000 0.0.0.0:* LISTEN 48987/redis-server
tcp 0 0 127.0.0.1:10001 0.0.0.0:* LISTEN 48905/redis-server
tcp 0 0 172.200.1.201:10001 0.0.0.0:* LISTEN 48905/redis-server
tcp 0 0 127.0.0.1:10002 0.0.0.0:* LISTEN 49069/redis-server
tcp 0 0 172.200.1.201:10002 0.0.0.0:* LISTEN 49069/redis-server
tcp 0 0 127.0.0.1:10003 0.0.0.0:* LISTEN 49151/redis-server
tcp 0 0 172.200.1.201:10003 0.0.0.0:* LISTEN 49151/redis-server
tcp 0 0 127.0.0.1:10004 0.0.0.0:* LISTEN 49233/redis-server
tcp 0 0 172.200.1.201:10004 0.0.0.0:* LISTEN 49233/redis-server
tcp 0 0 127.0.0.1:10005 0.0.0.0:* LISTEN 49315/redis-server
tcp 0 0 172.200.1.201:10005 0.0.0.0:* LISTEN 49315/redis-server
tcp 0 0 127.0.0.1:20000 0.0.0.0:* LISTEN 48987/redis-server
tcp 0 0 172.200.1.201:20000 0.0.0.0:* LISTEN 48987/redis-server
tcp 0 0 127.0.0.1:20001 0.0.0.0:* LISTEN 48905/redis-server
tcp 0 0 172.200.1.201:20001 0.0.0.0:* LISTEN 48905/redis-server
tcp 0 0 127.0.0.1:20002 0.0.0.0:* LISTEN 49069/redis-server
tcp 0 0 172.200.1.201:20002 0.0.0.0:* LISTEN 49069/redis-server
tcp 0 0 127.0.0.1:20003 0.0.0.0:* LISTEN 49151/redis-server
tcp 0 0 172.200.1.201:20003 0.0.0.0:* LISTEN 49151/redis-server
tcp 0 0 127.0.0.1:20004 0.0.0.0:* LISTEN 49233/redis-server
tcp 0 0 172.200.1.201:20004 0.0.0.0:* LISTEN 49233/redis-server
tcp 0 0 127.0.0.1:20005 0.0.0.0:* LISTEN 49315/redis-server
tcp 0 0 172.200.1.201:20005 0.0.0.0:* LISTEN 49315/redis-server
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# ps -ef | grep redis | grep -v "grep"
root 50114 1 0 22:46 ? 00:00:00 redis-server 172.200.1.201:10000 [cluster]
root 50196 1 0 22:46 ? 00:00:00 redis-server 172.200.1.201:10001 [cluster]
root 50278 1 0 22:46 ? 00:00:00 redis-server 172.200.1.201:10002 [cluster]
root 50360 1 0 22:46 ? 00:00:00 redis-server 172.200.1.201:10003 [cluster]
root 50442 1 0 22:46 ? 00:00:00 redis-server 172.200.1.201:10004 [cluster]
root 50563 1 0 22:46 ? 00:00:00 redis-server 172.200.1.201:10005 [cluster]
[root@redis201.oldboyedu.com ~]#
8.将节点加入集群管理
[root@redis201.oldboyedu.com ~]# redis-trib.rb create --replicas 1 172.200.1.201:10000 172.200.1.201:10001 172.200.1.201:10002 172.200.1.201:10003 172.200.1.201:10004 172.200.1.201:10005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.200.1.201:10000
172.200.1.201:10001
172.200.1.201:10002
Adding replica 172.200.1.201:10003 to 172.200.1.201:10000
Adding replica 172.200.1.201:10004 to 172.200.1.201:10001
Adding replica 172.200.1.201:10005 to 172.200.1.201:10002
M: 88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000
slots:0-5460 (5461 slots) master
M: c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001
slots:5461-10922 (5462 slots) master
M: c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002
slots:10923-16383 (5461 slots) master
S: 022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003
replicates 88e97940a7371296fb3d51589440dc43ea256599
S: a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004
replicates c907ac66c16daa720735e89addc5a854c3c40a0f
S: d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005
replicates c1146f293fca9207a60358b849be8ae6af7c6e2f
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.200.1.201:10000)
M: 88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004
slots: (0 slots) slave
replicates c907ac66c16daa720735e89addc5a854c3c40a0f
M: c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003
slots: (0 slots) slave
replicates 88e97940a7371296fb3d51589440dc43ea256599
S: d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005
slots: (0 slots) slave
replicates c1146f293fca9207a60358b849be8ae6af7c6e2f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis201.oldboyedu.com ~]#
参数说明:
(1)我们使用"--replicas 1"指定了集群的replica节点(也就是slave节点)数量是1,即一个master节点对应一个replica节点;
(2)在执行上述命令操作时,会弹出一行交互式界面,输出"(type 'yes' to accept):",我们手动输入"yes"即可;
温馨提示:
根据上面输出的结果也不难发现,我手动输入6个redis实例,但"redis-trib.rb"工具默认会将前3个设置为master节点,将后3个设置为slave节点。
综上所述,生产环境中我们在规划集群时,千万不要把"同一组主从"放在同一个物理机上,避免因为物理机器宕机而导致整个分片数据丢失!
9.查看集群的状态
[root@redis201.oldboyedu.com ~]# redis-cli -p 10005 CLUSTER NODES
88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000 master - 0 1614869987763 1 connected 0-5460
c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002 master - 0 1614869987260 3 connected 10923-16383
022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003 slave 88e97940a7371296fb3d51589440dc43ea256599 0 1614869986757 4 connected
c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001 master - 0 1614869986253 2 connected 5461-10922
a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004 slave c907ac66c16daa720735e89addc5a854c3c40a0f 0 1614869988268 5 connected
d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005 myself,slave c1146f293fca9207a60358b849be8ae6af7c6e2f 0 0 6 connected
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 10005 CLUSTER NODES | grep master # 只查看Master节点
88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000 master - 0 1614870017001 1 connected 0-5460
c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002 master - 0 1614870016497 3 connected 10923-16383
c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001 master - 0 1614870016497 2 connected 5461-10922
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 10005 CLUSTER NODES | grep slave # 只查看Slave节点
022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003 slave 88e97940a7371296fb3d51589440dc43ea256599 0 1614870019521 4 connected
a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004 slave c907ac66c16daa720735e89addc5a854c3c40a0f 0 1614870017506 5 connected
d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005 myself,slave c1146f293fca9207a60358b849be8ae6af7c6e2f 0 0 6 connected
[root@redis201.oldboyedu.com ~]#
每项的含义如下:
id:
节点ID,是一个40字节的随机字符串,这个值在节点启动的时候创建,并且永远不会改变(除非使用CLUSTER RESET HARD命令)。
ip:port:
客户端与节点通信使用的地址.
flags:
逗号分割的标记位,可能的值有: myself, master, slave, fail?, fail, handshake, noaddr, noflags. 下一部分将详细介绍这些标记.
master:
如果节点是slave,并且已知master节点,则这里列出master节点ID,否则的话这里列出”-“。
ping-sent:
最近一次发送ping的时间,这个时间是一个unix毫秒时间戳,0代表没有发送过.
pong-recv:
最近一次收到pong的时间,使用unix时间戳表示.
config-epoch:
节点的epoch值(or of the current master if the node is a slave)。每当节点发生失败切换时,都会创建一个新的,独特的,递增的epoch。如果多个节点竞争同一个哈希槽时,epoch值更高的节点会抢夺到。
link-state:
node-to-node集群总线使用的链接的状态,我们使用这个链接与集群中其他节点进行通信.值可以是 connected 和 disconnected.
slot:
哈希槽值或者一个哈希槽范围. 从第9个参数开始,后面最多可能有16384个 数(limit never reached)。代表当前节点可以提供服务的所有哈希槽值。如果只是一个值,那就是只有一个槽会被使用。如果是一个范围,这个值表示为起始槽-结束槽,节点将处理包括起始槽和结束槽在内的所有哈希槽。
各flags的含义 (上面所说数据项3):
myself:
当前连接的节点.
master:
节点是master.
slave:
节点是slave.
fail?:
节点处于PFAIL 状态。 当前节点无法联系,但逻辑上是可达的 (非 FAIL 状态).
fail:
节点处于FAIL 状态. 大部分节点都无法与其取得联系将会将改节点由 PFAIL 状态升级至FAIL状态。
handshake:
还未取得信任的节点,当前正在与其进行握手.
noaddr:
没有地址的节点(No address known for this node).
noflags:
连个标记都没有(No flags at all).
参考链接:
http://www.redis.cn/commands/cluster-nodes.html
温馨提示:
(1)如果有"myself"字样,说明你正在连接的是哪个节点;
(2)注意观察槽位编号信息,比如"172.200.1.201:10000 master"被分配到的槽位号是"0-5460",而"172.200.1.201:10001"被分配到的槽位号是"5461-10922","172.200.1.201:10002 master"被分配到的槽位号是"10923-16383";
(3)每个redis实例都有其独有的id编号,比如"172.200.1.201:10000 master"的编号为"88e97940a7371296fb3d51589440dc43ea256599",这有点类似于MySQL的"server_uuid";
三.跨节点集群配置
一.所有节点操作
(1)安装ruby环境
yum -y install ruby rubygems
(2)切换软件源
gem source -l
gem source -a http://mirrors.aliyun.com/rubygems/
gem source --remove https://rubygems.org/
gem source -l
(3)安装redis的驱动
gem install redis -v 3.3.3
二.配置redis cluster
(1)集群规划
10.0.0.106:
10601
10602
10.0.0.107:
10701
10702
10.0.0.108:
10801
10802
(2)10.0.0.106创建配置文件
install -d /oldboyedu/{data,logs}/{redis10601,redis10602}
cat > /oldboyedu/softwares/redis/redis10601.conf <<EOF
port 10601
daemonize yes
pidfile /oldboyedu/data/redis10601/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10601/redis.log
dbfilename oldboyedu_linux.rdb
dir /oldboyedu/data/redis10601
bind 10.0.0.106
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
EOF
cat > /oldboyedu/softwares/redis/redis10602.conf <<EOF
port 10602
daemonize yes
pidfile /oldboyedu/data/redis10602/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10602/redis.log
dbfilename oldboyedu_linux.rdb
dir /oldboyedu/data/redis10602
bind 10.0.0.106
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
EOF
(2)10.0.0.107创建配置文件
install -d /oldboyedu/{data,logs}/{redis10701,redis10702}
cat > /oldboyedu/softwares/redis/redis10701.conf <<EOF
port 10701
daemonize yes
pidfile /oldboyedu/data/redis10701/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10701/redis.log
dbfilename oldboyedu_linux.rdb
dir /oldboyedu/data/redis10701
bind 10.0.0.107
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
EOF
cat > /oldboyedu/softwares/redis/redis10702.conf <<EOF
port 10702
daemonize yes
pidfile /oldboyedu/data/redis10702/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10702/redis.log
dbfilename oldboyedu_linux.rdb
dir /oldboyedu/data/redis10702
bind 10.0.0.107
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
EOF
(2)10.0.0.108创建配置文件
install -d /oldboyedu/{data,logs}/{redis10801,redis10802}
cat > /oldboyedu/softwares/redis/redis10801.conf <<EOF
port 10801
daemonize yes
pidfile /oldboyedu/data/redis10801/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10801/redis.log
dbfilename oldboyedu_linux.rdb
dir /oldboyedu/data/redis10801
bind 10.0.0.108
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
EOF
cat > /oldboyedu/softwares/redis/redis10802.conf <<EOF
port 10802
daemonize yes
pidfile /oldboyedu/data/redis10802/redis.pid
loglevel notice
logfile /oldboyedu/logs/redis10802/redis.log
dbfilename oldboyedu_linux.rdb
dir /oldboyedu/data/redis10802
bind 10.0.0.108
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置Redis Cluster模式
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
EOF
三.启动服务并测试
(1)10.0.0.106
redis-server /oldboyedu/softwares/redis/redis10601.conf
redis-server /oldboyedu/softwares/redis/redis10602.conf
(2)10.0.0.107
redis-server /oldboyedu/softwares/redis/redis10701.conf
redis-server /oldboyedu/softwares/redis/redis10702.conf
(1)10.0.0.108
redis-server /oldboyedu/softwares/redis/redis10801.conf
redis-server /oldboyedu/softwares/redis/redis10802.conf
四.配置redis cluster集群
(1)将redis实例加入集群
redis-trib.rb create --replicas 1 10.0.0.106:10601 10.0.0.106:10602 10.0.0.107:10701 10.0.0.107:10702 10.0.0.108:10801 10.0.0.108:10802
(2)查看集群状态
redis-cli -h 10.0.0.107 -p 10701 CLUSTER NODES
五.测试
略!
作业:
调研"redis codis"集群部署!
四.可能会遇到的错误
1.[ERR] Sorry, can't connect to node 127.0.0.1:10001
错误原因:
无法连接到集群,很有可能是防火墙未关闭,如果使用redis-cli命令可以正常连接。还有另外一个可能,即配置了redis密码。
解决方案:
将"protected-mode"的设置为no,即"protected-mode no"。
2.zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
解决方案:
make MALLOC=libc