In the production environment, Sentinel master-slave architecture is the most widely used Redis architecture, because a single point is prone to failures, and fragmented cluster is too complex. The author tries to build a Redis cluster architecture on a server with one master, two slaves and three sentry monitors. As the sentry may also have a single point of failure, so the author also uses three sentry to monitor. For learning, it is enough to build a pseudo-cluster.
1. Configuration template
The author creates a redis configuration template file: template-redis.conf, and a sentinel configuration template: template-sentinel.conf for replication configuration.
1.1 Create Redis Profile Template
- Copy a redis.conf file from the Redis source directory to the / usr/local/etc/redis directory
- Create redis configuration template file: / usr/local/etc/redis/template-redis.conf
- When copying the template, only the port number in the configuration file needs to be replaced, that is, only the global replacement of 6379 can replace the port number, log file, pid file and so on in the configuration file.
# Introducing the original configuration include /usr/local/etc/redis/redis.conf ######################################## Custom Configuration ######################################## # Modify Binding ip bind 127.0.0.1 # Setting Close Protection Mode protected-mode no # Set the port number to 6379 port 6379 # Set background boot to yes daemonize yes # Setting up pid file pidfile /logs/redis/redis.pid.6379 # Setting up log files logfile /logs/redis/redis.log.6379 # Setting RDB dum file name dbfilename dump.rdb.6379 # Setting up dump file storage directory dir /data/redis # Setting Primary Node Password masterauth 123456 # Set slave node read-only replica-read-only yes # Set the current node access password to requirepass 123456 # Set the maximum number of connections on the client side maxclients 1000 # Setting maximum memory maxmemory 2GB # Set up a post-maximum memory policy to report errors maxmemory-policy noeviction # Start AOF persistence mode appendonly yes # Setting AOF File Name appendfilename "appendonly.aof.6379" # Set AOF write disk policy, write once per second appendfsync everysec
1.2 Create sentinel configuration template
- Copy a sentinel.conf configuration file from the source directory, or create a new one, because sentinel configuration items are relatively small. Name it template-sentinel.conf
- When copying the template, only the port number in the configuration file needs to be replaced, that is, only the global replacement of 6379 can replace the port number, log file, pid file and so on in the configuration file.
# Setting up ip bind 127.0.0.1 # Binding port number port 26379 # Setting up background operation daemonize yes # Specify pid file pidfile /logs/redis/sentinel.pid.26379 # Specify log files logfile /logs/redis/sentinel.log.26379 # Specify startup directory dir /tmp # Monitor sentinel monitor redis.localhost 127.0.0.1 6480 2 # redis password sentinel auth-pass redis.localhost 123456 # Specify the number of milliseconds that sentinel considers redis downtime to be 10 seconds sentinel down-after-milliseconds redis.localhost 10000 # When specifying a failover, how many slave nodes synchronize data to the master node at most sentinel parallel-syncs redis.localhost 1 # Specifies how many milliseconds the total failover time is, by default sentinel failover-timeout redis.localhost 180000 # When failover occurs, script configuration cannot be modified sentinel deny-scripts-reconfig yes
2. Create configuration files
1.1 Create directories
Before creating configuration, relevant directories should be created. The author's directory plan is as follows:
- / usr/local/etc/redis: Store related configuration files
- / logs/redis: Store redis logs and pid files
- / Data/redis: RDB file in redis database, AOF file storage directory
mkdir -p /usr/local/etc/redis /logs/redis /data/redis
2.1 Copy three redis configurations
Create three redis configuration files using redis configuration template
# Copy three new configuration files cp template-redis.conf /usr/local/etc/redis/redis.conf.6480 cp template-redis.conf /usr/local/etc/redis/redis.conf.6481 cp template-redis.conf /usr/local/etc/redis/redis.conf.6482
2.2 Replacement of related configurations
Use the sed command to replace globally all items (logs, filenames, etc.) that need to be distinguished in the configuration file
# Replace configuration files sed -i "s/6379/6480/g" /usr/local/etc/redis/redis.conf.6480 sed -i "s/6379/6481/g" /usr/local/etc/redis/redis.conf.6481 sed -i "s/6379/6482/g" /usr/local/etc/redis/redis.conf.6482
2.3 Replicate three sentinel configurations
Create three sentinel profiles using redis configuration template
cp template-sentinel.conf /usr/local/etc/redis/sentinel.conf.26480 cp template-sentinel.conf /usr/local/etc/redis/sentinel.conf.26481 cp template-sentinel.conf /usr/local/etc/redis/sentinel.conf.26482
2.4 Replacement of related configurations
Use the sed command to replace globally all items (logs, filenames, etc.) that need to be distinguished in the configuration file
# Replace configuration files sed -i "s/6379/6480/g" /usr/local/etc/redis/sentinel.conf.26480 sed -i "s/6379/6481/g" /usr/local/etc/redis/sentinel.conf.26481 sed -i "s/6379/6482/g" /usr/local/etc/redis/sentinel.conf.26482
3. Start the cluster
3.1 Start the redis cluster
The redis server is started successively by redis-server command, and the master-slave relationship is not established after starting.
redis-server /usr/local/etc/redis/redis.conf.6480 redis-server /usr/local/etc/redis/redis.conf.6481 redis-server /usr/local/etc/redis/redis.conf.6482
3.2 Setting up Master-Slave Relations
By commanding the establishment of a master-slave relationship, I think this approach is better than specifying in the configuration file.
redis-cli -p 6481 -a 123456 slaveof 127.0.0.1 6480 redis-cli -p 6482 -a 123456 slaveof 127.0.0.1 6480
3.3 Start sentinel cluster
Using redis-sentinel to start sentinel clusters in turn, sentinel automatically builds relationships.
redis-sentinel /usr/local/etc/redis/sentinel.conf.26480 redis-sentinel /usr/local/etc/redis/sentinel.conf.26481 redis-sentinel /usr/local/etc/redis/sentinel.conf.26482
3.4 View Clusters
- You can log on to sentinel client to view relevant messages, or you can view sentinel cluster relationships directly from the command line. From the output results, we can see that:
- Redis cluster name: redis.localhost
- Main node status: ok
- Main node address: 127.0.0.1:6480
- Number of slave nodes: 2
- Number of sentinel s: 3
$ redis-cli -p 26481 info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=redis.localhost,status=ok,address=127.0.0.1:6480,slaves=2,sentinels=3
4. Test failover
4.1 Close maste Server
$ redis-cli -a 123456 -p 6480 shutdown
4.2 View cluster status through sentinel
$ redis-cli -p 26481 info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=redis.localhost,status=ok,address=127.0.0.1:6482,slaves=2,sentinels=3
4.3 View sentinel logs
5648:X 09 May 2019 10:52:10.288 # +sdown master redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:10.340 # +odown master redis.localhost 127.0.0.1 6480 #quorum 3/2 5648:X 09 May 2019 10:52:10.340 # +new-epoch 1 5648:X 09 May 2019 10:52:10.340 # +try-failover master redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:10.378 # +vote-for-leader e5acadd929ffd1feefdde0602d3a72172908fd90 1 5648:X 09 May 2019 10:52:10.445 # 156e8a353eea2e78bf16230e144086956e5c7c8e voted for e5acadd929ffd1feefdde0602d3a72172908fd90 1 5648:X 09 May 2019 10:52:10.445 # 2279bbefc80a9f66259f5d148a73638d92448653 voted for e5acadd929ffd1feefdde0602d3a72172908fd90 1 5648:X 09 May 2019 10:52:10.507 # +elected-leader master redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:10.507 # +failover-state-select-slave master redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:10.608 # +selected-slave slave 127.0.0.1:6482 127.0.0.1 6482 @ redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:10.608 * +failover-state-send-slaveof-noone slave 127.0.0.1:6482 127.0.0.1 6482 @ redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:10.699 * +failover-state-wait-promotion slave 127.0.0.1:6482 127.0.0.1 6482 @ redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:11.496 # +promoted-slave slave 127.0.0.1:6482 127.0.0.1 6482 @ redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:11.496 # +failover-state-reconf-slaves master redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:11.505 * +slave-reconf-sent slave 127.0.0.1:6481 127.0.0.1 6481 @ redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:12.549 # -odown master redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:12.549 * +slave-reconf-inprog slave 127.0.0.1:6481 127.0.0.1 6481 @ redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:12.549 * +slave-reconf-done slave 127.0.0.1:6481 127.0.0.1 6481 @ redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:12.615 # +failover-end master redis.localhost 127.0.0.1 6480 5648:X 09 May 2019 10:52:12.615 # +switch-master redis.localhost 127.0.0.1 6480 127.0.0.1 6482 5648:X 09 May 2019 10:52:12.616 * +slave slave 127.0.0.1:6481 127.0.0.1 6481 @ redis.localhost 127.0.0.1 6482 5648:X 09 May 2019 10:52:12.616 * +slave slave 127.0.0.1:6480 127.0.0.1 6480 @ redis.localhost 127.0.0.1 6482 5648:X 09 May 2019 10:52:22.666 # +sdown slave 127.0.0.1:6480 127.0.0.1 6480 @ redis.localhost 127.0.0.1 6482