三台主机系统:
[root@master ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
redis版本:
redis-4.0.14
部署redis 1主2从:
主库master的redis.conf配置文件如下:
[root@host-172-17-80-204 bin]# cat ../etc/redis.conf | grep -v '^$' | grep -v '^#'
bind 172.17.80.204 127.0.0.1
protected-mode yes
port 6379
daemonize yes
logfile "/usr/local/redis/redis.log"
dir /usr/local/redis
slave-serve-stale-data yes
slave-read-only no
#masterauth "redhat6.3" #主库不需要配置
requirepass "redhat6.3"
appendonly yes
appendfilename "appendonly.aof"
从库node1的redis.conf配置文件如下:
[root@host-172-17-80-60 redis]# cat etc/redis.conf | grep -v '^$' | grep -v '^#'
bind 172.17.80.60 127.0.0.1
protected-mode yes
port 6379
daemonize yes
logfile "/usr/local/redis/redis.log"
dir /usr/local/redis
slaveof 172.17.80.204 6379
slave-serve-stale-data yes
slave-read-only no
masterauth "redhat6.3"
requirepass "redhat6.3" #此密码可以单独配置
appendonly yes
appendfilename "appendonly.aof"
从库node2的redis.conf配置文件如下:
[root@host-172-17-80-109 redis]# cat etc/redis.conf | grep -v '^$' | grep -v '^#'
bind 172.17.80.109 127.0.0.1
protected-mode yes
port 6379
daemonize yes
logfile "/usr/local/redis/redis.log"
dir /usr/local/redis
slaveof 172.17.80.204 6379
slave-serve-stale-data yes
slave-read-only no
masterauth "redhat6.3"
requirepass "redhat6.3" #此密码可以单独配置
appendonly yes
appendfilename "appendonly.aof"
登录master主库,查看主从的配置信息:
部署redis 哨兵集群:
依次将三台机器解压的redis包的sentinel.conf配置文件复制到reids的etc目录下:
cp redis-4.0.14/sentinel.conf /usr/local/redis/etc/
Master主上sentinel文件的配置:
[root@master etc]# cat sentinel.conf | grep -v '^$' | grep -v '^#'
bind 172.17.80.204 127.0.0.1
port 26379
daemonize yes
dir /usr/local/redis
#主节点别名为mymaster,后面是ip和端口,2代表判断主节点失败至少需要2个sentinel节点同意
sentinel monitor master 172.17.80.204 6379 2
sentinel auth-pass master "redhat6.3"
#主节点故障30秒后启用新的主节点
sentinel down-after-milliseconds master 30000
#故障转移时最多可以有1个从节点同时对主节点进行数据同步,数字越大,用时越短,存在网络和IO开销
sentinel parallel-syncs master 1
sentinel failover-timeout master 180000
sentinel deny-scripts-reconfig yes
node1上的sentinel文件的配置:
[root@node01 etc]# cat sentinel.conf | grep -v '^$' | grep -v '^#'
bind 172.17.80.60 127.0.0.1
port 26380
daemonize yes
dir /usr/local/redis
sentinel monitor master 172.17.80.204 6379 2
sentinel auth-pass master "redhat6.3"
sentinel down-after-milliseconds master 30000
sentinel parallel-syncs master 1
sentinel failover-timeout master 180000
sentinel deny-scripts-reconfig yes
node2上的sentinel文件的配置:
[root@host-172-17-80-109 etc]# cat sentinel.conf | grep -v '^#' | grep -v '^$'
bind 172.17.80.109 127.0.0.1
port 26381
daemonize yes
dir /usr/local/redis
sentinel monitor master 172.17.80.109 6379 2
sentinel auth-pass master "redhat6.3"
sentinel down-after-milliseconds master 30000
sentinel parallel-syncs master 1
sentinel failover-timeout master 180000
sentinel deny-scripts-reconfig yes
登录master的哨兵查看sentinel配置信息:
[root@master etc]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=master,status=ok,address=172.17.80.204:6379,slaves=2,sentinels=3
127.0.0.1:26379>
登录node1的哨兵查看sentinel配置信息:
[root@node01 ~]# redis-cli -p 26380
127.0.0.1:26380> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=master,status=ok,address=172.17.80.204:6379,slaves=2,sentinels=3
登录node2的哨兵查看sentinel配置信息:
[root@host-172-17-80-109 ~]# redis-cli -p 26381
127.0.0.1:26381> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=master,status=ok,address=172.17.80.204:6379,slaves=2,sentinels=3
哨兵模式启动成功后,master的sentinel.conf配置文件发生变化:
哨兵模式启动成功后,node1的sentinel.conf配置文件发生变化:
哨兵模式启动成功后,node2的sentinel.conf配置文件发生变化:
测试哨兵模式:kill redis master节点,其他从节点变化:
主节点执行kill redis进程:
登录node1节点查看sentinel哨兵选举变化:
[root@node01 redis]# redis-cli -p 26380
127.0.0.1:26380> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=master,status=ok,address=172.17.80.109:6379,slaves=2,sentinels=3
登录node1 redis节点,查看redis主从配置信息:
[root@node01 redis]# redis-cli -a redhat6.3
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6379> info replication