集群配置最少需要三台机器,那么我就三台虚拟机,三台虚拟机分别安装同样的redis的环境
ip分别:
192.168.1.78 (redis sentinel集群监控)
192.168.1.62 (redis主)
192.168.1.68 (redis从)
redis配置:
主配置文件:修改bind
bind 127.0.0.1 192.168.1.62daemonize yes其余的默认即可
从的配置文件:
bind 0.0.0.0daemonize yesslave-read-only yesslaveof 192.168.1.62 6379
注意:redis配置主从的不同最主要的就是,多了一条slaveof xxxxxxxx
启动主和从redis
[root@cache01 ~]# netstat -luntp|grep redis-servertcp 0 0 192.168.1.62:6379 0.0.0.0:* LISTEN 1980/redis-server 1 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1980/redis-server 1
[root@cache02 ~]# netstat -lnutp|grep redis-servertcp 0 0 192.168.1.68:6379 0.0.0.0:* LISTEN 1958/redis-server 1 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1958/redis-server 1
查看从redis信息
[root@cache03 conf]# redis-cli -h 192.168.1.68 info replication# Replicationrole:slavemaster_host:192.168.1.62master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:394966slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
查看主redis信息
[root@cache03 conf]# redis-cli -h 192.168.1.62info replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.1.68,port=6379,state=online,offset=400791,lag=0master_repl_offset:400791repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:400790
配置redis sentinel集群监控服务 1.添加一份redis sentinel 配置文件
#mymasterport 26379sentinel monitor mymaster 192.168.1.62 6379 1sentinel down-after-milliseconds mymaster 5000sentinel failover-timeout mymaster 900000sentinel parallel-syncs mymaster 1#mymaster02(可以添加多组主从的redis监听)
现在可以进行验证了:
[root@cache03 ~]# redis-cli -h 192.168.1.62 shutdown
[root@cache03 ~]# redis-cli -h 192.168.1.68 info replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.1.62,port=6379,state=online,offset=19598,lag=0master_repl_offset:19598repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:19597
[root@cache01 ~]# service redis startStarting Redis server...[root@cache03 ~]# redis-cli -h 192.168.1.62 info replication# Replicationrole:slavemaster_host:192.168.1.68master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:23421slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
[root@cache03 ~]# redis-cli -h 192.168.1.68 shutdown
[root@cache03 bin]# redis-sentinel /app/server/redis/conf/sentinel.conf &8785:X 07 Jul 01:06:25.531 # +sdown master mymaster 192.168.1.68 63798785:X 07 Jul 01:06:25.531 # +odown master mymaster 192.168.1.68 6379 #quorum 1/18785:X 07 Jul 01:06:25.532 # +new-epoch 68785:X 07 Jul 01:06:25.532 # +try-failover master mymaster 192.168.1.68 63798785:X 07 Jul 01:06:25.642 # +vote-for-leader 0efacb4ca1b82a290a86a15cf1229f23896c2fe5 68785:X 07 Jul 01:06:25.642 # +elected-leader master mymaster 192.168.1.68 63798785:X 07 Jul 01:06:25.642 # +failover-state-select-slave master mymaster 192.168.1.68 63798785:X 07 Jul 01:06:25.705 # +selected-slave slave 192.168.1.62:6379 192.168.1.62 6379 @ mymaster 192.168.1.68 63798785:X 07 Jul 01:06:25.705 * +failover-state-send-slaveof-noone slave 192.168.1.62:6379 192.168.1.62 6379 @ mymaster 192.168.1.68 63798785:X 07 Jul 01:06:25.771 * +failover-state-wait-promotion slave 192.168.1.62:6379 192.168.1.62 6379 @ mymaster 192.168.1.68 63798785:X 07 Jul 01:06:26.759 # +promoted-slave slave 192.168.1.62:6379 192.168.1.62 6379 @ mymaster 192.168.1.68 63798785:X 07 Jul 01:06:26.759 # +failover-state-reconf-slaves master mymaster 192.168.1.68 63798785:X 07 Jul 01:06:26.760 # +failover-end master mymaster 192.168.1.68 63798785:X 07 Jul 01:06:26.760 # +switch-master mymaster 192.168.1.68 6379 192.168.1.62 63798785:X 07 Jul 01:06:26.760 * +slave slave 192.168.1.68:6379 192.168.1.68 6379 @ mymaster 192.168.1.62 63798785:X 07 Jul 01:06:31.837 # +sdown slave 192.168.1.68:6379 192.168.1.68 6379 @ mymaster 192.168.1.62 63798788:X 07 Jul 01:06:47.530 * Increased maximum number of open files to 10032 (it was originally set to 1024).8788:X 07 Jul 01:06:47.531 # Creating Server TCP listening socket *:26379: bind: Address already in use
[root@cache03 bin]# redis-cli -h 192.168.1.62 -p 6379 info replication# Replicationrole:masterconnected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
[root@cache02 ~]# service redis startStarting Redis server...
[root@cache03 bin]# redis-cli -h 192.168.1.68 -p 6379 info replication# Replicationrole:slavemaster_host:192.168.1.62master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:2999slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
[root@cache01 ~]# service redis stopStopping ...Waiting for Redis to shutdown ...Redis stopped[root@cache03 bin]# 8785:X 07 Jul 01:12:45.927 # +sdown master mymaster 192.168.1.62 63798785:X 07 Jul 01:12:45.927 # +odown master mymaster 192.168.1.62 6379 #quorum 1/18785:X 07 Jul 01:12:45.927 # +new-epoch 78785:X 07 Jul 01:12:45.927 # +try-failover master mymaster 192.168.1.62 63798785:X 07 Jul 01:12:45.968 # +vote-for-leader 0efacb4ca1b82a290a86a15cf1229f23896c2fe5 78785:X 07 Jul 01:12:45.968 # +elected-leader master mymaster 192.168.1.62 63798785:X 07 Jul 01:12:45.968 # +failover-state-select-slave master mymaster 192.168.1.62 63798785:X 07 Jul 01:12:46.068 # +selected-slave slave 192.168.1.68:6379 192.168.1.68 6379 @ mymaster 192.168.1.62 63798785:X 07 Jul 01:12:46.068 * +failover-state-send-slaveof-noone slave 192.168.1.68:6379 192.168.1.68 6379 @ mymaster 192.168.1.62 63798785:X 07 Jul 01:12:46.140 * +failover-state-wait-promotion slave 192.168.1.68:6379 192.168.1.68 6379 @ mymaster 192.168.1.62 63798785:X 07 Jul 01:12:47.082 # +promoted-slave slave 192.168.1.68:6379 192.168.1.68 6379 @ mymaster 192.168.1.62 63798785:X 07 Jul 01:12:47.082 # +failover-state-reconf-slaves master mymaster 192.168.1.62 63798785:X 07 Jul 01:12:47.082 # +failover-end master mymaster 192.168.1.62 63798785:X 07 Jul 01:12:47.082 # +switch-master mymaster 192.168.1.62 6379 192.168.1.68 63798785:X 07 Jul 01:12:47.086 * +slave slave 192.168.1.62:6379 192.168.1.62 6379 @ mymaster 192.168.1.68 63798785:X 07 Jul 01:12:52.102 # +sdown slave 192.168.1.62:6379 192.168.1.62 6379 @ mymaster 192.168.1.68 6379
[root@cache03 bin]# redis-cli -h 192.168.1.68 -p 6379 info replication# Replicationrole:masterconnected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
[root@cache01 ~]# service redis startStarting Redis server...
[root@cache03 bin]# 8785:X 07 Jul 01:13:53.100 # -sdown slave 192.168.1.62:6379 192.168.1.62 6379 @ mymaster 192.168.1.68 63798785:X 07 Jul 01:14:03.127 * +convert-to-slave slave 192.168.1.62:6379 192.168.1.62 6379 @ mymaster 192.168.1.68 6379
[root@cache03 bin]# redis-cli -h 192.168.1.62 -p 6379 info replication# Replicationrole:slavemaster_host:192.168.1.68master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:1859slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
==============================================================================================
[root@cache01 conf]# egrep -v "(^#|^$)" 6379.conf bind 0.0.0.0protected-mode yesport 6379tcp-backlog 511timeout 300tcp-keepalive 0daemonize yessupervised nopidfile "/var/run/redis_6379.pid"loglevel noticelogfile "/app/server/redis/logs/6379.log"databases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename "dump.rdb"dir "/"slave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100maxmemory-policy volatile-lruappendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes
[root@cache02 ~]# egrep -v "(^#|^$)" /app/server/redis/conf/6379.conf bind 127.0.0.1 192.168.1.68protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize yessupervised nopidfile "/var/run/redis_6379.pid"loglevel noticelogfile "/app/server/redis/logs/6379.log"databases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename "dump.rdb"dir "/"slave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100appendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yesslaveof 192.168.1.62 6379#注意主重切换时最关建的就是系统会redis配置文件中加上前面的一行(有这一行的就是从服务器)一定是主先宕机,才会发生主从切换,并会在哨兵端有输出