redis-sentinel cluster (k8s script)

Posted by Alt_F4 on Tue, 14 Jul 2020 16:40:04 +0200

1 redis backup

It is recommended that rdb and aof be turned on simultaneously.rdb is on by default and aof is off by default.Refer to rdb and aof for additional details

1.1Redis.confConfiguration Details

requirepass 123456

## The following is the rdb configuration
#dbfilename: persisted data stored locally in a file
dbfilename dump.rdb

#dir: persisted data is stored at the local path, and if redis-cli is started under / redis/redis-3.0.6/src, the data is stored in the current SRC directory
dir ./

##When snapshot triggers, save <seconds> <changes>  
##The following is 900 seconds before snapshot takes place with at least one change operation  
##Caution is required in setting this value to assess how intensive changes are in the system  
##You can turn off snapshot functionality by "save""  
#save time, which means that one key has been changed for persistent storage at 900s intervals; 10 key300s have been changed for storage; and 10,000 key60s have been changed for storage.
save 900 1
save 300 10
save 60 10000

##Whether to block client's Change Operations when an error occurs in snapshot and cannot continue, Error may be due to disk full/disk failure/OS level exception, etc.  
stop-writes-on-bgsave-error yes 

##Whether or not rdb file compression is enabled, defaulting to yes, and compression often means "extra cpu consumption", as well as this smaller file size and shorter network transfer time  
rdbcompression yes

##The following are aof configurations
##This option is the switch of aof function, default is "no", you can turn on aof function by "yes"  
##Only under "yes" will features such as aof override/file synchronization take effect  
appendonly yes  

##Specify aof file name  
appendfilename appendonly.aof  

##There are three legal values for specifying the file synchronization policy in aof operations: always everysec no, which defaults to everysec  
appendfsync everysec  

##Whether appendfsync suspends file synchronization during aof-rewrite,'no'means'not suspended','yes' means'suspended', and defaults to'no'
no-appendfsync-on-rewrite no  

##Minimum file size (mb,gb) triggered by aof file rewrite, rewrite will only be triggered if the aof file is larger than this size, default "64mb", recommended "512mb"  
auto-aof-rewrite-min-size 64mb  

##The percentage of the aof file that should grow when this rewrite triggers, relative to the last rewrite.  
##After each rewrite, redis records the size of the "new aof" file (for example, A) at this point, when the AOF file grows after A*(1 + p)  
##The next rewrite is triggered, and each time an aof record is added, the size of the current aof file is detected.  
auto-aof-rewrite-percentage 100  

Refer to other configurations

1.2 Start the master server

docker run -d -p 6379:6379 \
--name=redis-master \
-v redis.conf:/etc/redis.conf
redis:4.0.5 \
redis-server /etc/redis.conf

The primary node starts one.

1.3 Start from Server

docker run -d -p 6380:6379 \
--name=redis-slave \
--link=redis-master \
-v redis.conf:/etc/redis.conf
redis:4.0.5 \
redis-server /etc/redis.conf --slaveof redis-master 6379

Start at least one from the node.

Create a redis master server, then create a redis slave node, specified by the redis-server - slaveof redis-master 6379 command, indicating that the current node service is a redis-master slave.

1.4 Test

When data is manipulated on the redis primary node, the secondary node synchronizes the primary node data.

2 Create a redis-sentinel image

2.1Sentinel.confConfiguration Details

# Port on which the current Sentinel service is running
port 26379

# Temporary folder used by Sentinel service runtime
dir /tmp

# Sentinel monitors a master redis instance named mymaster with an IP address of redis-master and port number of 6379, and at least $SENTINEL_is required to judge the master as invalidQUORUM Sentinel processes agree that automatic failover will not execute as long as the number of Sentinels agreed is not up to the standard
sentinel monitor mymaster redis-master 6379 $SENTINEL_QUORUM

#Specifies the number of milliseconds that Sentinel will need to invalidate the Redis instance.Sentinel marks the instance as subjectively offline when it does not return a PING or returns an error directly after that time.Only a Sentinel process marking an instance as subjectively offline does not necessarily result in automatic failover of the instance: only after a sufficient number of Sentinels have marked an instance as subjectively offline will the instance be marked as objectively offline, and automatic failover will take place
sentinel down-after-milliseconds mymaster $SENTINEL_DOWN_AFTER

# Specifies how many new primary instances can be synchronized from Redis instances at most when performing a failover. The smaller the number, the longer the synchronization time, and the longer the failover will take to complete when there are more Redis instances
sentinel parallel-syncs mymaster 1

# If the failover operation is not completed within this time (ms), the failover is considered to have failed
sentinel failover-timeout mymaster $SENTINEL_FAILOVER

# Set Primary Service Password
sentinel auth-pass mymaster $PASSWORD

Refer to other configurations

2.2 Start sentinel service

docker run -d -p 6379:6379 \
--name=redis-master \
-v redis.conf:/etc/sentinel.conf
redis:4.0.5 \
redis-server /etc/sentinel.conf --sentinel

Sentinel nodes are configured at least two or more.SENTINEL_The number of QUORUM depends on the number of sentinel nodes, typically reduced by 1.

3 kubernetes script

For k8s related scripts, refer to:

Reference resources:

[Deploy entinel-based highly available Redis clusters using Docker Compose]

[redis persistence]

Topics: Redis snapshot Docker network