Ultra-detailed, and more graphic description redis redis cluster approach and set up a pseudo-cluster

Over more graphic, the novice friendliness is excellent. Process knock command, they will be knocking on the wrong, but in order to cut a good view of the right, once a little problem, in order to demonstrate good results, it is necessary to start from scratch knock. Let us look at and cherish.

Understanding redis clusters before, if you want to know redis stand-alone version can be viewed, springboot integration redis . Well, to start following.

Each instance may be referred to as a node redis, and install a default port redis start node is not closed to start another port, a new node. In another machine and start the installation redis, also a new node.

Node sub master node (master),, data synchronization slave node (slave) from the master node to a plurality of slave nodes.

When redis3.0 began to support the cluster, the cluster is not uniform Redis inlet, the client (client) connected to any node in the cluster (node) connected to the cluster, the cluster is the internal nodes (PING-PONG mechanism communicate with each other ).

@ [Heel]

First, the cluster approach

1, master and slave modes

A master can have multiple slave, but a slave can only correspond to a master. Thus, when a slave hung up without affecting the master reading and writing and reading of other slave after the reboot, the data will come from the synchronization master.

In the master-slave mode, because there is only one master node can be written, and the main, can be read from the node, it is usually responsible for writing the master node, the node is responsible from reading.

redis master and slave modes _lgx211 However, when only master hung up after, although this does not affect the reading slave, but redis no longer provide writing services, you need to master after the restart, only to re-redis provide external writing services.

2, Sentinel mode

sentinel model is based on a master-slave mode on the basis of avoiding a single master hung up after, redis not provide writing services. Because all the data backup master node from the node, then after the hanging when the master, the slave will choose Sentinel as a master, and modify their profiles, profiles of other slave will be modified, such properties slaveof They will point to the new master, such as a password previously configured. In this case, the client is not directly connected Redis, but the connection sentinel ip and port, to provide specific Redis service by the sentinel. Sentinel mode _lgx211 After hanging up before the master is restarted, it will no longer receive a new master, but the master data synchronization as a slave.

3, Cluster mode

Cluster model is based on a Sentinel mode, when data of a plurality of dynamic expansion when needed to both front die, the need for data slice, according to certain rules redis data allocated to multiple machines. Cluster mode _lgx211 The model to support dynamic expansion, you can add or delete nodes online, and clients can be connected to any one master node to read and write, but this time the role of slave nodes just backup. As to why the dynamic expansion can be done, mainly because there is no use Redis cluster consistency hash, but the use of hash slots. 16384 clusters have Redis hash slots, each key 16384 of the CRC16 check by modulo to determine which slot is placed, and each is responsible for a part of the cluster node hash slot.

So this is very easy to add or delete nodes, for example, if I want to add a new a new node, I just need to get from the existing node portion of the trench to come; if I want to remove a node, you only need the node grooves moved to other nodes, then the node a without any grooves can be removed from the cluster. Since the hash slot will move from one node to another node does not stop the service, so whether to add or delete a node to change the hash slot number will not cause the state of the cluster unavailable.

需要注意的是,该模式下不支持同时处理多个key(如MSET/MGET),因为redis需要把key均匀分布在各个节点上,并发量很高的情况下同时创建key-value会降低性能并导致不可预测的行为。

二,搭建集群

这里就直接搭建较为复杂的Cluster模式集群,也是企业级开发过程中使用最多的。

1,准备工作

Linux可以连接外网,有wget(用于在线下载redis),系统安装好gcc环境,(不然编译redis会报错)。

2,下载、解压、移到指定目录,编译Redis
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
tar xzf redis-5.0.4.tar.gz
mv redis-5.0.4  /usr/local/redis
cd /usr/local/redis/redis-5.0.4
make
make install

安装完成,在/usr/local/bin/目录下就会看见 Here Insert Picture Description

3,建redis各节点目录
mkdir /usr/local/redis-cluster
cd /usr/local/redis-cluster
mkdir -p 9001/data 9002/data 9003/data 9004/data 9005/data 9006/data
mkdir bin

最终目录结构如下 Here Insert Picture Description

4,redis集群的运行脚本

把之前安装好的redis的src目录下运行脚本拷贝过来,每个redis版本的运行脚本有细微差异,请以你自己的版本为准,就是下图绿色部分。

cd /usr/local/redis/redis-5.0.4/src
cp  mkreleasehdr.sh redis-benchmark redis-check-aof  redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb /usr/local/redis-cluster/bin

Here Insert Picture Description 最终效果如下图所示 Here Insert Picture Description

5,批量复制redis实例
cp /usr/local/redis/redis-5.0.4/* /usr/local/redis-cluster/9001
cp /usr/local/redis/redis-5.0.4/* /usr/local/redis-cluster/9002
cp /usr/local/redis/redis-5.0.4/* /usr/local/redis-cluster/9003
cp /usr/local/redis/redis-5.0.4/* /usr/local/redis-cluster/9004
cp /usr/local/redis/redis-5.0.4/* /usr/local/redis-cluster/9005
cp /usr/local/redis/redis-5.0.4/* /usr/local/redis-cluster/9006

最终效果图如下所示 Here Insert Picture Description

6,逐个修改redis配置

以 9001 的为例子,其余五个类似。

cd /usr/local/redis-cluster/9001
vi redis.conf

Here Insert Picture Description 打开配置文件,按 i进入编辑模式,按照出现的顺序,主要需要修改的地方是

  • bind 192.168.119.128(绑定当前电脑的 IP,这是我虚拟机的,你绑定成你虚拟机的ip)
  • port 9001(因为我这是一台机器运行6个redis实例,所以要启动6个实例,得为它配置6个不同的端口,若你是6台机器,默认的端口就行,无需更改)
  • daemonize yes(这是设置是否后台启动 Redis,默认 no ,但是生产环境肯定要默认就开启 Redis,所以这里设置为 yes 。)
  • pidfile /var/run/redis_9001.pid(_9001这个一定要和第一个配置的端口一样)
  • dir /usr/local/redis-cluster/9001/data/(数据文件存放位置,我换成指定目录下存放)
  • appendonly yes
  • cluster-enabled yes(启动集群模式)
  • cluster-config-file nodes9001.conf(9001这个也要和之前的端口对应)
  • cluster-node-timeout 15000(超时时间) Here Insert Picture Description 完成以上修改,Esc退出编辑模式,输入:wq 保存并退出。 类似同样的操作操作再来五次。

若是对redis的配置文件有兴趣,我在学习的过程中找到个详细的翻译版,可点击链接进去学习。

redis配置中文翻译

7,逐个启动redis节点
cd /usr/local/redis-cluster
/usr/local/bin/redis-server /usr/local/redis-cluster/9001/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9002/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9003/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9004/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9005/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9006/redis.conf

运行效果如图所示 Here Insert Picture Description 现在检查一下是否成功开启,如下图所示,都开启成功。

ps -el | grep redis

Here Insert Picture Description

8,搭建集群

此时的节点虽然都启动成功了,但他们还不在一个集群里面,不能互相发现,测试会报错:(error) CLUSTERDOWN Hash slot not served

/usr/local/redis-cluster/bin/redis-cli -h 192.168.119.128 -p 9001
keys *
set name mafly

如下图所示 Here Insert Picture Description 解决报错,如果你是redis5.0以前的,你需要安装集群所需的ruby相关依赖

yum install ruby
yum install rubygems
cd /usr/local/redis-cluster/
gem install redis

这步若安装报错,请查看Could not find a valid gem 'redis'

如果你是redis5.0及之后的,无需安装ruby依赖,redis安装目录里内置了集群命令行工具 redis-trib ,它是一个 Ruby 程序, 这个程序通过向实例发送特殊命令来完成创建新集群, 检查集群, 或者对集群进行重新分片工作。

redis-cli --cluster create 192.168.119.128:9001 192.168.119.128:9002 192.168.119.128:9003 192.168.119.128:9004 192.168.119.128:9005 192.168.119.128:9006 --cluster-replicas 1

--cluster-replicas 1 这个指的是从机的数量,表示我们希望为集群中的每个主节点创建一个从节点。

红色选框是给三个主节点分配的共16384个槽点。

黄色选框是主从节点的分配情况。

蓝色选框是各个节点的详情。 Here Insert Picture Description

9,测试

现在通过客户端命令连接上,通过集群命令看一下状态和节点信息等

/usr/local/redis-cluster/bin/redis-cli -c -h 192.168.119.128 -p 9001
cluster info
cluster nodes

效果图如下,集群搭建成功。 Here Insert Picture Description 现在往9001这个主节点写入一条信息,我们可以在9002这个主节点取到信息,集群间各个节点可以通信。

现在往9001这个主节点写入一条信息,我们可以在9002这个主节点取到信息,集群间各个节点可以通信。

 set name lgx
 get name

Here Insert Picture Description

三,故障转移

1,故障转移机制详解
  1. 集群中的节点会向其它节点发送PING消息(该PING消息会带着当前集群和节点的信息),如果在规定时间内,没有收到对应的PONG消息,就把此节点标记为疑似下线。

  2. 当被分配了slot槽位的主节点中有超过一半的节点都认为此节点疑似下线(就是其它节点以更高的频次,更频繁的与该节点PING-PONG),那么该节点就真的下线。

  3. 其它节点收到某节点已经下线的广播后,把自己内部的集群维护信息也修改为该节点已事实下线。

  4. 节点资格审查:然后对从节点进行资格审查,每个从节点检查最后与主节点的断线时间,如果该值超过配置文件的设置,那么取消该从节点的资格。

  5. 准备选举时间:这里使用了延迟触发机制,主要是给那些延迟低的更高的优先级,延迟低的让它提前参与被选举,延迟高的让它靠后参与被选举。(延迟的高低是依据之前与主节点的最后断线时间确定的)

  6. 选举投票:当从节点获取选举资格后,会向其他带有slot槽位的主节点发起选举请求,由它们进行投票,优先级越高的从节点就越有可能成为主节点,当从节点获取的票数到达一定数值时(如集群内有N个主节点,那么只要有一个从节点获得了N/2+1的选票即认为胜出),就会替换成为主节点。

  7. 替换主节点:被选举出来的从节点会执行slaveof no one把自己的状态从slave变成master,然后执行clusterDelSlot操作撤销故障主节点负责的槽,并执行 clusterAddSlot把这些槽分配给自己,之后向集群广播自己的pong消息,通知集群内所有的节点,当前从节点已变为主节点。

  8. 接管相关操作:新的主节点接管了之前故障的主节点的槽信息,接收和处理与自己槽位相关的命令请求。

2,故障转移测试

这是之前集群中具体节点的情况,我简化成如下,可以向上回看图片中的集群信息。 Original cluster _lgx211 这里关闭该9001端口的进程,即模拟该主节点挂掉。

 netstat -tunlp | grep 9001
 kill 15705

Here Insert Picture Description Log hang redis node will be denied service, accessible through a master node is still running, and then view the information in the cluster again

/usr/local/redis-cluster/bin/redis-cli -c -h 192.168.119.128 -p 9001

/usr/local/redis-cluster/bin/redis-cli -c -h 192.168.119.128 -p 9002

cluster nodes

Here Insert Picture Description In short, before the information becomes a cluster follows  After a failover cluster _lgx211 the master node now, I just hang up the restart, recheck the internal nodes of the cluster, as follows shown below.

cd /usr/local/redis-cluster/

/usr/local/bin/redis-server /usr/local/redis-cluster/9001/redis.conf

/usr/local/redis-cluster/bin/redis-cli -c -h 192.168.119.128 -p 9002

cluster nodes

Here Insert Picture Description In short, the situation is now node in the cluster as follows

Recovery cluster _lgx211

Source: http://www.1994july.club/seojishu/

Guess you like

Origin www.cnblogs.com/1994july/p/12442290.html