Redis Cluster集群搭建

NoSQL笔记 2020年05月26日

本篇笔记记录了在linux的CentOS7发行版下,使用Redis Cluster搭建Redis集群的过程

工作准备

Redis节点

192.168.75.240:6379	192.168.75.240:6380
192.168.75.241:6379	192.168.75.241:6380
192.168.75.242:6379	192.168.75.242:6380

系统
CentOS7.8
selinux已关闭
firewalld已关闭
Redis
5.0.5
注释:

  • 节点为什么至少是6个?因为一个Redis集群至少包括3主3从,奇数是为了选举。
  • firewalld不关闭的话,需要开启Redis cluster端口=Redis端口+10000,集群管理端口总是redis端口+10000,如以上节点集群管理端口号就是16379和16380

安装Redis,3台服务器都要安装

创建源码保存目录

mkdir -p /usr/local/src
cd /usr/local/src

下载Redis

wget http://download.redis.io/releases/redis-5.0.5.tar.gz

安装编译器

yum -y install gcc gcc-c++

解压

tar -zxvf redis-5.0.5.tar.gz
cd redis-5.0.5

编译,安装

make MALLOC=libc
make PREFIX=/usr/local/redis install

设置环境变量

vim /etc/profile

结尾处添加

export PATH=$PATH:/usr/local/redis/bin

立即生效

source /etc/profile

进入安装目录

cd /usr/local/redis

各服务器按端口号创建Redis工作目录

mkdir -p /usr/local/redis/6379
mkdir -p /usr/local/redis/6380

各服务器复制安装包下配置文件到安装目录

cp /usr/local/src/redis-5.0.5/redis.conf /usr/local/redis/redis_6379.conf
cp /usr/local/src/redis-5.0.5/redis.conf /usr/local/redis/redis_6380.conf

各服务器修改配置文件
redis_6379.conf

bind 0.0.0.0
protected-mode no
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
logfile "/var/log/redis/6379.log"
dir /usr/local/redis/6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000

redis_6380.conf

bind 0.0.0.0
protected-mode no
port 6380
daemonize yes
pidfile /var/run/redis_6380.pid
logfile "/var/log/redis/6380.log"
dir /usr/local/redis/6380
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000

各服务器启动Redis

redis-server /usr/local/redis/redis_6379.conf
redis-server /usr/local/redis/redis_6380.conf

查看log

[root@localhost redis]# tail -f /var/log/redis/6379.log 
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

......
^C
[root@localhost redis]# tail -f /var/log/redis/6380.log 
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

......

创建集群,在其中一台服务器执行(旧版本可能需要安装ruby,具体请看官方文档)

redis-cli --cluster create --cluster-replicas 1 192.168.75.240:6379 192.168.75.240:6380 192.168.75.241:6379 192.168.75.241:6380 192.168.75.242:6379 192.168.75.242:6380

执行结果

[root@localhost redis]# redis-cli --cluster create --cluster-replicas 1 192.168.75.240:6379 192.168.75.240:6380 192.168.75.241:6379 192.168.75.241:6380 192.168.75.242:6379 192.168.75.242:6380
>>> 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.75.241:6380 to 192.168.75.240:6379
Adding replica 192.168.75.242:6380 to 192.168.75.241:6379
Adding replica 192.168.75.240:6380 to 192.168.75.242:6379
M: b16f676efe8d7998bfe56386434895ced77e79e7 192.168.75.240:6379
   slots:[0-5460] (5461 slots) master
S: c36e8e3175f53cccdf8640424192593c230ebd89 192.168.75.240:6380
   replicates afad77c65770a962e53b89b275578a5ef382f039
M: 19f46bcef63286be0ebbcfd2acb9beae31f471d4 192.168.75.241:6379
   slots:[5461-10922] (5462 slots) master
S: 726b7ba73ff0e4849a13fe1612ebc952d1c8786c 192.168.75.241:6380
   replicates b16f676efe8d7998bfe56386434895ced77e79e7
M: afad77c65770a962e53b89b275578a5ef382f039 192.168.75.242:6379
   slots:[10923-16383] (5461 slots) master
S: 096e85ce032914c9c0f3107b6d3c49e612a57ab7 192.168.75.242:6380
   replicates 19f46bcef63286be0ebbcfd2acb9beae31f471d4
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.75.240:6379)
M: b16f676efe8d7998bfe56386434895ced77e79e7 192.168.75.240:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: c36e8e3175f53cccdf8640424192593c230ebd89 192.168.75.240:6380
   slots: (0 slots) slave
   replicates afad77c65770a962e53b89b275578a5ef382f039
M: afad77c65770a962e53b89b275578a5ef382f039 192.168.75.242:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 19f46bcef63286be0ebbcfd2acb9beae31f471d4 192.168.75.241:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 096e85ce032914c9c0f3107b6d3c49e612a57ab7 192.168.75.242:6380
   slots: (0 slots) slave
   replicates 19f46bcef63286be0ebbcfd2acb9beae31f471d4
S: 726b7ba73ff0e4849a13fe1612ebc952d1c8786c 192.168.75.241:6380
   slots: (0 slots) slave
   replicates b16f676efe8d7998bfe56386434895ced77e79e7
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis]# 

创建命令中--cluster-replicas 1表示每个主节点对应一个从节点
我们看到集群为我们创建了三个主节点,槽位信息如下

Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383

登录集群

任一节点执行命令

redis-cli -c -h 192.168.75.240 -p 6379

-c,使用集群方式登录
查看集群信息

192.168.75.240: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:561
cluster_stats_messages_pong_sent:556
cluster_stats_messages_sent:1117
cluster_stats_messages_ping_received:551
cluster_stats_messages_pong_received:561
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1117

查看节点信息

192.168.75.240:6379> CLUSTER NODES
c36e8e3175f53cccdf8640424192593c230ebd89 192.168.75.240:6380@16380 slave afad77c65770a962e53b89b275578a5ef382f039 0 1590241662000 5 connected
b16f676efe8d7998bfe56386434895ced77e79e7 192.168.75.240:6379@16379 myself,master - 0 1590241665000 1 connected 0-5460
afad77c65770a962e53b89b275578a5ef382f039 192.168.75.242:6379@16379 master - 0 1590241664764 5 connected 10923-16383
19f46bcef63286be0ebbcfd2acb9beae31f471d4 192.168.75.241:6379@16379 master - 0 1590241663000 3 connected 5461-10922
096e85ce032914c9c0f3107b6d3c49e612a57ab7 192.168.75.242:6380@16380 slave 19f46bcef63286be0ebbcfd2acb9beae31f471d4 0 1590241663000 6 connected
726b7ba73ff0e4849a13fe1612ebc952d1c8786c 192.168.75.241:6380@16380 slave b16f676efe8d7998bfe56386434895ced77e79e7 0 1590241663710 4 connected

我们看到,集群已经正常运行,完毕!