1. Installation environment
Cluster Machine Redis Service Sentinel Description
Virtual machine: 10.202.107.207 redis service port 6379 sentinel service port 26379 master node
VM: 10.202.107.208 redis service port 6379 sentinel service port 26379 slave node
VM: 10.202.107.209 redis service port 6379 sentinel service port 26379 slave node
That is: 3 redis services (1 master and 2 slaves), 3 sentinel services
2. Redis installation (please refer to Redis installation )
3. Redis master-slave configuration
3.1, create a directory
mkdir conf mkdir run mkdir logs mkdir data mkdir sentinel
Virtual machine: 10.202.107.207 redis.conf configuration file:
daemonize yes pidfile /usr/dev/redis/redis-4.0.1/run/redis.pid port 6379 tcp-backlog 511 bind 10.202.107.207 timeout 180 tcp-keepalive 60 loglevel notice logfile /usr/dev/redis/redis-4.0.1/logs/redis.log databases 16 save 900 1 save 300 10 save 60 100 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename its_redis.rdb dir /usr/dev/redis/redis-4.0.1/data slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass 123456 maxclients 1000 maxmemory 4096mb maxmemory-policy volatile-lru maxmemory-samples 3 appendonly no appendfilename its_redis.aof appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes masterauth 123456
Virtual machine: 10.202.107.208 redis.conf configuration file:
daemonize yes pidfile /usr/dev/redis/redis-4.0.1/run/redis.pid port 6379 tcp-backlog 511 bind 10.202.107.208 timeout 180 tcp-keepalive 60 loglevel notice logfile /usr/dev/redis/redis-4.0.1/logs/redis.log databases 16 save 900 1 save 300 10 save 60 100 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename its_redis.rdb dir /usr/dev/redis/redis-4.0.1/data slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass 123456 maxclients 1000 maxmemory 4096mb maxmemory-policy volatile-lru maxmemory-samples 3 appendonly no appendfilename its_redis.aof appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes masterauth 123456 slaveof 10.202.107.207 6379
Virtual machine: 10.202.107.209 redis.conf configuration file:
daemonize yes pidfile /usr/dev/redis/redis-4.0.1/run/redis.pid port 6379 tcp-backlog 511 bind 10.202.107.209 timeout 180 tcp-keepalive 60 loglevel notice logfile /usr/dev/redis/redis-4.0.1/logs/redis.log databases 16 save 900 1 save 300 10 save 60 100 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename its_redis.rdb dir /usr/dev/redis/redis-4.0.1/data slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass 123456 maxclients 1000 maxmemory 4096mb maxmemory-policy volatile-lru maxmemory-samples 3 appendonly no appendfilename its_redis.aof appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes masterauth 123456 slaveof 10.202.107.207 6379
Related commands:
1. View the redis process ps -ef | grep redis 2. Start redis server ./src/redis-server redis.conf 3. Start redis Client ./src/redis-cli -h 10.202.107.207 -p 6379 -a 123456 4. Enter the password 10.202.107.207:6379> auth 123456 5. View information info
View the master-slave relationship
./src/redis-cli -h 10.202.107.207 -p 6379 -a 123456
10.202.107.207:6379> info replication
master-slave switch
After the main service hangs up from the execution, it will switch to the main service:
./src/redis-cli -p 6380 slaveof NO ONE
After the master is restored, the slave executes:
./src/redis-cli -p 6380 slaveof 10.202.107.207 6379
4. Sentinel master-slave configuration
Virtual machine: 10.202.107.207 sentinel.conf configuration file:
# Daemon mode, ie background mode daemonize yes port 26379 bind 10.202.107.207 sentinel monitor its_sentinel 10.202.107.207 6379 2#2 means that in the sentinel cluster, as long as there are two nodes in the sentinel cluster that detect the failure of the redis master node, the switch will be performed, and the single sentinel node is invalid #Specify working directory dir "/usr/dev/redis/redis-4.0.1/sentinel" pidfile "/usr/dev/redis/redis-4.0.1/run/sentinel.pid" # log level loglevel notice logfile "/usr/dev/redis/redis-4.0.1/logs/sentinel.log" #If after 6 seconds, the master is still not alive, start failover sentinel failover-timeout its_sentinel 60000 #redis master node password sentinel auth-pass its_sentinel 123456 The #option specifies how many slave servers can synchronize the new master at the same time when performing a failover. The smaller the number, the less time it takes to complete the failover. sentinel config-epoch its_sentinel 0 sentinel leader-epoch its_sentinel 0 sentinel current-epoch 1 sentinel announce-ip "10.202.107.207"
Virtual machine: 10.202.107.208 sentinel.conf configuration file:
# Daemon mode, ie background mode daemonize yes port 26379 bind 10.202.107.208 sentinel monitor its_sentinel 10.202.107.207 6379 2#2 means that in the sentinel cluster, as long as there are two nodes in the sentinel cluster that detect the failure of the redis master node, the switch will be performed, and the single sentinel node is invalid #Specify working directory dir "/usr/dev/redis/redis-4.0.1/sentinel" pidfile "/usr/dev/redis/redis-4.0.1/run/sentinel.pid" # log level loglevel notice logfile "/usr/dev/redis/redis-4.0.1/logs/sentinel.log" #If after 6 seconds, the master is still not alive, start failover sentinel failover-timeout its_sentinel 60000 #redis master node password sentinel auth-pass its_sentinel 123456 The #option specifies how many slave servers can synchronize the new master at the same time when performing a failover. The smaller the number, the less time it takes to complete the failover. sentinel config-epoch its_sentinel 0 sentinel leader-epoch its_sentinel 0 sentinel current-epoch 1 sentinel announce-ip "10.202.107.208"
Virtual machine: 10.202.107.209 sentinel.conf configuration file:
# Daemon mode, ie background mode daemonize yes port 26379 bind 10.202.107.209 sentinel monitor its_sentinel 10.202.107.207 6379 2#2 means that in the sentinel cluster, as long as there are two nodes in the sentinel cluster that detect the failure of the redis master node, the switch will be performed, and the single sentinel node is invalid #Specify working directory dir "/usr/dev/redis/redis-4.0.1/sentinel" pidfile "/usr/dev/redis/redis-4.0.1/run/sentinel.pid" # log level loglevel notice logfile "/usr/dev/redis/redis-4.0.1/logs/sentinel.log" #If after 6 seconds, the master is still not alive, start failover sentinel failover-timeout its_sentinel 60000 #redis master node password sentinel auth-pass its_sentinel 123456 The #option specifies how many slave servers can synchronize the new master at the same time when performing a failover. The smaller the number, the less time it takes to complete the failover. sentinel config-epoch its_sentinel 0 sentinel leader-epoch its_sentinel 0 sentinel current-epoch 1 sentinel announce-ip "10.202.107.209"
Start the sentinel service: ./src/redis-sentinel ./conf/sentinel.conf