Redis cluster construction

brief introduction

  • Redis cluster adopts P2P mode, which is completely decentralized, and there is no central node or proxy node;
  • Redis clusters do not have a unified portal. When client s connect to the cluster, they can connect to any node in the cluster. Nodes in the cluster communicate with each other (PING-PONG mechanism), and each node is a redis instance
  • Redis divides all keys into 16384 slots, and each redis instance is responsible for some of the slots. All information in the cluster (nodes, ports, slots, etc.) is updated through regular data exchange between nodes.
  • Redis cluster voting fault tolerance mechanism: realize high availability of the cluster, that is, judge whether the node is healthy (whether it can be used normally)
    • If more than half of the nodes in the cluster vote that a node is down, the node will fail
    • If any node in the cluster hangs and the node has no slave node (backup node), the cluster hangs
    • 16384 slots (hash slots) are built in the cluster, and all physical nodes are mapped to these 16384 [0-16383] slots, or these slots are equally allocated to each node. When a key value needs to be stored in the redis cluster, redis will first perform the crc16 algorithm on the key, and then get a result. The result is then summed to 16384. The remainder will correspond to one of [0-16383] slots, and then determine which node the key value is stored in. Therefore, once a node hangs, the slot corresponding to the node cannot be used, which will cause the cluster to fail to work normally.

Cluster setup - single machine

     Redis cluster needs at least three nodes. Because the voting fault tolerance mechanism requires more than half of the nodes to think that a node is hung, so two nodes cannot form a cluster.
     To ensure the high availability of the cluster, each node needs to have a slave node, that is, a backup node, so the Redis cluster needs at least 6 servers.
     Because I don't have so many servers and can't start so many virtual machines, a pseudo distributed cluster is built here, that is, a server runs six redis instances virtually, and the port number is modified to (7001-7006). Of course, the redis cluster construction of the actual production environment is the same as here.

1. Disposition

	[user@server app]$ mkdir redis-cluster
	[user@server app]$ cd redis-cluster
	//create folder
	[user@server redis-cluster]$ mkdir bin data log conf scripts
	//Copy execution script
	[user@server redis-cluster]$ cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server /app/redis-cluster/bin
	//Write a script to copy the configuration file
	[user@server redis-cluster]$ cd scripts
	[user@server scripts]$ vim cp-conf.sh
	# Click i to enter editing
	cp /app/redis/redis.conf ../conf/7001.conf
	cp /app/redis/redis.conf ../conf/7002.conf
	cp /app/redis/redis.conf ../conf/7003.conf
	cp /app/redis/redis.conf ../conf/7004.conf
	cp /app/redis/redis.conf ../conf/7005.conf
	cp /app/redis/redis.conf ../conf/7006.conf
	# End of editing: click Esc, shift +:, wq
	//Execute copy script
	[user@server scripts]$ ./cp-conf.sh
	//To modify the configuration file, it is recommended to open it with a tool and ctrl+f to modify it
	[user@server scripts]$ vim /app/redis-cluser/conf/7001.conf 

     Modify the following items:

	# Node port number
	port 7001  
	# Bind current machine IP
	bind 192.168.119.131  
	# Redis background operation
	daemonize yes  
	# Data file storage location
	dir /app/redis-cluster/data   
	# pid 7001 and port should correspond
	# pidfile: a file containing a process identification number (pid) stored in a well-defined location in the file system, thus allowing other programs to find the pid of a running script.
	pidfile /var/run/redis_7001.pid  
	# log file
	logfile "/app/redis-cluster/logs/7001.log"  
	#  Start cluster mode	
	cluster-enabled yes
	# The configuration file of the cluster is automatically generated for the first time 700070017002
	cluster-config-file  nodes_7001.conf  
	# The request timeout is 15 seconds by default and can be set by yourself
	cluster-node-timeout 15000
	# Is it necessary for each node to be available before the cluster is available? Shut down
	cluster-require-full-coverage no
	# aof logging is enabled whenever necessary. It records a log every time it writes
	appendonly yes

2. Enter app / redis cluster / bin and start the service node

	//Write a startup script
	[user@server scripts]$ vim start-all.sh
	cd ../bin
	./redis-server ../conf/7001.conf
	./redis-server ../conf/7002.conf
	./redis-server ../conf/7003.conf
	./redis-server ../conf/7004.conf
	./redis-server ../conf/7005.conf
	./redis-server ../conf/7006.conf
	/*If an error is reported:
	*** FATAL CONFIG FILE ERROR (Redis 6.2.3) ***
	Reading the configuration file, at line 302
	>>> 'logfile "/app/redis-cluster/logs/7001.log"'
	Can't open the log file: No such file or director
	The following file needs to be created: touch / APP / redis cluster / logs / 7001.log
	Can't open the log file: Permission denied
	Add permissions: chmod 777 7001.log  
	*/
	[user@server scripts]$ ./start-all.sh
	[user@server scripts]$ ps -ef|grep redis
	appuser    4882      1  0 18:54 ?        00:00:00 ./redis-server 10.200.202.41:7001 [cluster]
	appuser    4884      1  0 18:54 ?        00:00:00 ./redis-server 10.200.202.41:7002 [cluster]
	appuser    4886      1  0 18:54 ?        00:00:00 ./redis-server 10.200.202.41:7003 [cluster]
	appuser    4891      1  0 18:54 ?        00:00:00 ./redis-server 10.200.202.41:7004 [cluster]
	appuser    4893      1  0 18:54 ?        00:00:00 ./redis-server 10.200.202.41:7005 [cluster]
	appuser    4903      1  0 18:54 ?        00:00:00 ./redis-server 10.200.202.41:7006 [cluster]
	appuser    4932 141318  0 18:54 pts/1    00:00:00 grep --color=auto redis
	
	cd redis01
	./redis-server redis.conf
	cd ..
	cd redis02
	./redis-server redis.conf
	cd ..
	cd redis03
	./redis-server redis.conf
	cd ..
	cd redis04
	./redis-server redis.conf
	cd ..
	cd redis05
	./redis-server redis.conf
	cd ..
	cd redis06
	./redis-server redis.conf
	cd ..

6. Start the cluster. The first three are the master nodes and the last three are the slave nodes, - a is the password, which must be consistent with that in the configuration file:

  • ./redis-cli --cluster create --cluster-replicas 1 10.200.202.41:7000 10.200.202.41:7001 10.200.202.41:7002 10.200.202.41:7003 10.200.202.41:7004 10.200.202.41:7005 -a 123456@rds (password in. conf)
    [1 indicates that we want to create a slave node for each master node in the cluster]
    [adding replica 10.200.202.41:7004 to 10.200.202.41:7000 mainly, 7004 from]

7. Test [/ APP / redis server / bin]

  • ./redis-cli -h 10.200.202.41 -p 7002
  • auth [password]
  • set key value
  • get key
  • exit

8. Supplementary [path: / APP / redis server / bin]

  • Start the server: redis server conf / 7000.conf
  • Start the client: redis cli - P 6379
  • Remote startup: redis cli - H host - P port - a password
  • Check whether the redis service is started: PING [PONG]

reference material

Keywords: Ruby Database Redis

Added by denhamd2 on Fri, 01 Oct 2021 02:43:32 +0300