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

阅读更多

CentOS7 Systemctl管理Redis和sentinel启动与停止与开机启动

1. 创建服务
用service来管理服务的时候,是在/etc/init.d/目录中创建一个脚本文件,来管理服务的启动和停止,在systemctl中,也类似,文件目录有所不同,在/lib/systemd/system目录下创建一个脚本文件redis.service,里面的内容如下:
[Unit]
Description=Redis
After=network.target

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

[Install]
WantedBy=multi-user.target

[Unit] 表示这是基础信息
Description 是描述
After 是在那个服务后面启动,一般是网络服务启动后启动
[Service] 表示这里是服务信息
ExecStart 是启动服务的命令
ExecStop 是停止服务的指令
[Install] 表示这是是安装相关信息
WantedBy 是以哪种方式启动:multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。
更详细的service文件说明请访问:这里
2. 创建软链接
创建软链接是为了下一步系统初始化时自动启动服务
ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service

3. 刷新配置

刚刚配置的服务需要让systemctl能识别,就必须刷新配置
#systemctl daemon-reload

如果没有权限可以使用sudo
# systemctl daemon-reload

4. 启动、重启、停止
启动redis
#systemctl start redis
重启redis
#systemctl restart redis
停止redis
#systemctl stop redis
5. 开机自启动
redis服务加入开机启动
#systemctl enable redis
禁止开机启动
#systemctl disable redis
6. 查看状态
查看状态
#systemctl status redis

同样:增加redis-sentiel的启停服务:

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

特别要注意:

阅读更多