Use of Redis: Cluster construction details, redis cluster Construction Details
Key words: Server Load balancer high availability, redis Cluster
Requirement: Concept of high availability of Server Load balancer
What is high availability of Server Load balancer?
Nginx is generally used as a server Load balancer server. It can be seen that it is in a very important position in the network. Once the Nginx server fails to provide services, the impact will be serious. Therefore, high availability of Server Load balancer is required.
High Availability-master-slave backup
Keepalived + nginx implements master-slave backup
The purpose of Keepalived is to check the status of the server. If a web server goes down or fails to work, Keepalived will detect it and remove the faulty server from the system, in addition, other servers are used to replace the work of the server. When the server works normally, Keepalived automatically adds the server to the server group. All these work is completed automatically without manual interference, only the faulty server needs to be manually repaired.
How keepalived works
Keepalived is implemented based on the VRRP Protocol. VRRP is short for Virtual Router Redundancy Protocol, that is, Virtual routing Redundancy Protocol.
The vro redundancy protocol can be considered as a high-availability Router Protocol. N vrouters provide the same functions to form a vro Group, which contains one master node and multiple backups, the master has a vip that provides external services (the default route of other machines in the LAN where the router is located is this vip). The master will issue multicast, when the backup fails to receive the vrrp package, the master is deemed to be down. In this case, a backup instance must be elected as the master Based on the VRRP priority. In this way, the high availability of the router can be ensured.
Keepalived consists of three modules: core, check, and vrrp. The core module is the core of keepalived and is responsible for starting and maintaining the main process and loading and parsing global configuration files. Check is responsible for health checks, including common health check methods. The vrrp module is used to implement the VRRP protocol.
Requirement: Build a redis Cluster
Understand the differences between clusters and master-slave Databases
Basic concepts of redis Clusters
Redis clusters should have at least three nodes. To ensure high availability of the cluster, each node must have a backup machine. Therefore, a redis cluster requires at least six servers.
The pseudo-distribution mode is set up here. You can run six redis instances on one server and change the redis port number to 7001-7006.
Build a redis Cluster
Install the ruby Environment
yum install rubyyum install rubygems
Install the package used by the ruby script
# Install the gem install redis-3.0.7.gem offline
# Install gem install redis-v 3.0.7 online
Create 6 servers and change the port number of the six servers to 7001--7006.
1. Change the port number to 7001-70062, remove cluster-enabled yes3 from the cluster # Note, and add ports 7001 and 7001 to the Security Group if they are ECs instances, such as ports 17001.
Clear cache data for each node
bin/redis-cli -p 7002 -c127.0.0.1:7002> flushallOK127.0.0.1:7002> cluster resetOK127.0.0.1:7002> quit[root@VM_241_143_centos redis02]# cd ..[root@VM_241_143_centos redis-cluster]# cd redis03[root@VM_241_143_centos redis03]# bin/redis-cli -p 7003 -c127.0.0.1:7003> flushallOK127.0.0.1:7003> cluster resetOK127.0.0.1:7003> quit
Use a custom shell script to start six servers
cd /usr/local/redis-cluster/redis01bin/redis-server redis.confcd ../redis02bin/redis-server redis.confcd ../redis03bin/redis-server redis.confcd ../redis04bin/redis-server redis.confcd ../redis05bin/redis-server redis.confcd ../redis06bin/redis-server redis.conf
Use a custom shell script to close six servers and submit them to you.
Run the following code to build a Cluster Environment
./redis-trib.rb create --replicas 1 10.31.152.30:7001 10.31.152.30:7002 10.31.152.30:7003 10.31.152.30:7004 10.31.152.30:7005 10.31.152.30:7006
./redis-trib.rb create --replicas 1 10.31.166.22:9001 10.31.166.22:9002 10.31.166.22:9003 10.31.166.22:9004 10.31.166.22:9005 10.31.166.22:9006
./redis-trib.rb create --replicas 1 119.29.181.95:7001 119.29.181.95:7002 119.29.181.95:7003 119.29.181.95:7004 119.29.181.95:7005 119.29.181.95:7006
Two successfully created Clusters
How the client connects to machines in the Cluster
#-P port number #-c enable reidis cluster mode. When connecting to the redis cluster node, use bin/redis-cli-p 7004-c.
Store data to the cluster node for testing to see which node the data is stored
The redis cluster has 16384 built-in hash slots. When you need to store key-value pairs in the cluster, redis first uses the CRC16 algorithm to calculate a result for the key, calculate the remainder of 16384 based on this result, so that each key corresponds to a hash slot ranging from 0 to. redis maps the hash slots to different nodes based on the number of nodes.