前言:在前一章了解redis的基本介绍后,这一章主要介绍redis的实战部署,文章有点长请一步步耐心看完,我相信肯定会有收获的,这里用的资源包是2022年最新的redis版本可能会跟旧版本不同,在此章节中redis的集群搭建是在同一台服务器中部署,通过开放不同端口实现
目录
一、redis主从集群的搭建
二、哨兵模式的搭建
三、分片集群的搭建
什么是插槽
如何转移插槽
集群自动故障转移
一、redis的主从集群搭建
先把redis的配置文件放到/tmp/文件夹下 方便操作和管理 比较直观
然后就是修改配置了
[root@giant-fox redis-7.0.4]# cp redis.conf /tmp/
[root@giant-fox tmp]# cd /tmp/
[root@giant-fox tmp]# mkdir -p {7001,7002,7003}
[root@giant-fox tmp]# cp redis.conf 7001/redis.conf
[root@giant-fox tmp]# cp redis.conf 7002/redis.conf
[root@giant-fox tmp]# cp redis.conf 7003/redis.conf
修改每一个端口的配置
首先关闭AOF持久化存储 打开RDB持久化存储 在主从节点做全局同步的时候用的是RDB持久化存储 因此做上述操作
[root@giant-fox tmp]# sed -i '1a replica-announce-ip 192.168.1.11' 7001/redis.conf
# 这一步是在第一行加入此内容 表明IP地址防止发生混乱
[root@giant-fox tmp]# cat 7001/redis.conf
replica-announce-ip 192.168.1.11
将#127.0.0.1 注释掉
bind 0.0.0.0
protected-mode no
port 6379
daemonize yes
replicaof 192.168.1.11 6381 # 让6381 成为master节点
# save “” 看这个是否注释掉 注释掉说明开启RDB存储
databases 1
dir /tmp/7001/
appendonly no 关闭AOF存储
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
#分别cp到7002 7003 修改配置文件
[root@giant-fox tmp]# sed -i -e 's/6379/6380/g' -e 's/dir .\//dir \/tmp\/7002\//g' 7002/redis.conf
[root@giant-fox tmp]# sed -i -e 's/6379/6381/g' -e 's/dir .\//dir \/tmp\/7003\//g' 7003/redis.conf
#启动redis
[root@giant-fox tmp]# redis-server 7001/redis.conf
[root@giant-fox tmp]# redis-server 7002/redis.conf
[root@giant-fox tmp]# redis-server 7003/redis.conf
[root@giant-fox tmp]# ps -ef |grep redis
root 15923 1 0 Oct26 ? 00:01:14 redis-server 0.0.0.0:6380
root 15929 1 0 Oct26 ? 00:01:17 redis-server 0.0.0.0:6381
root 16415 1 0 01:03 ? 00:00:00 redis-server 0.0.0.0:6379
root 16421 16375 0 01:03 pts/3 00:00:00 grep --color=auto redis
查看 6381端口是否成为master节点以及验证主从是否同步
[root@giant-fox tmp]# redis-cli -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.11,port=6380,state=online,offset=260603,lag=0
slave1:ip=192.168.1.11,port=6379,state=online,offset=260603,lag=0
master_failover_state:no-failover
master_replid:d7c6ccbe29e7ebd60a5b575de1df5eb326e4c01f
master_replid2:f196d3b45fe3f7922201d7363b2b76851b9b9a43
master_repl_offset:260742
second_repl_offset:84228
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:260728
127.0.0.1:6381> set name giantfox
OK
[root@giant-fox tmp]# redis-cli -p 6380
127.0.0.1:6380> get name
"giantfox"
OK 到此主从同步以及搭建完成 接一下搭建哨兵模式 监控整个集群
二、哨兵模式部署
先创建文件夹
[root@giant-fox tmp]# mkdir -p {27001,27002,27003}
[root@giant-fox tmp]# cat 27001/sentinel.conf
port 27001
#开放的端口
sentinel announce-ip "192.168.1.11"
#声明IP 防止发生混乱
sentinel monitor mymaster 192.168.1.11 6381 2
#监控集群 mymaster是集群的名字 后面是监控的集群的IP和端口 2是主观下线的依据 slave投票数超过2当前master就下线
sentinel down-after-milliseconds mymaster 5000
#master与slave断开链接时的超时时间
sentinel failover-timeout mymaster 60000
#宕机后故障恢复的超时时间 默认值就行
dir "/tmp/27001"
#目录
这里的sentinel配置文件可以copy过来 也可以自己写 比较简单
[root@giant-fox tmp]# cp 27001/sentinel.conf 27002/
[root@giant-fox tmp]# cp 27001/sentinel.conf 27003/
[root@giant-fox tmp]# sed -i -e 's/27001/27002/g' 27002/sentinel.conf
[root@giant-fox tmp]# sed -i -e 's/27001/27003/g' 27003/sentinel.conf
修改三个哨兵的端口
开启哨兵 哨兵是前端开启的 所以我们要打开三个窗口去观察
克隆窗口即可 然后在每个窗口开启不同端口的哨兵
[root@giant-fox tmp]# redis-sentinel 27001/sentinel.conf
[root@giant-fox tmp]# redis-sentinel 27002/sentinel.conf
[root@giant-fox tmp]# redis-sentinel 27003/sentinel.conf
开启成功
接一下模拟宕机 验证是否重新选择新的master
[root@giant-fox tmp]# ps -ef |grep redis
root 15923 1 0 Oct26 ? 00:01:16 redis-server 0.0.0.0:6380
root 15929 1 0 Oct26 ? 00:01:19 redis-server 0.0.0.0:6381
root 16415 1 0 01:03 ? 00:00:01 redis-server 0.0.0.0:6379
root 16451 16258 0 01:13 pts/0 00:00:00 redis-sentinel *:27001 [sentinel]
root 16457 16324 0 01:13 pts/1 00:00:00 redis-sentinel *:27002 [sentinel]
root 16463 16341 0 01:14 pts/2 00:00:00 redis-sentinel *:27003 [sentinel]
[root@giant-fox tmp]# kill -9 15929
16463:X 27 Oct 2022 01:14:47.609 # +switch-master mymaster 192.168.1.11 6381 192.168.1.11 6379
16463:X 27 Oct 2022 01:14:47.609 * +slave slave 192.168.1.11:6380 192.168.1.11 6380 @ mymaster 192.168.1.11 6379
16463:X 27 Oct 2022 01:14:47.609 * +slave slave 192.168.1.11:6381 192.168.1.11 6381 @ mymaster 192.168.1.11 6379
16463:X 27 Oct 2022 01:14:47.611 * Sentinel new configuration saved on disk
16463:X 27 Oct 2022 01:14:52.680 # +sdown slave 192.168.1.11:6381 192.168.1.11 6381 @ mymaster 192.168.1.11 6379
[root@giant-fox tmp]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.11,port=6380,state=online,offset=356954,lag=0
master_failover_state:no-failover
master_replid:44b61d7519dc2471a43b75fad58cfa6e1330b7f5
master_replid2:d7c6ccbe29e7ebd60a5b575de1df5eb326e4c01f
master_repl_offset:357232
second_repl_offset:345450
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:231195
repl_backlog_histlen:126038
至此哨兵部署成功
三、分片集群的部署
这里需要先理解一下什么是分片集群
在搭建分片集群之前需要把之前配置的环境删除掉
[root@giant-fox tmp]# ps -ef |grep redis
root 16488 16375 0 01:29 pts/3 00:00:00 grep --color=auto redis
跟之前一样,先创建文件夹
[root@giant-fox tmp]# mkdir -p {7001,7002,7003,8001,8002,8003}
[root@giant-fox tmp]# ls
7001 7002 7003 8001 8002 8003
[root@giant-fox 7001]# vim redis.conf
[root@giant-fox tmp]# cat 7001/redis.conf #这里的配置环境不一样
port 7001
cluster-enabled yes #默认是关闭的需要开启
cluster-config-file /tmp/7001/nodes.conf
cluster-node-timeout 5000
dir /tmp/7001
bind 0.0.0.0
daemonize yes
replica-announce-ip 192.168.1.11
protected-mode no
databases 1
logfile /tmp/7001/run.log
[root@giant-fox tmp]# cp 7001/redis.conf 7002/
[root@giant-fox tmp]# cp 7001/redis.conf 7003/
[root@giant-fox tmp]# cp 7001/redis.conf 8001/
[root@giant-fox tmp]# cp 7001/redis.conf 8002/
[root@giant-fox tmp]# cp 7001/redis.conf 8003/
[root@giant-fox tmp]# printf '%s\n' 7001 7002 7003 8001 8002 8003 | xargs -I{} -t sed -i 's/7001/{}/g' {}/redis.conf
# 这里命令可以实现一键修改 很方便
sed -i s/7001/7001/g 7001/redis.conf
sed -i s/7001/7002/g 7002/redis.conf
sed -i s/7001/7003/g 7003/redis.conf
sed -i s/7001/8001/g 8001/redis.conf
sed -i s/7001/8002/g 8002/redis.conf
sed -i s/7001/8003/g 8003/redis.conf
#使用一键部署命令启动redis服务
[root@giant-fox tmp]# printf '%s\n' 7001 7002 7003 8001 8002 8003 | xargs -I{} -t redis-server {}/redis.conf
redis-server 7001/redis.conf
redis-server 7002/redis.conf
redis-server 7003/redis.conf
redis-server 8001/redis.conf
redis-server 8002/redis.conf
redis-server 8003/redis.conf
[root@giant-fox tmp]# ps -ef |grep redis
root 16609 1 0 01:46 ? 00:00:00 redis-server 0.0.0.0:7001 [cluster]
root 16628 1 0 01:48 ? 00:00:00 redis-server 0.0.0.0:7002 [cluster]
root 16630 1 0 01:48 ? 00:00:00 redis-server 0.0.0.0:7003 [cluster]
root 16632 1 0 01:48 ? 00:00:00 redis-server 0.0.0.0:8001 [cluster]
root 16646 1 0 01:48 ? 00:00:00 redis-server 0.0.0.0:8002 [cluster]
root 16652 1 0 01:48 ? 00:00:00 redis-server 0.0.0.0:8003 [cluster]
root 16658 16375 0 01:48 pts/3 00:00:00 grep --color=auto redis
#绑定我们的集群 可以通过redis-cli --cluster help 查看具体帮助
[root@giant-fox tmp]# redis-cli --cluster create --cluster-replicas 1 192.168.1.11:7001 192.168.1.11:7002 192.168.1.11:7003 192.168.1.11:8001 192.168.1.11:8002 192.168.1.11:7001 192.168.1.11:8003
>>> Performing hash slots allocation on 7 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.1.11:8002 to 192.168.1.11:7001
Adding replica 192.168.1.11:7001 to 192.168.1.11:7002
Adding replica 192.168.1.11:8003 to 192.168.1.11:7003
Adding extra replicas...
Adding replica 192.168.1.11:8001 to 192.168.1.11:7001
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 59ba851a2cb5328aef57173195e69feb3e1a6723 192.168.1.11:7001
slots:[0-5460] (5461 slots) master
M: 747408641dec3bb72142471e9b70ec64fa5627c4 192.168.1.11:7002
slots:[5461-10922] (5462 slots) master
M: d2e0edf0c32338cf51d275eb85b3c70a696ab524 192.168.1.11:7003
slots:[10923-16383] (5461 slots) master
S: 634792def7229bbdcca455fd7e0bbefb9a4b8f0e 192.168.1.11:8001
replicates d2e0edf0c32338cf51d275eb85b3c70a696ab524
S: 57d5a76bb12eba77460386ee2a6a279f17406336 192.168.1.11:8002
replicates 747408641dec3bb72142471e9b70ec64fa5627c4
S: 59ba851a2cb5328aef57173195e69feb3e1a6723 192.168.1.11:7001
replicates 59ba851a2cb5328aef57173195e69feb3e1a6723
S: 4179bbd0454246f65977959784a79831237f9768 192.168.1.11:8003
replicates 59ba851a2cb5328aef57173195e69feb3e1a6723
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.1.11:7001)
M: 59ba851a2cb5328aef57173195e69feb3e1a6723 192.168.1.11:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 634792def7229bbdcca455fd7e0bbefb9a4b8f0e 192.168.1.11:8001
slots: (0 slots) slave
replicates d2e0edf0c32338cf51d275eb85b3c70a696ab524
M: 747408641dec3bb72142471e9b70ec64fa5627c4 192.168.1.11:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 57d5a76bb12eba77460386ee2a6a279f17406336 192.168.1.11:8002
slots: (0 slots) slave
replicates 747408641dec3bb72142471e9b70ec64fa5627c4
S: 4179bbd0454246f65977959784a79831237f9768 192.168.1.11:8003
slots: (0 slots) slave
replicates 59ba851a2cb5328aef57173195e69feb3e1a6723
M: d2e0edf0c32338cf51d275eb85b3c70a696ab524 192.168.1.11:7003
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.
可用通过 [root@giant-fox tmp]# redis-cli -p 7001 cluster nodes 查看集群信息 任意选择一个端口都可以查看
到这里集群的搭建已经完成了
插槽的概念
插槽是Redis对Key进行分片的单元。在Redis的集群实现中,内置了数据自动分片机
制,集群内部会将所有的key映射到16384个插槽中,集群中的每个数据库实例负责其中部
分的插槽的读写。Redis会将key的有效部分,使用CRC16算法计算出散列值,然后对16384取余数,从而把key分配到插槽中。
#插槽Key跟集群绑定
[root@giant-fox tmp]# redis-cli -c -p 7001
127.0.0.1:7001> set num 123
OK
127.0.0.1:7001> get num
"123"
127.0.0.1:7001> set a 1
-> Redirected to slot [15495] located at 192.168.1.11:7003
OK
192.168.1.11:7003> get num
#可以看到端口发生了变化 因为a的插槽在7003端口上
-> Redirected to slot [2765] located at 192.168.1.11:7001
"123"
#控制某一类实例控制在同意插槽当中
192.168.1.11:7001> set {a}num 11
-> Redirected to slot [15495] located at 192.168.1.11:7003
OK
将7004端口添加到集群当中
[root@giant-fox tmp]# mkdir 7004
[root@giant-fox tmp]# cp 7001/redis.conf 7004/
[root@giant-fox tmp]# sed -i 's/7001/7004/g' 7004/redis.conf
[root@giant-fox tmp]# redis-server 7004/redis.conf
[root@giant-fox tmp]# ps -ef |grep redis
root 16609 1 0 01:46 ? 00:00:01 redis-server 0.0.0.0:7001 [cluster]
root 16628 1 0 01:48 ? 00:00:01 redis-server 0.0.0.0:7002 [cluster]
root 16630 1 0 01:48 ? 00:00:01 redis-server 0.0.0.0:7003 [cluster]
root 16632 1 0 01:48 ? 00:00:01 redis-server 0.0.0.0:8001 [cluster]
root 16646 1 0 01:48 ? 00:00:01 redis-server 0.0.0.0:8002 [cluster]
root 16652 1 0 01:48 ? 00:00:01 redis-server 0.0.0.0:8003 [cluster]
root 16687 1 0 02:03 ? 00:00:00 redis-server 0.0.0.0:7004 [cluster]
[root@giant-fox tmp]# redis-cli cluster nodes
Could not connect to Redis at 127.0.0.1:6379: Connection refused
[root@giant-fox tmp]# redis-cli -p 7001 cluster nodes
634792def7229bbdcca455fd7e0bbefb9a4b8f0e 192.168.1.11:8001@18001 slave d2e0edf0c32338cf51d275eb85b3c70a696ab524 0 1666836369561 3 connected
747408641dec3bb72142471e9b70ec64fa5627c4 192.168.1.11:7002@17002 master - 0 1666836369561 2 connected 5461-10922
59ba851a2cb5328aef57173195e69feb3e1a6723 192.168.1.11:7001@17001 myself,master - 0 1666836369000 1 connected 0-5460
57d5a76bb12eba77460386ee2a6a279f17406336 192.168.1.11:8002@18002 slave 747408641dec3bb72142471e9b70ec64fa5627c4 0 1666836369561 2 connected
4179bbd0454246f65977959784a79831237f9768 192.168.1.11:8003@18003 slave 59ba851a2cb5328aef57173195e69feb3e1a6723 0 1666836369059 1 connected
619d436d000e4c80e33c8e4ebaaae484f5e41b49 192.168.1.11:7004@17004 master - 0 1666836370062 0 connected
d2e0edf0c32338cf51d275eb85b3c70a696ab524 192.168.1.11:7003@17003 master - 0 1666836370062 3 connected 10923-16383
添加成功
#插槽转移
[root@giant-fox tmp]# redis-cli --cluster reshard 192.168.1.11:7001
到此插槽转移成功 其中最下方是问你 需要转移多少插槽 你转移3000个 然后问你转移插槽的ID是哪个填上面7001端口的ID 接受的端口是7004端口的ID 最后done结束