keepalived手册中未更新的功能挖掘及使用介绍_李小红_新浪博客
vrrp_script chk_sshd {
script "killall -0 sshd" # cheaper than pidof
interval 2 # check every 2 seconds
weight -4 # default prio: -4 if KO
}
vrrp_script chk_haproxy {
script "killall -0 haproxy" # cheaper than pidof
interval 2 # check every 2 seconds
}
vrrp_script chk_http_port {
script "/tcp/127.0.0.1/80" # connects and exits
interval 1 # check every second
weight -2 # default prio: -2 if connect fails
}
vrrp_script chk_https_port {
script "/tcp/127.0.0.1/443"
interval 1
weight -2
}
vrrp_script chk_smtp_port {
script "/tcp/127.0.0.1/25"
interval 1
weight -2
}
vrrp_instance VI_1 {
interface eth0 state MASTER virtual_router_id 51 priority 100 virtual_ipaddress {
192.168.200.18/25
}
track_interface {
eth1 weight 2 # prio = +2 if UP
eth2 weight -2 # prio = -2 if DOWN
eth3 # no weight, fault if down
}
track_script {
chk_sshd # use default weight from the script
chk_haproxy weight 2 # +2 if process is present
chk_http_port
chk_https_port
chk_smtp_port
}
}
vrrp_instance VI_2 {
interface eth1 state MASTER virtual_router_id 52 priority 100 virtual_ipaddress {
192.168.201.18/26
}
track_interface {
eth0 weight 2 # prio = +2 if UP
eth2 weight -2 # prio = -2 if DOWN
eth3 # no weight, fault if down
}
track_script {
chk_haproxy weight 2
chk_http_port
chk_https_port
chk_smtp_port
}
}
track_script指定检查脚本,定期运行它们来改变优先级,并最终引发主备切换。如果配过交换机的vrrp/hsrp部分的话,应该对这个功能比较熟悉
2、notify_stop
keepalived停止运行前运行notify_stop指定的脚本
下面是我这边应用上面两个功能介绍:
1、两台双master台数据库,用keepalivd的vrrp模式给客户端提供一个HA的虚拟IP
这个要求检查mysql数据库的状态,如果mysql出现问题,就降低优先级,这样虽然机器正常,也会引发新一轮的master选举,以保证mysql服务的高可用性
先定义两个检查脚本
vrrp_script chk_mysqld {
}
vrrp_script chk_schedown {
}
上面的chk_schedown脚本,给管理员提供一个切换的地方,如果管理员在master上手工touch /var/run/down,这样,流量会比这台机器上切走了。
安装配置keepalived 使nginx高可用
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz
tar -zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make
make install
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
wget http://nginx.org/download/nginx-1.7.0.tar.gz
tar -zvxf nginx-1.7.0.tar.gz
cd nginx1.7.0
./configure --with-pcre=/root/pcre-8.35 --with-zlib=/root/zlib-1.2.8
make && make install
yum install openssl-devel
wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
vi /etc/keepalived/keepalived.conf
10.124.55.134:
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/nginx_pid.sh" ############该脚本我们在后面书写
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER ############ 辅机为 BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 10.124.55.134
priority 102 ########### 权值要比 back 高
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
10.124.55.150 ############ 此处的虚拟IP 地址即 我们web所 ####要访问的IP地址 必须在同一网段
}
}
10.124.55.135:
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/nginx_pid.sh" ############该脚本我们在后面书写
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP ############ 辅机为 BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 10.124.55.135
priority 105 ########### 权值要比 master 低
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
10.124.55.150 ############ 此处的虚拟IP 地址即 我们web所 ####要访问的IP地址 必须在同一网段
}
}
vi /opt/nginx_pid.sh
输入以下配置
nginxpid=`ps -C nginx --no-header | wc -l`
if [ $nginxpid -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 5
if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
