Redis Cluster: Redis Cluster provides a real-time high-availability solution. In Redis Cluster cluster, the downtime of any node will not affect the service, and the nodes in the slave role can seamlessly replace the downmaster Nodes continue to provide services externally.
For example, in a Redis Cluster with three masters and three slaves, Redis Cluster defines 16384 (0 ~ 16383) slots, and distributes these slots evenly among the three master nodes of Redis Cluster, such as 0 ~ 5000 Slots belong to master node A, 5001 ~ 10000 slots belong to master node B, 10001 ~ 16383 slots belong to master node C, the check value of any key in Redis after CRC16 check is always in the range of 0 ~ 16383 According to the check value of the key, the key is stored in the node that is consistent with the slot value. A1, B1, and C1 are the slave nodes of A, B, and C, respectively, and automatically synchronize the slot information and slot information from the corresponding master node. Key data, when the master node goes down, the slave node immediately performs fault escaping and becomes the master node to continue to provide services.
A Redis Cluster with three masters and three slaves is shown below
We deploy the Redis Cluster environment with a three-master and three-slave architecture
CPU name | IP address | Roles | installation manual | version |
redis-master-01 | 172.16.101.54 | Master | /usr/local/redis | Version 3.12 |
redis-master-02 | 172.16.101.55 | Master | ||
redis-master-03 | 172.16.101.56 | Master | ||
redis-slave-01 | 172.16.101.58 | Slave | ||
redis-slave-02 | 172.16.101.59 | Slave | ||
redis-slave-03 | 172.16.101.60 | Slave |
1. The system environment configuration of each node
Create a new user and group to run redis
# groupadd -r dba # useradd -r -g dba -G root redis # cat /home/redis/.bash_profile ................ PATH=/usr/local/redis/src:$PATH:$HOME/.local/bin:$HOME/bin ................
System kernel parameter optimization
# cat /etc/sysctl.conf ....................... vm.overcommit_memory=1 vm.swappiness=1 fs.aio-max-nr=1048576 fs.file-max= 7672460 net.ipv4.ip_local_port_range=9000 65500 net.core.rmem_default=262144 net.core.rmem_max=4194304 net.core.wmem_default=262144 net.core.wmem_max=1048586 net.core.somaxconn = 2048 kernel.sem= 50100 64128000 50100 1280 kernel.shmall=5242880 kernel.shmmax=12884901888 .......................
Redis running resource optimization
# cat / etc / security / limits.conf .................................. redis soft nproc 65536 redis hard nproc 65536 redis soft nofile 65536 redis hard nofile 65536 .................................
Large page file optimization
# echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
2. Redis installation of each node
# mkdir /usr/local/redis # chown -R redis.dba /usr/local/redis $ cd /usr/local/redis $ tar -zxf redis-3.2.12.tar.gz $ mv redis-3.2.12/* . $ rm -rf redis-3.2.12* $ make $ mkdir {data,log,conf} $ mv redis.conf conf
Three. Configure the redis configuration file of each node, as shown below, only the main parameters are listed here
bind 0.0.0.0 protected-mode no port 6379 tcp-backlog 1024 timeout 0 tcp-keepalive 0 daemonize yes supervised systemd pidfile /var/run/redis_6379.pid loglevel notice logfile "/usr/local/redis/log/redis.log" dir "/usr/local/redis/data" slave-read-only yes repl-ping-slave-period 3 repl-timeout 15 slave-priority 100 min-slaves-to-write 1 min-slaves-max-lag 10 rename-command flushall "" rename-command flushdb "" rename-command save "" rename-command keys "" maxclients 10000 maxmemory 4gb maxmemory-policy allkeys-lru appendonly yes appendfilename "appendonly.aof" appendfsync everysec cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes
Four. Configure redis as a system service
# cat /usr/lib/systemd/system/redis.service [Unit] Description=Redis In-Memory Data Store Documentation=https://redis.io/documentation After=syslog.target After=network.target [Service] Type=notify User=redis Group=dba Restart=always # Disable OOM kill on the Redis OOMScoreAdjust=-1000 Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj Environment=PG_OOM_ADJUST_VALUE=0 ExecStart=/usr/local/redis/src/redis-server /usr/local/redis/conf/redis.conf ExecStop=/bin/kill -15 $MAINPID LimitNOFILE=65536 [Install] WantedBy=multi-user.target
# systemctl daemon-reload
# systemctl enable redis
# systemctl start redis
# systemctl status redis
After each node is successfully started, a unique node-id will be automatically generated, and after the cluster function is enabled by default, each node considers itself to be the master node.
172.16.101.54
25630:M 12 Apr 00:59:59.477 * No cluster configuration found, I'm 89a83af689b194b18c7f5c0dae105c329a6a831f
172.16.101.55
6232:M 12 Apr 00:59:59.450 * No cluster configuration found, I'm 6c9558d05f3d5a1b2dac4ebe31d581b1581f6112
172.16.101.56
28099:M 12 Apr 00:59:59.478 * No cluster configuration found, I'm 684908fde2ccc85fa2caf2fbbbfaa0e9f470cf04
172.16.101.58
25490:M 12 Apr 01:01:07.832 * No cluster configuration found, I'm 310edd63d50d3d1412e6413b9c4d2ff31e2fae5f
172.16.101.59
27836:M 12 Apr 01:01:07.835 * No cluster configuration found, I'm 63eef92da475574c62485543ac57898d13618520
172.16.101.60
15314:M 12 Apr 01:01:07.839 * No cluster configuration found, I'm c7095b2f9adf4adada4fb0afd953ab7439ccc058
View current cluster node information
redis-master-01:6379> info cluster # Cluster cluster_enabled:1 redis-master-01:6379> cluster info cluster_state:fail cluster_slots_assigned:0 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:1 cluster_size:0 cluster_current_epoch:0 cluster_my_epoch:0 cluster_stats_messages_sent:0 cluster_stats_messages_received:0 redis-master-01:6379> cluster nodes 89a83af689b194b18c7f5c0dae105c329a6a831f :6379 myself,master - 0 0 0 connected
It can be seen from the above
- The current cluster function has been enabled, cluster_enabled: 1
- Cluster function fail state, cluster_state: fail
- No slots are allocated in the cluster, cluster_slots_assigned: 0
- In the cluster, only the simple information of its own nodes can be viewed.