Redis的哨兵模式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fAAArA3r-1602044054882)(/home/afang/文档/笔记/第三阶段/第二周/第五天 Redis 数据库详解/assets/1190037-20180203181035312-1532457764.png)]
这里需要了解的是,主观下线和客观下线,主观下线是指单个实例确定这个哨兵下线,而客观下线指的是多个实例确定这个哨兵下线,不能处于工作状态.
准备三台服务器,这里准备的是一台master,两台slave,服务器的允许的前提下,可以多准备两台slave.
主机的名称IPredis192.168.122.166slave192.168.122.210slave01192.168.122.181安装redis-rpm源(三台装)
[root@redis-master ~]# yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm安装redis
[root@redis-master ~]# yum -y --enablerepo=remi install redis设置开机自启
[root@redis-master ~]# systemctl enable redis修改主从的redis配置文件
[root@redis-master ~]#vim /etc/redis.conf #可以将原先的配置文件备份 bind 192.168.122.166 127.0.0.1 protected-mode no port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no #打开之后,其他的哨兵将会连接不到 supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/redis.log databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb rdb-del-sync-files no dir /var/lib/redis replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-diskless-load disabled repl-disable-tcp-nodelay no replica-priority 100 acllog-max-len 128 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no lazyfree-lazy-user-del no 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 aof-use-rdb-preamble 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 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes jemalloc-bg-thread yes requirepass redis # 配置 master密码 [root@redis-slave ~]# vim /etc/redis.conf bind 192.168.122.210 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/redis.log databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb rdb-del-sync-files no dir /var/lib/redis replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-diskless-load disabled repl-disable-tcp-nodelay no replica-priority 100 acllog-max-len 128 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no lazyfree-lazy-user-del no 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 aof-use-rdb-preamble 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 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes jemalloc-bg-thread yes requirepass redis replicaof 192.168.122.166 6379 # 配置为 master的从, masterauth redis # 配置mast的认证密码 [root@redis-slave01 ~]# vim /etc/redis.conf bind 192.168.122.181 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/redis.log databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb rdb-del-sync-files no dir /var/lib/redis replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-diskless-load disabled repl-disable-tcp-nodelay no replica-priority 100 acllog-max-len 128 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no lazyfree-lazy-user-del no 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 aof-use-rdb-preamble 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 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes jemalloc-bg-thread yes requirepass redis replicaof 192.168.122.166 6379 # 配置为master的从 masterauth redis # 配置master的认证密码注意启动的顺序:主Redis --> 从Redis --> Sentinel1/2/3
[root@redis-master ~]# systemctl start/restart redis [root@redis-master ~]# chown -R redis.redis /etc/redis-sentinel.conf # yum 安装需要redis用户写入配置问价权限,注意每台都要执行 [root@redis-master ~]# systemctl restart redis-sentinel.service [root@redis-master ~]# systemctl status redis-sentinel.service ● redis-sentinel.service - Redis Sentinel Loaded: loaded (/usr/lib/systemd/system/redis-sentinel.service; disabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis-sentinel.service.d └─limit.conf Active: active (running) since Fri 2020-06-26 10:43:18 CST; 12s ago Process: 26934 ExecStop=/usr/libexec/redis-shutdown redis-sentinel (code=exited, status=1/FAILURE) Main PID: 27107 (redis-sentinel) Status: "Ready to accept connections" CGroup: /system.slice/redis-sentinel.service └─27107 /usr/bin/redis-sentinel *:26379 [sentinel] Jun 26 10:43:18 redis1 systemd[1]: Starting Redis Sentinel... Jun 26 10:43:18 redis1 systemd[1]: Started Redis Sentinel. [root@redis-master ~]# ps -ef |grep redis redis 26485 1 0 10:03 ? 00:00:03 /usr/bin/redis-server 192.168.152.133:6379 redis 27107 1 0 10:43 ? 00:00:00 /usr/bin/redis-sentinel *:26379 [sentinel] root 27150 25442 0 10:51 pts/0 00:00:00 grep --color=auto redis [root@redis-slave ~]# systemctl start/restart redis [root@redis-slave ~]# chown -R redis.redis /etc/redis-sentinel.conf [root@redis-slave ~]# systemctl restart redis-sentinel.service [root@redis-slave ~]# systemctl status redis-sentinel.service ● redis-sentinel.service - Redis Sentinel Loaded: loaded (/usr/lib/systemd/system/redis-sentinel.service; disabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis-sentinel.service.d └─limit.conf Active: active (running) since Fri 2020-06-26 10:45:28 CST; 13s ago Main PID: 26389 (redis-sentinel) Status: "Ready to accept connections" CGroup: /system.slice/redis-sentinel.service └─26389 /usr/bin/redis-sentinel *:26379 [sentinel] Jun 26 10:45:28 redis2 systemd[1]: Starting Redis Sentinel... Jun 26 10:45:28 redis2 systemd[1]: Started Redis Sentinel. [root@redis-slave01 ~]# ps -ef |grep redis redis 26199 1 0 10:08 ? 00:00:02 /usr/bin/redis-server 192.168.152.134:6379 redis 26389 1 0 10:45 ? 00:00:00 /usr/bin/redis-sentinel *:26379 [sentinel] root 26431 25372 0 10:52 pts/0 00:00:00 grep --color=auto redis [root@redis-slave01 ~]# systemctl start/restart redis [root@redis-slave01 ~]# chown -R redis.redis /etc/redis-sentinel.conf [root@redis-slave01 ~]# systemctl restart redis-sentinel.service [root@redis-slave01 ~]# systemctl status redis-sentinel.service ● redis-sentinel.service - Redis Sentinel Loaded: loaded (/usr/lib/systemd/system/redis-sentinel.service; disabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis-sentinel.service.d └─limit.conf Active: active (running) since Fri 2020-06-26 10:48:48 CST; 21s ago Process: 26410 ExecStop=/usr/libexec/redis-shutdown redis-sentinel (code=exited, status=1/FAILURE) Main PID: 26474 (redis-sentinel) Status: "Ready to accept connections" CGroup: /system.slice/redis-sentinel.service └─26474 /usr/bin/redis-sentinel *:26379 [sentinel] Jun 26 10:48:48 redis3 systemd[1]: Starting Redis Sentinel... Jun 26 10:48:48 redis3 systemd[1]: Started Redis Sentinel. [root@redis3 ~]# ps -ef |grep redis redis 26202 1 0 10:10 ? 00:00:02 /usr/bin/redis-server 192.168.152.135:6379 redis 26474 1 0 10:48 ? 00:00:00 /usr/bin/redis-sentinel *:26379 [sentinel] root 26494 25363 0 10:49 pts/0 00:00:00 grep --color=auto redis部署完成之后,停止redis的情况下,从的哨兵会替换主,就证明自己的部署成功了