Single Linux cluster server implementation Redis

A, Redis Introduction

Redis is currently caching middleware large-scale use, because of its powerful, efficient and convenient features, has been widely used.

Redis 3.0.0 released in 2015, has supported the official redis cluster. redis cluster in the design, to take into account to the center, to the middleware. That is, each node in the cluster are equal relationship, each node respective data and save the state of the entire cluster. Each node and all other nodes are connected, and these connections remain active, thus ensuring that we only need to connect any node in the cluster, you can get the data to other nodes.

Two, Redis cluster description

Redis is a cluster data can be shared among multiple facilities Redis nodes. Redis Redis cluster does not support commands that need to process a plurality of keys, such as execution commands need to move data between a plurality of nodes, and in the case of high load, which will reduce the performance redis command cluster and cause unpredictable the behavior of.

Redis cluster to provide a degree of availability through the partition, even if some of the nodes in the cluster fails or unable to communicate, the cluster can continue processing request command.

Three, Redis cluster principle

Redis is how reasonable it made with these nodes and data?

Redis does not use consistent hashing to traditional allocation data, instead of using another known way hash slot allocation. Redis cluster default allocation of 16384 slot, when we set a key, will use CRC16 algorithm (cyclic redundancy check) to get the slot belongs, then the hash key assigned to the node slots mountain range, specific algorithm : CRC16 (key)% 16384.

Note: You must be at least three primary node before you create a cluster, or the cluster fails.

Redis cluster at work, after which a master node goes down there are other master nodes referendum and elections, and when the results of voting to determine the master node is down by more than half if the state more than half of the entire cluster is the master node goes down fail. That's why you need to build at least three primary node is available Redis cluster environment.

Redis cluster benefits:

  • 1) Automatic data having a plurality of nodes on the ability to split segmentation;
  • 2) When a part of the cluster nodes fails or is unable to communicate, still has the ability to continue processing the command request;
  • 3) All rredis nodes interconnected to each other, the internal binary transmission speed and bandwidth optimization protocol; binary protocol: transmission process to have a message header and message entities. If the header length is fixed. By parsing the message header message entities can be obtained;
  • fail 4) through the cluster node is a node detecting failure of more than half only take effect;
  • 5) redis client node is connected, without an intermediate proxy layer, the client need not be connected to all cluster nodes in the cluster connected to an available node;

Redis distribution used in the hash tank has good and bad, the advantage is very clear, for example: I want to add a node D, this approach is redis cluster from the front of each node of each slot to pick up part of the D . This will become substantially:
Node A Node B coverage covering 1365-5460 6827-10922 12288-16383 Node C Node D covers the same coverage 01364,5461-6826,10923-12287 delete a node is similar, can be deleted after the move is completed the node of;

Therefore redis cluster is such a shape, as shown:
Single Linux cluster server implementation Redis

Four, Redis-cluster mode from the master

Redis-cluster in order to guarantee a high availability of data added from the master mode, the master node corresponds to one or a plurality of slave nodes. Master node provides access to data, the data is acquired from the backup master node from the node; when the primary node is down, this will select the master node to act as a slave node, thus ensuring the cluster does not hang up.

Fifth, the implementation of case

About multiple Linux clusters can achieve redis reference Redis database cluster

(1) Case Environment

Because it is achieved using a single server cluster environment, so they are distinguished according to their port!

(2) implementation case

Download Redis Cluster

1) Create a six node

[root@docker ~]# tar zxf redis-4.0.14.tar.gz -C /usr/local
[root@docker ~]# cd /usr/local/redis-4.0.14/
[root@docker redis-4.0.14]# make && make install     //编译安装redis
[root@redis ~]# mkdir -p /usr/local/cluster/700{0..5}
//由于是在单台创建多个节点,所以事先把这个目录创建好,用于存放各个节点的配置信息
[root@docker ~]# cp /usr/local/redis-4.0.14/redis.conf /usr/local/cluster/7000
//将redis原本的配置文件复制一份
[root@docker ~]# vim /usr/local/cluster/7000/redis.conf       //编译7000节点的配置文件
  92 port 7000                             //修改监听端口
 158 pidfile /var/run/redis_7000.pid              //修改pid文件名
 672 appendonly yes                 //开启aof持久化
 676 appendfilename "appendonly-7000.aof"           //修改持久化的配置文件名
 814 cluster-enabled yes                   //开启redis的群集模式
 822 cluster-config-file nodes-7000.conf     //修改群集的配置文件名
 828 cluster-node-timeout 5000             //修改群集的等待时间
[root@docker ~]# cd /usr/local/cluster/7000
[root@docker 7000]# redis-server redis.conf          //必须进入相应的目录启动redis服务

But when you start, this error will appear, as shown:
Single Linux cluster server implementation Redis

[root@redis 7000]# vim /usr/local/cluster/7000/redis.conf          //编写7000节点的配置文件
 136 daemonize yes                      //开启守护进程让其在后台运行
[root@redis 7000]# echo 512 > /proc/sys/net/core/somaxconn 
[root@redis 7000]# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
[root@redis 7000]# sysctl -p
vm.overcommit_memory = 1
[root@redis 7000]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
//根据刚才提示的警告信息对redis服务简单进行一个优化

2) to start each node

[root@redis 7000]# redis-server redis.conf                  //进入其目录运行redis服务
[root@redis 7000]# cp redis.conf ../7001
[root@redis 7000]# cp redis.conf ../7002
[root@redis 7000]# cp redis.conf ../7003
[root@redis 7000]# cp redis.conf ../7004
[root@redis 7000]# cp redis.conf ../7005
//因为需要开启六个节点,所以需要将其配置文件分为六份
[root@redis 7000]# sed -i s/7000/7001/g ../7001/redis.conf 
[root@redis 7000]# sed -i s/7000/7002/g ../7002/redis.conf 
[root@redis 7000]# sed -i s/7000/7003/g ../7003/redis.conf 
[root@redis 7000]# sed -i s/7000/7004/g ../7004/redis.conf 
[root@redis 7000]# sed -i s/7000/7005/g ../7005/redis.conf 
//因为六个节点都是根据端口来区分,所以配置文件中只需修改以端口号命名的内容
[root@redis 7000]# cd ../7001 && redis-server redis.conf
[root@redis 7001]# cd ../7002 && redis-server redis.conf
[root@redis 7002]# cd ../7003 && redis-server redis.conf
[root@redis 7003]# cd ../7004 && redis-server redis.conf
[root@redis 7004]# cd ../7005 && redis-server redis.conf
//都是进入其相应的目录启动服务
[root@redis 7005]# netstat -anpt | grep redis     
tcp        0      0 127.0.0.1:7004          0.0.0.0:*               LISTEN      5615/redis-server 1 
tcp        0      0 127.0.0.1:7005          0.0.0.0:*               LISTEN      5620/redis-server 1 
tcp        0      0 127.0.0.1:17000         0.0.0.0:*               LISTEN      5524/redis-server 1 
tcp        0      0 127.0.0.1:17001         0.0.0.0:*               LISTEN      5592/redis-server 1 
tcp        0      0 127.0.0.1:17002         0.0.0.0:*               LISTEN      5605/redis-server 1 
tcp        0      0 127.0.0.1:17003         0.0.0.0:*               LISTEN      5610/redis-server 1 
tcp        0      0 127.0.0.1:17004         0.0.0.0:*               LISTEN      5615/redis-server 1 
tcp        0      0 127.0.0.1:17005         0.0.0.0:*               LISTEN      5620/redis-server 1 
tcp        0      0 127.0.0.1:7000          0.0.0.0:*               LISTEN      5524/redis-server 1 
tcp        0      0 127.0.0.1:7001          0.0.0.0:*               LISTEN      5592/redis-server 1 
tcp        0      0 127.0.0.1:7002          0.0.0.0:*               LISTEN      5605/redis-server 1 
tcp        0      0 127.0.0.1:7003          0.0.0.0:*               LISTEN      5610/redis-server 1 
//查看端口都已经在监听,剩下的17000这样的端口是群集之间沟通使用的

3) for each node is assigned a hash slot, and joins the cluster environment

[root@redis 7005]# redis-cli -p 7000              //随便指定一个端口进入redis终端
127.0.0.1:7000> CLUSTER INFO                  //查看群集详细信息

cluster_state:fail                          //发现现在是fail(失败)的状态
……………………                       //省略 部分内容
127.0.0.1:7000> CLUSTER NODES           //查看群集节点
d554512885b2679d432d0d6b011c9ea56ea1ebeb :7000@17000 myself,master - 0 0 0 connected
//发现加入群集的节点只有7000这个节点
127.0.0.1:7000> exit
[root@redis 7005]# redis-cli -h 127.0.0.1 -p 7000 cluster addslots {0..5461}
OK
[root@redis 7005]# redis-cli -h 127.0.0.1 -p 7001 cluster addslots {5462..10922}
OK
[root@redis 7005]# redis-cli -h 127.0.0.1 -p 7002 cluster addslots {10923..16383}
OK
//为各个群集阶段分配hash槽
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7000
OK
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7001
OK
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7002
OK
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7003
OK
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7004
OK
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7005
OK
//用来连接不同的开启集群支持的 Redis 节点,以进入工作集群
[root@redis 7005]# redis-cli -p 7000
127.0.0.1:7000> CLUSTER INFO
cluster_state:ok                     //再次查看群集状态发现已经是“ok”的状态
……………………                   //省略部分内容
127.0.0.1:7000> CLUSTER NODES                   //再次查看群集节点信息

Figure:
Single Linux cluster server implementation Redis

Default environment 7000 is the master node 7003, 7001 is the master node 7004, 7002 is the master node 7005, as follows:

[root@redis 7005]# redis-cli -p 7003          //连接7003的节点
127.0.0.1:7003> CLUSTER REPLICATE d554512885b2679d432d0d6b011c9ea56ea1ebeb
OK           //这个是7000的ID号
127.0.0.1:7003> exit
[root@redis 7005]# redis-cli -p 7004          //连接7004的节点
127.0.0.1:7004> CLUSTER REPLICATE 182a55ee8b38afbf5b48209979f220dc4da7c14a
OK                    //这个是7001的ID号
127.0.0.1:7004> exit
[root@redis 7005]# redis-cli -p 7005          //连接7005的节点
127.0.0.1:7005> CLUSTER REPLICATE 82b62bda5b31d8ee27aee8d7320663ee86276e9f
OK                   //这个是7002的ID号
127.0.0.1:7005> CLUSTER NODES           //再次查看群集节点的详细信息

Figure:
Single Linux cluster server implementation Redis

4) test access data

[root@redis 7005]# redis-cli -p 7000            //进入节点插入数据进行测试
127.0.0.1:7000> set name lzj
(error) MOVED 5798 127.0.0.1:7001           
//发现出现错误,提示应该是7001的节点来存储这个数据
127.0.0.1:7000> exit
[root@redis 7005]# redis-cli -p 7000 -c          //使用“-c”选项表示进入群集中
127.0.0.1:7000> set name lzj                  //重新插入数据
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK               //提示将数据存放在7001节点上
127.0.0.1:7001> get name               //获取键所对应的值
"lzj"

5) Simulation of a single node failure

[root@redis 7001]# cd ../7001
[root@redis 7001]# redis-cli -p 7001 shutdown
//模拟7001节点故障
[root@redis 7001]# redis-cli -p 7000
127.0.0.1:7000> CLUSTER NODES            //进入节点查看群集节点的状态
127.0.0.1:7004> get name
"lzj"

Figure:
Single Linux cluster server implementation Redis

[root@redis 7001]# redis-server redis.conf 
//进入7001对应的目录,启动redis节点
[root@redis 7001]# redis-cli -p 7000  
127.0.0.1:7000> CLUSTER NODES               //进入群集查看群集状态

Figure:
Single Linux cluster server implementation Redis

6) install software ruby

Download ruby required packages

[root@redis ~]# yum -y install rpm-build openssl openssl-devel
//安装ruby所需依赖
[root@redis ~]# tar zxf ruby-2.3.1.tar.gz -C /usr/src
cd [root@redis ~]# cd /usr/src/ruby-2.3.1/
[root@redis ruby-2.3.1]# ./configure --prefix=/usr/local/ruby && make && make install
//编译安装ruby,可能时间较长
[root@redis ~]# ln -s /usr/local/ruby/bin/* /usr/local/bin
//为gem命令创建符号链接
[root@redis ~]# ln -s /usr/local/redis-4.0.14/src/* /usr/local/bin
//为redis常用命令创建符号链接
[root@redis ~]# gem install redis-3.3.0.gem 
//使用gem命令安装Redis群集必备软件包

7) Add the primary node 7006

[root@redis ~]# redis-trib.rb check 127.0.0.1:7000
//检查群集节点状态

Figure:
Single Linux cluster server implementation Redis

[root@redis ~]# mkdir /usr/local/cluster/7006
[root@redis ~]# cd /usr/local/cluster/7006
//创建相应目录并进入
[root@redis 7006]# cp ../7000/redis.conf .
[root@redis 7006]# sed -i s/7000/7006/g redis.conf 
//复制配置文件、并进行修改
[root@redis 7006]# redis-server redis.conf 
//修改7006节点的redis服务
[root@redis ~]# redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000
//将7006节点加入到7000节点群集环境中(默认是master状态)
[root@redis ~]# redis-trib.rb check 127.0.0.1:7000
//再次查看群集状态

FIG:
Single Linux cluster server implementation Redis
when theme, accidentally on the block, can be found in 7006 that there is no groove node point, you can not store data!

[root@redis ~]# redis-trib.rb reshard 127.0.0.1:7000                //分配7000这个群集的槽点
How many slots do you want to move (from 1 to 16384)? 4096    //针对多少个节点进行操作
What is the receiving node ID?7add42108b3fc3e9c0af2846fb06ceaf8b172c87  
//给7006节点,这里输入的是7006节点的ID号
Source node #1:all       //从所有节点开始分配
[root@redis ~]# redis-trib.rb check 127.0.0.1:7000
//再次查看群集状态

Figure:
Single Linux cluster server implementation Redis

8) adding from node 7007

[root@redis ~]# mkdir /usr/local/cluster/7007
[root@redis ~]# cd /usr/local/cluster/7007
//创建相应目录并进入
[root@redis 7007]# cp ../7000/redis.conf .
[root@redis 7007]# sed -i s/7000/7007/g redis.conf 
//复制配置文件并进行修改
[root@redis 7007]# redis-server redis.conf
//启动7007节点redis服务
[root@redis ~]# redis-trib.rb add-node --slave 127.0.0.1:7007 127.0.0.1:7000
//往7000群集中添加7007节点,而且指定其为slave
[root@redis ~]# redis-trib.rb check 127.0.0.1:7000
//查看群集状态

Figure:
Single Linux cluster server implementation Redis

9) Add the 7008 node

[root@redis ~]# mkdir /usr/local/cluster/7008
[root@redis ~]# cd /usr/local/cluster/7008
[root@redis 7008]# cp ../7000/redis.conf .
[root@redis 7008]# sed -i s/7000/7008/g redis.conf 
[root@redis 7008]# redis-server redis.conf 
//创建相应目录并进行修改配置文件,启动服务
[root@redis ~]# redis-trib.rb add-node --slave --master-id 7add42108b3fc3e9c0af2846fb06ceaf8b172c87 127.0.0.1:7008 127.0.0.1:7000
//向群集中添加节点,指定其为salve,并指定主节点ID号(7006节点的ID)
[root@redis ~]# redis-trib.rb check 127.0.0.1:7000
//查看群集状态

Figure:
Single Linux cluster server implementation Redis

10) The 7000 node deleted

[root@redis ~]# redis-trib.rb reshard 127.0.0.1:7000                                     //对群集节点进行操作
How many slots do you want to move (from 1 to 16384)? 4096                     //针对多个节点进行操作
What is the receiving node ID? 
7add42108b3fc3e9c0af2846fb06ceaf8b172c87             //这里输入将删除的槽节点给那个节点(7006节点)
Source node d554512885b2679d432d0d6b011c9ea56ea1ebeb    //输入删除的节点
Source node  done
Do you want to proceed with the proposed reshard plan (yes/no)? yes
//表示确认
[root@redis ~]# redis-trib.rb del-node 127.0.0.1:7000 d554512885b2679d432d0d6b011c9ea56ea1ebeb
//删除7000群集中的7000节点
[root@redis ~]# redis-trib.rb check 127.0.0.1:7001        //查看群集状态

Figure:
Single Linux cluster server implementation Redis

-------------- end of this article, thanks for reading --------------------

Guess you like

Origin blog.51cto.com/14157628/2460634