Redis Cluster手动搭建

redis cluster搭建

准备

文件跳转

https://www.cnblogs.com/Yunya-Cnblogs/p/14608937.html

一台机器,多实例

端口:6380,6381,6382,6383,6384,6385

1. 创建多实例目录
[root@db01 ~]# mkdir -p /etc/redis/638{0..5}
[root@db01 ~]# ll /etc/redis/
total 0
drwxr-xr-x 2 root root 74 Sep  5 15:07 6380
drwxr-xr-x 2 root root  6 Sep  5 15:11 6381
drwxr-xr-x 2 root root  6 Sep  5 15:11 6382
drwxr-xr-x 2 root root  6 Sep  5 15:11 6383
drwxr-xr-x 2 root root  6 Sep  5 15:11 6384
drwxr-xr-x 2 root root  6 Sep  5 15:11 6385

2. # 编写redis 6380 配置文件(剩下的同里)
cat >/etc/redis/6380/redis.conf<< EOF
port 6380
daemonize yes
pidfile /etc/redis/6380/redis.pid
loglevel notice
logfile /etc/redis/6380/redis.log
dbfilename dump.rdb
dir /etc/redis/6380
bind 0.0.0.0
protected-mode no
requirepass 123
masterauth 123
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes_6380.conf
EOC

# redis 6381配置
port 6381
daemonize yes
pidfile /etc/redis/6381/redis.pid
loglevel notice
logfile /etc/redis/6381/redis.log
dbfilename dump.rdb
dir /etc/redis/6381
bind 0.0.0.0
protected-mode no
requirepass 123
masterauth 123
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes_6381.conf

## 配置文件解析
# 监听端口
port 6380
# 是否以后台进程运行
daemonize yes
# pid文件位置
pidfile /etc/redis/6380/redis.pid
# 日志级别,分别有:
# debug :适用于开发和测试
# verbose :更详细信息
# notice :适用于生产环境
loglevel notice
# 日志文件位置
logfile /etc/redis/6380/redis.log
# rdb持久化数据文件名
dbfilename dump.rdb
# 数据库(dump.rdb)文件存放目录
dir /etc/redis/6380
# 监听在指定的IP地址,为了方便改为0.0.0.0
bind 127.0.0.1 172.16.1.52
# 关闭保护模式(不关的情况,非本机连接后,操作不了)
protected-mode no
## 开启主从需要下面的
# 密码设置为123,-a指定
requirepass 123
# 主库密码为123
masterauth 123
# 集群相关配置
# 是否以集群模式启动
cluster-enabled yes
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 15000
# 生成的集群节点配置文件名,文件名需要修改
cluster-config-file nodes_6379.conf

3. 使用system管理redis
##db01
[root@db01 ~]# cp /usr/lib/systemd/system/{redis,redis6380}.service
[root@db01 ~]# vim /usr/lib/systemd/system/redis6380.service 
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6380/redis.conf

[Install]
WantedBy=multi-user.target

4. 启动redis
[root@db01 ~]# systemctl restart redis6380 redis6381 redis6382 redis6383 redis6384 redis6385

# 查看端口
[root@db01 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:6380            0.0.0.0:*               LISTEN      35859/redis-server  
tcp        0      0 0.0.0.0:6381            0.0.0.0:*               LISTEN      35864/redis-server  
tcp        0      0 0.0.0.0:6382            0.0.0.0:*               LISTEN      35863/redis-server  
tcp        0      0 0.0.0.0:6383            0.0.0.0:*               LISTEN      35860/redis-server  
tcp        0      0 0.0.0.0:6384            0.0.0.0:*               LISTEN      35861/redis-server  
tcp        0      0 0.0.0.0:6385            0.0.0.0:*      

# 集群模式启动,进程后会加上[cluster]的字样:
[root@db01 ~]# ps -ef | grep redis
root      13217  11734  0 12:14 pts/1    00:00:00 redis-cli -p 26379
root      32811   1095  0 15:28 pts/0    00:00:00 vim redis.conf cd
root      38526      1  0 16:13 ?        00:00:00 /app/redis/src/redis-server 0.0.0.0:6380 [cluster]
root      38528      1  0 16:13 ?        00:00:00 /app/redis/src/redis-server 0.0.0.0:6382 [cluster]
root      38529      1  0 16:13 ?        00:00:00 /app/redis/src/redis-server 0.0.0.0:6383 [cluster]
root      38530      1  0 16:13 ?        00:00:00 /app/redis/src/redis-server 0.0.0.0:6384 [cluster]
root      38531      1  0 16:13 ?        00:00:00 /app/redis/src/redis-server 0.0.0.0:6385 [cluster]
root      39960      1  0 16:25 ?        00:00:00 /app/redis/src/redis-server 0.0.0.0:6381 [cluster]
root      40043   1095  0 16:25 pts/0    00:00:00 grep --color=auto redis

# 配置文件目录下会生成一个关于集群的文件
[root@db01 6381]# ll
total 28
-rw-r--r-- 1 root root    88 Sep  5 16:25 dump.rdb
-rw-r--r-- 1 root root   176 Sep  5 16:12 nodes_6381.conf
-rw-r--r-- 1 root root   293 Sep  5 16:24 redis.conf
-rw-r--r-- 1 root root 11221 Sep  5 16:25 redis.log
-rw-r--r-- 1 root root     6 Sep  5 16:25 redis.pid

[root@db01 6381]# cat nodes_6381.conf 
2f7067f51c461d9f915a50a12a853ab5226e690b 127.0.0.1:6385@16385,,tls-port=0,shard-id=5eda7ec4b33b28de977fa4ae531fd0b22d207c05 master - 0 1693902902206 5 connected
# 第一段信息是这个Redis服务作为集群节点的一个身份编码
# 别名为集群的node-id

快捷施法(集合了,加入集群,主从配置,分配槽位)

## 未设置密码的情况
这个分片槽位随机
redis-cli --cluster create --cluster-replicas 1 172.16.1.51:7000 172.16.1.51:7001 172.16.1.52:7002 172.16.1.52:7003 172.16.1.53:7004 172.16.1.53:7005

加入集群

现在虽然说每个服务都成功启动了,但是彼此之间并没有任何联系。

所以下一步要做的就是将6个服务加入至一个集群中,如下操作示例

[root@db01 6381]# redis-cli -a 123 -p 6380
# 查看节点
127.0.0.1:6380> cluster nodes
ca9777dadb02d0562ea436ee921e88decdd6f666 :6380@16380 myself,master - 0 0 0 connected 8771
# 添加节点
127.0.0.1:6380> cluster meet 127.0.0.1 6381
OK
127.0.0.1:6380> cluster meet 127.0.0.1 6382
OK
127.0.0.1:6380> cluster meet 127.0.0.1 6383
OK
127.0.0.1:6380> cluster meet 127.0.0.1 6384
OK
127.0.0.1:6380> cluster meet 127.0.0.1 6385
OK
# 查看当前集群所有的节点:
127.0.0.1:6380> cluster nodes
fed8f36794212ff1a038b8e184998bd348c7bd09 127.0.0.1:6382@16382 master - 0 1693902901391 0 connected
4b170c8a20b3fbd471922a30bccbf2254d6aad46 127.0.0.1:6384@16384 master - 0 1693902903430 4 connected
49485f04d03a7aee83a04846fb9628f9924f2db7 127.0.0.1:6381@16381 master - 0 1693902901000 1 connected
2f7067f51c461d9f915a50a12a853ab5226e690b 127.0.0.1:6385@16385 master - 0 1693902901188 0 connected
4c5d55c72cf75f23ec1dc5ee33183f0d0a7596e7 127.0.0.1:6383@16383 master - 0 1693902902411 3 connected
ca9777dadb02d0562ea436ee921e88decdd6f666 127.0.0.1:6380@16380 myself,master - 0 1693902900000 2 connected 8771

查看端口监听

可以发现Gossip监听的1000+端口出现了,此时代表集群各个节点之间已经能互相通信了:

[root@db01 6381]# netstat -lntup | grep redis
tcp        0      0 0.0.0.0:6380            0.0.0.0:*               LISTEN      38526/redis-server  
tcp        0      0 0.0.0.0:6381            0.0.0.0:*               LISTEN      39960/redis-server  
tcp        0      0 0.0.0.0:6382            0.0.0.0:*               LISTEN      38528/redis-server  
tcp        0      0 0.0.0.0:6383            0.0.0.0:*               LISTEN      38529/redis-server  
tcp        0      0 0.0.0.0:6384            0.0.0.0:*               LISTEN      38530/redis-server  
tcp        0      0 0.0.0.0:6385            0.0.0.0:*               LISTEN      38531/redis-server  
tcp        0      0 0.0.0.0:16380           0.0.0.0:*               LISTEN      38526/redis-server  
tcp        0      0 0.0.0.0:16381           0.0.0.0:*               LISTEN      39960/redis-server  
tcp        0      0 0.0.0.0:16382           0.0.0.0:*               LISTEN      38528/redis-server  
tcp        0      0 0.0.0.0:16383           0.0.0.0:*               LISTEN      38529/redis-server  
tcp        0      0 0.0.0.0:16384           0.0.0.0:*               LISTEN      38530/redis-server  
tcp        0      0 0.0.0.0:16385           0.0.0.0:*               LISTEN      38531/redis-server  

主从配置

6个服务之间并没有任何主从关系,所以现在进行主从配置,记录下上面cluster nodes命令输出的node-id信息,只记录主节点:

[root@db01 6381]# redis-cli -a 123 -p 6380
127.0.0.1:6380> cluster nodes
fed8f36794212ff1a038b8e184998bd348c7bd09 127.0.0.1:6382@16382 master - 0 1693903318212 0 connected
4b170c8a20b3fbd471922a30bccbf2254d6aad46 127.0.0.1:6384@16384 master - 0 1693903317000 4 connected
49485f04d03a7aee83a04846fb9628f9924f2db7 127.0.0.1:6381@16381 master - 0 1693903318000 1 connected
2f7067f51c461d9f915a50a12a853ab5226e690b 127.0.0.1:6385@16385 master - 0 1693903319228 5 connected
4c5d55c72cf75f23ec1dc5ee33183f0d0a7596e7 127.0.0.1:6383@16383 master - 0 1693903318000 3 connected
ca9777dadb02d0562ea436ee921e88decdd6f666 127.0.0.1:6380@16380 myself,master - 0 1693903317000 2 connected 8771

# 首先是的6381,将它映射到6380:
[root@db01 6381]# redis-cli -a 123 -p 6381
127.0.0.1:6381> cluster replicate ca9777dadb02d0562ea436ee921e88decdd6f666
OK
# 首先是的6383,将它映射到6381:
[root@db01 6381]# redis-cli -a 123 -p 6383
127.0.0.1:6383> cluster replicate fed8f36794212ff1a038b8e184998bd348c7bd09
OK
# 首先是的6385,将它映射到6384:
[root@db01 6381]# redis-cli -a 123 -p 6385
127.0.0.1:6385> cluster replicate 4b170c8a20b3fbd471922a30bccbf2254d6aad46
OK

# 查看集群节点信息
[root@db01 6381]# redis-cli -a 123 -p 6380
127.0.0.1:6380> cluster nodes
fed8f36794212ff1a038b8e184998bd348c7bd09 127.0.0.1:6382@16382 master - 0 1693903861797 0 connected
4b170c8a20b3fbd471922a30bccbf2254d6aad46 127.0.0.1:6384@16384 master - 0 1693903860000 4 connected
49485f04d03a7aee83a04846fb9628f9924f2db7 127.0.0.1:6381@16381 slave ca9777dadb02d0562ea436ee921e88decdd6f666 0 1693903856000 2 connected
2f7067f51c461d9f915a50a12a853ab5226e690b 127.0.0.1:6385@16385 slave 4b170c8a20b3fbd471922a30bccbf2254d6aad46 0 1693903860779 4 connected
4c5d55c72cf75f23ec1dc5ee33183f0d0a7596e7 127.0.0.1:6383@16383 slave fed8f36794212ff1a038b8e184998bd348c7bd09 0 1693903858732 0 connected
ca9777dadb02d0562ea436ee921e88decdd6f666 127.0.0.1:6380@16380 myself,master - 0 1693903858000 2 connected 8771

分配槽位

# 删除槽位
127.0.0.1:6380> cluster delslots 8771
[root@db01 6381]# redis-cli -a 123 -p 6384 cluster delslots 8771

# 开始分配:
[root@db01 6381]# redis-cli -a 123 -p 6380 cluster addslots {0..5461}
[root@db01 6381]# redis-cli -a 123 -p 6384 cluster addslots {5462..10922}
[root@db01 6381]# redis-cli -a 123 -p 6382 cluster addslots {10923..16383}

# 检查槽位是否分配正确,这里进行内容截取:
127.0.0.1:6380> cluster nodes
fed8f36794212ff1a038b8e184998bd348c7bd09 127.0.0.1:6382@16382 master - 0 1693906834000 0 connected 10923-16383
4b170c8a20b3fbd471922a30bccbf2254d6aad46 127.0.0.1:6384@16384 master - 0 1693906835494 4 connected 5462-10922
ca9777dadb02d0562ea436ee921e88decdd6f666 127.0.0.1:6380@16380 myself,master - 0 1693906833000 2 connected 0-5461
127.0.0.1:6380> 

检查状态

使用以下命令检查集群状态是否ok,如果槽位全部分配完毕应该是ok,不然的话就检查你分配槽位时是否输错了数量:

127.0.0.1:6380> 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:5
cluster_my_epoch:2
cluster_stats_messages_ping_sent:3906
cluster_stats_messages_pong_sent:3955
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:7866
cluster_stats_messages_ping_received:3955
cluster_stats_messages_pong_received:3911
cluster_stats_messages_received:7866
total_cluster_links_buffer_limit_exceeded:0

MOVED重定向

现在我们在6380的master节点上进行写入:

$ redis-cli -a 123 -p 6380

node1:6379> set k1 "v1"
(error) MOVED 12706 192.168.0.140:6379

它会提示你去node2的master上进行写入。

这个就是MOVED重定向。

-c参数

如何解决这个问题?其实在登录的时候加上参数-c即可,-c参数无所谓你的Redis是否是集群模式,建议任何登录操作都加上,这样即使是Redis集群也会自动进行MOVED重定向:

[root@db01 6381]# redis-cli -c -a 123 -p 6380
127.0.0.1:6380> set k1 "v1"
-> Redirected to slot [12706] located at 127.0.0.1:6382
OK

一并对主从进行验证,这条数据是写入至了6382的Master中,我们登录6383的Slave中进行查看:

[root@db01 6381]# redis-cli -a 123 -p 6383
127.0.0.1:6383> keys *
1) "k1"

故障转移

# 模拟6380下线宕机,此时应该由6381接管它的工作:
127.0.0.1:6380> cluster nodes
fed8f36794212ff1a038b8e184998bd348c7bd09 127.0.0.1:6382@16382 master - 0 1693906834000 0 connected 10923-16383
4b170c8a20b3fbd471922a30bccbf2254d6aad46 127.0.0.1:6384@16384 master - 0 1693906835494 4 connected 5462-10922
49485f04d03a7aee83a04846fb9628f9924f2db7 127.0.0.1:6381@16381 slave ca9777dadb02d0562ea436ee921e88decdd6f666 0 1693906835000 2 connected
2f7067f51c461d9f915a50a12a853ab5226e690b 127.0.0.1:6385@16385 slave 4b170c8a20b3fbd471922a30bccbf2254d6aad46 0 1693906834471 4 connected
4c5d55c72cf75f23ec1dc5ee33183f0d0a7596e7 127.0.0.1:6383@16383 slave fed8f36794212ff1a038b8e184998bd348c7bd09 0 1693906833456 0 connected
ca9777dadb02d0562ea436ee921e88decdd6f666 127.0.0.1:6380@16380 myself,master - 0 1693906833000 2 connected 0-5461

# 停止主机,6380显示停止
[root@db01 6381]# systemctl stop redis6380.service 
[root@db01 6381]# redis-cli -a 123 -p 638
ca9777dadb02d0562ea436ee921e88decdd6f666 127.0.0.1:6380@16380 master,fail - 1693907549353 1693907545000 2 disconnected
49485f04d03a7aee83a04846fb9628f9924f2db7 127.0.0.1:6381@16381 master - 0 1693907570000 6 connected 0-5461

# 开启主机后变为从库
[root@db01 6381]# systemctl start redis6380.service 
[root@db01 6381]# redis-cli -a 123 -p 6380
127.0.0.1:6380> cluster nodes
49485f04d03a7aee83a04846fb9628f9924f2db7 127.0.0.1:6381@16381 master - 0 1693907688000 6 connected 0-5461
ca9777dadb02d0562ea436ee921e88decdd6f666 127.0.0.1:6380@16380 myself,slave 49485f04d03a7aee83a04846fb9628f9924f2db7 0 1693907689000 6 connected
posted @ 2023-10-09 08:40  普里莫  阅读(45)  评论(0编辑  收藏  举报