Home  >  Article  >  Database  >  How to build a redis cluster

How to build a redis cluster

步履不停
步履不停Original
2019-06-24 10:57:482193browse

How to build a redis cluster

Introduction to the installation environment and version

Use two virtual machines to simulate 6 nodes , one machine has 3 nodes, creating 3 master and 3 slave environments.

redis adopts redis-3.2.4 version.

The two virtual machines are CentOS, one CentOS6.5 (IP: 192.168.31.245) and one CentOS7 (IP: 192.168.31.210).

Installation process

1. Download and unzip

<br>
cd /root/software
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar -zxvf redis-3.2.4.tar .gz <br>

2. 编译安装

cd redis-3.2.4make && make install

3. 将 redis-trib.rb 复制到 /usr/local/bin 目录下

cd src
cp redis-trib.rb /usr/local/bin/  

4. 创建 Redis 节点

首先在 192.168.31.245 机器上 /root/software/redis-3.2.4 目录下创建 redis_cluster 目录;

mkdir redis_cluster  

在 redis_cluster 目录下,创建名为7000、7001、7002的目录,并将 redis.conf 拷贝到这三个目录中

mkdir 7000 7001 7002<br>cp redis.conf redis_cluster/7000cp redis.conf redis_cluster/7001cp redis.conf redis_cluster/7002  

分别修改这三个配置文件,修改如下内容

port  7000                                        //端口7000,7002,7003        
bind 本机ip                                       //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize    yes                               //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002
cluster-enabled  yes                           //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志
  • 接着在另外一台机器上(192.168.31.210),的操作重复以上三步,只是把目录改为7003、7004、7005,对应的配置文件也按照这个规则修改即可

5. 启动各个节点

第一台机器上执行
redis-server redis_cluster/7000/redis.conf
redis-server redis_cluster/7001/redis.conf
redis-server redis_cluster/7002/redis.conf
 
另外一台机器上执行
redis-server redis_cluster/7003/redis.conf
redis-server redis_cluster/7004/redis.conf
redis-server redis_cluster/7005/redis.conf

6. 检查 redis 启动情况

##一台机器<br>ps -ef | grep redis
root      61020      1  0 02:14 ?        00:00:01 redis-server 127.0.0.1:7000 [cluster]    
root      61024      1  0 02:14 ?        00:00:01 redis-server 127.0.0.1:7001 [cluster]    
root      61029      1  0 02:14 ?        00:00:01 redis-server 127.0.0.1:7002 [cluster]    
 
netstat -tnlp | grep redis
tcp        0      0 127.0.0.1:17000             0.0.0.0:*                   LISTEN      61020/redis-server 
tcp        0      0 127.0.0.1:17001             0.0.0.0:*                   LISTEN      61024/redis-server 
tcp        0      0 127.0.0.1:17002             0.0.0.0:*                   LISTEN      61029/redis-server 
tcp        0      0 127.0.0.1:7000              0.0.0.0:*                   LISTEN      61020/redis-server 
tcp        0      0 127.0.0.1:7001              0.0.0.0:*                   LISTEN      61024/redis-server 
tcp        0      0 127.0.0.1:7002              0.0.0.0:*                   LISTEN      61029/redis-server12345678910111213
    ##另外一台机器
ps -ef | grep redis
root       9957      1  0 02:32 ?        00:00:01 redis-server 127.0.0.1:7003 [cluster]
root       9964      1  0 02:32 ?        00:00:01 redis-server 127.0.0.1:7004 [cluster]
root       9971      1  0 02:32 ?        00:00:01 redis-server 127.0.0.1:7005 [cluster]
root      10065   4744  0 02:38 pts/0    00:00:00 grep --color=auto redis
netstat -tlnp | grep redis
tcp        0      0 127.0.0.1:17003         0.0.0.0:*               LISTEN      9957/redis-server 1tcp        0      0 127.0.0.1:17004         0.0.0.0:*               LISTEN      9964/redis-server 1tcp        0      0 127.0.0.1:17005         0.0.0.0:*               LISTEN      9971/redis-server 1tcp        0      0 127.0.0.1:7003          0.0.0.0:*               LISTEN      9957/redis-server 1tcp        0      0 127.0.0.1:7004          0.0.0.0:*               LISTEN      9964/redis-server 1tcp        0      0 127.0.0.1:7005          0.0.0.0:*               LISTEN      9971/redis-server 1

7.创建集群

Redis 官方提供了 redis-trib.rb 这个工具,就在解压目录的 src 目录中,第三步中已将它复制到 /usr/local/bin 目录中,可以直接在命令行中使用了。使用下面这个命令即可完成安装。

redis-trib.rb  create  --replicas  1  192.168.31.245:7000 192.168.31.245:7001  192.168.31.245:7002 192.168.31.210:7003  192.168.31.210:7004  192.168.31.210:7005

其中,前三个 ip:port 为第一台机器的节点,剩下三个为第二台机器。

等等,出错了。这个工具是用 ruby 实现的,所以需要安装 ruby。安装命令如下:

yum -y install ruby ruby-devel rubygems rpm-build

gem install redis

After running the redis-trib.rb command, the following prompt will appear:

How to build a redis cluster

Enter yes, and then the following content will appear, indicating that the installation is successful.

How to build a redis cluster 

8. Cluster verification

Connect the node of port 7002 of the cluster on the first machine, and connect the node 7005 on the other machine. The connection method is redis-cli -h 192.168.31.245 -c -p 7002 , add the parameter -C to connect to the cluster, because the above redis.conf changes bind to the ip address, so -h Parameters cannot be omitted.

Execute the command set hello world on node 7005. The execution result is as follows:

##Then on another port 7002, check the content with key hello,

get hello, the execution result is as follows:

How to build a redis cluster

It means that the cluster is operating normally.

Briefly explain the principle

When redis cluster was designed, decentralization and middleware were taken into consideration. In other words, every node in the cluster has an equal relationship and is peer-to-peer. Each node saves its own Data and the status of the entire cluster. Each node is connected to all other nodes, and these connections remain active, which ensures that we only need to connect to any node in the cluster to obtain data from other nodes.

Redis cluster does not use traditional consistent hashing to allocate data, but uses another method called hash slot (hash slot) to allocate data. Redis cluster is allocated 16384 slots by default. When we set a key, we will use the CRC16 algorithm to take the modulo to get the corresponding slot, and then divide the key into the hash slot range. On the node, the specific algorithm is: CRC16(key) % 16384. So when we saw set and get during testing, we jumped directly to the node with port 7000.

The Redis cluster will store the data in a master node, and then synchronize the data between the master and its corresponding salve. When reading data, the data is also obtained from the corresponding master node according to the consistent hashing algorithm. Only when a master dies, a corresponding salve node will be started to act as the master.

It should be noted that: there must be 3 or more master nodes, otherwise it will fail when creating the cluster, and when the number of surviving master nodes is less than half of the total number of nodes, The entire cluster cannot provide services.

For more Redis-related technical articles, please visit the Redis Tutorial## column to learn!

The above is the detailed content of How to build a redis cluster. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn