192.168.1.111 主
192.168.1.112 从
192.168.1.113 从
yum install gcc gcc-c++ -y
cd /usr/local
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
3台均按以下命令安装redis
cd /usr/local
tar -xzvf redis.tar.gz
cd redis
make MALLOC=libc
make
make install
修改master 192.168.1.111的redis.conf
daemonize yes pidfile "/var/run/redis.pid" port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "/usr/local/redis/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" 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 "密码" Appendonly yes 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 masterauth "密码" protected-mode yes
修改slave 192.168.1.112和192.168.1.113的/usr/local/redis/redis.conf
daemonize yes pidfile "/var/run/redis.pid" port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "/usr/local/redis/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" 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 "密码" appendonly yes 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 masterauth "密码" protected-mode yes slaveof 192.168.1.111 6379
率先启动master redis 然后依次启动slave
/usr/local/redis/src/redis-server /usr/local/redis/redis.conf
可以在/usr/local/redis/redis.log 中 看到3台机子连接成功的信息
也可以在redis中 添加key 测试是否会同步
在192.168.1.111上执行如下命令
在slave机子上执行如下操作,会看到slave 上有和master 一样的key 名,value也一致
protected-mode no sentinel deny-scripts-reconfig yes sentinel monitor mymaster 192.168.1.111 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 15000 sentinel auth-pass mymaster 密码 # Generated by CONFIG REWRITE port 26379 dir "/usr/local/redis" sentinel config-epoch mymaster 1 sentinel leader-epoch mymaster 1 sentinel known-slave mymaster 192.168.1.112 6379 sentinel known-slave mymaster 192.168.1.113 6379 sentinel current-epoch 1 sentinel announce-ip "192.168.1.111"
protected-mode no sentinel myid 3750d9547cf4e99c142984032492d43908ed8790 sentinel deny-scripts-reconfig yes sentinel monitor mymaster 192.168.1.111 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 15000 sentinel auth-pass mymaster 密码 # Generated by CONFIG REWRITE port 26379 dir "/usr/local/redis" sentinel config-epoch mymaster 1 sentinel leader-epoch mymaster 1 sentinel known-slave mymaster 192.168.1.112 6379 sentinel known-slave mymaster 192.168.1.113 6379 sentinel current-epoch 1 sentinel announce-ip "192.168.1.111"
首先启动master 然后slave
/usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel.conf &
启动完毕后可以用如下命令查看哨兵信息
**redis-cli -p 26379 INFO Sentinel **
查看日志 master 重新选举192.168.1.112为新master
再次查看哨兵信息 master 已经变化
重新启动192.168.1.111的redis 查看信息