环境:
centos7.6
node1 master 192.168.1.172
node backup 192.168.1.173
vip 192.168.1.174
首先realserver安装分别安装httpd和keepalived,yum安装即可
在node1 /var/www/html/目录创建index.html文件
开启httpd服务访问192.168.1.172
在node2 /var/www/html/目录创建index.html
开启httpd服务访问192.168.1.173
keepalived配置
master配置
global_defs {
notification_email {
ceshi@qq.com
}
# notification_email_from Alexandre.Cassen@firewall.loc
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
router_id node1
# vrrp_skip_check_adv_addr
# vrrp_strict
# vrrp_garp_interval 0
# vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens160 #设置监听网口
virtual_router_id 51
priority 100 #数字越大,等级越高,保证比backup高即可
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.174
}
}
backup配置
参照上面配置文件,修改如下配置
router_id node2
state BAKCUP
priority 99
保存后,开启主备keepalived systemctl start keepalived
用ip a l命令查看虚ip在哪台服务器上
浏览器访问192.168.1.174可以看到是访问的node1
将master的keepalived关闭或网口关闭关机等操作,虚ip自动飘到backup。
以上有个弊端,如果master上的httpd服务挂了并不会自动切到bakcup
解决方法:
更改配置文件,增加端口监听
global_defs {
notification_email {
ceshi@qq.com
}
# notification_email_from Alexandre.Cassen@firewall.loc
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
router_id node1
# vrrp_skip_check_adv_addr
# vrrp_strict
# vrrp_garp_interval 0
# vrrp_gna_interval 0
}
vrrp_script chk_httpd_port {
script "</dev/tcp/127.0.0.1/80"
interval 2 #检测时间间隔
weight 2 #条件成立 权重减2
}
vrrp_instance VI_1 {
state BACKUP #该值主备要一致
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.174
}
track_script {
chk_httpd_port
}
}