redis-cluster 集群搭建方案

下载redis 源码包,编译安装redis

#下载redis
wget http://download.redis.io/releases/redis-5.0.8.tar.gz

#解压并 进入redis解压包中
tar -zxvf redis-5.0.8.tar.gz
cd redis-5.0.8

# 创建redis 目录
mkdir /var/local/redis-cluster/7001 -p

# make 安装redis 二进制服务包
make install PREFIX=/var/local/redis-cluster/7001

修改redis 参数

# 注释到绑定ip参数
#bind 127.0.0.1

# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes

protected-mode no

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 7001

# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

pidfile /var/run/redis_7001.pid

# output for logging but daemonize, logs will be sent to /dev/null
logfile "/var/local/redis-cluster/7001/logs/redis.log"

复制几个不同的redis 服务节点

cp 7001/ -R 7002
cp 7001/ -R 7003
cp 7001/ -R 7004
cp 7001/ -R 7005
cp 7001/ -R 7006
cp 7001/ -R 7007
cp 7001/ -R 7008

修改复制的服务相应的配置信息

# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes

protected-mode no

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port <7002 - 7008>

# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

pidfile /var/run/redis_<7002 - 7008>.pid

# output for logging but daemonize, logs will be sent to /dev/null
logfile "/var/local/redis-cluster/<7002-7008>/logs/redis.log"

创建redis 集群

./redis-cli --cluster create 192.168.1.102:7001 192.168.1.102:7002 192.168.1.102:7003 192.168.1.102:7004 192.168.1.102:7005 192.168.1.102:7006 --cluster-replicas 1

客户端命令行模式连接redis 集群

# -c 表示以集群的方式连接
./redis-cli -h 192.168.1.102 -p 7001 -c

查看集群状态

[root@master1 bin]# ./redis-cli -h 192.168.1.102 -p 7001 -c
192.168.1.102:7001> 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:855
cluster_stats_messages_pong_sent:833
cluster_stats_messages_sent:1688
cluster_stats_messages_ping_received:828
cluster_stats_messages_pong_received:855
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1688

查看集群中节点信息

192.168.1.102:7001> cluster nodes
227b6da7e4ba4414f90d3b1eb0085df4a5a84681 192.168.1.102:7006@17006 slave 0d01f85fab5e086afde3eebcd0de39c544dae02c 0 1595870090000 6 connected
0d01f85fab5e086afde3eebcd0de39c544dae02c 192.168.1.102:7003@17003 master - 0 1595870091016 3 connected 10923-16383
a10b150d343122ba0a9676842b4494bedb277276 192.168.1.102:7004@17004 slave 1acc43a0295b08139b7bd29241500c66ba40499a 0 1595870088980 4 connected
24d804068d10070904f46320117e44df7fb98e1e 192.168.1.102:7005@17005 slave 0407753bce270c9c4bea6bc8bc52f033157d234e 0 1595870089000 5 connected
1acc43a0295b08139b7bd29241500c66ba40499a 192.168.1.102:7001@17001 myself,master - 0 1595870089000 1 connected 0-5460
0407753bce270c9c4bea6bc8bc52f033157d234e 192.168.1.102:7002@17002 master - 0 1595870090003 2 connected 5461-10922

增加节点扩容

# 增加节点
./redis-cli --cluster add-node 192.168.1.102:7007 192.168.1.102:7001

# 执行结果
[root@master1 bin]# ./redis-cli --cluster add-node 192.168.1.102:7007 192.168.1.102:7001
>>> Adding node 192.168.1.102:7007 to cluster 192.168.1.102:7001
>>> Performing Cluster Check (using node 192.168.1.102:7001)
M: 1acc43a0295b08139b7bd29241500c66ba40499a 192.168.1.102:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 227b6da7e4ba4414f90d3b1eb0085df4a5a84681 192.168.1.102:7006
   slots: (0 slots) slave
   replicates 0d01f85fab5e086afde3eebcd0de39c544dae02c
M: 0d01f85fab5e086afde3eebcd0de39c544dae02c 192.168.1.102:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: a10b150d343122ba0a9676842b4494bedb277276 192.168.1.102:7004
   slots: (0 slots) slave
   replicates 1acc43a0295b08139b7bd29241500c66ba40499a
S: 24d804068d10070904f46320117e44df7fb98e1e 192.168.1.102:7005
   slots: (0 slots) slave
   replicates 0407753bce270c9c4bea6bc8bc52f033157d234e
M: 0407753bce270c9c4bea6bc8bc52f033157d234e 192.168.1.102:7002
   slots:[5461-10922] (5462 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.
>>> Send CLUSTER MEET to node 192.16

为新增节点分配 槽点(slot) rehash

# 为新节点分配槽点
./redis-cli --cluster reshard 192.168.1.102:7007

为新增master 节点添加从节点

# 执行命令添加从节点
./redis-cli --cluster add-node 192.168.1.102:7008 192.168.1.102:7007 --cluster-slave --cluster-master-id 7641497e971067b5226d06cb33a33a5e94398078


[root@master1 bin]# ./redis-cli --cluster add-node 192.168.1.102:7008 192.168.1.102:7007 --cluster-slave --cluster-master-id 7641497e971067b5226d06cb33a33a5e94398078
>>> Adding node 192.168.1.102:7008 to cluster 192.168.1.102:7007
>>> Performing Cluster Check (using node 192.168.1.102:7007)
M: 7641497e971067b5226d06cb33a33a5e94398078 192.168.1.102:7007
   slots:[0-1332],[5461-6794],[10923-12255] (4000 slots) master
S: 24d804068d10070904f46320117e44df7fb98e1e 192.168.1.102:7005
   slots: (0 slots) slave
   replicates 0407753bce270c9c4bea6bc8bc52f033157d234e
M: 0407753bce270c9c4bea6bc8bc52f033157d234e 192.168.1.102:7002
   slots:[6795-10922] (4128 slots) master
   1 additional replica(s)
M: 0d01f85fab5e086afde3eebcd0de39c544dae02c 192.168.1.102:7003
   slots:[12256-16383] (4128 slots) master
   1 additional replica(s)
S: 227b6da7e4ba4414f90d3b1eb0085df4a5a84681 192.168.1.102:7006
   slots: (0 slots) slave
   replicates 0d01f85fab5e086afde3eebcd0de39c544dae02c
M: 1acc43a0295b08139b7bd29241500c66ba40499a 192.168.1.102:7001
   slots:[1333-5460] (4128 slots) master
   1 additional replica(s)
S: a10b150d343122ba0a9676842b4494bedb277276 192.168.1.102:7004
   slots: (0 slots) slave
   replicates 1acc43a0295b08139b7bd29241500c66ba40499a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.1.102:7008 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 192.168.1.102:7007.
[OK] New node added correctly.

缩容测试

# 执行缩容命令
./redis-cli --cluster del-node 192.168.1.102:7008 3104e8001e93f9117b3a5421e020ee9753b4677b


[root@master1 bin]# ./redis-cli --cluster del-node 192.168.1.102:7008 3104e8001e93f9117b3a5421e020ee9753b4677b
>>> Removing node 3104e8001e93f9117b3a5421e020ee9753b4677b from cluster 192.168.1.102:7008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,802评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,109评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,683评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,458评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,452评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,505评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,901评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,550评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,763评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,556评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,629评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,330评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,898评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,897评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,140评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,807评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,339评论 2 342