1. Preparation before installation
Before installing the redis cluster, prepare two centOS 7 hosts, the IP address of host A : 192.168.83.136 , the IP address of host B : 192.168.83.137 ; distribute three redis nodes on hosts A and B ; host A 's three-node redis port is 7000 ,7001,7002 ; The port of the three-node redis of host B is: 7003,7004,7005.
2. Create a node
System redis is installed in the /usr/local/ directory.
1. Create the redis_cluster folder
Create the redis_cluster folder under the /usr/local/redis3.2/ folder
$:mkdir redis_cluster
2. Create a node
$: mkdir 7000 7001 7002 // represent three nodes and their corresponding ports 7000 7001 7002
3. Create node redis.conf configuration
Create the redis.conf configuration of the corresponding node, named the port number
$:cp /usr/local/redis-3.2/redis.conf usr/local/redis-3.2/redis_cluster/7000/
Rename
$: Mv reids.conf 7000.conf
4. Modify the node redis.conf configuration
daemonize yes //redis runs in the background
pidfile /var/run/redis_ 7000 .pid //pidfile corresponds to 7000, 7002, 7003
port 7000 // Port 7000 , 7002, 7003
cluster-enabled yes // Open the cluster and remove the comment #
cluster-config-file nodes_ 7000 .conf // The configuration file of the cluster is automatically generated for the first time
cluster-node-timeout 5000 // The request timeout setting of 5 seconds is enough
appendonly yes //aof log is turned on as needed, it will record a log for each write operation
Remarks: The red port above is modified to the corresponding node port number , and the node configuration of redis is also completed according to the above operation , and the following configuration is enabled to start
3. Configuration enable configuration redis7000 , redis7001 , redis7002 ,
1. Create a node startup file
Create startup configuration files for three nodes in the /etc/init.d file directory, named redis7000 , redis7001 , and redis7002 .
$: vi /etc/init.d/redis7000
2. Start the file configuration project
Redis7000 startup file configuration content:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Start and Stop redis7000
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=7000
EXEC=/usr/local/redis3.2/src/redis-server
CLIEXEC=/usr/local/redis3.2/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis3.2/redis_cluster/7000/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
be
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
be
;;
*)
echo "Please use start or stop as first argument"
;;
esac
Note: Pay attention to modify the red part in the above content
3 , configure the startup
Change permissions
chmod +x /etc/init.d/redis7000
set to start
chkconfig redis7000 on
4. Manually start redis
service redis7000 start
service redis7001 start
service redis7002 start
4. Open the firewall
1. Open the firewall:
firewall-cmd --zone=public --add-port=7000/tcp --permanent
firewall-cmd --zone=public --add-port=17000/tcp --permanent
firewall-cmd --zone=public --add-port=7001/tcp --permanent
firewall-cmd --zone=public --add-port=17001/tcp --permanent
firewall-cmd --zone=public --add-port=7002/tcp --permanent
firewall-cmd --zone=public --add-port=17002/tcp --permanent
2. Restart the firewall
systemctl restart firewalld.service
5. Create a cluster
The redis nodes for building the cluster have been prepared before , and then we need to connect these nodes to build the cluster. An official tool is provided: redis-trib.rb(/usr/local/redis-3.2.1/src/redis-trib.rb) You can see that this bird cannot be executed directly by looking at the suffix. It is a program written in ruby , so we also have to install ruby.
install ruby
1. Install ruby
$:yum -y install ruby ruby-devel rubygems rpm-build
2. Install the redis interface
Then use the gem command to install the redis interface gem is a toolkit of ruby .
$:gem install redis // Wait for a while, this is a bit slow
3. Create a cluster
Enter the /usr/local/redis3.2/src/ directory and execute the following command
$:./redis-trib.rb create --replicas 1 192.168.83.136:7000 192.168.83.136:7001 192.168.83.136:7002 192.168.83.137:7003 192.168.83.137:7004 192.168.83.137:7005
Forgot to take a screenshot here
6. Redis cluster operation command
1. Client connection command
Single client connection command, this command must be in the corresponding redis installation directory src
$: ./redis-cli -h 192.168.83.136 -p 7000
Cluster client connection command
$: ./redis-cli c -h 192.168.83.136 -p 7000
2. Cluster command
3. Cluster status
cluster info : print information about the cluster
cluster nodes : Lists all nodes ( node ) currently known to the cluster, along with information about these nodes.
cluster forget <node_id> : Remove the node specified by node_id from the cluster. Columns such as:
CLUSTER FORGET 2ae88288b7bb1a13b959de9abf0c29e5aa185d15
cluster meet <ip> <port> : Add the node specified by ip and port to the cluster and make it a part of the cluster. For example: CLUSTER MEET 192.168.83.137 7004
cluster replicate <node_id> : Set the current node as the slave node of the node specified by node_id .
cluster saveconfig : Save the configuration file of the node to the hard disk.
1. Slot _
cluster addslots <slot> [slot ...] : Assign one or more slots ( slot ) to the current node.
cluster delslots <slot> [slot ...] : Removes the assignment of one or more slots to the current node.
cluster flushslots : Removes all slots assigned to the current node, making the current node a node with no slots assigned.
cluster setslot <slot> node <node_id> : assign the slot to the node specified by node_id , if the slot is already assigned to
another node, then let the other node delete the slot > before assigning it.
cluster setslot <slot> migrating <node_id> : Migrate the slot of this node to the node specified by node_id .
cluster setslot <slot> importing <node_id> : Import slots from the node specified by node_id to this node.
cluster setslot <slot> stable : Cancel the import ( import ) or migration ( migrate ) of the slot.
2. Key
cluster keyslot <key> : In which slot the computed key key should be placed.
cluster countkeysinslot <slot> : Returns the number of key-value pairs currently contained in the slot .
cluster getkeysinslot <slot> <count> : returns the keys in count slots