Associated article: Springboot builds the Redis-Cluster client, connects and uses the Redis cluster we built in this article

1. Prepare six servers

Server address Server Angle color
192.168.1.1 master
192.168.1.2 instead master
192.168.1.3 master
192.168.1.4 slave
192.168.1.5 slave
192.168.1.6 slave

A redis cluster contains at least six nodes (three active and three standby nodes). If only four nodes are started, it says: the Redis cluster needs at least three masters. It is impossible for four machines to have one replica for each master.

2. Download Redis (Stable version 5.0.4)

Wget http://download.redis.io/releases/redis-5.0.4.tar.gz tar XZF redis - 5.0.4. Tar. GzcdRedis - 5.0.4 makeCopy the code

# yum install -y GCC # yum install -y GCC # yum install -y GCC

3. Edit the redis. Conf

port 6379
daemonize yes   Run in background mode
protected-mode no   In cluster mode, set to no, and set to yes with the bind parameter, only the IP to be bind can access our redis

dir /data/redis If you have the maximum disk space on your machine, use df -h to retrieve disk information.
logfile redis-6379.log  # the name of the generated diary
loglevel notice # Diary level (for production)

timeout 1800    # How long after the client is idle to close the connection, in seconds. 0 means never close, and the value must be greater than the minimum idle time of the connection pool set by the client
tcp-keepalive 0 # 0 indicates that no TCP ACK is sent to the client to detect if the client is shut down in the absence of communication. There is idle detection on the client, so there is no need for the server to detect the state of the client

maxmemory 4gb   The maximum amount of memory redis can use, if not set, Redis will always run out of system memory
maxmemory-policy volatile-lfu    Lfu performs better than LRU when redis reaches maxMemory
lfu-log-factor 10
lfu-decay-time 1

dbfilename redis-6379.rdb # RDB file name generated
rdbcompression yes  # Enable RDB file compression
stop-writes-on-bgsave-error yes  # bgSave stops writing in case of error to ensure bgSave succeeds
rdbchecksum yes Check RDB file integrity

appendonly yes  Aof is recommended to be disabled on the primary node and enabled on the secondary node
appendfsync everysec    # AOF Flush strategy
auto-aof-rewrite-min-size 64mb  # Rewrite when aOF file is old
auto-aof-rewrite-percentage 100 # Aof growth rate
no-appendfsync-on-rewrite yes   No normal AOF will be performed when aOF is rewritten
appendfilename redis-6379.aof    The name of the aOF file generated

# Ziplist is used when the hash size is less than 512 bytes and each value is less than 64 bytes
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2    The list structure of # redis is a quickList, and each quickList consists of multiple QuickListNodes, each of which has a pointer to the actual stored zipList. In this case, -2 refers to the length of the zipList, 8kb, beyond this number, a new zipList will be created
list-compress-depth 0   # Compression depth is 0, indicating zipList does not compress, without compression, push/pop will be fast; If the depth is 1, the first and last ziplists of the QuickList are not compressed, and the other ziplists are compressed. If the depth is 2, it means that the first ziplist and the second ziplist of the QuickList are not compressed, and the rest are compressed. And so on
set-max-intset-entries 512  Intset becomes dict when the number of elements in an intset reaches 512
zset-max-ziplist-entries 128    Same with hash, because set is a special case of hash, the value of a set is null
zset-max-ziplist-value 64
# hll-sparse-max-bytes 3000

slowlog-max-len 1000    Slow query queue length
slowlog-log-slower-than 1000   # How much time is defined as slow query units in microseconds

cluster-enabled yes Whether to start as a cluster
cluster-config-file redis-nodes.conf
cluster-require-full-coverage no    If all the 16384 slots in the cluster are available or all the master nodes have no problems, the external service is provided to ensure the integrity of the cluster
cluster-node-timeout 15000    # How often each node sends messages to each other, in milliseconds. When the last communication between a node and another node exceeds cluster-node-timeout/2, a node sends the ping command. The node also carries the slots array (2KB) and 1/10 of the cluster's status data (about 1KB for 10 nodes). This parameter also affects the failover time

client-output-buffer-limit normal 0 0 0 # Do not limit the normal client buffer
client-output-buffer-limit replica 512mb 128mb 60   If the slave buffer exceeds 512MB or the buffer exceeds 128MB for 60 seconds, the slave node will be suspended
client-output-buffer-limit pubsub 32mb 8mb 60

replica-lazy-flush yes  Flush operation after receiving RDB file from library
lazyfree-lazy-eviction yes  Memory reaches maxmemory
lazyfree-lazy-expire yes    # key expired and deleted
lazyfree-lazy-server-del yes    The # rename command deletes destKey
Copy the code

4. Create the dir path configured in redis

mkdir -p /data/redis
Copy the code

5. Start redis on six nodes

src/redis-server redis.conf
Copy the code

6. Linux configuration before starting the cluster (skip)

sudo sysctl vm.overcommit_memory=1

sudo sysctl vm.swappiness=0

echo never > /sys/kernel/mm/transparent_hugepage/enabled

echo 511 > /proc/sys/net/core/somaxconn
Copy the code

7. Start the cluster

SRC /redis-cli --cluster create 192.168.1.1:6379 192.168.1.2:6379 192.168.1.3:6379 192.168.1.4:6379 192.168.1.5:6379 192.168.1.6:6379-1 - cluster - replicasView cluster command
src/redis-cli --cluster help
src/redis-cli cluster nodes
src/redis-cli cluster info
Copy the code
Description of Linux configuration parameters before starting the cluster

overcommit_memory

0: indicates that the kernel checks to see if there is enough memory available for the process to use. If there is enough memory available, the memory request is allowed. Otherwise, memory requests fail and errors are returned to the application process

1: indicates that the kernel allows all physical memory to be overallocated, regardless of the current memory state.

cat /proc/sys/vm/overcommit_memory

sudo sysctl vm.overcommit_memory=1
Copy the code

We need to set this value to 1 and then set a reasonable maxMemory for Redis to ensure that the machine has 20%-30% free memory and that forks, BGSave, and aOF overrides do not block the main thread

swappiness

0: would rather be OOM killed than swap. Avoid redis dying when running out of physical memory (if redis is currently high available, dying is better than blocking by calling swap)

Cat /proc/sys/vm/swappiness Takes effect immediately:echo0 > /proc/sys/vm-swappiness takes effect permanently:echoVm. swappiness=0 >> /etc/sysctl.conf or sysctl vm.swappiness=0Copy the code

THP

Speed up the fork; You are advised to disable this function; otherwise, large memory overhead may occur

echo never > /sys/kernel/mm/transparent_hugepage/enabled
Copy the code

TCP backlog

The default TCP backlog value in Redis is 511. You can adjust the tcp-backlog value by modifying the configuration. If the tcp-backlog value in Linux is smaller than the tcp-backlog value in Redis, perform the following operations

echo 511 > /proc/sys/net/core/somaxconn

Copy the code