用keepalived 来实现一个双节切换
首先下载keepalived 我这边用的是keepalived-1.2.17.tar.gz
准备2台机器 安装keepalived 一台做主机 一台做备机
在安装nginx前,需要确保系统安装了openssl-devel,否则后面的编译不通过。
yum install openssl-devel
安装tar xzf keepalived-1.2.17.tar.gz
cd keepalived-1.2.17
./configure
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived
chkconfig keepalived on
mkdir /etc/keepalived
ln -s /usr/local/sbin/keepalived /usr/sbin/
主机的配置文件:
global_defs {
notification_email {
admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/home/tomcat/bin/monitor.sh"
interval 3
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface em1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.240
}
track_script {
chk_http_port
}
}
备机 配置文件:
global_defs {
notification_email {
admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/home/tomcat/bin/monitor.sh"
interval 3
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface em1
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.240
}
track_script {
chk_http_port
}
}
这样 当主机 启动的时候 虚拟ip240 在主机上面 可以用ip addr 观察
如果当主机宕机了 则虚拟ip240在备机上 可以用ip addr 进行观察
keepalived 里面还可以定时任务的执行自己制定的脚本 比如上面的tomcat的监控脚本