Cluster node PlanningIn a cluster with three nodes, each node has one master node and one slave node. In theory, six servers are required (no money is needed ). Build a pseudo-distributed cluster and use six redis instances for simulation.
Build a ruby Environment
- To build a cluster, you must use the official ruby script and install the ruby environment.
Yum install rubyyum install rubygems # install ruby Package Manager
- Install the gem package required by ruby: http://download.csdn.net/detail/xuda27/9668339
gem install redis-3.0.0.gem
Cluster ConstructionStep 1: Create 6 apsaradb for redis instances with a port number ranging from 7001 ~ 7006 1. In/usr/local/
Createredis-cluster
Directory and/usr/local/redis
Copy the redis folder to the redis-cluster folder, rename the folder redis01, and create redis02, redis03 ,...... Redis06. 2. Copy the ruby script for cluster creationredis-cluster
Directory, that is, under the redis source code package (src directory)redis-trib.rb
Copyredis-cluster
Directory.
#1. copy all the files in the redis directory to the redis-cluster directory and rename the folder redis01 cp-r redis/usr/local/redis-cluster/redis01 #2. copy the redis-trib.rb to the cp redis-trib.rb/usr/local/redis-cluster directory in redis-cluster
Step 2: Modify the redis. conf configuration file in the bin directory of the six redis instances. 1. Modify the port number.The redis01 port is modified, and the remaining five redis instances have ports ranging from 7002 to 7006. 2. Open the comment before cluster-enable.Tell redis to use the cluster.Step 3: Start six redis instances. Inredis-cluster
Create under directorystartup.sh
It is used to start six redis instances andstartup.sh
Grant the execution permission.
# Create startup. sh [root @ vm_16109110_centos redis-cluster] # vim startup. sh cd redis01/bin. /redis-server redis. confcd .. cd .. cd redis02/bin. /redis-server redis. confcd .. cd .. cd redis03/bin. /redis-server redis. confcd .. cd .. cd redis04/bin. /redis-server redis. confcd .. cd .. cd redis05/bin. /redis-server redis. confcd .. cd .. cd redis06/bin. /redis-server redis. confcd .. cd .. # grant the execution permission chmod + x startup. sh # Start 6 redis instances. /startup. sh # view redis process running: ps aux | grep redis
redis-cluster
The Created directory structure is as follows:
Step 5: Create a cluster. You need to know the IP address of the Linux server.ifconfig
Command to view.
./redis-trib.rb create --replicas 1 10.104.161.110:7001 10.104.161.110:7002 10.104.161.110:7003 10.104.161.110:7004 10.104.161.110:7005 10.104.161.110:7006
Cluster creation Information:
# The master node has a slot, and the slave node does not have a slot> sort Ming hash slots allocation on 6 nodes... using 3 masters: 10.104.161.110: Weight: 7003 Adding replica 10.104.161.110: 7004 to weight: 7001 Adding replica failed: 7005 to weight: 7002 Adding replica 10.104.161.110: 7006 to 10.104.161.110: 7003 M: 2017a1d2c7a02cd38e9f61138975863fc8c20d0ea 10.104.161.110: 7001 slots: 0-5460 (5461 slots) mas TerM: Lost 10.104.161.110: 7002 slots: 5461-10922 (5462 slots) masterM: Lost 10.104.161.110: 7003 slots: 10923-16383 (5461 slots) masterS: Lost 10.104.161.110: 7004 replicates lost: e2439d0da-c7abbf437b76b50ebe1db6066f51ef 10.104.161.110: 7005 replicates e2a9a2 8c3842d37aa602182110f93598c2936da5S: 1127ebb21380ebb23c45d3c5a4c79c92840470df 10.104.161.110: 7006 replicates when I set the above configuration? (Type 'yes' to accept ): yes >>>> Nodes configuration updated >>> Assign a different config epoch to each node >>>> Sending cluster meet messages to join the clusterWaiting for the cluster to join ......>> grouping Cluster Check (using node 10.104.161.110: 7001) M: Drawing 10.104.161.110: 7001 slots: 0-5460 (5461 slots) masterM: Drawing 10.104.161.110: 7002 slots: 5461-10922 (5462 slots) masterM: Drawing 10.104.161.110: 7003 slots: 10923-16383 (5461 slots) masterM: Drawing 10.104.161.110: 7004 slots: (0 slots) master replicates segment: Drawing 10.104.161.110: 7005 slots: (0 slots) master replicates partition: ipv10.104.161.110: 7006 slots: (0 slots) master replicates partition [OK] All nodes agree about slots configuration. >>> check for open slots... >>> check slots coverage... [OK] All 16384 slots covered.
Test ClusterRedis-cli is available in the bin from redis01 to redis06. You can choose one of them, for example:
#-H indicates Host IP address-p indicates port (7001-7006)-c indicates cluster redis01/bin/redis-cli-h 10.104.161.110-p 7002-c
Test blind play:
[root@VM_161_110_centos redis-cluster]# redis01/bin/redis-cli -h 10.104.161.110 -p 7001 -c10.104.161.110:7001> set b 99OK10.104.161.110:7001> get b"99"10.104.161.110:7001> get a-> Redirected to slot [15495] located at 10.104.161.110:7003"100"10.104.161.110:7003> pingPONG
Note: If you set up redis on a remote server, remember to enable ports 7001 to 7006 and 6379, so you can use redis later.
# Open port 7001 iptables-a input-ptcp -- dport 7001-j ACCEPT