Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Building configuration of Redis High availability Cluster

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the construction and configuration of Redis high-availability clusters. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Redis's cluster master-slave model is a highly available cluster architecture. The main contents of this chapter are: the construction of highly available clusters, Jedis connection clusters, new cluster nodes, deletion of cluster nodes, and other configuration supplementary instructions.

High availability cluster building

Cluster technology is a relatively new technology. Through cluster technology, we can obtain relatively high benefits in terms of performance, reliability and flexibility at a lower cost, and its task scheduling is the core technology of the cluster system.

Clustering has been supported since Redis 3.0. 16384 hash slots are built into the Redis cluster. Redis maps 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 host is dead, the host is really dead and the whole cluster is unavailable.

If you assign multiple slaves to each host in the cluster. The mainframe has hung up and can still work normally from the upper position. However, if more than half of the hosts in the cluster are down, the cluster is not available whether there are slaves or not.

Preparatory work before construction

Build 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 1: install the ruby environment

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

Step 3: find the redis-trib.rb file in the redis decompressed 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

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

[root@itdragon] # ruby-- versionruby 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 Server "> prepare six redis servers

Same as master-slave replication logic, copy redis.conf file 6 times, port from 6000 to 6005

[root@itdragon bin] # cp redis.conf redis6000.conf [root@itdragon bin] # vim redis6000.conf port xxxx # modify Port cluster-enabled yes # Open comment Open the cluster mode cluster-config-file nodes-xxxx.conf # configuration file pidfile / var/run/redis_xxxx.pid # pidfile file logfile "xxxx.log" # log file dbfilename dump_xxxx.rdb # rdb persistence file cluster-node-timeout 5000 # request timed out Unit millisecond appendonly yes # enable aof persistence [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 uplix start-all.sh [root@itdragon bin] #. / start-all.sh [root@itdragon Bin] # ps aux | grep redisroot 28001 0.00.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 1: copy six redis.conf and modify the relevant configuration. If you find it troublesome, you can use the file I configured: https://github.com/ITDragonBlog/daydayup/tree/master/Redis/reids.conf.

Step 2: add batch start redis service programs, and increase the execution permission

Step 3: check whether the six redis services have started successfully

Master-slave cluster construction

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

[root@itdragon bin] # / redis-trib.rb create-- replicas 1112.74.83.71: 6000112.74.83.71 Creating cluster > Performing hash slots allocation on 6 nodes...Using 3 masters:112.74.83.71:6000112.74.83.71:6001112.74.83.71:6002Adding replica 112.74. 83.71:6003 to 112.74.83.71:6000Adding replica 112.74.83.71:6004 to 112.74.83.71:6001Adding replica 112.74.83.71:6005 to 112.74.83.71:6002. # omit Can I set the above configuration? (type 'yes' to accept): yes. # omit [OK] All nodes agree about slots configuration. > > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered. # there are 16384 available slots to provide services. [root@itdragon bin] #. / redis-cli-h 112.74.83.71-p 6002-c112.74.83.71 set testKey value- > set testKey value- > Redirected to slot [5203] located at 112.74.83.71:6000OK112.74.83.71:6000 > cluster infocluster_state:ok.112.74.83.71:6000 > cluster nodes0968ef8f5ca96681da4abaaf4ca556c2e6dd0242 112.74.83.71root@itdragon bin 600216002 master- 0 1512035804722 3 connected 10923-1638313ddd4c1b8c00926f61aa6daaa7fd8d87ee97830 112.74.83.71 slave 0968ef8f5ca96681da4abaaf4ca556c2e6dd0242 01512035803720 6 connecteda3bb22e04deec2fca653c606edf5b02b819f924f 112.74.83.71 slave 1d4779469053930f30162e89b6711d27a112b601 01512035802000 4 connected1d4779469053930f30162e89b6711d27a112b601 112.74.83.71 6000000 myself Master-0 1512035802000 1 connected 0-5460a3b99cb5d22f5cbd293179e262f5eda931733c88 112.74.83.71 5460a3b99cb5d22f5cbd293179e262f5eda931733c88 6001mm 16001 master-01512035802719 2 connected 5461-10922915a47afc4f9b94389676b4e14f78cba66be9e5d 112.74.83.71 Vera 6004 slave a3b99cb5d22f5cbd293179e262f5eda931733c88 01512035801717 5 connected

Step 1: set up 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 _ mai _ c means to connect to redis in cluster mode

Step 3: save the data

Step 4: cluster info query cluster status information

Step 5: cluster nodes queries cluster node information. There is a pit, which will be described later.

Possible problems

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.

To be clear, modify the cluster-config-file nodes.conf file to avoid duplicate names, or delete the file and recreate the cluster.

Cluster nodes queries cluster node information

This is a very important order, and the information we need to care about are:

First parameter: node ID

The second parameter: there is a pit in IP:PORT@TCP. There was an error in parsing the previous version of jedis-2.9.0.

The third parameter: flag (Master,Slave,Myself,Fail...)

Fourth parameter: if it is a slave, it is the node ID of the host

The last two parameters: the state of the connection and the location of the slot.

Jedis connection cluster

The first step is to configure the firewall

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

Finally, the integration of Spring

Unit testing

/ * * Cluster test * if the following similar error is prompted: * java.lang.NumberFormatException: For input string: "6002 / 16002" * if the installed redis version is greater than 4, the jedis version may be lower. Choose 2.9.0 * because among the messages printed by cluster nodes, there is no @ 16002 tcp port information before version 4 * 0968ef8f5ca96681da4abaaf4ca556c2e6dd0242 112.74.83.71 0968ef8f5ca96681da4abaaf4ca556c2e6dd0242 112.74.83.71 master-01512035804722 3 connected 10923-16383 * / @ Testpublic void testJedisCluster () throws IOException {HashSet 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 Test:" + cluster.get ("cluster-key")); cluster.close ();}

Possible problems

Java.lang.NumberFormatException: For input string: "600216002"

If the version of redis 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 Primary 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 connected # 112.74.83.71 connected # there is no distribution slot [root] # / redis-cli-h 112.74.83.71-p 6000 cluster nodes916d26e9638dc51e168f32969da11e19c875f48f 112.74.83.71 Itdragon bin] #. / redis-trib.rb reshard 112.74.83.71:6000How many slots do you want to move (from 1 to 16384)? 500What is the receiving node ID? 916d26e9638dc51e168f32969da11e19c875f48fPlease enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs.Source node # 1:allDo 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 nodes916d26e9638dc51e168f32969da11e19c875f48f 112.74.83.71 redis-cli 6006 master-01512116047897 7 connected 0165 5461-5627 10923-11088

Step 1: create a new host for redis6006.conf and start the Redis service

Step 2: add a new host node. If you print "[OK] New node added correctly." Indicates that the addition was successful.

Step 3: query the cluster node information and find that although the host at port 6006 has been added, there is no content behind the connection status, that is, there is no allocation slot

Step 4: assign slots to port 6006 hosts

First parameter: the number of slots to be moved

Second parameter: ID of the node that accepts the slot

Third parameter: enter "all" to get slots from all original nodes

Fourth parameter: enter "yes" to start moving the slot to the target node id

Step 5: query the cluster node information and find that the host at port 6006 has been assigned slots

Core commands:

. / redis-trib.rb add-node add host ip:port cluster any node ip:port

. / redis-trib.rb reshard cluster any node ip:port

Possible problems

[ERR] Sorry, can't connect to node 112.74.83.71:6006

Description: the new host must be in the startup state.

Add slave node

[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 master-id 916d26e9638dc51e168f32969da11e19c875f48f 112.74.83.71 root@itdragon bin 6007 112.74.83.71-p 6000 cluster nodes80315a4dee2d0fa46b8ac722962567fc903e797a 112.74.83.716007 slave 916d26e9638dc51e168f32969da11e19c875f48f 01512117377000 7 connected

Step 1: create a new host for redis6007.conf and start the Redis service

Step 2: add slave node to the original command-- slave-- master-id master node ID

Step 3: query cluster node information

Delete a node

Before deleting a node, make sure that the node has no value, otherwise prompt: is not empty! Reshard data away and try again. If the node has a value, the slot needs to be allocated.

. / redis-trib.rb del-node 112.74.83.71 916d26e9638dc51e168f32969da11e19c875f48f configuration file supplement

The following configurations were introduced in the previous chapters of the Redis tutorial

1 enable the daemon of Redis: daemonize yes

2 specify the pid file write file name: pidfile / var/run/redis.pid

3 designated Redis port: port 6379

4 the address of the bound host: bind 127.0.0.1

5 Redis persistence enables compressed data by default: rdbcompression yes

6 specify rdb file name: dbfilename dump.rdb

7 specify the rdb file location: dir. /

8 when the slave starts, it automatically synchronizes data from master: slaveof

< masterip>

< masterport>

9 enable aof persistence method: appendonly yes

10 specify aof file name: appendfilename appendonly.aof

11 trigger aof snapshot mechanism: appendfsync everysec (no/always)

This chapter is the last chapter in the Redis tutorial, so let's talk about the rest of the configuration together.

1 sets the client connection timeout. 0 means to shut down: timeout 300

2 set Redis log level, debug, verbose (default), notice, warning:loglevel verbose

3 set the number of databases: databases 1

4 set Redis connection password: requirepass foobared

5 set the maximum number of client connections at the same time. By default, there is no limit: maxclients 128

6 specify Redis maximum memory limit: maxmemory

< bytes>

7 specify whether the virtual memory mechanism is enabled: vm-enabled no

8 specify virtual memory file path: vm-swap-file / tmp/redis.swap

9 specify to include other configuration files: include / path/to/local.conf

This is the end of the configuration of the Redis high availability cluster. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report