Redis cluster installation and configuration
1. Redis cluster has been supported since 3.0, at least 3 master nodes are required for the cluster to work properly.
Any instance in the cluster can read and write data, and more than half of the nodes in the cluster fail
The entire cluster is not available, use haproxy or lvs to achieve load balancing and back-end health detection
2. Installation
tar xf redis-3.0.2.tar.gz
cd redis-3.0.2
make
make PREFIX = / usr / local / redis install
mkdir / usr / local / redis / etc
mkdir / usr / local / redis / var
cp redis.conf / usr / local / redis / etc
cp src / redis-trib.rb / usr / local / redis / bin /
3. Configure the cluster (3 machines start two instances each) (Do not support setting password authentication)
mv /usr/local/redis/etc/redis.conf /usr/local/redis/etc/redis.conf.ori
cat >> / usr / local / redis / etc / redis_6379.conf << EOF
daemonize yes
bind 192.168.1.221
port 6379
timeout 300
loglevel notice
logfile "/usr/local/redis/var/redis.log"
databases 16
dbfilename dump.rdb
dir "/ usr / local / redis / var"
maxclients 10000
maxmemory 1024MB
#Enable logging, equivalent to MySQL's binlog
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
#Cluster configuration
cluster-enabled yes
#No need to create cluster automatic update
cluster-config-file nodes_6379.conf
cluster-node-timeout 5000
EOF
cat >> / usr / local / redis / etc / redis_6380.conf << EOF
daemonize yes
bind 192.168.1.221
port 6380
timeout 300
loglevel notice
logfile "/usr/local/redis/var2/redis.log"
databases 16
dbfilename dump.rdb
dir "/ usr / local / redis / var2"
#requirepass 123456
#masterauth 123456
maxclients 10000
maxmemory 1024MB
#Enable logging, equivalent to MySQL's binlog
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
#Cluster configuration
cluster-enabled yes
#No need to create cluster automatic update
cluster-config-file nodes_6380.conf
cluster-node-timeout 5000
EOF
4. Start the instance in the cluster
/ usr / local / redis / bin / redis-server /usr/local/redis/etc/redis_6379.conf
/ usr / local / redis / bin / redis-server /usr/local/redis/etc/redis_6380.conf
#View startup status
ps -ef | grep redis
#Connection test
/ usr / local / redis / bin / redis-cli -h 192.168.1.221 -p 6379 quit
/ usr / local / redis / bin / redis-cli -h 192.168.1.221 -p 6380 quit
/ usr / local / redis / bin / redis-cli -h 192.168.1.222 -p 6379 quit
/ usr / local / redis / bin / redis-cli -h 192.168.1.222 -p 6380 quit
/ usr / local / redis / bin / redis-cli -h 192.168.1.223 -p 6379 quit
/ usr / local / redis / bin / redis-cli -h 192.168.1.223 -p 6380 quit
#Out of service
/ usr / local / redis / bin / redis-cli -h 192.168.1.221 -p 6379 shutdown
/ usr / local / redis / bin / redis-cli -h 192.168.1.221 -p 6380 shutdown
/ usr / local / redis / bin / redis-cli -h 192.168.1.222 -p 6379 shutdown
/ usr / local / redis / bin / redis-cli -h 192.168.1.222 -p 6380 shutdown
/ usr / local / redis / bin / redis-cli -h 192.168.1.223 -p 6379 shutdown
/ usr / local / redis / bin / redis-cli -h 192.168.1.223 -p 6380 shutdown
5. Create a cluster
#Install ruby environment
yum install -y ruby rubygems
gem install redis
#Create cluster
/usr/local/redis/bin/redis-trib.rb create --replicas 1 192.168.1.221:6379 192.168.1.221:6380 192.168.1.222:6379 192.168.1.222:6380 192.168.1.223:6379 192.168.1.223:6380
#Enter yes, the creation is successful, the system will automatically match the master and slave.
#-replicas 1 Manually add slave nodes by yourself
#Represents the creation of 1 slave node for each master node. In a commercial environment, such as 30 machines, it can be configured as 3, and each master node has 3 slave nodes.
6. Cluster management
1. View the machine ID, master-slave, and hash slots in the cluster, you can view the machine status in the cluster
/usr/local/redis/bin/redis-trib.rb check 192.168.1.221:6379
2. Re-shard the data:
/usr/local/redis/bin/redis-trib.rb reshard 192.168.1.221:6379
3. List all the master nodes in the cluster
/ usr / local / redis / bin / redis-cli -h 192.168.1.221 -p 6379 cluster nodes | grep master
4. Let a master node crash and test failover
/ usr / local / redis / bin / redis-cli -h 192.168.1.221 -p 6379 debug segfault
5. Add the master node to the cluster
/usr/local/redis/bin/redis-trib.rb add-node 192.168.1.221:6381 192.168.1.221:6379
# 192.168.1.222: 6381 is the new node, 192.168.1.221:6379 is a node already in the cluster
#It should be noted that at this time, the new node cannot become the real master node, and it needs to be re-sharded.
/usr/local/redis/bin/redis-trib.rb reshard 192.168.1.221:6381
1). The automatic sharding program will ask to move several hash slots, if you want to ensure the uniformity of the data, it should be 16384 / N at this time,
N represents the number of all current nodes including the current host who wish to become master nodes.
2). Then you will be asked which node to move to, just enter the id of the new master node.
3). It will ask which nodes to move the data from. At this time, you can select the ID of some nodes, or you can directly select all, from all the master nodes, so that the data will be divided equally.
6. Increase from node to cluster
/usr/local/redis/bin/redis-trib.rb add-node --slave 192.168.1.222:6381 192.168.1.221:6379
#At this time, the master node of the slave node is not specified, generally one will be automatically selected, and the master node can also be specified, as follows:
/usr/local/redis/bin/redis-trib.rb add-node --slave --master-id 6b45acde96e4225856c8652070e8c8edeec00c79 192.168.1.223:6381 192.168.1.221:6379
# 192.168.1.222: 6381 Newly added node
# 192.168.1.221: 6379 nodes already in the cluster
7. Delete a node (redis instance will be closed after deletion)
1). Delete from node: d598f4cf29cc4fea3616a00a0e707f71a7777cf1 refers to the ID of the redis that was deleted
/usr/local/redis/bin/redis-trib.rb del-node 192.168.1.223:6381 56cfd4a0d4631fdf046c20caac7e5f1a0fce3b39
2). The master node is deleted, at this time you should ensure that the data of the master node is empty, if it is not empty, you need to re-shard the data of the node to other nodes
/usr/local/redis/bin/redis-trib.rb reshard 192.168.1.223:6381
(The deleted master node, first perform data migration to other master nodes)
/usr/local/redis/bin/redis-trib.rb del-node 192.168.1.223:6381 791b834b1a5292bba236956c076849bb45a0ff17
8. Cluster related commands: view after redis-cli -c -p command
CLUSTER NODES: cluster information, including node ID, master-slave relationship
CLUSTER INFO: Check whether the cluster is healthy
CLUSTER MEET <ip> <port> Add the nodes specified by ip and port to the cluster and make it a part of the cluster.
CLUSTER FORGET <node_id> removes the node specified by node_id from the cluster.
CLUSTER REPLICATE <node_id> Set the current node as the slave node of the node specified by node_id.
CLUSTER KEYSLOT <key> Calculates the slot where the key key should be placed.
CLUSTER COUNTKEYSINSLOT <slot> returns the number of key-value pairs currently contained in the slot.
/ usr / local / redis / bin / redis-cli -h 192.168.1.221 -p 6379 CLUSTER NODES
7. Matters needing attention
Cluster notes: More than half of the clusters in the cluster are hung up, and the entire cluster will be out of service.
When deploying, it is recommended to set the master node to the memory type and the slave node to the solidified storage type.
The host hangs up, automatically elected from the opportunity to become the host.
Redis cluster configuration