Redis

关注公众号 jb51net

关闭
首页 > 数据库 > Redis > Redis集群关闭重启

Redis集群的关闭与重启操作

作者:郝少

这篇文章主要介绍了Redis集群的关闭与重启操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Redis集群关闭与重启

1、注意

[root@master bin]# ./redis-cli --cluster create 192.168.230.21:7001 192.168.230.21:7002 192.168.230.21:7003 192.168.230.21:8001 192.168.230.21:8002 192.168.230.21:8003 --cluster-replicas 1 -a 123456

上面的命令只能在新创健集群的时候执行一次,目的是为了建立内部各个节点的对应关系,比如主从关系,这些关系仅且只能在一个集群中初始化时对应一次;

如果再此执行,则会出现如下错误:

[root@master bin]# ./redis-cli --cluster create 192.168.230.21:7001 192.168.230.21:7002 192.168.230.21:7003 192.168.230.21:8001 192.168.230.21:8002 192.168.230.21:8003 --cluster-replicas 1 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[ERR] Node 192.168.230.21:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

2、集群关闭

集群关闭直接将各个节点的进程kill掉即可;

[root@master bin]# ps -ef | grep redis
root      11516      1  0 16:15 ?        00:00:10 ./redis-server 192.168.230.21:7002 [cluster]
root      11521      1  0 16:15 ?        00:00:09 ./redis-server 192.168.230.21:7003 [cluster]
root      11526      1  0 16:15 ?        00:00:10 ./redis-server 192.168.230.21:8001 [cluster]
root      11531      1  0 16:15 ?        00:00:10 ./redis-server 192.168.230.21:8002 [cluster]
root      11536      1  0 16:15 ?        00:00:10 ./redis-server 192.168.230.21:8003 [cluster]
root      11869      1  0 16:33 ?        00:00:07 ./redis-server 192.168.230.21:7001 [cluster]
root      12528   9737  0 17:39 pts/7    00:00:00 grep --color=auto redis
[root@master bin]# kill -9 11516
[root@master bin]# kill -9 11521
[root@master bin]# kill -9 11526
[root@master bin]# kill -9 11531
[root@master bin]# kill -9 11536
[root@master bin]# kill -9 11869
[root@master bin]# ps -ef | grep redis
root      12552   9737  0 17:40 pts/7    00:00:00 grep --color=auto redis

3、集群开启及连接

(1)集群开启

[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis01/redis.conf 
12553:C 31 Mar 2020 17:40:59.875 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12553:C 31 Mar 2020 17:40:59.875 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12553, just started
12553:C 31 Mar 2020 17:40:59.875 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis02/redis.conf 
12558:C 31 Mar 2020 17:41:03.697 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12558:C 31 Mar 2020 17:41:03.697 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12558, just started
12558:C 31 Mar 2020 17:41:03.697 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis03/redis.conf 
12563:C 31 Mar 2020 17:41:06.702 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12563:C 31 Mar 2020 17:41:06.702 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12563, just started
12563:C 31 Mar 2020 17:41:06.702 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis04/redis.conf 
12568:C 31 Mar 2020 17:41:09.742 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12568:C 31 Mar 2020 17:41:09.742 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12568, just started
12568:C 31 Mar 2020 17:41:09.742 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis05/redis.conf 
12574:C 31 Mar 2020 17:41:12.760 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12574:C 31 Mar 2020 17:41:12.760 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12574, just started
12574:C 31 Mar 2020 17:41:12.760 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis06/redis.conf 
12580:C 31 Mar 2020 17:41:16.406 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12580:C 31 Mar 2020 17:41:16.406 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12580, just started
12580:C 31 Mar 2020 17:41:16.406 # Configuration loaded
[root@master bin]# ps -ef | grep redis
root      12554      1  0 17:40 ?        00:00:00 ./redis-server 192.168.230.21:7001 [cluster]
root      12559      1  0 17:41 ?        00:00:00 ./redis-server 192.168.230.21:7002 [cluster]
root      12564      1  0 17:41 ?        00:00:00 ./redis-server 192.168.230.21:7003 [cluster]
root      12569      1  0 17:41 ?        00:00:00 ./redis-server 192.168.230.21:8001 [cluster]
root      12575      1  0 17:41 ?        00:00:00 ./redis-server 192.168.230.21:8002 [cluster]
root      12581      1  0 17:41 ?        00:00:00 ./redis-server 192.168.230.21:8003 [cluster]
root      12587   9737  0 17:41 pts/7    00:00:00 grep --color=auto redis

(2)集群连接

[root@master bin]# ./redis-cli -p 7001 -a 123456 -h 192.168.230.21 -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.230.21:7001> DBSIZE
(integer) 2
192.168.230.21:7001> keys *
1) "aa"
2) "ss"
192.168.230.21:7001> set str STR
-> Redirected to slot [6928] located at 192.168.230.21:7002
OK
192.168.230.21:7002>

Redis的三种集群方式

redis有三种集群方式:主从复制,哨兵模式和集群。

1.主从复制

主从复制原理:

主从复制优缺点:

优点:

缺点:

2.哨兵模式

当主服务器中断服务后,可以将一个从服务器升级为主服务器,以便继续提供服务,但是这个过程需要人工手动来操作。 为此,Redis 2.8中提供了哨兵工具来实现自动化的系统监控和故障恢复功能。

哨兵的作用就是监控Redis系统的运行状况。它的功能包括以下两个。

(1)监控主服务器和从服务器是否正常运行。

(2)主服务器出现故障时自动将从服务器转换为主服务器。

哨兵的工作方式:

哨兵模式的优缺点

优点:

缺点:

3.Redis-Cluster集群

redis的哨兵模式基本已经可以实现高可用,读写分离 ,但是在这种模式下每台redis服务器都存储相同的数据,很浪费内存,所以在redis3.0上加入了cluster模式,实现的redis的分布式存储,也就是说每台redis节点上存储不同的内容。

Redis-Cluster采用无中心结构,它的特点如下:

工作方式:

在redis的每一个节点上,都有这么两个东西,一个是插槽(slot),它的的取值范围是:0-16383。还有一个就是cluster,可以理解为是一个集群管理的插件。当我们的存取的key到达的时候,redis会根据crc16的算法得出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,通过这个值,去找到对应的插槽所对应的节点,然后直接自动跳转到这个对应的节点上进行存取操作。

为了保证高可用,redis-cluster集群引入了主从模式,一个主节点对应一个或者多个从节点,当主节点宕机的时候,就会启用从节点。当其它主节点ping一个主节点A时,如果半数以上的主节点与A通信超时,那么认为主节点A宕机了。如果主节点A和它的从节点A1都宕机了,那么该集群就无法再提供服务了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文