首页 > 编程开发 > 数据库教程 > redis教程

redis 集群使用主从复制架构-redis集群管理

2016-07-26 00:02:35| 发布: | 浏览: 1365

 redis集群使用主从架构如下图,能有效解决集群中节点连接不上造成集群挂掉的情况
 a) 在Redis集群中可以使用主从模式实现某一个节点的高可用
 b) 当该节点(master)宕机后,集群会将该节点的从数据库(slave)转变为(master)继续完成集群服务;


创建主从集群

 参考前面的主从复制和集群环境搭建,启动6个redis实例,分别是:
 6380(主) 6480(从)
 6381(主) 6481(从)
 6382(主) 6482(从)


[root@master redis-cluster-master-slave]# pwd

 /opt/redis/redis-cluster-master-slave
 [root@master redis-cluster-master-slave]# tree
 ├── 6380
 │   └── redis.conf
 ├── 6381
 │   └── redis.conf
 ├── 6382
 │   └── redis.conf
 ├── 6480
 │   └── redis.conf
 ├── 6481
 │   └── redis.conf
 └── 6482
     └── redis.conf


启动redis-server

 [root@master redis-cluster-master-slave]# /usr/local/bin/redis-server /opt/redis/redis-cluster-master-slave/6380/redis.conf
 [root@master redis-cluster-master-slave]# /usr/local/bin/redis-server /opt/redis/redis-cluster-master-slave/6381/redis.conf
 [root@master redis-cluster-master-slave]# /usr/local/bin/redis-server /opt/redis/redis-cluster-master-slave/6382/redis.conf
 [root@master redis-cluster-master-slave]# /usr/local/bin/redis-server /opt/redis/redis-cluster-master-slave/6480/redis.conf
 [root@master redis-cluster-master-slave]# /usr/local/bin/redis-server /opt/redis/redis-cluster-master-slave/6481/redis.conf
 [root@master redis-cluster-master-slave]# /usr/local/bin/redis-server /opt/redis/redis-cluster-master-slave/6482/redis.conf


创建集群,指定了从库数量为1,创建顺序为主库(3个)、从库(3个)

 [root@master redis-cluster-master-slave]# /opt/redis/redis-3.2.1/src/redis-trib.rb create --replicas 1 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382 192.168.56.101:6480 192.168.56.101:6481 192.168.56.101:6482
 >>> Creating cluster
 >>> Performing hash slots allocation on 6 nodes...
  Using 3 masters:
  192.168.56.101:6380
  192.168.56.101:6381
  192.168.56.101:6382
  Adding replica 192.168.56.101:6480 to 192.168.56.101:6380
  Adding replica 192.168.56.101:6481 to 192.168.56.101:6381
  Adding replica 192.168.56.101:6482 to 192.168.56.101:6382
 M: 768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15 192.168.56.101:6380
    slots:0-5460 (5461 slots) master
 M: 87d7a6fba680f6826fe680a73ab9ea692aa1b03f 192.168.56.101:6381
    slots:5461-10922 (5462 slots) master
 M: aba2eba21bbe0b39423afd940a40e692c4fe7408 192.168.56.101:6382
    slots:10923-16383 (5461 slots) master
 S: 74f0fd0468eefad443bcd4627069d217e9213c72 192.168.56.101:6480
    replicates 768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15
 S: 4ce9eab1382bc2e1886c29df3b4263e701bf5e6e 192.168.56.101:6481
    replicates 87d7a6fba680f6826fe680a73ab9ea692aa1b03f
 S: 4d09704a783936c024305797dec32e5d1debe922 192.168.56.101:6482
    replicates aba2eba21bbe0b39423afd940a40e692c4fe7408
 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.56.101:6380)
 M: 768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15 192.168.56.101:6380
    slots:0-5460 (5461 slots) master
 M: 87d7a6fba680f6826fe680a73ab9ea692aa1b03f 192.168.56.101:6381
    slots:5461-10922 (5462 slots) master
 M: aba2eba21bbe0b39423afd940a40e692c4fe7408 192.168.56.101:6382
    slots:10923-16383 (5461 slots) master
 M: 74f0fd0468eefad443bcd4627069d217e9213c72 192.168.56.101:6480
    slots: (0 slots) master
    replicates 768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15
 M: 4ce9eab1382bc2e1886c29df3b4263e701bf5e6e 192.168.56.101:6481
    slots: (0 slots) master
    replicates 87d7a6fba680f6826fe680a73ab9ea692aa1b03f
 M: 4d09704a783936c024305797dec32e5d1debe922 192.168.56.101:6482
    slots: (0 slots) master
    replicates aba2eba21bbe0b39423afd940a40e692c4fe7408
 [OK] All nodes agree about slots configuration.
 >>> Check for open slots...
 >>> Check slots coverage...
 [OK] All 16384 slots covered.


查看集群节点信息

 192.168.56.101:6380> cluster nodes
  74f0fd0468eefad443bcd4627069d217e9213c72 192.168.56.101:6480 slave 768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15 0 1468862135140 4 connected
  4ce9eab1382bc2e1886c29df3b4263e701bf5e6e 192.168.56.101:6481 slave 87d7a6fba680f6826fe680a73ab9ea692aa1b03f 0 1468862137161 5 connected
  87d7a6fba680f6826fe680a73ab9ea692aa1b03f 192.168.56.101:6381 master - 0 1468862133120 2 connected 5461-10922
  768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15 192.168.56.101:6380 myself,master - 0 0 1 connected 0-5460
  aba2eba21bbe0b39423afd940a40e692c4fe7408 192.168.56.101:6382 master - 0 1468862138173 3 connected 10923-16383
  4d09704a783936c024305797dec32e5d1debe922 192.168.56.101:6482 slave aba2eba21bbe0b39423afd940a40e692c4fe7408 0 1468862136149 6 connected


测试集群

 1、添加和查询数据
 192.168.56.101:6380> set abc 123
 -> Redirected to slot [7638] located at 192.168.56.101:6381
 OK
 192.168.56.101:6480> get abc
 -> Redirected to slot [7638] located at 192.168.56.101:6381
 "123"
 192.168.56.101:6480> keys *
 (empty list or set)
 [root@master redis-cluster]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6481 -c
 192.168.56.101:6481> keys *
 1) "abc"


添加和查询都正常,说明集群运行正常


2、测试集群中slave节点宕机

 将6480节点kill掉,查看情况。



查看集群节点信息,发现6480已经断开连接:

 192.168.56.101:6481> cluster nodes
 aba2eba21bbe0b39423afd940a40e692c4fe7408 192.168.56.101:6382 master - 0 1468862952812 3 connected 10923-16383
 87d7a6fba680f6826fe680a73ab9ea692aa1b03f 192.168.56.101:6381 master - 0 1468862953824 2 connected 5461-10922
  74f0fd0468eefad443bcd4627069d217e9213c72 192.168.56.101:6480 slave,fail 768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15 1468862829946 1468862827923 4 disconnected
 4d09704a783936c024305797dec32e5d1debe922 192.168.56.101:6482 slave aba2eba21bbe0b39423afd940a40e692c4fe7408 0 1468862951799 3 connected
 768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15 192.168.56.101:6380 master - 0 1468862949770 1 connected 0-5460
 4ce9eab1382bc2e1886c29df3b4263e701bf5e6e 192.168.56.101:6481 myself,slave 87d7a6fba680f6826fe680a73ab9ea692aa1b03f 0 0 5 connected


连接集群中的客户端,发现集群可用,可见从数据库宕机不会影响集群正常服务

 192.168.56.101:6481> get abc
 -> Redirected to slot [7638] located at 192.168.56.101:6381
 "123"
 192.168.56.101:6381> set k1 111
 -> Redirected to slot [12706] located at 192.168.56.101:6382
 OK
 192.168.56.101:6382> get k1
 "111"
 192.168.56.101:6382>
 [root@master redis-cluster]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6482 -c
 192.168.56.101:6482> get k1
 -> Redirected to slot [12706] located at 192.168.56.101:6382
 "111"
 192.168.56.101:6382>
 [root@master redis-cluster]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6382 -c
 192.168.56.101:6382> get k1
 "111"


恢复6480服务:

 [root@master redis-cluster-master-slave]# /usr/local/bin/redis-server /opt/redis/redis-cluster-master-slave/6480/redis.conf


测试6480中的数据:

 192.168.56.101:6480> get k1
 -> Redirected to slot [12706] located at 192.168.56.101:6382
 "111"


3、测试集群中master宕机

 假设6380宕机:



查看集群情况:

 192.168.56.101:6382> cluster nodes
  768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15 192.168.56.101:6380 master,fail - 1468863435404 1468863431554 1 disconnected
 4d09704a783936c024305797dec32e5d1debe922 192.168.56.101:6482 slave aba2eba21bbe0b39423afd940a40e692c4fe7408 0 1468863595024 6 connected
 aba2eba21bbe0b39423afd940a40e692c4fe7408 192.168.56.101:6382 myself,master - 0 0 3 connected 10923-16383
  74f0fd0468eefad443bcd4627069d217e9213c72 192.168.56.101:6480 master - 0 1468863594011 8 connected 0-5460
 4ce9eab1382bc2e1886c29df3b4263e701bf5e6e 192.168.56.101:6481 slave 87d7a6fba680f6826fe680a73ab9ea692aa1b03f 0 1468863597051 5 connected
 87d7a6fba680f6826fe680a73ab9ea692aa1b03f 192.168.56.101:6381 master - 0 1468863596038 2 connected 5461-10922


发现:

 1、6380节点失效不可用
 2、6480节点从slave转换为master


测试集群是否可用:

 192.168.56.101:6382> get abc
 -> Redirected to slot [7638] located at 192.168.56.101:6381
 "123"
 集群可用。


恢复6380,并查看集群信息

 [root@master redis-cluster-master-slave]# /usr/local/bin/redis-server /opt/redis/redis-cluster-master-slave/6380/redis.conf


192.168.56.101:6382> cluster nodes

  768ad8c6edc4b88b650bffc9ab0fd22f4e85ce15 192.168.56.101:6380   slave   74f0fd0468eefad443bcd4627069d217e9213c72 0 1468863759429 8 connected
 4d09704a783936c024305797dec32e5d1debe922 192.168.56.101:6482 slave aba2eba21bbe0b39423afd940a40e692c4fe7408 0 1468863757408 6 connected
 aba2eba21bbe0b39423afd940a40e692c4fe7408 192.168.56.101:6382 myself,master - 0 0 3 connected 10923-16383
  74f0fd0468eefad443bcd4627069d217e9213c72 192.168.56.101:6480 master - 0 1468863754363 8 connected 0-5460
 4ce9eab1382bc2e1886c29df3b4263e701bf5e6e 192.168.56.101:6481 slave 87d7a6fba680f6826fe680a73ab9ea692aa1b03f 0 1468863758418 5 connected
 87d7a6fba680f6826fe680a73ab9ea692aa1b03f 192.168.56.101:6381 master - 0 1468863756394 2 connected 5461-10922


发现:

 1、6380节点可用
 2、6480依然是主节点
 3、6380成为6480的从数据库


注:使用集群需要注意的事项

 1、 多键的命令操作(如MGET、MSET),如果每个键都位于同一个节点,则可以正常支持,否则会提示错误。
 2、 集群中的节点只能使用0号数据库,如果执行SELECT切换数据库会提示错误。


书生参考网络整理


如非特别注明,本站内容均为领悟书生原创,转载请务必注明作者和原始出处。
本文地址:http://www.656463.com/redis/ZZFVNb.htm

相关专题

  • redis教程

    redis教程

    REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。 Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。 它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。