apache+Keepalived实现站点高可用

前言: 一php朋友的公司没有运维,所以运维这一块很蛋痛,于是应他的请求,写上一些HA的东西在此,方便他查看
原理
master 192.168.1.21
backup 192.168.1.21
vip 192.168.1.80
软件安装
master and backup 都要

1
2
3
4
5
6
7
8
9
yum install libnl libnl-devel  openssl-devel -y
tar xvf keepalived-1.2.12.tar.gz && cd keepalived-1.2.12
#centos 6.7
./configure --sysconf=/etc \
 --with-kernel-dir=/usr/src/kernels/2.6.32-573.el6.x86_64
make -j2 && make install
ln -s /usr/local/sbin/keepalived /sbin/
chkconfig --add keepalived
chkconfig --level 35  keepalived on

master上的keepalived 相关配置
backup 上的keepalived 相关配置
backup 的脚本也是基本相同
只有
state MASTER -> state BACKUP
priority 100 -> 变小点 eg priority 80
nopreempt 设置在谁身上 他就是老大,就一直向外提供服务 所以建议写在backup上,也就是master出故障后,一直就先backup提供服务,
vim /etc/keepalived/keepalived.conf

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
28
29
30
31
32
33
34
35
36
37
38
!cat  /etc/keepalived/keepalived.conf
! Configuration File for keepalived master
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script check_httpd {
    script "killall -0  httpd"
    interval 2
}
vrrp_instance HA_1 {
    state MASTER
    interface eth0
    virtual_router_id 80
    priority 100
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    notify_mster "/etc/keepalived/master.sh"
    notify_backup "/etc/keepalived/backup.sh"
    notify_fault "/etc/keepalived/fault.sh"
    track_script {
    check_httpd
    }
    virtual_ipaddress {
        192.168.1.80/24  dev eth0
   }
 nopreempt  #设置在谁身上 他就是老大,就一直向外提供服务  所以建议写在backup上,也就是master出故障后,一直就先backup提供服务,
}

这几个看你的情况分别写上
notify_mster “/etc/keepalived/master.sh”
notify_backup “/etc/keepalived/backup.sh”
notify_fault “/etc/keepalived/fault.sh”
##
结果
web1
web2
###keepalived 选项说明

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
28
29
30
31
32
33
34
35
36
! ConfigurationFile for keepalived
global_defs {
notification_email{
[email protected]
#指定keepalived在发生切换时需要发送email到的对象,一行一个
}
notification_email_from [email protected]#指定发件人
smtp_server 127.0.0.1 #指定smtp服务器地址
smtp_connect_timeout 30#指定smtp连接超时时间
router_id LVS_DEVEL #运行keepalived机器的一个标识
}
vrrp_scriptcheck_nginx {
script"/etc/keepalived/monitor_nginx.sh" #切换脚本
interval 2#间隔几秒
weight 2#权重
}
vrrp_instanceVI_1 {
state MASTER#指定那个为master,那个为backup
#nopreempt不抢占
interface eth0#设置实例绑定的网卡
virtual_router_id 51 #VPID标记
mcast_src_ip 172.16.2.195 #本机IP 与从不同
priority 100#优先级,另一台改为90 与从不同
advert_int 1#检查间隔,默认1s
#认证
authentication {
auth_type PASS
auth_pass chtopnet
}
track_script {
check_nginx#指定应用的脚本
}
virtual_ipaddress {
172.16.2.1#设置虚拟IP
}
}

监控脚本

1
2
3
4
5
6
7
8
9
vim/etc/keepalived/monitor_nginx.sh
#!/bin/bash
PROC=`/usr/sbin/lsof -i:80 | wc -l`
if [ $PROC -eq 0 ]; then
/usr/local/nginx/sbin/nginx
sleep 5
PROC=`/usr/sbin/lsof -i:80 | wc -l`
[ $PROC -eq 0 ] && /etc/init.d/keepalived stop
Fi