How to build a Redis cluster
Redis cluster is a distributed service cluster composed of multiple master-slave node groups. It has replication, high availability and sharding features. Redis cluster can complete node removal and failover functions without sentinel. Each node needs to be set to cluster mode. This cluster mode has no central node and can be expanded horizontally. According to official documents, it can be linearly expanded to tens of thousands of nodes (the official recommendation is no more than 1,000 nodes). The performance and high availability of the redis cluster are better than the previous version of sentinel mode, and the cluster configuration is very simple.

Build environment
System: centos7.4
Server Kingsoft Cloud
Install ruby environment
[root@jsy-bj-test00 ~]# yum install -y ruby rubygems
Copy 6 copies of redis service
[work@jsy-bj-test00 ~]$ cp -rp redis redis1 [work@jsy-bj-test00 ~]$ cp -rp redis redis2 [work@jsy-bj-test00 ~]$ cp -rp redis redis3
Redis configuration file modification
#六个节点需做如下更改[work@jsy-bj-test00 ~]$ vim redis1/etc/redis.conf [work@jsy-bj-test00 ~]$ sed -i 's/port 6379/port 6380/g' redis5/etc/redis.conf #修改端口 port 6380#打开注释,开启集群模式cluster-enabled yes#集群的配置文件cluster-config-file nodes-6380.conf [work@jsy-bj-test00 ~]$ sed -i 's/cluster-config-file nodes-6379.conf/cluster-config-file nodes-6380.conf/g' redis5/etc/redis.conf#pidfile文件pidfile /var/run/redis_6380.pid [work@jsy-bj-test00 ~]$ sed -i 's/pidfile \/var\/run\/redis_6380.pid/pidfile \/var\/run\/redis_6380.pid/g' redis5/etc/redis.conf#日志文件 logfile "/home/work/logs/redis/6380.log"[work@jsy-bj-test00 ~]$ sed -i 's/logfile "\/home\/work\/logs\/redis\/6379.log"/logfile "\/home\/work\/logs\/redis\/6380.log"/g' redis5/etc/redis.conf#rdb持久化文件 dbfilename dump6380.rdb [work@jsy-bj-test00 ~]$ sed -i 's/dbfilename dump6379.rdb/dbfilename dump6380.rdb/g' redis5/etc/redis.conf#请求超时,单位毫秒cluster-node-timeout 5000#开启aof持久化方式appendonly yes#配置持久化文件appendfilename "appendonly6379.aof"[work@jsy-bj-test00 ~]$ sed -i 's/appendfilename "appendonly6379.aof"/appendfilename "appendonly6383.aof"/g' redis5/etc/redis.conf
Write cluster start script and stop script
Start script start_all.sh
/home/work/redis/bin/redis-server /home/work/redis/etc/redis.conf & /home/work/redis/bin/redis-server /home/work/redis/etc/redis6380.conf & /home/work/redis/bin/redis-server /home/work/redis/etc/redis6381.conf & /home/work/redis/bin/redis-server /home/work/redis/etc/redis6382.conf & /home/work/redis/bin/redis-server /home/work/redis/etc/redis6383.conf & /home/work/redis/bin/redis-server /home/work/redis/etc/redis6384.conf &
Close the script stop_all.sh
/home/work/redis/bin/redis-cli -p 6379 -a test shutdown /home/work/redis/bin/redis-cli -p 6380 -a test shutdown /home/work/redis/bin/redis-cli -p 6381 -a test shutdown /home/work/redis/bin/redis-cli -p 6382 -a test shutdown /home/work/redis/bin/redis-cli -p 6383 -a test shutdown /home/work/redis/bin/redis-cli -p 6384 -a test shutdown
Execute the create cluster command
The following error is reported
[work@jsy-bj-test00 src]$ ./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError) from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'from ./redis-trib.rb:25:in `
‘
Cause: The error when installing the redis interface reported that the ruby version is too low
[root@jsy-bj-test00 ~]# gem install redisFetching: redis-4.1.3.gem (100%) ERROR: Error installing redis: redis requires Ruby version >= 2.3.0.
The solution is to download the latest stable version source code from the official website http://www.ruby-lang.org/en/downloads/ for compilation and installation
[work@jsy-bj-test00 soft]$ tar zxvf ruby-2.7.0.tar.gz [work@jsy-bj-test00 soft]$ cd ruby-2.7.0 [work@jsy-bj-test00 ruby-2.7.0]$ ./configure --prefix=/home/work/ruby && make && make install
Install redis interface
[work@jsy-bj-test00 bin]$ gem install redis
Execute the create cluster command again
[work@jsy-bj-test00 src]$ ./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... ...... >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join... >>> Performing Cluster Check (using node 127.0.0.1:6379) ...... [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
Creation is successful, check cluster status
[work@jsy-bj-test00 ~]$ ./redis/bin/redis-cli -h 127.0.0.1 -p 6379 -c 127.0.0.1:6379> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_sent:627 cluster_stats_messages_received:627
View cluster node information
127.0.0.1:6379> cluster nodes 565246bf31d8e05e464db7455521b1a9f165a9cd 127.0.0.1:6380 master - 0 1578447776230 2 connected 5461-10922 1b99b2a1e4b530501476ab48422c75f30423fd19 127.0.0.1:6383 slave 565246bf31d8e05e464db7455521b1a9f165a9cd 0 1578447778233 5 connected 735ad5778458059316794b9378d4b81aaff20322 127.0.0.1:6379 myself,master - 0 0 1 connected 0-5460 757c2c11ecebfc607aa10a6877e348d0e2da484f 127.0.0.1:6381 master - 0 1578447777732 3 connected 10923-16383 f22efc2bcfcd11cee6487ebc9c75de3b59f5e1d0 127.0.0.1:6382 slave 735ad5778458059316794b9378d4b81aaff20322 0 1578447776230 4 connected b8bba94b9647caa8600363144fd7108082e45f56 127.0.0.1:6384 slave 757c2c11ecebfc607aa10a6877e348d0e2da484f 0 1578447777232 6 connected#这是很重要的命令,我们需要关心的信息有:#第一个参数:节点ID#第二个参数:IP:PORT@TCP 这里一个坑,jedis-2.9.0之前的版本解析@出错#第三个参数:标志(Master,Slave,Myself,Fail...)#第四个参数:如果是从机则是主机的节点ID#最后两个参数:连接的状态和槽的位置。
Cluster management related commands
#集群增加节点,先复制两个配置文件,并修改配置文件内容[work@jsy-bj-test00 etc]$ cp -p redis.conf redis6385.conf [work@jsy-bj-test00 etc]$ cp -p redis.conf redis6386.conf [work@jsy-bj-test00 etc]$ sed -i 's/6379/6385/g' redis6385.conf [work@jsy-bj-test00 etc]$ sed -i 's/6379/6386/g' redis6386.conf
Start 6385 nodes
[work@jsy-bj-test00 bin]$ ./redis-server /home/work/redis/etc/redis6385.conf &
Softly connect the cluster management tool to the bin of redis
[work@jsy-bj-test00 bin]$ ln -s /home/work/soft/redis-3.2.11/src/redis-trib.rb /home/work/redis/bin/redis-trib.rb
Add the new node to the cluster master
[work@jsy-bj-test00 bin]$ ./redis-trib.rb add-node 127.0.0.1:6385 127.0.0.1:6379 >>> Adding node 127.0.0.1:6385 to cluster 127.0.0.1:6379 >>> Performing Cluster Check (using node 127.0.0.1:6379) ...... [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:6385 to make it join the cluster. [OK] New node added correctly.
Check the cluster status, no slots are allocated
[work@jsy-bj-test00 bin]$ ./redis-cli -h 127.0.0.1 -p 6379 -c cluster nodes d00d05f601df0b69df0c2cc532b636d2c83347be 127.0.0.1:6385 master - 0 1578450280191 0 connected
Assign slots to 6385 nodes
[work@jsy-bj-test00 bin]$ ./redis-trib.rb reshard 127.0.0.1:6379 >>> Performing Cluster Check (using node 127.0.0.1:6379) ...... [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)? 500 What is the receiving node ID? d00d05f601df0b69df0c2cc532b636d2c83347be 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:allDo you want to proceed with the proposed reshard plan (yes/no)? yes#第一个参数:需要移动槽的个数,#第二个参数:接受槽的节点ID,#第三个参数:输入"all"表示从所有原节点中获取槽,#第四个参数:输入"yes"开始移动槽到目标结点id#查看6385节点信息,已经分配槽位[work@jsy-bj-test00 bin]$ ./redis-cli -h 127.0.0.1 -p 6379 -c cluster nodes d00d05f601df0b69df0c2cc532b636d2c83347be 127.0.0.1:6385 master - 0 1578452422167 7 connected 0-165 5461-5627 10923-11088
Add slave nodes, start 6386 nodes and join the cluster
[work@jsy-bj-test00 bin]$ ./redis-server /home/work/redis/etc/redis6386.conf & [work@jsy-bj-test00 bin]$ ./redis-trib.rb add-node --slave --master-id d00d05f601df0b69df0c2cc532b636d2c83347be 127.0.0.1:6386 127.0.0.1:6385 >>> Adding node 127.0.0.1:6386 to cluster 127.0.0.1:6385 >>> Performing Cluster Check (using node 127.0.0.1:6385) ...... [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:6386 to make it join the cluster. Waiting for the cluster to join. >>> Configure node as replica of 127.0.0.1:6385. [OK] New node added correctly.
View 6386 Node status
[work@jsy-bj-test00 bin]$ ./redis-cli -h 127.0.0.1 -p 6379 -c cluster nodes 6845878cbef3fe25f19a70a8db3eb29abb1b9ea6 127.0.0.1:6386 slave d00d05f601df0b69df0c2cc532b636d2c83347be 0 1578452805439 7 connected
Delete node
[work@jsy-bj-test00 bin]$ ./redis-trib.rb del-node 127.0.0.1:6383 1b99b2a1e4b530501476ab48422c75f30423fd19 >>> Removing node 1b99b2a1e4b530501476ab48422c75f30423fd19 from cluster 127.0.0.1:6383 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.
