Foreword
In Redis Cluster Cluster (a) understand the concepts of clusters in this section we will learn to build a cluster platform.
1 Introduction
The cluster should at least have an odd number of nodes, so build a cluster requires a minimum of three hosts, and each node has at least one backup node, so the following to create a minimum of six machines to complete Redis Cluster cluster (the primary node, the backup node redis -cluster cluster to determine)
the true cluster: there are six redis service six servers (the six different numbers redis service host, port number, which may be identical)
192.168.1.1.110: 6379
192.168.1.1.111: 6379
192.168.1.1. 112: 6379
false cluster: there is one server 6 redis service (this service redis same six host number [because using the same server], the port number is not the same)
192.168.1.111: 6379
192.168.1.111:6380
192.168.1.111 : 6382
2. build a clustered environment
Here is limited by our own equipment, so we use the add cluster structures
[1] to create Redis node installation directory
mkdir /usr/local/redis_cluster //指定目录下 创建 redis_cluster
[2] In redis_cluster directory, create a folder 7001-7006
mkdir 7001 7002 7003 7004 7005 7006
[3], respectively, and redis-conf copied to the folder 7001-7006
cp /root/redis-4.0.1/redis.conf ./7001
[4] the configuration file are modified as follows, as revised modified content
<1> protected-mode in order to prohibit access to the public network redis Cache, redis strengthen security.
It enabled condition, there are two:
1) IP is not the bind
2) does not set the access password
because we did not set a password above and build process, it satisfies the second point, so redis on Linux in safe mode, which you can not let go easily establish a connection from outside the virtual machine, if external access: redis.conf protection mode is set to the mODE NO-protected
<2> modify other configuration files are as follows (at redis.conf)
bind 127.0.0.1 进行注释
port 7001 改掉原来的端口号6379
dameonize yes 改为后台运行(守护进程)
pidfile /var/run/redis_7001.pid 把该文件的端口进行更改
cluster-enabled yes 将注释去掉(默认是不开启集群的)
cluster-config-file nodes-7001.conf 将注释去掉,并把文件名改为以端口号为主
[5] redis activate each node
copy table redis-4.0.1 / src files to the respective directories redis 7001-7006.
cd redis-4.0.1 //进入桌面redis解压目录下
cp -r ./src /usr/local/redis_cluster/7001 //进行拷贝,依次复制到7001-7006
Start each node Redis
cd /usr/local/redis_cluster/ // 进去redis集群配置文件
./7001/src/redis-server ./7001/redis.conf //依次启动7001-7006各节点服务
[6] Check start situation Redis
ps -ef | grep -i redis
After completion of the above steps, we are on a single server started six Redis service, which is what we call false cluster environment to build complete, then we will be able to officially create the cluster.
3. Create a cluster
Redis official offers redis-trib.rb this tool, in the src directory unpacked directory, (for ease of operation) copy their files to / usr / local / bin directory (the equivalent of an environment variable in the Java directory, you can always process operations anywhere), can directly access this command
cd redis-4.0.1/
cd src
cp redis-trib.rb /usr/local/bin/
Can be executed directly from the command line: ip: port mode
redis-trib.rb create --replicas 1 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
If this error occurs:
/usr/bin/env: ruby:没有那个文件或目录
This tool is used ruby achieve, so you need to install ruby.
4. Installation ruby
Run the following command
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis
If an error in the implementation of the words
[root@localhost bin]# gem install redis
ERROR: Error installing redis:
redis requires Ruby version >= 2.3.0.
The cause of the above error: Centos tacit support to the ruby 2.0.0, the installation may gem lowest redis need is 2.3.0
solution is to install rvm, then ruby version upgrade
5. Installation RVM and corresponding version ruby
(Refer to detailed process may be installed above article)
6. Redis Cluster Cluster Setup
After the above operations, we managed to build a complete variety of scenarios, so we once again with the command of redis-trib.rb this tool will not be the error.
redis-trib.rb create --replicas 1 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
Whether to allow the default configuration yes (and it will automatically build a cluster)
7.Redis Cluster Cluster Verification
[1] Redis cluster test:
For testing purposes, can be redis-cli command into client / usr / local / bin directory
cd /home/gs/redis-4.0.1/src # /home/gs/redis-4.0.1是我安装redis的目录
cp redis-cli /usr/local/bin
[2] on a machine (or) the connection port 7001 of the cluster node:
redis-cli -h 127.0.0.1 -c -p 7001
-C additional parameters may be connected to the cluster, because of the above redis.conf will bind to the ip address, so -h parameter can not be omitted.
Analysis: adding a node corresponding to the key data in 7001, due to the machine cluster are equal, it is possible to redirect to another machine (e.g., above the primary server 7002) and it may be relevant from the server 7005 read command.
[3] redis cluster in the design, to take into account to the center, to the middleware, that is, each node in the cluster are equal relationship. Are peers. Each node keeps the respective state data and the entire cluster. Each node and all other nodes are connected, and these connections remain active, thus ensuring that we only need to connect any node in the cluster, you can get data from other nodes.
[4] The basic command
info replication to look at the effect of the cluster Cluster Nodes Cluster Info command and command
cluster nodes to obtain information of the cluster node
to appeal paste the information into a text editor, you will be found.
Redis each node has an ID value, this ID will be used permanently redis this specific example, in order to have unique names in the examples the context of the cluster, each node remembers using this node ID to each other, rather than by IP or port. IP address and port may change, but the only node identifier does not change throughout the life cycle of the node. We simply call this node identifier ID
[5] We can also carry out the relevant tests clusters by the Java
<1> At this time we want to open the corresponding port authority
in turn open port 70017002 ... 7006 (following commands only for Centos7 above)
查看已经开放的端口 : firewall-cmd --list-ports
开启端口: firewall-cmd --zone=public --add-port=7001/tcp --permanent
重启防火墙 firewall-cmd --reload
<2> java test code
public static void main(String[] args){
//第一步:使用JedisCluster对象,需要一个Set<HostAndPort>参数,Redis节点的列表
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
nodes.add(new HostAndPort(“127.0.0.1”, 7001));
nodes.add(new HostAndPort(“127.0.0.1”, 7002));
nodes.add(new HostAndPort(“127.0.0.1”, 7003));
nodes.add(new HostAndPort(“127.0.0.1”, 7004));
nodes.add(new HostAndPort(“127.0.0.1”, 7005));
nodes.add(new HostAndPort(“127.0.0.1”, 7006));
JedisCluster jedisCluster = new JedisCluster (nodes);
// 第二步:直接使用JedisCluster对象操作redis, 在系统中单例存在
jedisCluster.set(“qfjy”, “千锋教育”);
String result = jediscluster.get(“qfjy”);
//第三步:打印结果
System.out.println(result);
}