keepalived是基于VRRP协议来实现的WEB服务高可用方案, 从而提高网站的高可用。
一个web服务器至少两台或多台服务器在运行keepalived,主服务器宕机的时候,备份服务器就会接管虚拟IP继续运转,不会因为主服务器宕机而停止服务。
下面就是keepalived心跳监测具体实现(http端口测试):
首先准备两台服务器:
192.168.1.110 (主)
192.168.1.209 (备)
1、下载keepalived(两个都下载)
# yum install keepalived
2、修改配置文件(主)
# ip addr
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/81" # connects and exits
interval 1 # check every second
weight -2 # default prio: -2 if connect fails
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736 #如上图所示
virtual_router_id 51
priority 100 #大于配置文件
advert_int 1
authentication {
auth_type PASS
auth_pass 1111#安全验证
}
virtual_ipaddress {
192.168.1.190 #虚拟ip
}
track_script {
chk_http_port
}
}
3、修改配置文件(从)
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/81" # connects and exits
interval 1 # check every second
weight -2 # default prio: -2 if connect fails
}
vrrp_instance VI_1 {
state BACKUP #区分
interface eno16777736 #如上图所示
virtual_router_id 51
priority 80 #需要比主小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 #安全验证
}
virtual_ipaddress {
192.168.1.190 #虚拟ip
}
track_script {
chk_http_port
}
}
两个服务器除个别地方需改动,其他一样
4、修改之后两个配置文件都需要重启
# systemctl restart keepalived
5、此时虚拟ip在主服务器中显示
# ip addr
主服务器中http服务如果突然挂掉,虚拟ip就会在备份服务器中显示,也就是说keepalived检测到81端口停止,就从主服务器把服务转到了备份中继续运转。