CentOS 6.5 build Redis3.2.8 single machine distributed cluster
- 2020-06-07 05:55:58
- OfStack
preface
Recently, a set of Redis3.0 pseudo-distributed cluster was set up on the server. One problem was found, that is, Shell scripting ability and the importance of operation and maintenance tools need to be improved urgently.
Cluster environment installation
1. Install Redis
$ cd /usr/local # The installation directory
$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz
$ tar xzf redis-3.2.8.tar.gz
$ mv redis-3.2.8/ redis
$ cd redis
$ make
$ make install
Note: the official command is missing make install, causing redis-ES16en to be used only in the src directory of redis
2. Install Ruby & rubygems
$ yum install ruby ruby-devel rubygems
Create a clustered environment
The directories required to create the cluster [at least 6 master,3 slave, formula 6+2N]
$ mkdir -p /usr/local/redis/cluster/16001/
$ mkdir -p /usr/local/redis/cluster/16002/
$ mkdir -p /usr/local/redis/cluster/16003/
$ mkdir -p /usr/local/redis/cluster/16004/
$ mkdir -p /usr/local/redis/cluster/16005/
$ mkdir -p /usr/local/redis/cluster/16006/
Copy ES35en.conf to each node
$ cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16001/redis.conf
$ cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16002/redis.conf
$ cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16003/redis.conf
$ cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16004/redis.conf
$ cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16005/redis.conf
$ cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16006/redis.conf
Then modify redis. conf under each node, the main changes are as follows
port < port >
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
logfile "redis-server.log"
appendonly yes
4. Remote access
Remote access configuration
If you want remote access, you need to modify bind in ES49en.conf
The default is 127.0.0.1 loopback address. If you want remote access, you need to change it to LAN address or 0.0.0.0. Of course, you can also use dual address mode, such as my LAN address is 192.168.12.213
$ bind 192.168.12.213 127.0.0.1 # recommended
$ bind 0.0.0.0 # Is not recommended
For details, please refer to Redis to enable remote login connection
2. Open the firewall port
$ service iptables status # View firewall status
$ iptables -I INPUT -p tcp --dport 16001 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 16002 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 16003 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 16004 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 16005 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 16006 -j ACCEPT
(4) telnet link
telnet 192.168.12.213 16001
Start the instance of Redis
# Start each of the 6 redis instances (at this point, the nodes are running as Redis Cluster, but the cluster is not automatically built because they are in the "I don't know you, you don't belong to me" state, each of them is a single Redis node, or a cluster containing only one node)
$ cd /usr/local/redis/cluster/16001 && redis-server redis.conf > redis-server.log 2>&1 &
$ cd /usr/local/redis/cluster/16002 && redis-server redis.conf > redis-server.log 2>&1 &
$ cd /usr/local/redis/cluster/16003 && redis-server redis.conf > redis-server.log 2>&1 &
$ cd /usr/local/redis/cluster/16004 && redis-server redis.conf > redis-server.log 2>&1 &
$ cd /usr/local/redis/cluster/16005 && redis-server redis.conf > redis-server.log 2>&1 &
$ cd /usr/local/redis/cluster/16006 && redis-server redis.conf > redis-server.log 2>&1 &
2 in the command > & 1 represents the standard error stream input into the standard output stream, that is, into redis-ES81en.log for each node, as for the last & On linux, ES85en-ES86en itself is the background startup, so this & You can not add
Of course, we can listen for every redis-ES92en.log change with the tail command
tail -f /usr/local/redis/cluster/16002/redis-server.log
Once all redis above has started, we can detect it with any of the following commands
$ yum install ruby ruby-devel rubygems
0
Create the cluster
1. Install the redis gem plug-in
We need to install the gem tool that supports redis, otherwise an error will be reported
$ yum install ruby ruby-devel rubygems
1
Note: If gem install redis --version 3.0.0 fails, you need to modify the source of gem under 1
$ yum install ruby ruby-devel rubygems
2
Create a cluster
Let the above instances communicate with each other (1 represents one salve per master)
$ /usr/local/redis/src/redis-trib.rb create --replicas 1 \
127.0.0.1:16001 \
127.0.0.1:16002 \
127.0.0.1:16003 \
127.0.0.1:16004 \
127.0.0.1:16005 \
127.0.0.1:16006
3. Cluster information viewing
Cluster creation is complete and you can view the information using the following command:
$ yum install ruby ruby-devel rubygems
4
Output information:
$ yum install ruby ruby-devel rubygems
5
From the above information, you can clearly see which are the master and slave nodes.
4. Save cluster information
Save the node's configuration file to the hard disk
$ yum install ruby ruby-devel rubygems
6
Connect ports (16001 - can be tested using other ports > 16006).
$ yum install ruby ruby-devel rubygems
7
Election port detection
If you're careful, you'll notice that after we created the cluster, one of the new ports started
By following the command
netstat -lntp | grep 'redis'
You will find the following ports, which are used for voting and switching between primary and secondary
26001 26002 26003 26004 26005 26006