yum -y install gcc gcc-c++ make
yum -y install wget #安装下载工具
yum -y install openssl-devel #安装keepalived依赖软件包
yum -y install popt-devel #安装keepalived依赖软件包
二、安装nginx
tar -zxf nginx-1.2.8.tar.gz
./configure --prefix=/etc/local/nginx
测试安装成功
三、安装keepalived-1.2.0
(wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz)
tar -zxf keepalived-1.2.0.tar.gz
cd keepalived-1.2.0
./configure --prefix=/usr/local/keepalived
错误:use IPVS Framework 、 IPVS sync daemon support两个必须为YES,才算编译成功
make
四、复制文件:
2、ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived
/etc/rc.d/init.d/keepalived
3、ln -s /usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/keepalived
4、ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived
5、chkconfig keepalived on
五、配置master配置文件:
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
admin@lvtao.net #设置报警邮件地址,可以设置多个,每行一个。
}
notification_email_from admin@lvtao.net #设置邮件的发送地址
smtp_server 127.0.0.1 #设置smtp server地址
smtp_connect_timeout 30 #设置连接smtp server的超时时间
router_id LVS_DEVEL #表示运行keepalived服务器的一个标识。发邮件时显示在邮件主题的信息
}
vrrp_script chk_nginx {
script "/usr/local/keepalived/sbin/check_nginx.sh" #该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix,如果启动失败则停止keepalived,准备让其它机器接管。
interval 2 #每2s检测一次
weight 2 #检测失败(脚本返回非0)则优先级2
}
vrrp_instance VI_1 {
state MASTER #指定keepalived的角色,MASTER表示主服务器,BACKUP表示备用服务器
interface eth0 #指定HA监测网络的接口
virtual_router_id 55 #虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的
priority 100 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级
advert_int 1 #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
authentication { #设置验证类型和密码
auth_type PASS #设置验证类型,主要有PASS和AH两种
auth_pass lvtaosuai #设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
}
virtual_ipaddress { #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个
192.168.35.201
192.168.35.202
track_script {
chk_nginx #引用VRRP脚本,即在 vrrp_script 部分指定的名字。定期运行它们来改变优先级,并最终引发主备切换。
}
}
六、配置backup配置文件:
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
admin@lvtao.net
}
notification_email_from admin@lvtao.net
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/usr/local/keepalived/sbin/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP #指定keepalived的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器
interface eth0
virtual_router_id 55
priority 50
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass lvtaosuai
}
virtual_ipaddress {
192.168.35.201
192.168.35.202
track_script {
chk_nginx
}
}
七、脚本编写:
vi /usr/local/keepalived/sbin/check_nginx.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == ""];then
#echo 1
/etc/init.d/nginx start
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == ""
];then
/etc/init.d/keepalived stop
#echo 2
fi
fi
八、给脚本执行权限
九、测试
service keepalived start #启动Nginx-Master
service keepalived start #启动Nginx-Backup
ip addr #2台服务器分别执行,绑定虚拟IP在Nginx-Master
service keepalived stop #停止Nginx-Backup
ip addr #2台服务器分别执行,绑定虚拟IP在Nginx-Backup
service keepalived start #再启动Nginx-Backup
ip addr #2台服务器分别执行,绑定虚拟IP在Nginx-Master
分享:http://wangzhijian.blog.51cto.com/6427016/1701577
yun部署keepalived http://deidara.blog.51cto.com/400447/302402/