环境
系统:centos7.0
主DS:192.168.162.140
备DS:192.168.162.145
RS1:192.168.162.141
RS2:192.168.162.142
VIP:192.168.162.250
Direct Server:
主备DS都要安装负载均衡和高可用软件:
#yum -y install ipvsadm
#yum -y install keepalived
添加子网卡:
#ifconfig ens33:0 192.168.162.250 broadcast 192.168.162.250 netmask 255.255.255.255 up
#route add -host 192.168.162.250 dev ens33:0
主DS编辑高可用配置文件
#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
#全局配置
global_defs {
notification_email {
}
}
#VRRP配置
vrrp_instance VI_1 {
state MASTER #角色类型MASTER|BACKUP
interface ens33 #网卡名称
virtual_router_id 51 #虚拟路由id(需要与BACKUP一致,数字任意写)
priority 100 #优先级
advert_int 1 #每1秒检查一次
#nopreempt #非抢占模式
authentication {
auth_type PASS #认证类型
auth_pass 1111 #认证密码
}
virtual_ipaddress {
192.168.162.250 #虚拟ip(vip)
}
}
#LVS配置
virtual_server 192.168.162.250 80 {
delay_loop 3 #健康检查时间间隔
lb_algo rr #负载均衡调度算法
lb_kind DR #负载均衡转发规则
protocol TCP #协议
real_server 192.168.162.141 80 { #要监控的real_server的ip和端口号
weight 1 #权重
TCP_CHECK { #基于tcp协议的检查
connect_timeout 3 #连接时间超时
retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
}
}
real_server 192.168.162.142 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
上述配置中拷贝一份到备机上
scp keepalived.conf root@192.168.162.145:/etc/keepalived
需要改写的地方:
state MASTER #角色类型MASTER|BACKUP,改为BACKUP
priority 55 #优先级,这里需要设置比master机器小的值
注意:若主DS故障,自动切换备DS后。修复故障后,为了降低风险。需要在VRRP配置里取消”#nopreempt #非抢占模式“的注释,并且将“state MASTER”改为"state BACKUP"。否则修复完成后有会切换回主DS。
RS端操作完全相同
ifconfig lo:0 192.168.162.250 broadcast 192.168.162.250 netmask 255.255.255.255 up #添加loopback子网卡
route add -host 192.168.162.250 dev lo:0
echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
#收到arp请求闭嘴不说话
echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
#可以封虚ip的回包
修改RS默认页面内容
#vim /var/www/html/index.html
RS1添加this is RS1.
RS2添加this is RS2.
启动服务
DS:
#systemctl start keepalived.service
RS
#systemctl start httpd.service
l另开一台虚拟机测试是否可用:
#curl 192.168.162.250
this is RS1.
#curl 192.168.162.250
this is RS2.