机器 | IP |
---|---|
db01 | 192.168.0.51(master) |
db02 | 192.168.0.52(slave) |
db03 | 192.168.0.53(slave) |
1:VIP随MASTER飘动,无需手动改
2:主从关系切换由sentinel管理
3:keepalived三个state全部设置为BACKUP,如果role:master的话,优先级+10(默认三个优先级全为100),不能开启nopreempt,否则只要keepalived运行,就不会释放VIP(即使这个节点已经变为redis-slave了)
[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
[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
初始时,db02,db03需要在配置文件中写slaveof db01,后续sentinel会自动去改
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"
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
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
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
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
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
将IP换了即可,三个节点全部设置为BACKUP,但是不能设置为nopreempt
! 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
}
}
! 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
}
}
! 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
}
}
1:三台节点启动redis
2:三台节点启动sentinel
3: 三台节点启动keepalived