阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Redis集群搭建

30次阅读
没有评论

共计 3954 个字符,预计需要花费 10 分钟才能阅读完成。

导读 redis 最开始使用主从模式做集群,若 master 宕机需要手动配置 slave 转为 master;后来为了高可用提出来哨兵模式,该模式下有一个哨兵监视 master 和 slave,若 master 宕机可自动将 slave 转为 master,但它也有一个问题,就是不能动态扩充;所以在 3.x 提出 cluster 集群模式。
安装 Redis 集群准备

采用原生搭建的方式搭建 3 主 3 从的 Redis Cluster,分别给 3 个 master 节点配置一个 slave 节点,总计 6 个 Redis 节点。
但由于我们只有 4 台机器,所以选择在其中的两台机器上安装两个 redis。

修改配置文件
port 6380 # server 端口,4 台 server 用默认的 6379,所以无需修改,另外两台则改成 6380。# bind 127.0.0.1 // 这一行要注释,否则无法远程连接
cluster-enabled yes
cluster-config-file nodes.conf //node.conf 文件不用管。cluster-node-timeout 5000
appendonly yes //aof
daemonize yes 
protected-mode no
启动服务
[root@localhost]# redis-server redis.conf

到这里我们就有以集群模式启动的 6 台 redis server,但是每台服务器还没有进行 slot 指派, 此时是不能对外提供服务的。

开放防火墙端口
[root@test-3]# firewall-cmd --zone=public --add-port={6379/tcp,16379/tcp} --permanent
[root@test-3]# firewall-cmd --reload
搭建集群(slot 指派)

用 redis 官方提供的 redis-cli 命令的 –cluster 选项。
下面的命令将六台服务器组成一个集群
其中复制因子为 1 所, 以会有 3 台 master, 另外 3 台为 slave。
16384 个 slot 会尽可能均匀的指派给 3 台 master, 而 3 台 slave 异步的从其 master 进行复制。
任选一台机执行以下命令

[root@test-3 redis-6.2.5]# src/redis-cli --cluster create 192.168.0.129:6379 192.168.0.250:6379 test-3:6379 test-3:6380 test-4:6379 test-4:6380 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica test-3:6380 to 192.168.0.129:6379
Adding replica test-4:6380 to 192.168.0.250:6379
Adding replica test-4:6379 to sz-cms-3:6379
M: ae80cca550b15585b20b4ac7511f7751437f6ee 192.168.0.129:6379
   slots:[0-5460] (5461 slots) master
M: d7ca9360b74b777e7739e05d2265b6c4d5f5ca0 192.168.0.250:6379
   slots:[5461-10922] (5462 slots) master
M: 68569ee4c6bb4b60981ea34ee1b7c61b2ce285 test-3:6379
   slots:[10923-16383] (5461 slots) master
S: 3532972c73ce3b785d538983f725486e7a0fc9 test-3:6380
   replicates ae80cca550b558b20b4acc7511f7751437f6ee
S: b648cc92b55d07ec638c7d41a9ca24fc5d9160 test-4:6379
   replicates 68569ee4cbb4b609881e7a34ee1b7c61b2ce285
S: 25e9b9a3de4ac4c3e47eb3fdb9f8e80c98e728f test-4:6380
   replicates d7ca9360b74b777e773905d2265b6c4d5f5ca0
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 192.168.0.129:6379)
M: ae80cca550b185b20b4acc7511f7751437f6ee 192.168.0.129:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: d7ca9360b74b777e77397e05d25b6c4d5f5ca0 192.168.0.250:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 25e9b9a3de4c4c3e47b3fdb9f8e80c98e728f 192.168.0.141:6380
   slots: (0 slots) slave
   replicates d7ca9360b74b77e7739705d2265b6c4d5f5ca0
M: 68569ee4c6bb4609881e7a34ee1b7c1b2ce285 192.168.0.228:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: b648cc92b55d17ec638c7d41ca9a24fc5d9160 192.168.0.141:6379
   slots: (0 slots) slave
   replicates 68569ee4c6bbb609881e7a4ee1b7c612ce285
S: 3532972c3ce3b785d5389783f72546e7a0fc9 192.168.0.228:6380
   slots: (0 slots) slave
   replicates ae8ca550b5585b20b4cc7511f7751437f6ee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查看集群情况
[root@sz-cms-3 redis-6.2.5]# src/redis-cli 
127.0.0.1:6379> cluster nodes
3532972c7ce3b7885d38783f725486e7a0fc9 192.168.0.228:6380@16380 slave ae80cc550b1558b20b4acc7511f77514376ee 0 1629874202654 1 connected
25eb9ade4ac4c3478eb3fdb98e80c9e728f 192.168.0.141:6380@16380 slave d7ca930b74b77777397e0d2265b6cd5f5ca0 0 1629874204664 2 connected
d7ca9360b7b777e7797e05d265b64d5f5ca0 192.168.0.250:6379@16379 master - 0 1629874205000 2 connected 5461-10922
ae8cca550b15585b0b4acc751f7751437f6ee 192.168.0.129:6379@16379 master - 0 1629874206673 1 connected 0-5460
68569ee4c6b4b609881e7a3ee1b761b2ce285 192.168.0.228:6379@16379 myself,master - 0 1629874206000 3 connected 10923-16383
b648c92b55d017ec68c7d41ca9c24fc5d9160 192.168.0.141:6379@16379 slave 6856ee4c6b4b609881e734ee17c61b2ce285 0 1629874206000 3 connected
添加密码

每个节点都需要自己上去设置。然后因为有两个节点使用了 6380 端口,所以命令里需要加入 -p 6380,其他节点用默认的即可。

[root@test-3 redis-replica]# src/redis-cli -p 6380
127.0.0.1:6380> config set masterauth 123456
OK
127.0.0.1:6380> config set requirepass 123456
OK
127.0.0.1:6380> config rewrite
OK

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计3954字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中