首页
编程随笔
Java笔记
Html/Css/Js
Android
后端笔记
服务器搭建
BUG收集
Java异常
Android异常
在线工具
Json格式化
编码/解码
Epub在线编辑
登录
发布文章
个人文章
退出登录
首页
技术教程
BUG收集
在线工具
资源下载
登录
发布文章
退出登录
搜索
当前位置:
首页
-
博客
- 正文
关闭
3主3从Redis集群配置
更新时间:2023-07-13 11:31:11
阅读数:2049
发布者:落幕
### redis集群搭建环境准备 ```shell 域名 : ip 主机1 test1:192.168.225.132 主机2 test2:192.168.225.130 主机3 test3:192.168.225.131 ``` 由于主机数量有限,一台linux都运行两台redis ```shell 主机1:192.168.225.132:6381 192.168.225.132:6382 主机2:192.168.225.130:6381 192.168.225.130:6382 主机3:192.168.225.131:6381 192.168.225.131:6382 ``` 简历集群的目录主要使用:/redis/cluster ### 新建6个独立的Redis实例服务 #### 新建目录 mkdir -p /redis/cluster 192.168.225.128 + 端口6381/6382 vim /redis/cluster/redisCluster6381.conf #### 6381redis存放内容 ```txt daemonize yes protected-mode no port 6381 logfile "/redis/cluster/cluster6381.log" pidfile /redis/cluster/cluster6381.pid dir /redis/cluster/cluster dbfilename dump6381.rdb appendonly yes appendfilename "appendonly6381.aof" requirepass 123456 masterauth 123456 cluster-enabled yes cluster-config-file nodes-6381.conf cluster-node-timeout 5000 ``` 参数见参数说明 #### 6382redis存放内容 vim /redis/cluster/redisCluster6382.conf ```txt daemonize yes protected-mode no port 6382 logfile "/redis/cluster/cluster6382.log" pidfile /redis/cluster/cluster6382.pid dir /redis/cluster dbfilename dump6382.rdb appendonly yes appendfilename "appendonly6382.aof" requirepass 123456 masterauth 123456 cluster-enabled yes cluster-config-file nodes-6382.conf cluster-node-timeout 5000 ``` 其他两台主机,拷贝redisCluster6382.conf和redisCluster6381.conf文件到/redis/cluster即可 ### 启动redis #### 启动6381 redis redis-server /opt/redis/redisCluster6381.conf #### 启动6382 redis redis-server /opt/redis/redisCluster6382.conf ### 通过redis-cli 命令为6台机器构建集群关系 #### 构建主从关系命令 ```shell // 一定要注意,此处要修改自己的IP为真实IP redis-cli -a 123456 --cluster create --cluster-replicas 1 192.168.225.132:6381 192.168.225.132:6382 192.168.225.130:6381 192.168.225.130:6382 192.168.225.131:6381 192.168.225.131:6382 ``` --cluster- replicas 1 表示为每个master创建一一个slave节点 Can I set the above configuration? (type 'yes' to accept):输入yes ![启动redis集群](https://www.speechb.com/blog/redis/redis-cluster2.png "启动redis集群") 集群启动信息 ![启动redis集群](https://www.speechb.com/blog/redis/redis-cluster1.png "启动redis集群") 出现All nodes agree about slots configuration.即成功搭建成功 ### 检测集群状态 #### 连接集群 ```shell # -a 登录redis 密码 -p 端口 redis-cli -a 123456 -p 6381 ``` #### info replication ```txt 127.0.0.1:6381> info replication # Replication role:master connected_slaves:1 slave0:ip=192.168.225.130,port=6382,state=online,offset=108,lag=0 master_failover_state:no-failover master_replid:18b19fc5524865b3bf69cabdd78a3e87394ba778 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:108 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:108 ``` role:master: 标识当前redis为master connected_slaves:1: 当前连接了一台从机 slave0:ip=192.168.225.130,port=6382,state=online,offset=108,lag=0: 从节点信息,ip和端口 #### cluster nodes ```txt 5977667fbfa2b75d7015c067819d3283c51224f0 192.168.225.130:6381@16381 master - 0 1689215775000 3 connected 5461-10922 452f736b17490ec1f2e33afb574490c807fa913f 192.168.225.130:6382@16382 slave 84f534d712ab956fd5f51efef8a0e749fb775fc4 0 1689215776622 1 connected 84f534d712ab956fd5f51efef8a0e749fb775fc4 192.168.225.132:6381@16381 myself,master - 0 1689215775000 1 connected 0-5460 e1da6d5ca5c1c4ee50b78dc4405afbb6950905aa 192.168.225.131:6382@16382 slave 5977667fbfa2b75d7015c067819d3283c51224f0 0 1689215775614 3 connected 913d5c8d4d160cdbdfbb2918f2b9c2aef8736e78 192.168.225.131:6381@16381 master - 0 1689215776522 5 connected 10923-16383 c1f047c0d0e815ead05f997645a16684bc42f73e 192.168.225.132:6382@16382 slave 913d5c8d4d160cdbdfbb2918f2b9c2aef8736e78 0 1689215775000 5 connected ``` 0-5460 5461-10922 10923-16383 为槽位范围 #### CLUSTER INFO ```text 127.0.0.1:6381> CLUSTER INFO 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:1 cluster_stats_messages_ping_sent:562 cluster_stats_messages_pong_sent:563 cluster_stats_messages_sent:1125 cluster_stats_messages_ping_received:558 cluster_stats_messages_pong_received:562 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:1125 total_cluster_links_buffer_limit_exceeded:0 ``` ### 数据操作 #### 对6381新增连个key 127.0.0.1:6381> set k1 v1 (error) MOVED 12706 192.168.225.131:6381 出现报错,这是由于需要注意槽位范围,
路由到位
如何解决 防止路由失效加参数-c并新增两个key: redis-cli -a 123456 -p 6381 -c 成功 ```text 127.0.0.1:6381> set k1 v1 -> Redirected to slot [12706] located at 192.168.225.131:6381 OK ``` 查看某个key该属于对应的槽位值 cluster keyslot 键名称 ```text 192.168.225.131:6381> cluster keyslot k1 (integer) 12706 ``` ### 集群配置参数说明 ```config # 设置后台开启 daemonize yes # 是否启动保护模式,默认yes protected-mode no # redis端口 port 6381 # 日志文件 logfile "/myredis/cluster/cluster6381.log" # pid存放未知 pidfile /myredis/cluster6381.pid # dir /myredis/cluster # rbd文件的文件名 dbfilename dump6381.rdb # 打开aof持久化 appendonly yes # aof文件名 appendfilename "appendonly6381.aof" # redis密码 requirepass 123456 # master密码 masterauth 123456 #开启集群 cluster-enabled yes # 集群的配置文件,该文件自动生成 cluster-config-file nodes-6381.conf #设置集群的超时时间 cluster-node-timeout 5000 ```