Linux-redis-5.0集群配置


设备环境

每个redis 节点采用相同的相同的redis版本、相同的密码、硬件配置

所有redis服务器必须没有任何数据

10.0.0.17           centos7          redis5.0       

10.0.0.27           centos7          redis5.0 

10.0.0.37           centos7          redis5.0 

10.0.0.47           centos7          redis5.0 

10.0.0.57           centos7          redis5.0 

10.0.0.67           centos7          redis5.0 

1、配置文件修改

redis配置文件修改,redis.conf

手动修改

[root@centos7-liyj ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456   #建议配置,否则后期的master和slave主从复制无法成功,还需再配置
requirepass 123456
cluster-enabled yes #取消此行注释,必须开启集群,开启后redis 进程会有cluster标识
cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态文件,记录主从关系及
slot范围信息,由redis cluster 集群自动创建和维护
cluster-require-full-coverage no   #默认值为yes,设为no可以防止一个节点不可用导致整
个cluster不可能

复制到其他节点上

[root@centos7-liyj ~]#scp /apps/redis/etc/redis.conf 10.0.0.27:/apps/redis/etc/
The authenticity of host '10.0.0.27 (10.0.0.27)' can't be established.
ECDSA key fingerprint is SHA256:a8HZZS4TTvzQbF1/XJKDZvry1Lwa+9/jYIYXRPwqfIk.
ECDSA key fingerprint is MD5:41:d8:d4:69:09:2c:35:d3:0a:91:79:cd:0e:e5:10:14.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.27' (ECDSA) to the list of known hosts.
root@10.0.0.27's password: 
redis.conf                                                                            100%   60KB  18.5MB/s   00:00    
[root@centos7-liyj ~]#scp /apps/redis/etc/redis.conf 10.0.0.37:/apps/redis/etc/
The authenticity of host '10.0.0.37 (10.0.0.37)' can't be established.
ECDSA key fingerprint is SHA256:a8HZZS4TTvzQbF1/XJKDZvry1Lwa+9/jYIYXRPwqfIk.
ECDSA key fingerprint is MD5:41:d8:d4:69:09:2c:35:d3:0a:91:79:cd:0e:e5:10:14.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.37' (ECDSA) to the list of known hosts.
root@10.0.0.37's password: 
redis.conf                                                                            100%   60KB  27.3MB/s   00:00    
[root@centos7-liyj ~]#scp /apps/redis/etc/redis.conf 10.0.0.47:/apps/redis/etc/
The authenticity of host '10.0.0.47 (10.0.0.47)' can't be established.
ECDSA key fingerprint is SHA256:a8HZZS4TTvzQbF1/XJKDZvry1Lwa+9/jYIYXRPwqfIk.
ECDSA key fingerprint is MD5:41:d8:d4:69:09:2c:35:d3:0a:91:79:cd:0e:e5:10:14.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.47' (ECDSA) to the list of known hosts.
root@10.0.0.47's password: 
redis.conf                                                                            100%   60KB  29.3MB/s   00:00    
[root@centos7-liyj ~]#scp /apps/redis/etc/redis.conf 10.0.0.57:/apps/redis/etc/
The authenticity of host '10.0.0.57 (10.0.0.57)' can't be established.
ECDSA key fingerprint is SHA256:a8HZZS4TTvzQbF1/XJKDZvry1Lwa+9/jYIYXRPwqfIk.
ECDSA key fingerprint is MD5:41:d8:d4:69:09:2c:35:d3:0a:91:79:cd:0e:e5:10:14.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.57' (ECDSA) to the list of known hosts.
root@10.0.0.57's password: 
Permission denied, please try again.
root@10.0.0.57's password: 
redis.conf                                                                            100%   60KB  23.3MB/s   00:00    
[root@centos7-liyj ~]#scp /apps/redis/etc/redis.conf 10.0.0.67:/apps/redis/etc/
The authenticity of host '10.0.0.67 (10.0.0.67)' can't be established.
ECDSA key fingerprint is SHA256:a8HZZS4TTvzQbF1/XJKDZvry1Lwa+9/jYIYXRPwqfIk.
ECDSA key fingerprint is MD5:41:d8:d4:69:09:2c:35:d3:0a:91:79:cd:0e:e5:10:14.
Are you sure you want to continue connecting (yes/no)? yte^H^H^H
Please type 'yes' or 'no': yes
Warning: Permanently added '10.0.0.67' (ECDSA) to the list of known hosts.
root@10.0.0.67's password: 
Permission denied, please try again.
root@10.0.0.67's password: 
redis.conf                                                                            100%   60KB  42.1MB/s   00:00    

所有所有节点启动或重启redis服务

systemctl enable --now redis  #redis未启动,执行此行
systemctl  restart redis      #redis已经启动,重启即可

 

redis集群配置

创建集群

#命令redis-cli的选项 –cluster-replicas 1 表示每个master对应一个slave节点

[root@centos7-liyj ~]#redis-cli -a 123456 --cluster create 10.0.0.17:6379 10.0.0.27:6379 10.0.0.37:6379 10.0.0.47:6379 10.0.0.57:6379 10.0.0.67:6379 --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 10.0.0.57:6379 to 10.0.0.17:6379
Adding replica 10.0.0.67:6379 to 10.0.0.27:6379
Adding replica 10.0.0.47:6379 to 10.0.0.37:6379
M: 97b46220a05f4ec75a07ee0a468dc0a871d715a3 10.0.0.17:6379    #带M的为master
   slots:[0-5460] (5461 slots) master                      #当前master的槽位起始 和结束位
M: ec6ec291571a8d16629897b3aa0628ac5c161820 10.0.0.27:6379
   slots:[5461-10922] (5462 slots) master
M: bd51f66ca1a1b07be2f0f0fd7ae1f829475a6809 10.0.0.37:6379
   slots:[10923-16383] (5461 slots) master
S: 0f63f38aa1cf2f330a7d2f96c647f3d96533ea9a 10.0.0.47:6379    #带S的slave
   replicates bd51f66ca1a1b07be2f0f0fd7ae1f829475a6809
S: 04db5ec527aa1c3b62a8a743b145bb3e1467249d 10.0.0.57:6379
   replicates 97b46220a05f4ec75a07ee0a468dc0a871d715a3
S: 32e7a2cd069a3fbff823ac9661dd20bdc9fcc012 10.0.0.67:6379
   replicates ec6ec291571a8d16629897b3aa0628ac5c161820
Can I set the above configuration? (type 'yes' to accept): yes #输入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 10.0.0.17:6379)
M: 97b46220a05f4ec75a07ee0a468dc0a871d715a3 10.0.0.17:6379
   slots:[0-5460] (5461 slots) master                      #已经分配的槽位
   1 additional replica(s)                                 #分配了一个slave
S: 0f63f38aa1cf2f330a7d2f96c647f3d96533ea9a 10.0.0.47:6379
   slots: (0 slots) slave                                  #slave没有分配槽位
   replicates bd51f66ca1a1b07be2f0f0fd7ae1f829475a6809     #对应的master的10.0.0.37的 ID
S: 32e7a2cd069a3fbff823ac9661dd20bdc9fcc012 10.0.0.67:6379
   slots: (0 slots) slave
   replicates ec6ec291571a8d16629897b3aa0628ac5c161820     #对应的master的10.0.0.27的ID
S: 04db5ec527aa1c3b62a8a743b145bb3e1467249d 10.0.0.57:6379
   slots: (0 slots) slave
   replicates 97b46220a05f4ec75a07ee0a468dc0a871d715a3     #对应的master的10.0.0.17的 ID
M: ec6ec291571a8d16629897b3aa0628ac5c161820 10.0.0.27:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: bd51f66ca1a1b07be2f0f0fd7ae1f829475a6809 10.0.0.37:6379
   slots:[10923-16383] (5461 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.                               #所有槽位(16384个)分配完成

查看主从状态

10.0.0.17 节点
[root@centos7-liyj ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.57,port=6379,state=online,offset=1204,lag=1
master_replid:7b55e11479093abac4b63c965839dbcff729a016
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1218
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1218

10.0.0.27节点
[root@centos7-liyj ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.67,port=6379,state=online,offset=1204,lag=0
master_replid:a45fc1d876fcd29a7f9877b60b7228102e8abb38
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1218
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1218

10.0.0.37节点
[root@centos7-liyj ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.47,port=6379,state=online,offset=1204,lag=1
master_replid:f226151c30ebce938d03e773f544b43b6fcbfe13
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1218
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1218

10.0.0.47节点
[root@centos7-liyj ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.37
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:1218
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:f226151c30ebce938d03e773f544b43b6fcbfe13
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1218
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1218

10.0.0.57节点
[root@centos7-liyj ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.17
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1218
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7b55e11479093abac4b63c965839dbcff729a016
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1218
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1218

10.0.0.67节点
[root@centos7-lyj ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.27
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1218
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a45fc1d876fcd29a7f9877b60b7228102e8abb38
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1218
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1218

 

验证集群状态

[root@centos7-lyj ~]#redis-cli -a 123456 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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:2
cluster_stats_messages_ping_sent:1284
cluster_stats_messages_pong_sent:1228
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:2517
cluster_stats_messages_ping_received:1228
cluster_stats_messages_pong_received:1289
cluster_stats_messages_received:2517

 

原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/273459.html

(0)
上一篇 2022年7月10日
下一篇 2022年7月10日

相关推荐

发表回复

登录后才能评论