Redis stand-alone installation and cluster installation (version 5.0.9)

Redis stand-alone and cluster configuration (version 5.0.9)

Taken from the Internet and use it for future reference! And recorded some of the problems I encountered!

Stand-alone configuration

Text steps

1. Download the redis compressed package, and then decompress the file (tar -xzvf xxx)
2. Enter the redis file directory after decompression, compile the redis source file (make, without the c environment, gcc)
3. Install the compiled redis source file Go to the /usr/local/redis directory. If there is no redis directory in the /local directory, the redis directory will be created automatically
. 4. Enter the /usr/local/redis/bin directory and start redis directly. /redis-server (this is the front end Start redis)
5. Change the redis startup mode to backend startup, the specific method: copy the redis.conf file under the decompressed redis file to the /usr/local/redis/bin directory, and then modify the redis.conf file- >daemonize no changed to yes
6. In the /bin directory, start redis through ./redis-server redis.conf (this is a background start)

Actual operation

In our actual operation, the above scenario shows that our redis stand-alone server has been successfully installed, but there is a drawback, that is, when we start the server, it is started in the foreground. If we close the session, the server process corresponds to it. Is stopped, so we need to set the redis server to be started in the background.
1. First, we will modify this place under the redis.conf file and change it to yes.

2. After the modification, perform the following steps again, and finally the background Start the server successfully

Cluster version configuration

Introduction to cluster concepts

1. Only singleton mode was supported before redis3.0 version, and clusters were only supported after 3.0 version.
2. The redis cluster adopts the P2P model, which is completely decentralized, and there is no central node or proxy node.
3. The redis cluster does not have a unified entrance. When the client connects to the cluster, it can connect to any node in the cluster. The nodes within the cluster communicate with each other (PING-PONG mechanism), and each node is a redis instance.
4. In order to achieve the high availability of the cluster, that is, to determine whether the node can be used normally, redis-cluster has a voting fault tolerance mechanism: if more than half of the nodes in the cluster vote that a node is down, then the node is down (fail) , This is the method to judge whether the node is down.
5. So what if it is judged whether the cluster is down? -> If any node in the cluster is down, and the node has no slave node (backup node), then the cluster is down. This is a method to determine whether the cluster is down.
6. Why does any node hang up when the cluster hangs? -> Because the cluster has built-in 16384 slots (hash slots), and all physical nodes are mapped to these 16384[0-16383] slots, or these slots are equally distributed to each node. When you need to store a data (key-value) in the redis cluster, redis will first perform the crc16 algorithm on the key, and then get a result. Then take the remainder of this result to 16384, this remainder will correspond to one of the slots [0-16383], and then determine which node the key-value is stored in. Therefore, once a node is down, the slot corresponding to the node cannot be used, which will cause the cluster to fail to work normally. When reading, first do crc16 on this key and take the remainder to get the corresponding stored node.
7. In summary, each redis cluster can theoretically have up to 16,384 nodes.

Environment required for cluster construction

1. A redis cluster requires at least 3 nodes, because the voting fault tolerance mechanism requires that more than half of the nodes think that a node is down, and that node is down, so two nodes cannot form a cluster
. 2. To ensure the high availability of the redis cluster, you need Each node has a slave node, which is a backup node, so a redis cluster requires at least 6 servers

Steps to build redis cluster

Installation environment and version: centos7, redis5.0.9. Clusters with redis version before 5 need to install ruby ​​environment (this is time-consuming, and there are various problems to be solved), after redis5.0.0, all the functions of redis-trib.rb script have been integrated into redis-cli. This article builds a cluster based on redis-cli's -cluster.

Installation process

1. Download and unzip (I copied the files directly from the redis stand-alone version above)
2. Compile and install
make & make install
3. Create a redis node
Create a redis_cluster folder under /opt/redis-cluster/redis-5.0.5, At the same time, create redis7001, redis7002, redis7003, redis7004, redis7005, redis7006 folders in it, and copy the redis.conf folder to these 6 folders, respectively, modify the 6 configuration files, and modify the contents as follows:

port 7001       // 端口7001,7002,7003,7004,7005,7006
bind 本机ip     //本地是127.0.0.1
daemonize yes   // 设置redis后台运行
pidfile /var/run/redis_7001.pid   // pidfile文件对应7001 ... 7006
cluster-enable yes  // 开区集群,把注释去掉
cluster-config-file nodes_7001.conf  // 集群的配置,配置文件首次启动自动生成
cluster-node-timeout 15000  // 请求超时,默认15秒,可自行设置
appendonly yes  // aof日志开启,有需要就开启,时间长了文件会很大
masterauth 123456 // 密码的
requirepass 123456 // 密码的

4. Start each node, you can make a batch file start-all.sh
script content is like this:

start successfully:

5. Check the redis startup situation

[root@markusZhang redis-5.0.9]# netstat -tnlp | grep redis
tcp        0      0 127.0.0.1:7001          0.0.0.0:*               LISTEN      12164/src/redis-ser 
tcp        0      0 127.0.0.1:7002          0.0.0.0:*               LISTEN      12166/src/redis-ser 
tcp        0      0 127.0.0.1:7003          0.0.0.0:*               LISTEN      12171/src/redis-ser 
tcp        0      0 127.0.0.1:7004          0.0.0.0:*               LISTEN      12173/src/redis-ser 
tcp        0      0 127.0.0.1:7005          0.0.0.0:*               LISTEN      12175/src/redis-ser 
tcp        0      0 127.0.0.1:7006          0.0.0.0:*               LISTEN      12180/src/redis-ser 
tcp        0      0 127.0.0.1:17001         0.0.0.0:*               LISTEN      12164/src/redis-ser 
tcp        0      0 127.0.0.1:17002         0.0.0.0:*               LISTEN      12166/src/redis-ser 
tcp        0      0 127.0.0.1:17003         0.0.0.0:*               LISTEN      12171/src/redis-ser 
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      27577/./redis-serve 
tcp        0      0 127.0.0.1:17004         0.0.0.0:*               LISTEN      12173/src/redis-ser 
tcp        0      0 127.0.0.1:17005         0.0.0.0:*               LISTEN      12175/src/redis-ser 
tcp        0      0 127.0.0.1:17006         0.0.0.0:*               LISTEN      12180/src/redis-ser 

If one is missing, check which configuration file is wrong
. 6. Create a cluster

[root@markusZhang redis-5.0.9]# redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1 -a 123456

The –cluster-replicas 1 command means: one master and one slave configuration, six nodes are three masters and three slaves. The
last -a 123456 is the set password.

Use the following command to check the status of the cluster

[root@markusZhang redis-5.0.9]# redis-cli --cluster check 127.0.0.1:7001 -a 123456
# 后面的-a 123456 是设置的密码,如果你们设置密码,就添加上

to sum up

In this way, the stand-alone version and cluster version of redis are installed successfully. We can start to explore the mechanism of redis cluster!

Guess you like

Origin blog.csdn.net/MarkusZhang/article/details/108894679