Redis Cluster Set Up Complete Process Demo

Preface

This article is intended to locally simulate the process of building a redis server cluster.

What is the role of a Redis cluster?

Before experimenting with deployment, we need to understand why we need to set up a Redis cluster and what problems did it solve?What are the advantages?We can explore this issue from a single Redis server.

Problems with a single Redis server

If you have deployed MySQL master-slave replication read-write separation and high availability of MHA, it is easy to think about the problem with a single Redis server, which includes the following main points:

1) There is a single point of failure;

2) Not meeting the high concurrent requirements;

3) Data loss causes disaster (very low fault tolerance rate);

...

The easiest way to solve this problem is to store backups and expand them horizontally.This requires us to set up a Redis cluster to meet business needs.

Introduction to Redis Clusters

Redis Cluster is an assembly that provides data sharing across multiple Redis nodes

Redis clusters do not support commands that process multiple Keys, because this requires moving data between different nodes, which does not perform as well as Redis, and can lead to unexpected errors under high load

Redis clusters provide a degree of availability by partitioning to continue processing commands when a node is down or unreachable in the real world

Redis Cluster Advantages:

Automatically split data to different nodes

Business instructions can still be handled if part of the cluster node fails or is unreachable

Data fragmentation of Redis cluster

The Redis cluster does not use a consistency hash but introduces the concept of a hash slot.The Redis cluster has a total of 16384 hash slots (0-16383), each Key is modeled after the CRC16 check to determine how to store 16384, and each node of the cluster is responsible for a portion of the hash slots.

In a Redis cluster, adding or deleting nodes is supported without stopping the service.

The next step is to actually simulate the Redis cluster setup:

Environment: Centos7.4 with three network cards each (NAT mode)

ip simulated as master node: 20.0.0.139 20.0.0.144 20.0.0.145

ip simulated as slave node: 20.0.0.140 20.0.0.142 20.0.0.143

Configuration process

master node:

1. Install the compilation environment and required dependent Libraries

[root@localhost ~]# yum install cpp binutils glibc-kernheaders glibc-common glibc-devel gcc gcc-c++ make wget  -y

2. Choose whether to install offline or online by yourself (the article before offline installation already exists, here choose online installation)

[root@localhost ~]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz #Download the installation package, here is the 5.0.7 redis database

[root@localhost ~]# tar zxvf redis-5.0.7.tar.gz #decompression
[root@localhost ~]# mv redis-5.0.7 /usr/local/redis #rename
[root@localhost ~]# cd /usr/local/redis 
[root@localhost redis]# make #Compile
[root@localhost redis]# cd src/
[root@localhost src]# make install  #install

3. Modify the configuration file

[root@localhost src]# cd ..
[root@localhost redis]# ls
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-moduleapi  src
BUGS             deps     MANIFESTO  runtest          runtest-sentinel   tests
CONTRIBUTING     INSTALL  README.md  runtest-cluster  sentinel.conf      utils
[root@localhost redis]# vim redis.conf
#Modify the configuration for the following parameters
bind 127.0.0.1  #Note line 70 listen 127 address, indicating listen all addresses
protected-mode no   #Remove line 89 comment to turn off security
daemonize yes   #Remove line 137 comment to start as a stand-alone process
cluster-enabled yes #Remove line 833 comment to turn on Clustering
cluster-config-file nodes-6379.conf #Remove line 841 comment, cluster name file settings
cluster-node-timeout 15000  #Remove line 847 comment, cluster timeout setting
appendonly yes  #Remove the comment on line 700 to turn on aof persistence'

4. Optimize configuration and open redis service

[root@localhost redis]# mkdir -p /etc/redis
[root@localhost redis]# ln -s /usr/local/redis/redis.conf /etc/redis/6379.conf   #(Place profile in default profile drive)
[root@localhost redis]# ln -s /usr/local/redis/utils/redis_init_script /etc/init.d/redisd    #(configure the initialization file to a system self-starting folder, redisd is the service name, you can modify it yourself)
[root@localhost redis]# service redisd start   #(Open the redis service, named redisd)
[root@localhost redis]# netstat -ntpl|grep redis
[root@localhost redis]# systemctl stop firewalld
[root@localhost redis]# setenforce 0

Configure from the server following the above process

Continue configuring the mater server below

[root@localhost redis]# yum -y install ruby rubygems #Install the redis cluster control software tool

[root@localhost ~]# ls
anaconda-ks.cfg       redis-3.2.0.gem     Template Picture Download Desktop
initial-setup-ks.cfg   Public Video Document Music

[root@localhost ~]# gem install redis --version 3.2.0 

5. Check that the redis services on both servers are turned on and are ready to start simulating cluster creation

[root@localhost ~]# cd /usr/local/redis/src/
[root@localhost src]# redis-cli --cluster create 20.0.0.139:6379 20.0.0.144:6379 20.0.0.145:6379 20.0.0.140:6379 20.0.0.142:6379 20.0.0.143:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 20.0.0.142:6379 to 20.0.0.139:6379
Adding replica 20.0.0.143:6379 to 20.0.0.144:6379
Adding replica 20.0.0.140:6379 to 20.0.0.145:6379
M: 8124ffa07c4618fbe870c25a950262884f16e45d 20.0.0.139:6379
   slots:[0-16383] (5461 slots) master
M: 8124ffa07c4618fbe870c25a950262884f16e45d 20.0.0.144:6379
   slots:[0-16383] (5462 slots) master
M: 8124ffa07c4618fbe870c25a950262884f16e45d 20.0.0.145:6379
   slots:[0-16383] (5461 slots) master
S: fbbdefd030893607cada298533f1610f988522ee 20.0.0.140:6379
   replicates 8124ffa07c4618fbe870c25a950262884f16e45d
S: fbbdefd030893607cada298533f1610f988522ee 20.0.0.142:6379
   replicates 8124ffa07c4618fbe870c25a950262884f16e45d
S: fbbdefd030893607cada298533f1610f988522ee 20.0.0.143:6379
   replicates 8124ffa07c4618fbe870c25a950262884f16e45d
Can I set the above configuration? (type 'yes' to accept): yes #Enter yes here
>>> 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 #A few seconds or so

>>> Performing Cluster Check (using node 20.0.0.139:6379)
M: 8124ffa07c4618fbe870c25a950262884f16e45d 20.0.0.139:6379
   slots:[0-16383] (16384 slots) master
   1 additional replica(s)
S: fbbdefd030893607cada298533f1610f988522ee 20.0.0.140:6379
   slots: (0 slots) slave
   replicates 8124ffa07c4618fbe870c25a950262884f16e45d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Or refer to the following figure

6. Check systematically:

‚Äč

7. Verify data writing and reading

[root@localhost ~]# redis-cli -h 20.0.0.140 -p 6379 -c #Remote login
20.0.0.140:6379> set popda 5
-> Redirected to slot [13275] located at 20.0.0.139:6379
OK
20.0.0.139:6379> exit
[root@localhost ~]# redis-cli -h 20.0.0.142 -p 6379 -c
20.0.0.142:6379> get popda
-> Redirected to slot [13275] located at 20.0.0.139:6379
"5"
20.0.0.139:6379> 

Of course, there are many other ways to deploy Redis clusters, and container deployment (such as docker) has always been a favorite way.

Tags: Database Redis glibc yum MySQL

Posted on Tue, 24 Mar 2020 09:57:31 -0700 by jlh3590