安装编译所必要的依赖
yum install gcc openssl-devel libnl libnl3-devel libnfnetlink-devel
下载并编译
wget [http://www.keepalived.org/software/keepalived-1.3.5.tar.gz](http://www.keepalived.org/software/keepalived-1.3.5.tar.gz)
tar zxf keepalived-1.3.5.tar.gz
cd keepalived-1.3.5
./configure --prefix=/usr/local/keepalived --with-init=SYSV --不指定的话,默认不生成RECL6的启动脚本
make && make install
将文件拷贝到特定的目录
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp -p /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
chkconfig --add keepalived
chkconfig --list keepalived
chkconfig keepalived on
chmod +x /etc/init.d/keepalived
启动服务
service tomcat start
service keepalived start
检查主从切换日志:
tail -f /var/log/messages
重点说明:
一、重启keepalived服务后,ping不通vip地址?
vim /etc/keepalived/keepalived.conf
把下面这个注释就可以了
vrrp_strict
二、vip切换及恢复原则:
- 切换:master服务器的tomcat服务停止,则master上的keepalived会自动停止, vip漂移到backup服务器
- 恢复:master服务器修复完毕后, 请分别启动tomcat和keepalived,则vip会自动漂移到master服务器
配置文件参考
- keepalived.master.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
# vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/etc/keepalived/check-tomcat.sh"
interval 1
weight 2
}
vrrp_instance VI_1 {
state Primary
interface eno1
virtual_router_id 51
priority 99 #此处不同,要低于master
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_interface {
eno1
}
track_script {
chk_http_port
}
virtual_ipaddress {
172.20.30.33
}
}
- keepalived.slaver.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
# vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/etc/keepalived/check-tomcat.sh"
interval 1
weight -2
}
vrrp_instance VI_1 {
state Secondary
interface eno1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_interface {
eno1
}
track_script {
chk_http_port
}
virtual_ipaddress {
172.20.30.33
}
}
网友评论