3台虚拟机
192.168.1.52
192.168.1.53
192.168.1.54
上来做这个操作:
1.机器上编译安装(3.0.7版本)redis->wget http://download.redis.io/releases/redis-3.0.7.tar.gz
2.配置:
主机:192.168.1.52 从机:192.168.1.53 192.168.1.54
配置文件(redis.conf):
192.168.1.52:
daemonize yes pidfile "/var/run/redis.pid" port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "/home/log/redis.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/usr/local/redis-3.0.7" 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 no appendfilename "appendonly.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
从机(redis.conf):192.168.1.53 192.168.1.54
[[email protected] redis-3.0.7]# cat redis.conf | egrep -v "^$|^#" daemonize yes pidfile "/var/run/redis.pid" port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "/home/log/redis.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/usr/local/redis-3.0.7" slaveof 192.168.1.52 6379 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 no appendfilename "appendonly.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
3.将redis_server跑起来
查看信息:
redis-cli -h 192.168.1.52 -p 6379 info replication
# Replication role:master connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
redis-cli -h 192.168.1.53 -p 6379 info replication
# Replication role:slave master_host:192.168.1.52 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:15 slave_priority:100 slave_read_only:1 connected_slaves:1 slave0:ip=192.168.1.54,port=6379,state=online,offset=1,lag=0 master_repl_offset:1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:0
redis-cli -h 192.168.1.54 -p 6379 info replication
# Replication role:slave master_host:192.168.1.52 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:169 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
此时再来看192.168.1.52主从信息
syswjdeMacBook-Air:shell syswj$ ./watch_redis.sh 52 info 52 # Replication role:master connected_slaves:2 slave0:ip=192.168.1.53,port=6379,state=online,offset=211,lag=1 slave1:ip=192.168.1.54,port=6379,state=online,offset=211,lag=0 master_repl_offset:211 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:210
3.开启redis-sentinel进行主从监控切换管理(只开启一个实例)
配置
port 26379 dir "/tmp" sentinel monitor mymaster 192.168.1.52 6379 2 daemonize yes sentinel down-after-milliseconds mymaster 10000 sentinel config-epoch mymaster 10 sentinel leader-epoch mymaster 10 logfile "/home/log/sen.log" sentinel known-sentinel mymaster 127.0.0.1 26379 efbd30332f50847e0b0e9c5ab0adc5dfb91823d2 sentinel current-epoch 10
运行后:
3595:X 15 Apr 11:57:29.146 # Sentinel runid is fdc1c1d8a5adf48a5fecc229be42810a61eb8603 3595:X 15 Apr 11:57:29.146 # +monitor master mymaster 192.168.1.52 6379 quorum 2 3595:X 15 Apr 11:57:30.146 * +slave slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379 3595:X 15 Apr 11:57:30.156 * +slave slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379 3595:X 15 Apr 11:57:30.871 * -dup-sentinel master mymaster 192.168.1.52 6379 #duplicate of 127.0.0.1:26379 or fdc1c1d8a5adf48a5fecc229be42810a61eb8603 3595:X 15 Apr 11:57:30.871 * +sentinel sentinel 127.0.0.1:26379 127.0.0.1 26379 @ mymaster 192.168.1.52 6379
已经监控到两个slaves
4.测试主从切换
关闭掉主redis: redis-cli -h 192.168.1.52 -p 6379 shutdown
发现54被切换成主redis了,53依然是从redis,不过从属于54了
syswjdeMacBook-Air:shell syswj$ ./watch_redis.sh 54 info 54 # Replication role:master connected_slaves:1 slave0:ip=192.168.1.53,port=6379,state=online,offset=179,lag=1 master_repl_offset:179 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:178 syswjdeMacBook-Air:shell syswj$ ./watch_redis.sh 53 info 53 # Replication role:slave master_host:192.168.1.54 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:320 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 syswjdeMacBook-Air:shell syswj$
查看主从管理日志:
3670:X 15 Apr 12:06:14.376 # +sdown master mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:14.435 # +odown master mymaster 192.168.1.52 6379 #quorum 2/2 3670:X 15 Apr 12:06:14.435 # +new-epoch 12 3670:X 15 Apr 12:06:14.435 # +try-failover master mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:14.444 # +vote-for-leader 96e852d3d56a2dc45b2505e22bd430d1d6c7de13 12 3670:X 15 Apr 12:06:14.447 # 127.0.0.1:26379 voted for 96e852d3d56a2dc45b2505e22bd430d1d6c7de13 12 3670:X 15 Apr 12:06:14.548 # +elected-leader master mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:14.549 # +failover-state-select-slave master mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:14.610 # +selected-slave slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:14.610 * +failover-state-send-slaveof-noone slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:14.678 * +failover-state-wait-promotion slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:15.491 # +promoted-slave slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:15.491 # +failover-state-reconf-slaves master mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:15.548 * +slave-reconf-sent slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:16.550 * +slave-reconf-inprog slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:16.550 * +slave-reconf-done slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:16.648 # +failover-end master mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:16.648 # +switch-master mymaster 192.168.1.52 6379 192.168.1.54 6379 3670:X 15 Apr 12:06:16.651 * +slave slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.54 6379 3670:X 15 Apr 12:06:16.652 * +slave slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379 3670:X 15 Apr 12:06:26.683 # +sdown slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379
来测试重新开启机器52:
稍等一段世界发现:
52也成为54得slave了
syswjdeMacBook-Air:shell syswj$ ./watch_redis.sh 52 info 52 # Replication role:slave master_host:192.168.1.54 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:8252 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
哨兵日志如下:
3670:X 15 Apr 12:06:16.550 * +slave-reconf-done slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:16.648 # +failover-end master mymaster 192.168.1.52 6379 3670:X 15 Apr 12:06:16.648 # +switch-master mymaster 192.168.1.52 6379 192.168.1.54 6379 3670:X 15 Apr 12:06:16.651 * +slave slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.54 6379 3670:X 15 Apr 12:06:16.652 * +slave slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379 3670:X 15 Apr 12:06:26.683 # +sdown slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379 3670:X 15 Apr 12:07:54.741 # -sdown slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379 3670:X 15 Apr 12:08:04.734 * +convert-to-slave slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379
5自带shell脚本方便操作
#!/bin/bash set_redis() { result=`redis-cli -h 192.168.1.$1 -p 6379 set $2 $3` echo "192.168.1.$1:$result" } get_redis() { result=`redis-cli -h 192.168.1.$1 -p 6379 get $2` echo "192.168.1.$1:$result" } get_info() { echo $1 redis-cli -h 192.168.1.$1 -p 6379 info replication } shut_down() { echo $1 redis-cli -h 192.168.1.$1 -p 6379 shutdown } if [ $# -lt 2 ]; then echo "ip op a b" exit 0 fi case $2 in set) set_redis $1 $3 $4 ;; get) get_redis $1 $3 ;; info) get_info $1 ;; shut) shut_down $1 ;; *) echo "invalid parameter!..." ;; esac
时间: 2024-10-25 13:47:34