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-redisCluster 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 '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: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 < masterip> < masterport>
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 < bytes>
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!