1:集群
Redis 集群实现了对Redis的水平扩容,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N。Redis 集群通过分区(partition)来提供一定程度的可用性(availability): 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。
集群两种实现方式:
1:代理主机
2:去中心化配置
2:搭建集群
6台服务器搭建Redis集群:端口号分别为6379,6380,6381,6389,6390,6391。
主服务器分别为6379,6380,6381。
2.1:配置防火墙开启6个端口号
firewall-cmd --permanent --add-port=端口号/tcp
#开启后查询是否开启
firewall-cmd --query-port=端口号/tcp
#重启firewall
firewall-cmd --reload
2.2:配置xxx.conf文件
include redis.conf #引入redis.conf
port 端口号
pidfile /var/run/redis_端口号.pid
dbfilename dump端口号.rdb
cluster-enabled yes
cluster-config-file nodes-端口号.conf
cluster-node-timeout 15000 #失联时间超过15000毫秒即可切换主机
比如设置6379端口的conf文件:名为redis6379.conf
include /myRedis/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
为每一个redis服务设置conf文件。
2.3:启动6台Redis服务器
[root@LKCentos01 myRedis]# redis-server redis6379.conf
[root@LKCentos01 myRedis]# redis-server redis6380.conf
[root@LKCentos01 myRedis]# redis-server redis6381.conf
[root@LKCentos01 myRedis]# redis-server redis6389.conf
[root@LKCentos01 myRedis]# redis-server redis6390.conf
[root@LKCentos01 myRedis]# redis-server redis6391.conf
[root@LKCentos01 myRedis]# ps -ef | grep redis
root 16227 1 0 15:09 ? 00:00:00 redis-server *:6379 [cluster]
root 16235 1 0 15:09 ? 00:00:00 redis-server *:6380 [cluster]
root 16241 1 0 15:09 ? 00:00:00 redis-server *:6381 [cluster]
root 16247 1 0 15:09 ? 00:00:00 redis-server *:6389 [cluster]
root 16267 1 0 15:10 ? 00:00:00 redis-server *:6390 [cluster]
root 16273 1 0 15:10 ? 00:00:00 redis-server *:6391 [cluster]
root 16279 15709 0 15:10 pts/0 00:00:00 grep --color=auto redis
2.4:查看目录下是否生成结点配置文件
结点配置文件:nodes-端口号.conf
生成结点的配置文件:
[root@LKCentos01 myRedis]# ls
nodes-6380.conf nodes-6389.conf nodes-6391.conf redis6380.conf redis6389.conf redis6391.conf sentinel.conf
nodes-6379.conf nodes-6381.conf nodes-6390.conf redis6379.conf redis6381.conf redis6390.conf redis.conf
2.5:合体结点成为集群
需要进入redis的安装目录下的src目录下:
使用此命令搭建:
redis-cli --cluster create IP1:6379 IP2:6380 IP3:6381 IP4:6389 IP5:6390 IP6:6391 --cluster-replicas 1
–clister-replicas 1表示一台主机一台从机,刚好三组。
2.6:查看集群信息
使用此命令进入redis服务:集群为无中心化配置,无论从哪一个端口都可进入集群。
redis-cli -c -p 端口号
查看集群信息:CLUSTER NODES
master为主,slave为从。
127.0.0.1:6379> CLUSTER NODES
34e6d31d8d0100319922f8f137766613fced0741 192.168.111.130:6379@16379 myself,master - 0 1651059170000 1 connected 0-5460
9c8ef059ab65b15f05cba65188088edb8c6e96e1 192.168.111.130:6381@16381 master - 0 1651059172467 3 connected 10923-16383
a34c810bfb5520a04fa56ae9020e0a41c15fb8af 192.168.111.130:6391@16391 slave 34e6d31d8d0100319922f8f137766613fced0741 0 1651059172000 1 connected
0ed5e63e44bfbe07b89e5493f7a21111cf8298d8 192.168.111.130:6390@16390 slave 9c8ef059ab65b15f05cba65188088edb8c6e96e1 0 1651059172000 3 connected
025ee04022814bbcdff67038bcdafb32501b72d7 192.168.111.130:6389@16389 slave 5ff3619a812241945c7477009bb889e7aa1d489b 0 1651059171000 2 connected
5ff3619a812241945c7477009bb889e7aa1d489b 192.168.111.130:6380@16380 master - 0 1651059173472 2 connected 5461-10922
3:故障恢复
3.1:宕机
此时让主机6379宕机,查看6379的从机是否会变为新的主机。
查看进行,直接杀掉6379的进程即可。
[root@centos7 src]# ps -ef | grep redis
root 42944 1 0 19:31 ? 00:00:00 redis-server *:6379 [cluster]
root 42950 1 0 19:31 ? 00:00:00 redis-server *:6380 [cluster]
root 42958 1 0 19:31 ? 00:00:00 redis-server *:6381 [cluster]
root 42964 1 0 19:31 ? 00:00:00 redis-server *:6389 [cluster]
root 42970 1 0 19:31 ? 00:00:00 redis-server *:6390 [cluster]
root 42978 1 0 19:31 ? 00:00:00 redis-server *:6391 [cluster]
root 43273 36142 0 19:35 pts/2 00:00:00 grep --color=auto redis
[root@centos7 src]# kill -9 42944
3.2:查看是否有人替代
此时的6379端口的主机宕机,重新进入集群中,查看结点信息:
可用看到6379fail,而它的从机6391变为新的主机。
127.0.0.1:6380> CLUSTER NODES
5ff3619a812241945c7477009bb889e7aa1d489b 192.168.111.130:6380@16380 myself,master - 0 1651059432000 2 connected 5461-10922
025ee04022814bbcdff67038bcdafb32501b72d7 192.168.111.130:6389@16389 slave 5ff3619a812241945c7477009bb889e7aa1d489b 0 1651059428000 2 connected
0ed5e63e44bfbe07b89e5493f7a21111cf8298d8 192.168.111.130:6390@16390 slave 9c8ef059ab65b15f05cba65188088edb8c6e96e1 0 1651059433605 3 connected
a34c810bfb5520a04fa56ae9020e0a41c15fb8af 192.168.111.130:6391@16391 master - 0 1651059432000 7 connected 0-5460
9c8ef059ab65b15f05cba65188088edb8c6e96e1 192.168.111.130:6381@16381 master - 0 1651059432000 3 connected 10923-16383
34e6d31d8d0100319922f8f137766613fced0741 192.168.111.130:6379@16379 master,fail - 1651059366391 1651059364000 1 disconnected
3.3:恢复运行
让宕机的6379继续运行:
redis-server redis6379.conf
再次进入集群,查看6379主从性:
此时的6379作为6391的从机运行
[root@centos7 myRedis]# redis-cli -c -p 6379
127.0.0.1:6379> CLUSTER NODES
9c8ef059ab65b15f05cba65188088edb8c6e96e1 192.168.111.130:6381@16381 master - 0 1651060505000 3 connected 10923-16383
a34c810bfb5520a04fa56ae9020e0a41c15fb8af 192.168.111.130:6391@16391 master - 0 1651060504000 7 connected 0-5460
0ed5e63e44bfbe07b89e5493f7a21111cf8298d8 192.168.111.130:6390@16390 slave 9c8ef059ab65b15f05cba65188088edb8c6e96e1 0 1651060504000 3 connected
5ff3619a812241945c7477009bb889e7aa1d489b 192.168.111.130:6380@16380 master - 0 1651060503382 2 connected 5461-10922
34e6d31d8d0100319922f8f137766613fced0741 192.168.111.130:6379@16379 myself,slave a34c810bfb5520a04fa56ae9020e0a41c15fb8af 0 1651060503000 7 connected
025ee04022814bbcdff67038bcdafb32501b72d7 192.168.111.130:6389@16389 slave 5ff3619a812241945c7477009bb889e7aa1d489b 0 1651060505395 2 connected