Preparations for Cluster Construction
Create redis-cluster directory under / usr/local
mkdir /usr/local/redis-cluster
Previously, if you installed a stand-alone version, you could copy the installed directory.
Installation reference for stand-alone version
Copy the redis directory as a whole into redis-cluster and rename it redis01
cp -r redis redis-cluster/redis01
Enter redis01 and delete the original dump.rdb backup file
Modify redis.conf VI redis.conf
Delete # before cluster-enabled yes
wq preservation
To the redis-cluster directory, copy redis01 to the current directory
cp -r redis01 redis02
At least six redis are required to form a cluster (three masters, three backups)
Modify port port 7001-7006 of redis.conf in turn
Create a batch startAll.sh file in the redis-cluster directory
Set startAll.sh as an executable file
chmod +x startAll.sh
In the same way, create a file shutdownAll.sh to close the redis cluster
Start cluster
./startAll.sh
View process
ps aux | grep -i redis
Cluster building
To build redis cluster, we need to use ruby script and install ruby running environment.
Install ruby
yum install ruby
yum install rubygems
Upload redis-3.0.0.gem (I can find it without this file) to Linux and install it
gem install redis-3.0.0.gem
Copy the redis-trib.rb script file under redis-3.0.0/src to / usr/local/redis-cluster
cp redis-3.0.0/src/redis-trib.rb /usr/local/redis-cluster
Run the script
/ redis-trib.rb create-replicas 1 server address: 7001 server address: 7002 server address: 7003
Server Address: 7004 Server Address: 7005 Server Address: 7006
./redis-trib.rb create --replicas 1 192.168.25.134:7001 192.168.25.134:7002 192.168.25.134:7003 192.168.25.134:7004 192.168.25.134:7005 192.168.25.134:7006
Input yes to start building clusters
The following picture appears and the building is completed.
Linking Clusters with redis-cli
redis01/bin/redis-cli -p 700x -c
Viewing Cluster Information
cluster info
View Cluster Nodes
cluster nodes
Test link redis-cluster cluster cluster
@Test
public void testJedisCluster() throws Exception{
//Create a jedisCulster object and construct the parameter Set type. Each element in the set is HostAndPort type.
Set<HostAndPort> nodes = new HashSet<>();
//Adding nodes to a collection
nodes.add(new HostAndPort("192.168.25.134",7001));
nodes.add(new HostAndPort("192.168.25.134",7002));
nodes.add(new HostAndPort("192.168.25.134",7003));
nodes.add(new HostAndPort("192.168.25.134",7004));
nodes.add(new HostAndPort("192.168.25.134",7005));
nodes.add(new HostAndPort("192.168.25.134",7006));
//Using JedisCluster directly to operate redis, with connection pools, jedisCluster objects can be singletons
JedisCluster jedisCluster = new JedisCluster(nodes);
jedisCluster.set("testCluster", "jedis-cluster-test");
System.out.println(jedisCluster.get("testCluster"));
//Close connection pool
jedisCluster.close();
}
We still need to pay attention to the problem of firewalls. Configuration of firewalls is as follows
Restart firewall service iptables restart