0%

redis sentinel哨兵模式+keepalived

实现redis哨兵集群模式以及支持单机高可用连接

环境信息

hostname ip port cpu memory server
redis-sentinel-01 10.0.2.41 6379、26379 4c 8g redis、sentinel、keepalived
redis-sentinel-02 10.0.2.42 6379、26379 4c 8g redis、sentinel、keepalived
redis-sentinel-03 10.0.2.43 6379、26379 4c 8g redis、sentinel、keepalived
vip 10.0.2.44
1
2
3
4
5
6
7
8
9
10
11
12
13
部署目录
/usr/local/redis-5.0.12

配置文件
redis.conf
sentinel.conf

启动方式(先启动redis服务,后启动sentinel服务)
redis服务
systemctl restart redis

sentinel服务
redis-sentinel /usr/local/redis-5.0.12/sentinel.conf

应用连接方式

应用支持redis sentinel模式

1
2
3
4
10.0.2.41:26379
10.0.2.42:26379
10.0.2.43:26379
mymaster

应用不支持redis sentinel模式

1
10.0.2.44:6379

实现原理

    每台服务器分别安装sentinel和keepalived服务,master的优先级为100,backup的优先级为99,在salve服务器上配置vrrp_script检查脚本规则,检查slave当前的角色状态,一旦slave的redis角色状态为master,就把slave的优先级加2变为101,使其获得vip的权限;

    当master的redis服务挂掉后,sentinel会将redis slave提升为新的master,keepalived服务检查当前redis角色状态,如为master时将优先级加2获得vip,否则将优先级减2变为99,当原 master redis 服务起来后sentinel将其作为slave加入到主从复制当中;

安装redis和sentinel服务

下载redis 5.0.12

1
https://download.redis.io/releases/redis-5.0.12.tar.gz

解压并编译

1
2
3
tar xzvf redis-5.0.12.tar.gz
cd redis-5.0.12
make && make install

修改redis配置文件

主节点(假如10.0.2.41)

1
2
3
4
5
6
7
8
9
10
bind 0.0.0.0
daemonize yes
supervised systemd
logfile "/usr/local/redis-5.0.12/redis.log"
dir "/usr/local/redis-5.0.12/"
appendonly yes
rename-command flushall ""
rename-command flushdb ""
requirepass "aiBsXT7R7qoBJDm8"
masterauth "aiBsXT7R7qoBJDm8"

从节点(假如10.0.2.42、10.0.2.43)

1
2
3
4
5
6
7
8
9
10
11
bind 0.0.0.0
daemonize yes
supervised systemd
logfile "/usr/local/redis-5.0.12/redis.log"
dir "/usr/local/redis-5.0.12/"
appendonly yes
rename-command flushall ""
rename-command flushdb ""
requirepass "aiBsXT7R7qoBJDm8"
masterauth "aiBsXT7R7qoBJDm8"
replicaof 10.0.2.41 6379

创建 redis systemd 服务(每台)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# cat /etc/systemd/system/redis.service 
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=root
Group=root
Type=forking
ExecStart=/usr/local/redis-5.0.12/src/redis-server /usr/local/redis-5.0.12/redis.conf
ExecStop=/usr/local/redis-5.0.12/src/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

启动redis(每台)

1
2
systemctl daemon-reload
systemctl start redis

修改sentinel配置文件(每台)

1
2
3
4
daemonize yes
logfile "/usr/local/redis-5.0.12/sentinel.log"
sentinel monitor mymaster 10.0.2.41 6379 2
sentinel auth-pass mymaster aiBsXT7R7qoBJDm8

启动redis sentinel(每台)

1
# redis-sentinel sentinel.conf 

配置keepalived实现单机高可用

使用yum方式安装keepalived软件包(master和slave都安装)

1
yum install -y keepalived

修改keepalived配置文件

master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# cat /etc/keepalived/keepalived.conf

global_defs {
router_id redis01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
mcast_src_ip 10.0.2.41
virtual_router_id 99
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass redis
}
virtual_ipaddress {
10.0.2.44
}
}

slave

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# cat /etc/keepalived/keepalived.conf
global_defs {
router_id redis02
}
vrrp_script chk_redis_master {
script "/etc/keepalived/chkmaster.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
mcast_src_ip 10.0.2.42
virtual_router_id 99
priority 99
advert_int 2
authentication {
auth_type PASS
auth_pass redis
}
virtual_ipaddress {
10.0.2.44
}
track_script {
chk_redis_master
}
}

chkmaster.sh

1
2
3
4
5
6
7
8
9
10
# cat /etc/keepalived/chkmaster.sh 

#!/bin/bash
STATUS=`/usr/local/redis-5.0.12/src/redis-cli info | grep role:master | wc -l`
echo $STATUS
if [ "$STATUS" -eq 0 ];then
exit 1
else
exit 0
fi