导读 | 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
原文来自:
本文地址://q13zd.cn/redis-cluster-construction.html编辑:薛鹏旭,审核员:逄增宝
Linux命令大全:
Linux系统大全: