redis sentinel 主从切换(Failover)的容灾环境部署笔记

环境情况说明:

OS:Centos7.4 64bit

reids:4.0.6

0)Redis主从结构支持一主多从+n个sentinel模式,信息如下:

192.168.179.81   redis-master    redis(6379)、sentinel(26379)
192.168.179.82   redis-slave01   redis(6379)、sentinel(26379)
192.168.179.83   redis-slave02   redis(6379)、sentinel(26379)

关闭三个节点机器的iptables和selinux(所有节点机器上都要操作)

注意:本案例采用1主2从+3 sentinel的集群模式,所有从节点的配置都一样。

1)redis一键安装(三个节点上都要操作)

安装过程略:

2)redis启停脚本(三个节点上都要操作)

cd /lib/systemd/system

vi redis.service

[Unit]
Description=Redis
After=network.target

[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf –daemonize no
ExecStop=/usr/local/redis/bin/redis-cli -p 6379 shutdown

[Install]
WantedBy=multi-user.target

ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service

systemctl daemon-reload

3)redis-sentinel启停脚本示例(三个节点上都要操作)

cd /lib/systemd/system

vi redis-sentinel.service

[Unit]
Description=Redis
After=network.target

[Service]
ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf –sentinel
ExecStop=/usr/local/redis/bin/redis-cli -p 26379 shutdown

[Install]
WantedBy=multi-user.target

ln -s /lib/systemd/system/redis-sentinel.service /etc/systemd/system/multi-user.target.wants/redis-sentinel.service

systemctl daemon-reload

4)配置redis.conf

a)编辑redis-master主节点的redis.conf文件
[root@redis-master src]# mkdir -p /usr/local/redis/data/redis
[root@redis-master src]# cp /usr/local/redis/etc/redis.conf /usr/local/redis/etc/redis.conf.bak
[root@redis-master src]# vim /usr/local/redis/etc/redis.conf
bind 0.0.0.0
daemonize yes
pidfile “/usr/local/redis/var/redis-server.pid”
port 6379
tcp-backlog 128
timeout 0
tcp-keepalive 0
loglevel notice
logfile “/usr/local/redis/var/redis-server.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/data/redis”
#masterauth “20180408”
#requirepass “20180408”
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 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

b)编辑redis-slave01和redis-slave02两个从节点的redis.conf文件
[root@redis-slave01 src]# mkdir -p /usr/local/redis/data/redis
[root@redis-slave01 src]# cp /usr/local/redis/etc/redis.conf /usr/local/redis/etc/redis.conf.bak
[root@redis-slave01 src]# vim /usr/local/redis/etc/redis.conf
bind 0.0.0.0
daemonize yes
pidfile “/usr/local/redis/var/redis-server.pid”
port 6379
tcp-backlog 128
timeout 0
tcp-keepalive 0
loglevel notice
logfile “/usr/local/redis/var/redis-server.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/data/redis”
#masterauth “20180408”
#requirepass “20180408”
slaveof 192.168.178.81 6379
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 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

5)配置sentinel.conf(这个默认没有,需要自建)。三个节点的配置一样。
[root@redis-master src]# mkdir -p /usr/local/redis/data/sentinel
[root@redis-master src]# vim /usr/local/redis/etc/sentinel.conf
port 26379
pidfile “/usr/local/redis/var/redis-sentinel.pid”
dir “/usr/local/redis/data/sentinel”
daemonize yes
protected-mode no
logfile “/usr/local/redis/var/redis-sentinel.log”
sentinel monitor redisMaster 192.168.10.202 6379 2
sentinel down-after-milliseconds redisMaster 10000
sentinel parallel-syncs redisMaster 1
sentinel failover-timeout redisMaster 60000
6)启动redis和sentinel(三个节点都要操作)
设置系统变量
[root@redis-slave02 src]# vim /etc/profile
…….
export PATH=$PATH:/usr/local/redis/bin
[root@redis-slave02 src]# source /etc/profile
启动redis和sentinel
systemctl start redis redis-sentinel.service #一起启,不可以单独起。
停止redis redis-sentinel

systemctl stop redis redis-sentinel.service #一起停,不可以单独停。

7)查看redis和sentinel信息

过程略

8)客户端写入测试数据

过程略

9)模拟故障(通过sentinel实现主从切换,sentinel也要部署多台,即集群模式,防止单台sentinel挂掉情况)

过程略

10)客户端连接redis-sentinel演示。

过程略

以下文章点击率最高

Loading…


发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注