配置redis主从复制和sentinel模式
测试环境:
专注于为中小企业提供网站设计制作、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业岱岳免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
机器:192.168.110.132
redis主端口:6379
redis从端口:6380
redis从端口:6381
sentinel端口:26379
操作系统版本:CentOS release 6.5 (Final)
redis版本:3.2.6
Linux系统安装redis:
1、下载redis:
登陆redis官网https://redis.io/download,下载最新稳定版源码包redis-3.2.6.tar.gz。解压后进行编译即可。
tar -zxvf redis-3.2.6.tar.gz
cd redis-3.2.6/
make
2、编译配置文件redis.conf
redis.conf配置文件:
bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "/tmp/redis6379.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump6379.rdb dir /data/redis/ 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 appendonly yes appendfilename "appendonly6379.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-size -2 list-compress-depth 0 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
redis6380.cnf配置文件:
bind 0.0.0.0 protected-mode yes port 6380 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/var/run/redis_6380.pid" loglevel notice logfile "/tmp/redis6380.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump6380.rdb" dir "/data/redis" 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 10 appendonly yes appendfilename "appendonly6380.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-size -2 list-compress-depth 0 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
redis6381.conf配置文件:
bind 0.0.0.0 protected-mode yes port 6381 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/var/run/redis_6381.pid" loglevel notice logfile "/tmp/redis6381.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump6381.rdb" dir "/data/redis" slaveof 192.168.110.132 6380 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 appendonly yes appendfilename "appendonly6381.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-size -2 list-compress-depth 0 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
sentinel配置文件:
bind 0.0.0.0 protected-mode yes port 26379 dir /tmp sentinel monitor mymaster 192.168.110.132 6379 1 sentinel down-after-milliseconds mymaster 10000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 18000
3、启动redis。
启动redis十分简单,直接redis-server /path/redis.conf即可。
redis-server /usr/local/redis/redis.conf redis-server /usr/local/redis/redis6380.conf redis-server /usr/local/redis/redis6381.conf
4、检查redis主从状态:
6379端口:
127.0.0.1:6379> set title helloOK 127.0.0.1:6379> get title"hello" 127.0.0.1:6379> info Replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.110.132,port=6380,state=online,offset=1,lag=0master_repl_offset:1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:0
6380端口:
127.0.0.1:6380> get title"hello" 127.0.0.1:6380> info Replication# Replicationrole:slavemaster_host:192.168.110.132master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:2115slave_priority:10slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
6381端口:
127.0.0.1:6381> get title"hello" 127.0.0.1:6381> info Replication# Replicationrole:slavemaster_host:192.168.110.132master_port:6379master_link_status:upmaster_last_io_seconds_ago:2master_sync_in_progress:0slave_repl_offset:5970slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
5、启动sentinel:
redis-server /usr/local/redis/sentinel.conf --sentinel
6、杀掉redis的6379端口进程,观察failover
46030:X 13 Apr 10:15:46.085 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.6 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 46030 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 46030:X 13 Apr 10:15:46.086 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 46030:X 13 Apr 10:15:46.097 # Sentinel ID is 89f3e9eeacc7b99b9c95d7c20d25a241d6c1ccd9 46030:X 13 Apr 10:15:46.097 # +monitor master mymaster 192.168.110.132 6379 quorum 1 46030:X 13 Apr 10:15:46.098 * +slave slave 192.168.110.132:6380 192.168.110.132 6380 @ mymaster 192.168.110.132 6379 46030:X 13 Apr 10:16:36.359 * +slave slave 192.168.110.132:6381 192.168.110.132 6381 @ mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:37.344 # +sdown master mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:37.344 # +odown master mymaster 192.168.110.132 6379 #quorum 1/1 46030:X 13 Apr 10:19:37.344 # +new-epoch 1 46030:X 13 Apr 10:19:37.344 # +try-failover master mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:37.348 # +vote-for-leader 89f3e9eeacc7b99b9c95d7c20d25a241d6c1ccd9 1 46030:X 13 Apr 10:19:37.348 # +elected-leader master mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:37.348 # +failover-state-select-slave master mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:37.415 # +selected-slave slave 192.168.110.132:6380 192.168.110.132 6380 @ mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:37.415 * +failover-state-send-slaveof-noone slave 192.168.110.132:6380 192.168.110.132 6380 @ mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:37.477 * +failover-state-wait-promotion slave 192.168.110.132:6380 192.168.110.132 6380 @ mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:37.983 # +promoted-slave slave 192.168.110.132:6380 192.168.110.132 6380 @ mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:37.983 # +failover-state-reconf-slaves master mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:38.051 * +slave-reconf-sent slave 192.168.110.132:6381 192.168.110.132 6381 @ mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:38.194 * +slave-reconf-inprog slave 192.168.110.132:6381 192.168.110.132 6381 @ mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:39.217 * +slave-reconf-done slave 192.168.110.132:6381 192.168.110.132 6381 @ mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:39.299 # +failover-end master mymaster 192.168.110.132 6379 46030:X 13 Apr 10:19:39.299 # +switch-master mymaster 192.168.110.132 6379 192.168.110.132 6380 46030:X 13 Apr 10:19:39.299 * +slave slave 192.168.110.132:6381 192.168.110.132 6381 @ mymaster 192.168.110.132 6380 46030:X 13 Apr 10:19:39.299 * +slave slave 192.168.110.132:6379 192.168.110.132 6379 @ mymaster 192.168.110.132 6380 46030:X 13 Apr 10:19:49.319 # +sdown slave 192.168.110.132:6379 192.168.110.132 6379 @ mymaster 192.168.110.132 6380
由于6380的配置文件中slave-priority配置为10,比6381的slave-priority的100要小,故优先选择6380为master。因为sentinel配置文件中down-after-milliseconds设置为10s,所以failover在10s后确认master down后自动选举6380为新的master。
7、查看日志:
当选举6380为新的master的时候,原先6381会重新指向6380,并清理自身旧的数据,从6380重新sync全量的数据过来。
6380的日志:
45995:S 13 Apr 10:19:37.342 # Error condition on socket for SYNC: Connection refused 45995:M 13 Apr 10:19:37.477 * Discarding previously cached master state. 45995:M 13 Apr 10:19:37.478 * MASTER MODE enabled (user request from 'id=4 addr=192.168.110.132:46885 fd=7 name=sentinel-89f3e9ee-cmd a ge=231 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=0 qbuf-free=32768 obl=36 oll=0 omem=0 events=r cmd=exec') 45995:M 13 Apr 10:19:37.478 # CONFIG REWRITE executed with success. 45995:M 13 Apr 10:19:38.755 * Slave 192.168.110.132:6381 asks for synchronization 45995:M 13 Apr 10:19:38.755 * Full resync requested by slave 192.168.110.132:6381 45995:M 13 Apr 10:19:38.755 * Starting BGSAVE for SYNC with target: disk 45995:M 13 Apr 10:19:38.756 * Background saving started by pid 46050 46050:C 13 Apr 10:19:38.766 * DB saved on disk 46050:C 13 Apr 10:19:38.767 * RDB: 0 MB of memory used by copy-on-write 45995:M 13 Apr 10:19:38.855 * Background saving terminated with success 45995:M 13 Apr 10:19:38.855 * Synchronization with slave 192.168.110.132:6381 succeeded
6381的日志:
46039:S 13 Apr 10:19:37.746 # Error condition on socket for SYNC: Connection refused 46039:S 13 Apr 10:19:38.051 * Discarding previously cached master state. 46039:S 13 Apr 10:19:38.051 * SLAVE OF 192.168.110.132:6380 enabled (user request from 'id=4 addr=192.168.110.132:41739 fd=7 name=senti nel-89f3e9ee-cmd age=182 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=145 qbuf-free=32623 obl=36 oll=0 omem=0 events=r cmd=exec') 46039:S 13 Apr 10:19:38.052 # CONFIG REWRITE executed with success. 46039:S 13 Apr 10:19:38.755 * Connecting to MASTER 192.168.110.132:6380 46039:S 13 Apr 10:19:38.755 * MASTER <-> SLAVE sync started 46039:S 13 Apr 10:19:38.755 * Non blocking connect for SYNC fired the event. 46039:S 13 Apr 10:19:38.755 * Master replied to PING, replication can continue... 46039:S 13 Apr 10:19:38.755 * Partial resynchronization not possible (no cached master) 46039:S 13 Apr 10:19:38.756 * Full resync from master: 065cade6f55c04c839436d5d0d2c6abc56a5d862:1 46039:S 13 Apr 10:19:38.855 * MASTER <-> SLAVE sync: receiving 102 bytes from master 46039:S 13 Apr 10:19:38.855 * MASTER <-> SLAVE sync: Flushing old data 46039:S 13 Apr 10:19:38.855 * MASTER <-> SLAVE sync: Loading DB in memory 46039:S 13 Apr 10:19:38.855 * MASTER <-> SLAVE sync: Finished with success 46039:S 13 Apr 10:19:38.856 * Background append only file rewriting started by pid 46051 46039:S 13 Apr 10:19:38.897 * AOF rewrite child asks to stop sending diffs. 46051:C 13 Apr 10:19:38.897 * Parent agreed to stop sending diffs. Finalizing AOF... 46051:C 13 Apr 10:19:38.897 * Concatenating 0.00 MB of AOF diff received from parent. 46051:C 13 Apr 10:19:38.897 * SYNC append only file rewrite performed 46051:C 13 Apr 10:19:38.897 * AOF rewrite: 0 MB of memory used by copy-on-write 46039:S 13 Apr 10:19:38.956 * Background AOF rewrite terminated with success 46039:S 13 Apr 10:19:38.956 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB) 46039:S 13 Apr 10:19:38.956 * Background AOF rewrite finished successfully 46039:S 13 Apr 10:31:31.097 * 1 changes in 900 seconds. Saving... 46039:S 13 Apr 10:31:31.097 * Background saving started by pid 46071 46071:C 13 Apr 10:31:31.109 * DB saved on disk 46071:C 13 Apr 10:31:31.109 * RDB: 0 MB of memory used by copy-on-write 46039:S 13 Apr 10:31:31.198 * Background saving terminated with success
网站栏目:配置redis主从复制和sentinel模式
当前网址:http://myzitong.com/article/gcdcse.html