search
HomeDatabaseRedisDetailed introduction to Redis high availability cluster

Detailed introduction to Redis high availability cluster

The cluster master-slave model of Redis is a highly available cluster architecture. The main contents of this chapter include: setting up a high-availability cluster, connecting Jedis to the cluster, adding cluster nodes, deleting cluster nodes, and additional configuration instructions. (Recommendation: redis video tutorial)

High-availability cluster construction

Cluster (cluster) technology is a relatively New technologies, through cluster technology, can obtain relatively high gains in performance, reliability, and flexibility at lower costs, and task scheduling is the core technology in cluster systems.

Redis supports clustering after 3.0. There are 16384 hash slots built into Redis cluster. Redis will map hash slots to different nodes roughly equally based on the number of nodes.
All nodes are interconnected with each other (PING-PONG mechanism). When more than half of the hosts think that a certain host is down, the host is really down and the entire cluster will be unavailable.
If multiple slave machines are assigned to each host in the cluster. If the master machine hangs up, the slave machine can still work normally. However, if more than half of the hosts in the cluster are down, the cluster will be unavailable regardless of whether there are slave machines or not.

Preparations before building

Building ruby ​​environment

The redis cluster management tool redis-trib.rb relies on the ruby ​​environment.

[root@itdragon ~]# yum install ruby
[root@itdragon ~]# yum install rubygems
[root@itdragon ~]# gem install redis
[root@itdragon ~]# cd redis-4.0.2/src/
[root@itdragon src]# cp redis-trib.rb /usr/local/redis-4/bin/

Step one: Install the ruby ​​environment

Step two: Install the gem package (gem is used to extend or modify Ruby applications).

Step 3: Find the redis-trib.rb file in the redis decompression directory and copy it to the directory where the redis service is started for easy management.

Possible problems

1 redis requires Ruby version >= 2.2.2, the solution is as follows

2 There is no /usr/local/rvm/scripts/rvm Directory. It may be that the execution of the previous step failed

[root@itdragon ~]# ruby --version
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
[root@itdragon ~]# yum install curl
[root@itdragon ~]# curl -L get.rvm.io | bash -s stable
[root@itdragon ~]# source /usr/local/rvm/scripts/rvm 
[root@itdragon ~]# rvm list known
[root@itdragon ~]# rvm install 2.3.3
[root@itdragon ~]# rvm use 2.3.3
[root@itdragon ~]# gem install redis

Prepare six redis servers

The same as the master-slave replication logic, copy the redis.conf file 6 times, and the port is from 6000~6005

[root@itdragon bin]# cp redis.conf redis6000.conf
[root@itdragon bin]# vim redis6000.conf  
port xxxx                           #修改端口
cluster-enabled yes                 #打开注释,开启集群模式
cluster-config-file nodes-xxxx.conf #集群的配置文件
pidfile /var/run/redis_xxxx.pid     #pidfile文件
logfile "xxxx.log"                  #日志文件
dbfilename dump_xxxx.rdb            #rdb持久化文件
cluster-node-timeout 5000           #请求超时,单位毫秒
appendonly yes                      #开启aof持久化方式
[root@itdragon bin]# vim start-all.sh
./redis-server redis6000.conf
./redis-server redis6001.conf
./redis-server redis6002.conf
./redis-server redis6003.conf
./redis-server redis6004.conf
./redis-server redis6005.conf
[root@itdragon bin]# chmod u+x start-all.sh
[root@itdragon bin]# ./start-all.sh 
[root@itdragon bin]# ps aux | grep redis
root     28001  0.0  0.9 145964  9696 ?        Ssl  17:45   0:00 ./redis-server 112.74.83.71:6000 [cluster]
root     28003  0.0  0.9 145964  9696 ?        Ssl  17:45   0:00 ./redis-server 112.74.83.71:6001 [cluster]
root     28008  0.0  0.9 145964  9656 ?        Ssl  17:45   0:00 ./redis-server 112.74.83.71:6002 [cluster]
root     28013  0.0  0.9 145964  9656 ?        Ssl  17:45   0:00 ./redis-server 112.74.83.71:6003 [cluster]
root     28018  0.1  0.9 145964  9652 ?        Ssl  17:45   0:00 ./redis-server 112.74.83.71:6004 [cluster]
root     28023  0.0  0.9 145964  9656 ?        Ssl  17:45   0:00 ./redis-server 112.74.83.71:6005 [cluster]

Step one: Copy six redis.conf and modify the relevant configurations. If you find it troublesome, you can use the files I configured: https://github.com/ITDragonBlog/daydayup/tree/master /Redis/reids.conf
Step 2: Add a new batch to open the redis service program and increase execution permissions

Step 3: Check whether the six redis services are started successfully

Master-slave cluster construction

Cluster creation command: ./redis-trib.rb create creates a cluster, --replicas 1 assigns a slave to each host, followed by The other parameters are the ip:port of the redis service. Finally enter yes to accept the recommended configuration

[root@itdragon bin]# ./redis-trib.rb create --replicas 1 112.74.83.71:6000 112.74.83.71:6001 112.74.83.71:6002 112.74.83.71:6003 112.74.83.71:6004 112.74.83.71:6005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
112.74.83.71:6000
112.74.83.71:6001
112.74.83.71:6002
Adding replica 112.74.83.71:6003 to 112.74.83.71:6000
Adding replica 112.74.83.71:6004 to 112.74.83.71:6001
Adding replica 112.74.83.71:6005 to 112.74.83.71:6002
...... #省略
Can I set the above configuration? (type 'yes' to accept): yes
...... #省略
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. #有16384个可用的插槽提供服务说明搭建成功

[root@itdragon bin]# ./redis-cli -h 112.74.83.71 -p 6002 -c
112.74.83.71:6002> set testKey value
-> Redirected to slot [5203] located at 112.74.83.71:6000
OK

112.74.83.71:6000> cluster info
cluster_state:ok
......

112.74.83.71:6000> cluster nodes
0968ef8f5ca96681da4abaaf4ca556c2e6dd0242 112.74.83.71:6002@16002 master - 0 1512035804722 3 connected 10923-16383
13ddd4c1b8c00926f61aa6daaa7fd8d87ee97830 112.74.83.71:6005@16005 slave 0968ef8f5ca96681da4abaaf4ca556c2e6dd0242 0 1512035803720 6 connected
a3bb22e04deec2fca653c606edf5b02b819f924f 112.74.83.71:6003@16003 slave 1d4779469053930f30162e89b6711d27a112b601 0 1512035802000 4 connected
1d4779469053930f30162e89b6711d27a112b601 112.74.83.71:6000@16000 myself,master - 0 1512035802000 1 connected 0-5460
a3b99cb5d22f5cbd293179e262f5eda931733c88 112.74.83.71:6001@16001 master - 0 1512035802719 2 connected 5461-10922
915a47afc4f9b94389676b4e14f78cba66be9e5d 112.74.83.71:6004@16004 slave a3b99cb5d22f5cbd293179e262f5eda931733c88 0 1512035801717 5 connected

Step 1: Build the cluster./redis-trib.rb create, select yes to accept the recommended configuration

Step 2: Enter the cluster client ./redis-cli -h any host host -p any host port -c, -c means connecting to redis in cluster mode

Step 3: Save data

Step 4: cluster info Query cluster status information

Step 5: cluster nodes Query cluster node information. There is a pit here. Possible problems will be introduced later

Sorry, the cluster configuration file nodes.conf is already used by a different Redis Cluster node. Please make sure that different nodes use different cluster configuration files.

It is very clear, modify the cluster-config-file nodes.conf file to avoid duplication name, or delete the file and recreate the cluster.

cluster nodes Query cluster node information

This is a very important command. The information we need to care about are:

First parameter: Node ID

Second parameter: IP:PORT@TCP There is a pit here, versions before jedis-2.9.0 parse @error

Third parameter: Flag (Master, Slave, Myself, Fail...)

The fourth parameter: If it is a slave, it is the node ID of the host.

The last two parameters: the connection status and the slot location.

Jedis connects to the clusterFirst, configure the firewall

[root@itdragon ~]# vim /etc/sysconfig/iptables
-A INPUT -p tcp -m tcp --dport 6000 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6001 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6002 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6003 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6004 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6005 -j ACCEPT
[root@itdragon ~]# service iptables restart

Finally, integrate the Spring

<!-- jedis集群版配置 -->
<bean id="redisClient" class="redis.clients.jedis.JedisCluster">
    <constructor-arg name="nodes">
        <set>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                <constructor-arg name="port" value="6000" />
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                <constructor-arg name="port" value="6001" />
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                <constructor-arg name="port" value="6002" />
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                <constructor-arg name="port" value="6003" />
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                <constructor-arg name="port" value="6004" />
            </bean>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                <constructor-arg name="port" value="6005" />
            </bean>
        </set>
    </constructor-arg>
    <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
</bean>
<bean id="jedisClientCluster" class="com.itdragon.service.impl.JedisClientCluster"></bean>

unit Test

/**
 * 集群版测试
 * 若提示以下类似的错误:
 * java.lang.NumberFormatException: For input string: "6002@16002"
 * 若安装的redis 版本大于4,则可能是jedis 的版本低了。选择 2.9.0
 * 因为 cluster nodes 打印的信息中,4版本之前的是没有 @16002 tcp端口信息
 * 0968ef8f5ca96681da4abaaf4ca556c2e6dd0242 112.74.83.71:6002@16002 master - 0 1512035804722 3 connected 10923-16383
 */
@Test
public void testJedisCluster() throws IOException {
    HashSet<HostAndPort> nodes = new HashSet<>();
    nodes.add(new HostAndPort(HOST, 6000));
    nodes.add(new HostAndPort(HOST, 6001));
    nodes.add(new HostAndPort(HOST, 6002));
    nodes.add(new HostAndPort(HOST, 6003));
    nodes.add(new HostAndPort(HOST, 6004));
    nodes.add(new HostAndPort(HOST, 6005));
    JedisCluster cluster = new JedisCluster(nodes);
    cluster.set("cluster-key", "cluster-value");
    System.out.println("集群测试 : " + cluster.get("cluster-key"));
    cluster.close();
}

Possible problems

java.lang.NumberFormatException: For input string: "6002@16002"

If the redis version is above 4.0.0, It is recommended to use jedis-2.9.0 or above.

Source code:

https://github.com/ITDragonBlog/daydayup/tree/master/Redis/ssm-redis


Cluster node operation Add master node

[root@itdragon bin]# cp redis6005.conf redis6006.conf
[root@itdragon bin]# ./redis-server redis6006.conf
[root@itdragon bin]# ./redis-trib.rb add-node 112.74.83.71:6006 112.74.83.71:6000
[root@itdragon bin]# ./redis-cli -h 112.74.83.71 -p 6000 cluster nodes
916d26e9638dc51e168f32969da11e19c875f48f 112.74.83.71:6006@16006 master - 0 1512115612162 0 connected   # 没有分配槽
[root@itdragon bin]# ./redis-trib.rb reshard 112.74.83.71:6000
How many slots do you want to move (from 1 to 16384)? 500
What is the receiving node ID? 916d26e9638dc51e168f32969da11e19c875f48f
Please enter all the source node IDs.
  Type &#39;all&#39; to use all the nodes as source nodes for the hash slots.
  Type &#39;done&#39; once you entered all the source nodes IDs.
Source node #1:all
Do you want to proceed with the proposed reshard plan (yes/no)? yes
[root@itdragon bin]# ./redis-cli -h 112.74.83.71 -p 6000 cluster nodes
916d26e9638dc51e168f32969da11e19c875f48f 112.74.83.71:6006@16006 master - 0 1512116047897 7 connected 0-165 5461-5627 10923-11088

The first step: Create a new host of redis6006.conf and start the Redis service

The second step: Add a new host node, If "[OK] New node added correctly." is printed, it means the addition is successful.

Step 3: Query the cluster node information and find that although the host of port 6006 has been added, there is no content after the connection status, that is, no slot is allocated.

第四步:给6006端口主机分配槽,

第一个参数:需要移动槽的个数,

第二个参数:接受槽的节点ID,

第三个参数:输入"all"表示从所有原节点中获取槽,

第四个参数:输入"yes"开始移动槽到目标结点id

第五步:查询集群节点信息,发现6006端口的主机已经分配了槽

核心命令:
./redis-trib.rb add-node 新增主机ip:port 集群任意节点ip:port
./redis-trib.rb reshard 集群任意节点ip:port

可能存在的问题
[ERR] Sorry, can't connect to node 112.74.83.71:6006
说明:新增的主机必须要是启动状态。

添加从节点

[root@itdragon bin]# cp redis6006.conf redis6007.conf
[root@itdragon bin]# vim redis6007.conf 
[root@itdragon bin]# ./redis-server redis6007.conf
[root@itdragon bin]# ./redis-trib.rb add-node --slave --master-id 916d26e9638dc51e168f32969da11e19c875f48f 112.74.83.71:6007 112.74.83.71:6006
[root@itdragon bin]# ./redis-cli -h 112.74.83.71 -p 6000 cluster nodes
80315a4dee2d0fa46b8ac722962567fc903e797a 112.74.83.71:6007@16007 slave 916d26e9638dc51e168f32969da11e19c875f48f 0 1512117377000 7 connected

第一步:创建 redis6007.conf 的新主机,并启动Redis服务
第二步:新增从机节点,在原来的命令上多了 --slave --master-id 主节点ID
第三步:查询集群节点信息

删除结点

删除节点前,要确保该节点没有值,否则提示:is not empty! Reshard data away and try again. 若该节点有值,则需要把槽分配出去

./redis-trib.rb del-node 112.74.83.71:6006 916d26e9638dc51e168f32969da11e19c875f48f

配置文件补充

前几章Redis教程中介绍了以下配置

1 开启Redis 的守护进程 :daemonize yes

2 指定pid文件写入文件名 :pidfile /var/run/redis.pid

3 指定Redis 端口:port 6379

4 绑定的主机地址 :bind 127.0.0.1

5 Redis持久化默认开启压缩数据:rdbcompression yes

6 指定rdb文件名:dbfilename dump.rdb

7 指定rdb文件位置:dir ./

8 从机启动时,它会自动从master进行数据同步:slaveof

9 开启aof持久化方式:appendonly yes

10 指定aof文件名:appendfilename appendonly.aof

11 触发aof快照机制:appendfsync everysec (no/always)

本章节是Redis教程中的最后一章,把剩下的配置也一起说了吧

1 设置客户端连接超时时间,0表示关闭 :timeout 300

2 设置Redis日志级别,debug、verbose(默认)、notice、warning:loglevel verbose

3 设置数据库的数量:databases 1

4 设置Redis连接密码:requirepass foobared

5 设置同一时间最大客户端连接数,默认无限制:maxclients 128

6 指定Redis最大内存限制:maxmemory

7 指定是否启用虚拟内存机制:vm-enabled no

8 指定虚拟内存文件路径:vm-swap-file /tmp/redis.swap

9 指定包含其它的配置文件:include /path/to/local.conf

更多redis知识请关注redis数据库教程栏目。

The above is the detailed content of Detailed introduction to Redis high availability cluster. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
es和redis区别es和redis区别Jul 06, 2019 pm 01:45 PM

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

一起来聊聊Redis有什么优势和特点一起来聊聊Redis有什么优势和特点May 16, 2022 pm 06:04 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

实例详解Redis Cluster集群收缩主从节点实例详解Redis Cluster集群收缩主从节点Apr 21, 2022 pm 06:23 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

Redis实现排行榜及相同积分按时间排序功能的实现Redis实现排行榜及相同积分按时间排序功能的实现Aug 22, 2022 pm 05:51 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

详细解析Redis中命令的原子性详细解析Redis中命令的原子性Jun 01, 2022 am 11:58 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

一文搞懂redis的bitmap一文搞懂redis的bitmapApr 27, 2022 pm 07:48 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

实例详解Redis实现排行榜及相同积分按时间排序功能的实现实例详解Redis实现排行榜及相同积分按时间排序功能的实现Aug 26, 2022 pm 02:09 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

一起聊聊Redis实现秒杀的问题一起聊聊Redis实现秒杀的问题May 27, 2022 am 11:40 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools