Redis Cluster Building Instructions
Using two virtual machines to simulate 6 nodes and one virtual machine to create 3 master and 3 salve environments
Redis version: redis-3.2.4
linux version: CentOS 6.5 (10.60.44.76), CentOS 7.0 (10.60.44.105)
1. Download the redis installation package
Installation package is attached, redis-3.2.4.tar.gz
2. Install redis
Note: This stage is best operated under the root user
Decompress, compile, install
tar zxvf redis-3.2.4.tar.gz cd redis-3.2.4 make cd src && make install
Create a directory to store redis commands and configuration files
mkdir -p /usr/local/redis/bin mkdir -p /usr/local/redis/etc
Mobile redis files
# Mobile redis configuration file mv redis.conf /usr/local/redis/etc # Mobile redis command file mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-sentinel redis-trib.rb /usr/local/redis/bin
Start the redis database
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
View Redis running status
# Or netstat-tunpl | grep 6379 ps -ef | grep redis
3. Building Clusters
Create folders to store clusters
A folder named 7031 7032 7033 is placed in a 10.60.44.76 machine, and a folder named 7034 7035 7036 is placed in a 10.60.44.105 machine.
mkdir -p /usr/local/redis-cluster cd /usr/local/redis-cluster # Create each node folder mkdir 7031 7032 7033 # IP 10.60.44.76 on-board execution mkdir 7034 7035 7036 # IP 10.60.44.105 machine execution
Copy and modify configuration files
Here we take 10.60.44.76 machine as an example, another machine operates consistently.
cp /usr/local/redis/etc/redis.conf /usr/local/redis-cluster/7031 cd /usr/local/redis-cluster/7031 vi redis.conf
Modify the configuration file as follows
port 7031 # Binding ports bind This machine ip # Physical ip: e.g. 10.60.44.76 dir /usr/local/redis-cluster/7031 # Specify the data storage path cluster-enabled yes # Start cluster mode cluster-config-file nodes-7031.conf # Specify cluster node profile daemonize yes # Background boot cluster-node-timeout 5000 # Specify cluster node timeout appendonly yes # Specify persistence mode
After the redis.conf of 7031 is changed, it is copied to the remaining three directories, and then only need to replace 7031 of redis.conf as the corresponding node globally. The same is true for 10.60.44.105 machines.
Install Ruby and Start Plug-ins
Redis cluster startup needs redis-trib.rb implemented by Ruby, so we need to install Ruby and cluster startup plug-ins first.
yum install ruby yum install rubygems gem install redis
Open cluster nodes
Run the following shell in each of the two machines to start each node in the cluster
# 10.60.44.76 for((i=1;i<=3;i++)); do /usr/local/redis/bin/redis-server /usr/local/redis-cluster/703$i/redis.conf; done # 10.60.44.105 for((i=4;i<=6;i++)); do /usr/local/redis/bin/redis-server /usr/local/redis-cluster/703$i/redis.conf; done
View node status
Detecting the open state of the node is generally no problem.
ps -ef | grep redis-server
The results are as follows:
### 10.60.44.76 root 18992 1 0 04:37 ? 00:00:09 /usr/local/redis/bin/redis-server 10.60.44.76:7031 [cluster] root 18994 1 0 04:37 ? 00:00:09 /usr/local/redis/bin/redis-server 10.60.44.76:7032 [cluster] root 18998 1 0 04:37 ? 00:00:10 /usr/local/redis/bin/redis-server 10.60.44.76:7033 [cluster] root 41471 16554 0 05:29 pts/2 00:00:00 grep redis-server ### 10.60.44.105 root 50565 1 0 04:37 ? 00:00:07 /usr/local/redis/bin/redis-server 10.60.44.105:7034 [cluster] root 50567 1 0 04:37 ? 00:00:08 /usr/local/redis/bin/redis-server 10.60.44.105:7035 [cluster] root 50571 1 0 04:37 ? 00:00:08 /usr/local/redis/bin/redis-server 10.60.44.105:7036 [cluster] root 51273 34592 0 05:29 pts/1 00:00:00 grep --color=auto redis-server
Close the firewall
The specified ports need to be opened between nodes, where the firewall is closed directly
centos 6.5
service iptables stop # Close command: chkconfig iptables off # Permanently close the firewall service iptables status # Two commands run at the same time, and after running, check the firewall shutdown status
centos 7.0
systemctl stop firewalld.service # Stop firewall systemctl disable firewalld.service # Prohibit firewall boot-up firewall-cmd --state # View the default firewall status (not running after closing and running after opening)
Open Cluster
After each node is normally opened, it runs on either of the two machines:
/usr/local/redis/bin/redis-trib.rb create --replicas 1 10.60.44.76:7031 10.60.44.76:7032 10.60.44.76:7033 10.60.44.105:7034 10.60.44.105:7035 10.60.44.105:7036
Client Connect Cluster to View Cluster Status
Running on a machine:
# 10.60.44.76 /usr/local/redis/bin/redis-cli -c -h 10.60.44.76 -p 7031 set hello world
Running on another machine
# 10.60.44.105 /usr/local/redis/bin/redis-cli -c -h 10.60.44.105 -p 7034 get hello
The display indicates the success of the building.
Close the cluster
Closing the cluster requires closing the nodes one by one, which can be done automatically using scripts:
# 10.60.44.76 for((i=1;i<=3;i++)); do /usr/local/redis/bin/redis-cli -c -h 10.60.44.76 -p 703$i shutdown; done # 10.60.44.105 for((i=4;i<=6;i++)); do /usr/local/redis/bin/redis-cli -c -h 10.60.44.105 -p 703$i shutdown; done
additional
The following commands need to be deleted from the configuration file and database before starting the cluster each time:
# 10.60.44.76 for((i=1;i<=3;i++)); do rm -f /usr/local/redis-cluster/703$i/dump.rdb /usr/local/redis-cluster/703$i/redis-703$i.conf; done # 10.60.44.105 for((i=4;i<=6;i++)); do rm -f /usr/local/redis-cluster/703$i/dump.rdb /usr/local/redis-cluster/703$i/redis-703$i.conf; done