搭建redis集群遇到的问题

我的环境有一个6379端口的redis实例,想要创建redis,需要在本机上启动6381端口的实例,并开启集群功能

关键步骤

1.开启cluster-enabled
2.cluster meet <ip> <port>

测试过程

1.启动6381端口的redis实例

1.1配置文件。修改端口,开启cluster功能
[root@lzl redis-5.0.3]# cp redis6380.conf redis6381.conf  
[root@lzl redis-5.0.3]# vi redis6381.conf 
port修改为6381
cluster-enabled yes

1.2 开启6381端口的redis

 /usr/local/redis-5.0.3/src/redis-server /usr/local/redis-5.0.3/redis6381.conf &
[1] 15408
[root@lzl redis-5.0.3]# 15408:C 29 Oct 2020 17:03:52.577 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15408:C 29 Oct 2020 17:03:52.577 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=15408, just started
15408:C 29 Oct 2020 17:03:52.577 # Configuration loaded
15408:M 29 Oct 2020 17:03:52.578 * Increased maximum number of open files to 10032 (it was originally set to 1024).
  。。。 
15408:M 29 Oct 2020 17:03:52.579 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
15408:M 29 Oct 2020 17:03:52.579 * DB loaded from disk: 0.000 seconds
15408:M 29 Oct 2020 17:03:52.580 * Ready to accept connections

1.3 验证6381是否开启集群功能
[root@lzl redis-5.0.3]# redis-cli -c -p 6381
127.0.0.1:6381> ping
PONG
127.0.0.1:6381> cluster nodes   --仅有自己在集群中
f0e401c0b974fbaedb721e9c5827c7cbed5a9990 :6381@16381 myself,master - 0 0 0 connected 9394

2. 6379端口redis开启集群功能

2.1 开启cluster-enabled

[root@lzl ~]# redis-cli -c -p 6379
127.0.0.1:6379> cluster nodes
ERR This instance has cluster support disabled   --默认redis没有开启集群功能,需要取消cluster-enabled yes的注释,并重启redis
127.0.0.1:6379>  shutdown 
not connected> 

vi /usr/local/redis-5.0.3/redis.conf
cluster-enabled yes
[root@lzl ~]# redis-server /usr/local/redis-5.0.3/redis.conf &
[1] 15484
[root@lzl ~]# 15484:C 29 Oct 2020 17:16:42.672 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15484:C 29 Oct 2020 17:16:42.672 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=15484, just started
15484:C 29 Oct 2020 17:16:42.672 # Configuration loaded
15484:M 29 Oct 2020 17:16:42.673 * Increased maximum number of open files to 10032 (it was originally set to 1024).
...

9394. Taking responsibility for it.
15484:M 29 Oct 2020 17:16:42.697 * Ready to accept connections

[root@lzl ~]# 15484:M 29 Oct 2020 17:16:43.415 * Replica 127.0.0.1:6380 asks for synchronization
15484:M 29 Oct 2020 17:16:43.415 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '944904c9bf97e4be5e594e3458825b3323af931a', my replication IDs are 'f821378144a7c0e93c21aa0e47599d49c67fc0c5' and '0000000000000000000000000000000000000000')
15484:M 29 Oct 2020 17:16:43.415 * Starting BGSAVE for SYNC with target: disk
15484:M 29 Oct 2020 17:16:43.416 * Background saving started by pid 15488
15488:C 29 Oct 2020 17:16:43.424 * DB saved on disk
15488:C 29 Oct 2020 17:16:43.424 * RDB: 0 MB of memory used by copy-on-write
15484:M 29 Oct 2020 17:16:43.502 * Background saving terminated with success
15484:M 29 Oct 2020 17:16:43.502 * Synchronization with replica 127.0.0.1:6380 succeeded

2.2 检查6379是否开启cluster-enabled

[root@lzl ~]# redis-cli -p 6379
127.0.0.1:6379> cluster nodes 
bcfcaa6308ede0cabcb8d192cd4b8b6327b5e9cc :6379@16379 myself,master - 0 0 0 connected 9394

127.0.0.1:6379> cluster meet 127.0.0.1 6381   --命令运行的同时添加节点的信息也打印在另一个节点上,敲meet命令的节点为cluster中的master。集群信息最好用cluster nodes看。role会看到主备信息。
OK
127.0.0.1:6379> 15484:M 29 Oct 2020 17:21:05.738 # IP address for this node updated to 127.0.0.1   --自动输出的
                                                                                                15484:M 29 Oct 2020 17:21:06.141 * Replica 127.0.0.1:6381 asks for synchronization
                                                                                                                                                                                  15484:M 29 Oct 2020 17:21:06.141 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '439f66b6890df7430297509173dfc3737e02a7ca', my replication IDs are 'db5d1526eb65d0d662d3d4ab95a683584b18aad2' and '0000000000000000000000000000000000000000')
                               15484:M 29 Oct 2020 17:21:06.141 * Starting BGSAVE for SYNC with target: disk
                                                                                                            15484:M 29 Oct 2020 17:21:06.141 * Background saving started by pid 15517
                                                                                                                                                                                     15517:C 29 Oct 2020 17:21:06.156 * DB saved on disk
                     15517:C 29 Oct 2020 17:21:06.156 * RDB: 0 MB of memory used by copy-on-write
                                                                                                 15484:M 29 Oct 2020 17:21:06.171 * Background saving terminated with success
                                                                                                                                                                             15484:M 29 Oct 2020 17:21:06.171 * Synchronization with replica 127.0.0.1:6381 succeeded
127.0.0.1:6379> cluster nodes
bcfcaa6308ede0cabcb8d192cd4b8b6327b5e9cc 127.0.0.1:6379@16379 myself,master - 0 0 1 connected 9394
f0e401c0b974fbaedb721e9c5827c7cbed5a9990 127.0.0.1:6381@16381 slave bcfcaa6308ede0cabcb8d192cd4b8b6327b5e9cc 0 1603963403179 1 connected

127.0.0.1:6379> cluster nodes
bcfcaa6308ede0cabcb8d192cd4b8b6327b5e9cc 127.0.0.1:6379@16379 myself,master - 0 1603968643000 1 connected 9394
f0e401c0b974fbaedb721e9c5827c7cbed5a9990 127.0.0.1:6381@16381 slave bcfcaa6308ede0cabcb8d192cd4b8b6327b5e9cc 0 1604024984134 1 connected
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=15918,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=15918,lag=0
master_replid:db5d1526eb65d0d662d3d4ab95a683584b18aad2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:15918
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:15918


127.0.0.1:6379> set lzl 1
(error) CLUSTERDOWN The cluster is down
127.0.0.1:6379> 


[root@lzl redis-5.0.3]# vi redis6382.conf 
>>cluster-config-file nodes-6379.conf    

[root@lzl redis-5.0.3]# redis-server redis6382.conf &
[1] 17069
[root@lzl redis-5.0.3]# 17069:C 30 Oct 2020 10:44:26.950 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17069:C 30 Oct 2020 10:44:26.951 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=17069, just started
                                

17069:M 30 Oct 2020 10:44:26.971 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
17069:M 30 Oct 2020 10:44:26.971 # Server initialized
17069:M 30 Oct 2020 10:44:26.971 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
17069:M 30 Oct 2020 10:44:26.973 * DB loaded from disk: 0.002 seconds
17069:M 30 Oct 2020 10:44:26.973 # I have keys for unassigned slot 9394. Taking responsibility for it.
17069:M 30 Oct 2020 10:44:26.976 * Ready to accept connections


127.0.0.1:6382> cluster nodes
c742d675ff5acc57b39bfcbd72363c92b4f22f4b :6382@16382 myself,master - 0 0 0 connected 9394


127.0.0.1:6379> cluster meet 127.0.0.1 6382
OK


127.0.0.1:6382> cluster nodes
f0e401c0b974fbaedb721e9c5827c7cbed5a9990 127.0.0.1:6381@16381 slave bcfcaa6308ede0cabcb8d192cd4b8b6327b5e9cc 0 1604026001413 1 connected
bcfcaa6308ede0cabcb8d192cd4b8b6327b5e9cc 127.0.0.1:6379@16379 master - 0 1604026002419 1 connected 9394
c742d675ff5acc57b39bfcbd72363c92b4f22f4b 127.0.0.1:6382@16382 myself,slave bcfcaa6308ede0cabcb8d192cd4b8b6327b5e9cc 0 1604026000000 0 connected

1.根据《redis设计与实现》中搭建集群的方式,cluster meet后是主备master-slave,不是master-master

2.第二个slave加入集群时,需要另起cluster-config-file文件,不然起不来server

3 cluster meet后需要分配slot

4 redis-trib.rb脚本需要ruby环境

猜你喜欢

转载自blog.csdn.net/qq_40687433/article/details/109365111
今日推荐