master: 192.168.43.11
slave: 192.168.43.12
slave: 192.168.43.13
slave: 192.168.43.14
哨兵1:(sentinel1) :192.168.43.11
哨兵2:(sentinel2) :192.168.43.12
哨兵3:(sentinel3) :192.168.43.13
哨兵4:(sentinel4) :192.168.43.14
##环境安装
yum install gcc gcc-c++ tcl* -y
cd /usr/local/src
wget https://raw.githubusercontent.com/ww1230258u/server/master/redis-4.0.6.tar.gz
tar -zxvf redis-4.0.6.tar.gz
cd redis-4.0.6
make && make install PREFIX=/data/server/redis
# 复制解压缩的redis中的redis.conf和sentinel.conf 文件到/data/server/redis/bin
cp /usr/local/src/redis-4.0.6/redis.conf sentinel.conf /data/server/redis/bin/
##主从配置
master:
--> vim redis.conf
bind 0.0.0.0
protected-mode no
daemonize yes
slave-read-only yes
slave-priority 100
appendonly yes
appendfilename "appendonly_7001.aof"
appendfsync everysec
slave:(1~3)
--> vim redis.conf
bind 0.0.0.0
protected-mode no
daemonize yes
slave-read-only yes
slave-priority 90
appendonly yes
slaveof 192.168.43.11 6379
appendfilename "appendonly_7001.aof"
appendfsync everysec
##哨兵配置
master:
--> vim sentinel.conf
daemonize yes
protected-mode no
sentinel monitor redis1 192.168.43.11 6379 4
sentinel down-after-milliseconds mymaster 20
sentinel failover-timeout mymaster 60
slave:
--> vim sentinel.conf
daemonize yes
protected-mode no
sentinel monitor redis1 192.168.43.11 6379 4
sentinel failover-timeout mymaster 60
##启动
--启动redis
./redis-server redis.conf
--启动哨兵
./redis-sentinel sentinel.conf
--关闭服务
./redis-cli shutdown (#pkill -9 redis)
##查看主从信息
./redis-cli -h 192.168.43.11 -p 6379 info Replication
[root@zs bin]# ./redis-cli -h 192.168.43.11 -p 6379 info Replication
# Replication
role:master
connected_slaves:3
slave0:ip=192.168.43.14,port=6379,state=online,offset=462712,lag=0
slave1:ip=192.168.43.12,port=6379,state=online,offset=462416,lag=0
slave2:ip=192.168.43.13,port=6379,state=online,offset=462557,lag=0
master_replid:c6bf2b47df50ae1f636784de82d4e1a43faf0ec3
master_replid2:caf44c857ade788b27a71721b6abae3bbb2e99fb
master_repl_offset:462712
second_repl_offset:384453
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:380172
repl_backlog_histlen:82541
##验证
[root@zs bin]# ./redis-cli -h 192.168.43.11 -p 6379
192.168.43.11:6379> set k1 1111
OK
192.168.43.11:6379> set k2 2222
OK
192.168.43.11:6379> exit
[root@zs bin]# ./redis-cli -h 192.168.43.12 -p 6379
192.168.43.12:6379> get k1
"1111"
192.168.43.12:6379> set k3 3333
(error) READONLY You can't write against a read only slave.