keepalived的VIP实例
实验环境:
两台调度器,一主一备
两台web服务器
IP地址分别为
192.168.200.111
192.168.200.112
192.168.200.113
192.168.200.114
利用keepalived实现主备的故障切换,从而交替提供服务
配置主调度器:
1.安装依赖包
yum -y instsall ipvsadm keepalived
2.将配置文件进行备份,避免出错
cd /etc/keepalived
cp keepalived.conf keepalived.conf.bak
3.编辑配置文件
vim keepalived.conf
#全局配置
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_MASTER
}
state MASTER #角色名称
interface eno16777728 #心跳网卡
virtual_router_id 51
priority 100 #优先权
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.254
}
}
delay_loop 6
lb_algo rr
lb_kind DR #转换模式
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.200.114 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
4.配置完成后启动服务
systemctl start keepalived
5.查看VIP端口
ip a
6.查看调度服务
ipvsadm -Ln
7.将主配置文件发送给备调度器,修改相应内容
scp /etc/keepalived/keepalived.conf 192.168.200.112:/etc/keepalived/
配置节点服务器:
1.安装apache服务
yum -y install httpd
2.添加路由
ifconfig lo:0 192.168.200.254 netmask 255.255.255.255
route add -host 192.168.200.254 dev lo:0
3.创建测试网页
echo "11111" > /var/www/html/index.html
4.启动apache服务
systemctl start httpd
5.修改配置文件
vim /etc/sysctl.conf
添加内容;
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
6.加载配置信息
sysctl -p
##每台节点服务器都要进行相同的配置
实验测试:
1.关闭所有服务器的防火墙和Linux安全机制
iptables -F
systemctl stop firewalld
setenforce 0
2.在浏览器中输入虚拟服务器的IP地址
这里我们用的是192.168.200.254
实现轮询的结果