Redis哨兵+keepalived高可用实战

redis哨兵高可用

基本环境
机器IP
db01192.168.0.51(master)
db02192.168.0.52(slave)
db03192.168.0.53(slave)

1:VIP随MASTER飘动,无需手动改

2:主从关系切换由sentinel管理

3:keepalived三个state全部设置为BACKUP,如果role:master的话,优先级+10(默认三个优先级全为100),不能开启nopreempt,否则只要keepalived运行,就不会释放VIP(即使这个节点已经变为redis-slave了)

systemd服务管理
Redis
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis.pid
ExecStart=/opt/redis-6.2.1/src/redis-server /opt/redis-6.2.1/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/redis-6.2.1/src/redis-cli -a password shutdown
[Install]
WantedBy=multi-user.target
Sentinel
[Unit]
Description=Sentinel
After=network.target
[Service]
Type=forking
PIDFile=/var/run/sentinel.pid
ExecStart=/opt/redis-6.2.1/src/redis-sentinel /opt/redis-6.2.1/sentinel.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Redis配置

初始时,db02,db03需要在配置文件中写slaveof db01,后续sentinel会自动去改

db01-redis
daemonize yes
bind 0.0.0.0
port 6379
timeout 300
loglevel notice
logfile "/opt/redis-6.2.1/redis.log"
pidfile "/var/run/redis.pid"
databases 16
save 900 1
save 300 10
save 60 10000
dbfilename "dump.rdb"
rdbcompression yes
dir "/opt/redis-6.2.1"
maxclients 20480
appendonly no
appendfsync no
requirepass "password"
masterauth "password"
db02-redis
daemonize yes
bind 0.0.0.0
port 6379
timeout 300
loglevel notice
logfile "/opt/redis-6.2.1/redis.log"
pidfile "/var/run/redis.pid"
databases 16
save 900 1
save 300 10
save 60 10000
dbfilename "dump.rdb"
rdbcompression yes
dir "/opt/redis-6.2.1"
maxclients 20480
appendonly no
appendfsync no
requirepass "password"
masterauth "password"
slaveof 192.168.0.51 6379
db03-redis
daemonize yes
bind 0.0.0.0
port 6379
timeout 300
loglevel notice
logfile "/opt/redis-6.2.1/redis.log"
pidfile "/var/run/redis.pid"
databases 16
save 900 1
save 300 10
save 60 10000
dbfilename "dump.rdb"
rdbcompression yes
dir "/opt/redis-6.2.1"
maxclients 20480
appendonly no
appendfsync no
requirepass "password"
masterauth "password"
slaveof 192.168.0.51 6379
sentinel配置
db01-sentinel
protected-mode no
port 26379
daemonize yes
logfile "/opt/redis-6.2.1/sentinel.log"
pidfile "/var/run/sentinel.pid"
dir "/tmp"
sentinel monitor mymaster 192.168.0.51 6379 1 #只要一个节点认为master有问题,即可发送failover
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster password
db02-sentinel
protected-mode no
port 26379
daemonize yes
logfile "/opt/redis-6.2.1/sentinel.log"
pidfile "/var/run/sentinel.pid"
dir "/tmp"
sentinel monitor mymaster 192.168.0.51 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster password
db03-sentinel
protected-mode no
port 26379
daemonize yes
logfile "/opt/redis-6.2.1/sentinel.log"
pidfile "/var/run/sentinel.pid"
dir "/tmp"
sentinel monitor mymaster 192.168.0.51 6379 1
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster password
keepalived配置

将IP换了即可,三个节点全部设置为BACKUP,但是不能设置为nopreempt

db01-keepalived
! Configuration File for keepalived
global_defs {
   router_id LVS_DEVEL
   script_user root
   enable_script_security
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script sentinel_check {
    script "/etc/keepalived/sentinel-check.sh 127.0.0.1 6379 password"
    interval 2
    weight 10
    fall 3
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP ##
    interface ens33
    virtual_router_id 51
    priority 100
#    nopreempt  # 这里不要开nopreempt,只要keepalived在运行,会一直霸占VIP,我们要让VIP飘
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    unicast_src_ip 192.168.0.51
    unicast_peer {
        192.168.0.52
        192.168.0.53
    virtual_ipaddress {
        192.168.0.55/24
    }
    track_script {
        sentinel_check
    }
}
db02-keepalived
! Configuration File for keepalived
global_defs {
   router_id LVS_DEVEL
   script_user root
   enable_script_security
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script sentinel_check {
    script "/etc/keepalived/sentinel-check.sh 127.0.0.1 6379 password"
    interval 2
    weight 10
    fall 3
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP ####
    interface ens33
    virtual_router_id 51
    priority 100
#    nopreempt
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    unicast_src_ip 192.168.0.52
    unicast_peer {
        192.168.0.51
        192.168.0.53
    }
    virtual_ipaddress {
	192.168.0.55/24
    }
    track_script {
    	sentinel_check
    }
}
db03-keepalived
! Configuration File for keepalived
global_defs {
   router_id LVS_DEVEL
   script_user root
   enable_script_security
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script sentinel_check {
    script "/etc/keepalived/sentinel-check.sh 127.0.0.1 6379 password"
    interval 2
    weight 10
    fall 3
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
#    nopreempt
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    unicast_src_ip 192.168.0.53
    unicast_peer {
        192.168.0.52
        192.168.0.51
    }
    virtual_ipaddress {
	192.168.0.55/24
    }
    track_script {
    	sentinel_check
    }
}

检测脚本
#!/bin/bash 
if [ -z "$(redis-cli -h $1 -p $2 -a $3 info|grep role:master)" ]; then
	exit 1 
else 
	exit 0 
fi 
使用说明
1:三台节点启动redis
2:三台节点启动sentinel
3: 三台节点启动keepalived
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Redis主从+Keepalived是一种常见的Redis高可用方案。它的基本原理是通过使用Keepalived来实现Redis主从切换。具体步骤如下: 1. 首先,需要安装和配置Keepalived。可以按照以下步骤进行操作: - 下载并解压Keepalived的源代码。 - 进入解压后的目录,并执行以下命令进行编译和安装: ``` ./configure --prefix=/usr/local/keepalived/ make make install ``` - 拷贝所需的文件到相应的目录,例如: ``` cp /usr/local/src/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir -p /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf ``` - 修改keepalived.conf文件,根据实际情况配置虚拟IP(VIP)和监控脚本等参数。 2. 然后,需要配置Redis主从复制。可以按照以下步骤进行操作: - 在Redis的主节点上,修改redis.conf文件,将`slaveof`参数设置为空,即不指定从节点。 - 在Redis的从节点上,修改redis.conf文件,将`slaveof`参数设置为主节点的IP和端口,例如:`slaveof <master_ip> <master_port>`。 3. 最后,启动KeepalivedRedis服务。 - 在Master节点上,启动Keepalived服务。 - 在Master和Slave节点上,分别启动Redis服务。 这样,当Redis的Master节点发生故障时,Keepalived会检测到故障并自动切换到Slave节点,确保Redis服务的高可用性。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [redis高可用keepalived+redis主从部署](https://blog.csdn.net/liuguanghui1988/article/details/77098143)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Keepalived+redis主从](https://blog.csdn.net/qq_37668945/article/details/88618390)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺仔_牛奶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值