Redis 集群的搭建

集群搭建参考官网:https://redis.io/topics/cluster-tutorial

redis 集群至少需要三个 master 节点,我们这里搭建三个 master 节点,并且给每一个 master 在搭建一个 slave 节点,总共 6 个 redis 节点,这里用一台机器部署这 6 个节点。三主三从模式,Redis 5.0 以上。

1、创建 Redis 节点安装路径

[root@localhost ~]# mkdir /usr/local/redis_cluster 

2、创建 7000 -7005 文件夹,分配redis.conf

[root@localhost ~]# cd /usr/local/redis_cluster/
[root@localhost redis_cluster]# mkdir 7000 7001 7002 7003 7004 7005
[root@localhost redis_cluster]# wget http://download.redis.io/redis-stable/redis.conf
[root@localhost redis_cluster]# ls
7000  7001  7002  7003  7004  7005  redis.conf
[root@localhost redis_cluster]# cp redis.conf ./7000
[root@localhost redis_cluster]# cp redis.conf ./7001
[root@localhost redis_cluster]# cp redis.conf ./7002
[root@localhost redis_cluster]# cp redis.conf ./7003
[root@localhost redis_cluster]# cp redis.conf ./7004
[root@localhost redis_cluster]# cp redis.conf ./7005

3、配置每一个的 redis.conf

# 关闭保护模式 用于公网访问
protected-mode no
# 端口
port 7000

# 开启集群模式
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000

# 后台启动
daemonize yes
pidfile /var/run/redis_7000.pid
logfile "7000.log"

# dir /redis/data
# bind 127.0.0.1

# 连接从节点密码
masterauth 123456
# 设置密码
requirepass 123456

4、redis 启动

[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7000/redis.conf
[root@localhost redis_cluster]# ps -ef|grep -i redis
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7001/redis.conf
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7002/redis.conf
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7003/redis.conf
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7004/redis.conf
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7005/redis.conf

5、创建 redis 集群

官网告知:
创建集群
现在我们有许多实例正在运行,我们需要通过将一些有意义的配置写入节点来创建集群。
如果您使用的是Redis 5,这很容易完成,这是因为嵌入到中的Redis Cluster命令行实用程序为我们提供了帮助,该实用程序redis-cli可用于创建新集群,检查或重新分片现有集群等。

命令: ./redis-5.0.8/src/redis-cli --cluster create -a 123456 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1

[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-cli --cluster create -a 123456 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --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 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: d48cfd2e99a71377e42629495529fcbcfd0d9543 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
M: 370cf09b9d18445cb9ce10dea9466f95cc0394d3 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
M: 309eb1db4b3265f8898d144b0cfccd3f5e468574 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
S: 052e1e68b84e54901df10159b04fbd9afd05aedd 127.0.0.1:7003
   replicates d48cfd2e99a71377e42629495529fcbcfd0d9543
S: ee4dba83f69c7bcde1b0266b4d995ece7a1bc79c 127.0.0.1:7004
   replicates 370cf09b9d18445cb9ce10dea9466f95cc0394d3
S: 488163d590240d9ff325b8fbf94c877aef588117 127.0.0.1:7005
   replicates 309eb1db4b3265f8898d144b0cfccd3f5e468574
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:7000)
M: d48cfd2e99a71377e42629495529fcbcfd0d9543 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 309eb1db4b3265f8898d144b0cfccd3f5e468574 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: ee4dba83f69c7bcde1b0266b4d995ece7a1bc79c 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 370cf09b9d18445cb9ce10dea9466f95cc0394d3
S: 488163d590240d9ff325b8fbf94c877aef588117 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 309eb1db4b3265f8898d144b0cfccd3f5e468574
S: 052e1e68b84e54901df10159b04fbd9afd05aedd 127.0.0.1:7003
   slots: (0 slots) slave
   replicates d48cfd2e99a71377e42629495529fcbcfd0d9543
M: 370cf09b9d18445cb9ce10dea9466f95cc0394d3 127.0.0.1:7001
   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.

集群验证

在某台机器上 连接任意一个redis 客户端 就可以拿到所有的数据。

[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-cli -h 127.0.0.1 -a 123456 -c -p 7000
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7000> keys *
(empty list or set)
127.0.0.1:7000> info replication
127.0.0.1:7000> cluster nodes
309eb1db4b3265f8898d144b0cfccd3f5e468574 127.0.0.1:7002@17002 master - 0 1586487109560 3 connected 10923-16383
ee4dba83f69c7bcde1b0266b4d995ece7a1bc79c 127.0.0.1:7004@17004 slave 370cf09b9d18445cb9ce10dea9466f95cc0394d3 0 1586487109360 5 connected
488163d590240d9ff325b8fbf94c877aef588117 127.0.0.1:7005@17005 slave 309eb1db4b3265f8898d144b0cfccd3f5e468574 0 1586487109000 6 connected
052e1e68b84e54901df10159b04fbd9afd05aedd 127.0.0.1:7003@17003 slave d48cfd2e99a71377e42629495529fcbcfd0d9543 0 1586487108353 4 connected
d48cfd2e99a71377e42629495529fcbcfd0d9543 127.0.0.1:7000@17000 myself,master - 0 1586487108000 1 connected 0-5460
370cf09b9d18445cb9ce10dea9466f95cc0394d3 127.0.0.1:7001@17001 master - 0 1586487108857 2 connected 5461-10922

注意: 主节点写入的时候,不会立马同步到 slave 分支,异步的去获取 key-value 值。

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

推荐阅读更多精彩内容

  • 1 Redis介绍1.1 什么是NoSql为了解决高并发、高可扩展、高可用、大数据存储问题而产生的数据库解决方...
    克鲁德李阅读 5,257评论 0 36
  • redis集群分为服务端集群和客户端分片,redis3.0以上版本实现了集群机制,即服务端集群,3.0以下使用客户...
    hadoop_null阅读 1,581评论 0 6
  • 目录结构 集群搭建通过上述步骤,搭建了一个Redis服务,在服务管理器中也可以看到此服务。(注意服务器端口),同时...
    a9b854aded01阅读 377评论 0 1
  • NOSQL类型简介键值对:会使用到一个哈希表,表中有一个特定的键和一个指针指向特定的数据,如redis,volde...
    MicoCube阅读 3,952评论 2 27
  • Redis集群 使用redis做缓存工具 实现系统高可用,redis需要做主备。使用redis做分片集群。 向业务...
    tanghuibook阅读 548评论 0 0