本文主要介绍使用 keepalive 实现 presto 的主备高可用,只能有一个节点存活的情况下,使用此方案。
实验环境:CentOS 6 64 位
1. 软件安装
安装keepalive软件包
sudo yum install -y keepalived
presto部署和配置省略,假设进程已经启动,端口监听在8083。
2. 编写presto-master服务存活检测脚本(两台机器都需要)
$sudo vim /usr/bin/check_presto_alive.sh
#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
port_test=`nc -z -v localhost 8083|grep succeeded -c`;
if [ $port_test -eq 0 ]
then
echo 'presto server is died'
killall keepalived
fi
$sudo chmod +x /usr/bin/check_presto_alive.sh
2.机器Presto-master1的配置
$sudo vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script check_presto_alive {
script "/usr/bin/check_presto_alive.sh"
interval 3
weight -10
}
global_defs {
router_id LVS_PRESTO #运行keepalived机器的一个标识
}
vrrp_instance VI_1 {
interface bond0 #设置实例绑定的网卡
state MASTER #指定哪个为master,哪个为backup
virtual_router_id 92 #VPID标记,主备必须一样
priority 180 #优先级,高优先级竞选为master
vrrp_unicast_bind 192.168.0.1
vrrp_unicast_peer 192.168.0.2
authentication {
auth_type PASS #认证方式
auth_pass nenad #认证密码
}
virtual_ipaddress {
## 设置VIP,必须是同一网段虚拟IP
192.168.0.251
}
track_script {
check_presto_alive #presto存活检查
}
}
3.机器Presto-master2的配置
$sudo vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_PRESTO #运行keepalived机器的一个标识
}
vrrp_instance VI_1 {
interface bond0 #设置实例绑定的网卡
state BACKUP #指定哪个为master,哪个为backup
virtual_router_id 92 #VPID标记,主备必须一样
priority 170 #优先级,高优先级竞选为master
vrrp_unicast_bind 192.168.0.2 #本机IP
vrrp_unicast_peer 192.168.0.1 #对端IP
authentication {
auth_type PASS #认证方式
auth_pass nenad #认证密码
}
virtual_ipaddress {
## 设置VIP,必须是同一网段虚拟IP
192.168.0.251
}
notify_master "/etc/init.d/supervisord start presto"
notify_backup "/etc/init.d/supervisord stop presto"
notify_fault "/etc/init.d/supervisord stop presto"
}
4.重启 keepalive 生效(两台机器都执行)
$sudo /etc/init.d/keepalived restart
5.验证
用以下命令可以查看VIP已经绑定到特定的网卡上。
$ ip a
本实验验证了 VIP 的自动漂移,实现了presto-master的主备自动切换
注意:修复失败的服务后,必须重启所在机器的keepalive服务,否则keepalive是无法感知到服务恢复!
备注:presto关于supervisor的配置
$ cat /etc/supervisord.d/presto.conf
[program:presto]
directory=/opt/presto
command=/opt/presto/bin/launcher run
process_name = %(program_name)s
numprocs=1
numprocs_start=1
user=app
stopasgroup=true
killasgroup=true
log_stdout=true ; if true, log program stdout (default true)
log_stderr=true ; if true, log program stderr (def false)
stdout_logfile=/data/logs/supervisord/%(program_name)s-stdout.log ; child log path, use NONE for none; default AUTO
stderr_logfile=/data/logs/supervisord/%(program_name)s-stderr.log ; child log path, use NONE for none; default AUTO
stdout_logfile_maxbytes=100MB ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=3 ; # of logfile backups (default 10)
stderr_logfile_maxbytes=100MB
stderr_logfile_backups=3
environment=JAVA_HOME="/opt/java",PATH="/opt/java/bin:%(ENV_PATH)s"
autorestart=false