一、 背景
本文主要介紹使用 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