集群配置最少需要三台机器,那么我就三台虚拟机,三台虚拟机分别安装同样的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.62 daemonize yes 其余的默认即可
从的配置文件:
bind 0.0.0.0 daemonize yes slave-read-only yes slaveof 192.168.1.62 6379
注意:redis配置主从的不同最主要的就是,多了一条slaveof xxxxxxxx
启动主和从redis
[root@cache01 ~]# netstat -luntp|grep redis-server tcp 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-server tcp 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 # Replication role:slave master_host:192.168.1.62 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:394966 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
查看主redis信息
[root@cache03 conf]# redis-cli -h 192.168.1.62info replication # Replication role:master connected_slaves:1 slave0:ip=192.168.1.68,port=6379,state=online,offset=400791,lag=0 master_repl_offset:400791 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:400790
配置redis sentinel集群监控服务 1.添加一份redis sentinel 配置文件
#mymaster port 26379 sentinel monitor mymaster 192.168.1.62 6379 1 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 900000 sentinel 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 # Replication role:master connected_slaves:1 slave0:ip=192.168.1.62,port=6379,state=online,offset=19598,lag=0 master_repl_offset:19598 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:19597
[root@cache01 ~]# service redis start Starting Redis server... [root@cache03 ~]# redis-cli -h 192.168.1.62 info replication # Replication role:slave master_host:192.168.1.68 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:23421 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
[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 6379 8785:X 07 Jul 01:06:25.531 # +odown master mymaster 192.168.1.68 6379 #quorum 1/1 8785:X 07 Jul 01:06:25.532 # +new-epoch 6 8785:X 07 Jul 01:06:25.532 # +try-failover master mymaster 192.168.1.68 6379 8785:X 07 Jul 01:06:25.642 # +vote-for-leader 0efacb4ca1b82a290a86a15cf1229f23896c2fe5 6 8785:X 07 Jul 01:06:25.642 # +elected-leader master mymaster 192.168.1.68 6379 8785:X 07 Jul 01:06:25.642 # +failover-state-select-slave master mymaster 192.168.1.68 6379 8785: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 6379 8785: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 6379 8785: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 6379 8785: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 6379 8785:X 07 Jul 01:06:26.759 # +failover-state-reconf-slaves master mymaster 192.168.1.68 6379 8785:X 07 Jul 01:06:26.760 # +failover-end master mymaster 192.168.1.68 6379 8785:X 07 Jul 01:06:26.760 # +switch-master mymaster 192.168.1.68 6379 192.168.1.62 6379 8785:X 07 Jul 01:06:26.760 * +slave slave 192.168.1.68:6379 192.168.1.68 6379 @ mymaster 192.168.1.62 6379 8785:X 07 Jul 01:06:31.837 # +sdown slave 192.168.1.68:6379 192.168.1.68 6379 @ mymaster 192.168.1.62 6379 8788: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 # 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
[root@cache02 ~]# service redis start Starting Redis server...
[root@cache03 bin]# redis-cli -h 192.168.1.68 -p 6379 info replication # Replication role:slave master_host:192.168.1.62 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:2999 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
[root@cache01 ~]# service redis stop Stopping ... 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 6379 8785:X 07 Jul 01:12:45.927 # +odown master mymaster 192.168.1.62 6379 #quorum 1/1 8785:X 07 Jul 01:12:45.927 # +new-epoch 7 8785:X 07 Jul 01:12:45.927 # +try-failover master mymaster 192.168.1.62 6379 8785:X 07 Jul 01:12:45.968 # +vote-for-leader 0efacb4ca1b82a290a86a15cf1229f23896c2fe5 7 8785:X 07 Jul 01:12:45.968 # +elected-leader master mymaster 192.168.1.62 6379 8785:X 07 Jul 01:12:45.968 # +failover-state-select-slave master mymaster 192.168.1.62 6379 8785: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 6379 8785: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 6379 8785: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 6379 8785: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 6379 8785:X 07 Jul 01:12:47.082 # +failover-state-reconf-slaves master mymaster 192.168.1.62 6379 8785:X 07 Jul 01:12:47.082 # +failover-end master mymaster 192.168.1.62 6379 8785:X 07 Jul 01:12:47.082 # +switch-master mymaster 192.168.1.62 6379 192.168.1.68 6379 8785:X 07 Jul 01:12:47.086 * +slave slave 192.168.1.62:6379 192.168.1.62 6379 @ mymaster 192.168.1.68 6379 8785: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 # 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
[root@cache01 ~]# service redis start Starting 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 6379 8785: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 # Replication role:slave master_host:192.168.1.68 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:1859 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
==============================================================================================
[root@cache01 conf]# egrep -v "(^#|^$)" 6379.conf bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 300 tcp-keepalive 0 daemonize yes supervised no pidfile "/var/run/redis_6379.pid" loglevel notice logfile "/app/server/redis/logs/6379.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 "/" 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 maxmemory-policy volatile-lru 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-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
[root@cache02 ~]# egrep -v "(^#|^$)" /app/server/redis/conf/6379.conf bind 127.0.0.1 192.168.1.68 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 "/app/server/redis/logs/6379.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 "/" 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-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 slaveof 192.168.1.62 6379#注意主重切换时最关建的就是系统会redis配置文件中加上前面的一行(有这一行的就是从服务器)一定是主先宕机,才会发生主从切换,并会在哨兵端有输出